1 : #ifndef TAGCOLL_DISKINDEX_INT_H
2 : #define TAGCOLL_DISKINDEX_INT_H
3 :
4 : /** \file
5 : * Fast index for tag data, based on integer indexes
6 : */
7 :
8 : /*
9 : * Copyright (C) 2006 Enrico Zini <enrico@debian.org>
10 : *
11 : * This program is free software; you can redistribute it and/or modify
12 : * it under the terms of the GNU General Public License as published by
13 : * the Free Software Foundation; either version 2 of the License, or
14 : * (at your option) any later version.
15 : *
16 : * This program is distributed in the hope that it will be useful,
17 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : * GNU General Public License for more details.
20 : *
21 : * You should have received a copy of the GNU General Public License
22 : * along with this program; if not, write to the Free Software
23 : * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 : */
25 :
26 : #include <tagcoll/diskindex/mmap.h>
27 : #include <vector>
28 : #include <set>
29 :
30 : namespace tagcoll {
31 : namespace diskindex {
32 :
33 : /**
34 : * MMap-based index of a -> [x1, x2, x3] mappings
35 : *
36 : * The layout is:
37 : *
38 : * [offset of mapping for item 0, offset of mapping for item 1...]
39 : * [size of array][sorted array of ints pointed by index 0]
40 : * [size of array][sorted array of ints pointed by index 1]
41 : * [size of array][sorted array of ints pointed by index 2]
42 : * [...]
43 : * [number of items in the mapping]
44 : *
45 : * This allows fast lookups, as well as fast lookups of unions or intersections
46 : * of mapped arrays.
47 : *
48 : * The number of items for an ID not present in the index is assumed to be 0.
49 : */
50 : class Int : public MMap
51 : {
52 : protected:
53 274890 : inline int* buf() const { return (int*)m_buf; }
54 274890 : inline size_t ofs(int val) const { return buf()[val]; }
55 :
56 : public:
57 20 : Int() {}
58 : Int(const MasterMMap& master, int idx) : MMap(master, idx) {}
59 :
60 : const int* data(int val) const { return (val >= 0 && (unsigned)val < size()) ? buf() + ofs(val) + 1 : 0; }
61 0 : size_t size(int val) const { return (val >= 0 && (unsigned)val < size()) ? buf()[ofs(val)] : 0; }
62 274893 : size_t size() const { return m_buf ? ofs(0) : 0; }
63 : };
64 :
65 : /**
66 : * Creates an on-disk index to use for IntIndex
67 : */
68 : class IntIndexer : public MMapIndexer, public std::vector<std::set<int> >
69 4 : {
70 : public:
71 : /// Store the key->val mapping into the indexer
72 211196 : void map(unsigned int key, int val)
73 : {
74 211196 : if (size() <= key)
75 21142 : resize(key + 1);
76 211196 : (*this)[key].insert(val);
77 211196 : }
78 :
79 : /// Return the size of the encoded index data
80 : virtual int encodedSize() const;
81 :
82 : /// Write the index data in the given buffer, which should be at least
83 : /// encodedSize bytes
84 : virtual void encode(char* buf) const;
85 : };
86 :
87 : }
88 : }
89 :
90 : // vim:set ts=4 sw=4:
91 : #endif
|