Lucene++ - a full-featured, c++ search engine
API Documentation


 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Map.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2011 Alan Wright. All rights reserved.
3 // Distributable under the terms of either the Apache License (Version 2.0)
4 // or the GNU Lesser General Public License.
6 
7 #ifndef MAP_H
8 #define MAP_H
9 
10 #include <map>
11 #include "LuceneSync.h"
12 
13 namespace Lucene
14 {
16  template < class KEY, class VALUE, class LESS = std::less<KEY> >
17  class Map : public LuceneSync
18  {
19  public:
21  typedef std::pair<KEY, VALUE> key_value;
22  typedef std::map< KEY, VALUE, LESS, LuceneAllocator<key_value> > map_type;
23  typedef typename map_type::iterator iterator;
24  typedef typename map_type::const_iterator const_iterator;
25  typedef KEY key_type;
26  typedef VALUE value_type;
27 
28  virtual ~Map()
29  {
30  }
31 
32  protected:
33  boost::shared_ptr<map_type> mapContainer;
34 
35  public:
37  {
38  this_type instance;
39  instance.mapContainer = Lucene::newInstance<map_type>();
40  return instance;
41  }
42 
43  void reset()
44  {
45  mapContainer.reset();
46  }
47 
48  int32_t size() const
49  {
50  return (int32_t)mapContainer->size();
51  }
52 
53  bool empty() const
54  {
55  return mapContainer->empty();
56  }
57 
58  void clear()
59  {
60  mapContainer->clear();
61  }
62 
64  {
65  return mapContainer->begin();
66  }
67 
69  {
70  return mapContainer->end();
71  }
72 
74  {
75  return mapContainer->begin();
76  }
77 
79  {
80  return mapContainer->end();
81  }
82 
83  operator bool() const
84  {
85  return mapContainer;
86  }
87 
88  bool operator! () const
89  {
90  return !mapContainer;
91  }
92 
93  map_type& operator= (const map_type& other)
94  {
95  mapContainer = other.mapContainer;
96  return *this;
97  }
98 
99  void put(const KEY& key, const VALUE& value)
100  {
101  (*mapContainer)[key] = value;
102  }
103 
104  template <class ITER>
105  void putAll(ITER first, ITER last)
106  {
107  for (iterator current = first; current != last; ++current)
108  (*mapContainer)[current->first] = current->second;
109  }
110 
111  template <class ITER>
112  void remove(ITER pos)
113  {
114  mapContainer->erase(pos);
115  }
116 
117  template <class ITER>
118  ITER remove(ITER first, ITER last)
119  {
120  return mapContainer->erase(first, last);
121  }
122 
123  bool remove(const KEY& key)
124  {
125  return (mapContainer->erase(key) > 0);
126  }
127 
128  iterator find(const KEY& key)
129  {
130  return mapContainer->find(key);
131  }
132 
133  VALUE get(const KEY& key) const
134  {
135  iterator findValue = mapContainer->find(key);
136  return findValue == mapContainer->end() ? VALUE() : findValue->second;
137  }
138 
139  bool contains(const KEY& key) const
140  {
141  return (mapContainer->find(key) != mapContainer->end());
142  }
143 
144  VALUE& operator[] (const KEY& key)
145  {
146  return (*mapContainer)[key];
147  }
148  };
149 }
150 
151 #endif

clucene.sourceforge.net