LRAUV  revA
FastMap.h
Go to the documentation of this file.
1 
9 #ifndef FASTMAP_H_
10 #define FASTMAP_H_
11 
12 #include "FlexArray.h"
13 #include "MapEntry.h"
14 
27 template <typename S, typename T>
28 class FastMap
29 {
30 public:
31  FastMap( bool allowDups = false, const unsigned short initialSize = 32 )
32  : array_( true, initialSize, 16384 ),
33  allowDups_( allowDups ),
34  nullItem_( 0 )
35  {}
36  virtual ~FastMap()
37  {}
38 
39  bool put( S& key, T item, bool deleteOnCleanup = false )
40  {
41  int insertIndex = findIndex( key, 0, array_.getMaxIndex() );
42  if( insertIndex >= 0 && !allowDups_ )
43  {
44  return false;
45  }
46  // This puts duplicates in the order that they are added.
47  while( allowDups_
48  && insertIndex >= 0
49  && insertIndex <= array_.getMaxIndex()
50  && getIndexedEntry( insertIndex )->getKey() == key )
51  {
52  ++insertIndex;
53  }
54  // Need to insert at the indicated (xor-ed) location
55  // So, if insertIndex = -1, insert at 0
56  // So, if insertIndex = -2, insert at 1
57  if( insertIndex < 0 )
58  {
59  insertIndex ^= -1;
60  }
61  MapEntry<S, T>* entry = new MapEntry<S, T>( key, item, deleteOnCleanup );
62  array_.insert( insertIndex, entry );
63  return true;
64  }
65 
66  T get( const S& key, bool andPop = false )
67  {
68  int index = findIndex( key, 0, array_.getMaxIndex() );
69  if( index >= 0 )
70  {
71  return getIndexed( index, andPop );
72  }
73  return nullItem_;
74  }
75 
76  T pop( S& key )
77  {
78  return get( key, true );
79  }
80 
81  T getIndexed( unsigned int index, bool andPop = false )
82  {
83  MapEntry<S, T>* entry = getIndexedEntry( index, andPop );
84  if( 0 != entry )
85  {
86  T item = entry->getItem();
87  if( andPop )
88  {
89  delete entry;
90  }
91  return item;
92  }
93  return nullItem_;
94  }
95 
96  MapEntry<S, T>* getIndexedEntry( unsigned int index, bool andPop = false )
97  {
98  MapEntry<S, T>* entry = array_[index];
99  if( 0 != entry && andPop )
100  {
101  array_.pop( index );
102  }
103  return entry;
104  }
105 
106  T popIndexed( unsigned int index )
107  {
108  return getIndexed( index, true );
109  }
110 
111  S& findKey( T item, const S& defaultValue )
112  {
113  for( int i = 0; i <= array_.getMaxIndex(); ++i )
114  {
115  MapEntry<S, T>* entry = array_[i];
116  if( 0 != entry && entry->getItem() == item )
117  {
118  return entry->getKey();
119  }
120  }
121  return defaultValue; // returns -1 for an empty array
122  }
123 
124  int findIndex( const S& key, int minIndex = 0, int maxIndex = -1 )
125  {
126  if( maxIndex == -1 )
127  {
128  maxIndex = array_.getMaxIndex();
129  }
130  if( maxIndex < minIndex )
131  {
132  return -1; // returns -1 for an empty array
133  }
134  int midIndex = ( maxIndex + minIndex ) / 2;
135  MapEntry<S, T>* midEntry( array_[ midIndex ] );
136  //printf("midEntry at 0x%08X\n", (int)midEntry);
137  if( key == midEntry->getKey() )
138  {
139  return midIndex;
140  }
141  else if( key < midEntry->getKey() )
142  {
143  if( midIndex == minIndex )
144  {
145  return midIndex ^ -1;
146  }
147  return findIndex( key, minIndex, midIndex - 1 );
148  }
149  else
150  {
151  if( midIndex == maxIndex )
152  {
153  return ( midIndex + 1 ) ^ -1;
154  }
155  return findIndex( key, midIndex + 1, maxIndex );
156  }
157  }
158 
159  unsigned int size()
160  {
161  return array_.size();
162  }
163 
165  {
166  return array_;
167  }
168 
169  void clear( bool andDeleteValues = false )
170  {
171  for( unsigned int i = 0; andDeleteValues && i < size(); ++i )
172  {
173  delete getIndexed( i );
174  }
175  array_.clear();
176  }
177 
178 protected:
179 
183 
184 private:
185  // Note that the copy constructor below is private and not given a body.
186  // Any attempt to call it will return a compiler error.
187  FastMap( const FastMap<S, T>& old ); // disallow copy constructor
188 
189 };
190 
191 #endif /*FASTMAP_H_*/
Simple class for providing key-pointer mappings.
Definition: Slate.h:44
Wraps an key/value pair in a FastMap.
Definition: MapEntry.h:18
virtual T getItem()
Definition: MapEntry.h:39
int findIndex(const S &key, int minIndex=0, int maxIndex=-1)
Definition: FastMap.h:124
void insert(const unsigned int index, T item)
Definition: FlexArray.h:155
T nullItem_
Definition: FastMap.h:182
void clear(bool andDeleteValues=false)
Definition: FastMap.h:169
T pop(int index=-1)
Definition: FlexArray.h:171
unsigned int size()
Definition: FastMap.h:159
Contains the FlexArrayBase and FlexArray class declarations.
int getMaxIndex() const
Definition: FlexArray.cpp:47
unsigned int size() const
Definition: FlexArray.cpp:69
MapEntry< S, T > * getIndexedEntry(unsigned int index, bool andPop=false)
Definition: FastMap.h:96
T getIndexed(unsigned int index, bool andPop=false)
Definition: FastMap.h:81
Contains the MapEntry class declaration.
FlexArray< MapEntry< S, T > * > array_
Definition: FastMap.h:180
S & findKey(T item, const S &defaultValue)
Definition: FastMap.h:111
virtual S & getKey()
Definition: MapEntry.h:35
bool allowDups_
Definition: FastMap.h:181
T popIndexed(unsigned int index)
Definition: FastMap.h:106
Simple class providing a flexible size array of pointers.
Definition: DataReader.h:19
virtual ~FastMap()
Definition: FastMap.h:36
T pop(S &key)
Definition: FastMap.h:76
bool put(S &key, T item, bool deleteOnCleanup=false)
Definition: FastMap.h:39
void clear()
Definition: FlexArray.cpp:22
FlexArray< MapEntry< S, T > * > getArray()
Definition: FastMap.h:164
FastMap(bool allowDups=false, const unsigned short initialSize=32)
Definition: FastMap.h:31