Osmium  0.1
include/osmium/utils/stringtable.hpp
Go to the documentation of this file.
00001 #ifndef OSMIUM_UTILS_STRINGTABLE_HPP
00002 #define OSMIUM_UTILS_STRINGTABLE_HPP
00003 
00004 /*
00005 
00006 Copyright 2011 Jochen Topf <jochen@topf.org> and others (see README).
00007 
00008 This file is part of Osmium (https://github.com/joto/osmium).
00009 
00010 Osmium is free software: you can redistribute it and/or modify it under the
00011 terms of the GNU Lesser General Public License or (at your option) the GNU
00012 General Public License as published by the Free Software Foundation, either
00013 version 3 of the Licenses, or (at your option) any later version.
00014 
00015 Osmium is distributed in the hope that it will be useful, but WITHOUT ANY
00016 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
00017 PARTICULAR PURPOSE. See the GNU Lesser General Public License and the GNU
00018 General Public License for more details.
00019 
00020 You should have received a copy of the Licenses along with Osmium. If not, see
00021 <http://www.gnu.org/licenses/>.
00022 
00023 */
00024 
00025 #include <stdint.h>
00026 #include <string>
00027 #include <map>
00028 #include <iostream>
00029 
00030 namespace Osmium {
00031 
00040     class StringTable {
00041 
00043         typedef uint16_t string_id_t;
00044 
00061         struct string_info {
00065             uint16_t count;
00066 
00070             string_id_t interim_id;
00071         };
00072 
00073         friend bool operator<(const string_info& lhs, const string_info& rhs) {
00074             return lhs.count > rhs.count;
00075         }
00076 
00081         typedef std::map<std::string, string_info> string2string_info_t;
00082         string2string_info_t m_strings;
00083 
00088         typedef std::vector<string_id_t> interim_id2id_t;
00089         interim_id2id_t m_id2id_map;
00090 
00091         int m_size;
00092 
00093     public:
00094 
00095         StringTable() : m_strings(), m_id2id_map(), m_size(0) {
00096         }
00097 
00102         string_id_t record_string(const std::string& string) {
00103             string_info& info = m_strings[string];
00104             if (info.interim_id == 0) {
00105                 info.interim_id = ++m_size;
00106             } else {
00107                 info.count++;
00108             }
00109             return info.interim_id;
00110         }
00111 
00112         template<typename A, typename B>
00113         static std::pair<B,A> flip_pair(const std::pair<A,B>& p) {
00114             return std::pair<B,A>(p.second, p.first);
00115         }
00116 
00128         void store_stringtable(OSMPBF::StringTable* st) {
00129             typedef std::multimap<string_info, std::string> cmap;
00130             cmap sortedbycount;
00131 
00132             m_id2id_map.resize(m_size+1);
00133 
00134             std::transform(m_strings.begin(), m_strings.end(),
00135                            std::inserter(sortedbycount, sortedbycount.begin()), flip_pair<std::string, string_info>);
00136 
00137             int n=0;
00138             cmap::const_iterator end=sortedbycount.end();
00139             for (cmap::const_iterator it = sortedbycount.begin(); it != end; ++it) {
00140                 // add the string of the current item to the pbf StringTable
00141                 st->add_s(it->second);
00142 
00143                 // store the mapping from the interim-id to the real id
00144                 m_id2id_map[it->first.interim_id] = ++n;
00145             }
00146         }
00147 
00151         string_id_t map_string_id(const string_id_t interim_id) const {
00152             return m_id2id_map[interim_id];
00153         }
00154 
00158         void clear() {
00159             m_strings.clear();
00160             m_size = 0;
00161         }
00162 
00163     }; // class StringTable
00164 
00165 } // namespace Osmium
00166 
00167 #endif // OSMIUM_UTILS_STRINGTABLE_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines