StringIndex.h

Go to the documentation of this file.
00001 #ifndef TAGCOLL_STRING_INDEX_H
00002 #define TAGCOLL_STRING_INDEX_H
00003 
00008 /*
00009  * Copyright (C) 2006  Enrico Zini <enrico@debian.org>
00010  *
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024  */
00025 
00026 #include <tagcoll/Exception.h>
00027 #include <tagcoll/Serializer.h>
00028 #include <tagcoll/MMapIndex.h>
00029 #include <set>
00030 
00031 namespace Tagcoll
00032 {
00033 
00046 class StringIndex : public MMapIndex, public Converter<int, std::string>, public Converter<std::string, int>
00047 {
00048 protected:
00049     int offset(int val) const { return ((const int*)m_buf)[val]; }
00050     mutable std::vector<std::string> stringCache;
00051 
00052 public:
00053     StringIndex(const MasterMMapIndex& master, int idx) : MMapIndex(master, idx), stringCache(size()) {}
00054     virtual ~StringIndex() {}
00055 
00056     virtual std::string operator()(const int& id) const
00057     {
00058         if (id < 0)
00059             return std::string();
00060         if (stringCache.size() <= (unsigned)id)
00061             stringCache.resize(id + 1);
00062         if (stringCache[id].empty())
00063             if (const char* s = data(id))
00064                 stringCache[id] = std::string(s, size(id));
00065         return stringCache[id];
00066     }
00067     virtual int operator()(const std::string& item) const { return data(item.c_str()); }
00068 
00069     const char* data(int val) const { return (val >= 0 && (unsigned)val < size()) ? m_buf + offset(val) : ""; }
00070     size_t size(int val) const
00071     {
00072         if (val < 0 || (unsigned)val >= size())
00073             return 0;
00074         if ((unsigned)val == size() - 1)
00075             return m_size - offset(val) - 1;
00076         else
00077             return offset(val + 1) - offset(val) - 1;
00078     }
00079     int data(const char* str) const;
00080     size_t size() const { return *(unsigned int*)m_buf / sizeof(int); }
00081 };
00082 
00086 class StringIndexer : public MMapIndexer, public Converter<int, std::string>, public Converter<std::string, int>
00087 {
00088 protected:
00089     std::vector<std::string> data;
00090 
00091 public:
00092     virtual std::string operator()(const int& item) const { return data[item]; }
00093     virtual int operator()(const std::string& item) const;
00094 
00096     void map(const std::string& str);
00097     
00099     int encodedSize() const;
00100 
00103     void encode(char* buf) const;
00104 };
00105 
00106 };
00107 
00108 // vim:set ts=4 sw=4:
00109 #endif

Generated on Sat Aug 19 00:46:29 2006 for libtagcoll by  doxygen 1.4.7