Jamoma API  0.6.0.a19
TTHash.cpp
1 /*
2  * Jamoma Hash Table Class
3  * Copyright © 2008, Timothy Place
4  *
5  * License: This code is licensed under the terms of the "New BSD License"
6  * http://creativecommons.org/licenses/BSD/
7  */
8 
9 #include "TTHash.h"
10 #include "TTList.h"
11 #include "TTSymbolTable.h"
12 #include "TTMutex.h"
13 
14 #include "TTSymbolCache.h"
15 
16 #ifdef TT_PLATFORM_WIN
17  #include <hash_map>
18  using namespace stdext; // Visual Studio 2008 puts the hash_map in this namespace
19  typedef hash_map<TTPtrSizedInt,TTValue> TTHashMap;
20 #else
21 // #ifdef TT_PLATFORM_LINUX
22  // at least for GCC 4.6 on the BeagleBoard, the unordered map is standard
23  #include <unordered_map>
24 // #else
25 // #include "boost/unordered_map.hpp"
26 // using namespace boost;
27 // #endif
28 typedef std::unordered_map<TTPtrSizedInt,TTValue> TTHashMap;
29 #endif
30 
31 typedef TTHashMap::const_iterator TTHashMapIter;
32 #define HASHMAP ((TTHashMap*)(mHashMap))
33 #define mHASHMAP (*HASHMAP)
34 
35 /****************************************************************************************************/
36 
37 TTHash::TTHash()
38  :mThreadProtection(NO)
39 {
40  mHashMap = new TTHashMap;
41  mMutex = new TTMutex(false);
42 }
43 
44 
45 TTHash::~TTHash()
46 {
47  delete mMutex;
48  delete HASHMAP;
49 }
50 
51 
52 TTHash::TTHash(TTHash& that)
53 {
54  mHashMap = new TTHashMap;
55  mMutex = new TTMutex(false);
56 
57  *HASHMAP = *((TTHashMap*)that.mHashMap);
58 }
59 
60 
61 TTErr TTHash::append(const TTPtr key, const TTValue& value)
62 {
63  lock();
64  HASHMAP->insert(TTKeyVal(TTPtrSizedInt(key), value));
65  unlock();
66  return kTTErrNone;
67 }
68 
69 
70 TTErr TTHash::append(const TTSymbol key, const TTValue& value)
71 {
72  return append(TTPtr(key.rawpointer()), value);
73 }
74 
75 
77 {
78  return lookup(TTPtr(key.rawpointer()), value);
79 }
80 
81 
82 TTErr TTHash::lookup(const TTPtr key, TTValue& value)
83 {
84  lock();
85  TTHashMap* theMap = (TTHashMap*)mHashMap;
86  TTHashMapIter iter = theMap->find(TTPtrSizedInt(key));
87 
88  // TTPtrSizedInt a = iter->first;
89  // TTSymbol* b = (TTSymbol*)a;
90  // TTValue v = iter->second;
91  // TTValue v = (*theMap)[TTPtrSizedInt(&key)];
92 
93  if (theMap->end() != iter)
94  {
95  value = iter->second;
96  unlock();
97  return kTTErrNone;
98  }
99  else {
100  unlock();
101  return kTTErrValueNotFound;
102 
103  }
104 }
105 
106 
107 
109 {
110  lock();
111  HASHMAP->erase(TTPtrSizedInt(key.rawpointer()));
112  unlock();
113  return kTTErrNone;
114 }
115 
116 
118 {
119  lock();
120  HASHMAP->clear();
121  unlock();
122  return kTTErrNone;
123 }
124 
125 
127 {
128  lock();
129  hashKeys.clear();
130 
131 //#define HASHMAP ((TTHashMap*)(mHashMap))
132 //#define mHASHMAP (*HASHMAP)
133 
134  TTHashMap* theMap = (TTHashMap*)mHashMap;
135 
136  for (TTHashMapIter iter = theMap->begin(); iter != theMap->end(); iter++) {
137  TTPtrSizedInt a = iter->first;
138  TTSymbol b((TTSymbolBase*)a);
139  //TTValue v = iter->second;
140  //hashKeys.append(TTSymbolRef(*(TTSymbol*)iter->first));
141  hashKeys.append(b);
142  }
143  unlock();
144  return kTTErrNone;
145 }
146 
147 
148 TTErr TTHash::getKeysSorted(TTValue& hashKeysSorted, TTBoolean(*comparisonFunction)(TTValue&, TTValue&))
149 {
150  lock();
151  TTList listToSort;
152  TTValue v;
153  TTSymbol key;
154 
155  // fill a list to sort
156  for (TTHashMapIter iter = HASHMAP->begin(); iter != HASHMAP->end(); iter++) {
157  TTPtrSizedInt a = iter->first;
158  TTSymbol b((TTSymbolBase*)a);
159 
160  if (comparisonFunction) {
161  v = b; // the key
162  v.append(TTPtr(iter->second)); // a pointer to the stored value
163  listToSort.append(v);
164  }
165  else
166  listToSort.append(b);
167  }
168 
169  listToSort.sort(comparisonFunction);
170 
171  // fill the result
172  hashKeysSorted.clear();
173  for (listToSort.begin(); listToSort.end(); listToSort.next()) {
174 
175  if (comparisonFunction) {
176  key = listToSort.current()[0];
177  hashKeysSorted.append(key);
178  }
179  else
180  hashKeysSorted.append(listToSort.current());
181  }
182 
183  unlock();
184  return kTTErrNone;
185 }
186 
187 
188 TTErr TTHash::iterate(const TTPtr target, const TTHashIteratorType callback)
189 {
190  lock();
191 
192  for (TTHashMapIter iter = HASHMAP->begin(); iter != HASHMAP->end(); iter++)
193  callback(target, *iter);
194  unlock();
195  return kTTErrNone;
196 }
197 
198 
200 {
201  return HASHMAP->size();
202 }
203 
204 
206 {
207  return HASHMAP->empty();
208 }
209 
210 
211 void TTHash::lock()
212 {
213  if (mThreadProtection)
214  mMutex->lock();
215 }
216 
217 
218 void TTHash::unlock()
219 {
220  if (mThreadProtection)
221  mMutex->unlock();
222 }
223 
bool TTBoolean
Boolean flag, same as Boolean on the Mac.
Definition: TTBase.h:167
TTErr iterate(const TTPtr target, const TTHashIteratorType callback)
For each item in the hash, run specified function.
Definition: TTHash.cpp:188
TTErr getKeysSorted(TTValue &hashKeysSorted, TTBoolean(*comparisonFunction)(TTValue &, TTValue &)=NULL)
Get an array of all of the keys sorted for the hash table.
Definition: TTHash.cpp:148
TTErr lookup(const TTSymbol key, TTValue &value)
Find the value for the given key.
Definition: TTHash.cpp:76
TTUInt32 getSize()
Return the number of keys in the hash table.
Definition: TTHash.cpp:199
TTErr getKeys(TTValue &hashKeys)
Get an array of all of the keys for the hash table.
Definition: TTHash.cpp:126
The TTSymbolBase class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbolBase.h:28
Maintain a collection of TTValue objects indexed by TTSymbol pointers.
Definition: TTHash.h:36
void append(const T &anElementValueToAppend)
Insert a single TTElement at the end.
Definition: TTValue.h:243
void * TTPtr
A generic pointer.
Definition: TTBase.h:248
TTPtr rawpointer() const
Get the value of the raw pointer into the symbol table.
Definition: TTSymbol.h:134
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
TTErr append(const TTSymbol key, const TTValue &value)
Insert an item into the hash table.
Definition: TTHash.cpp:70
TTBoolean isEmpty()
Return true if the hash has nothing stored in it.
Definition: TTHash.cpp:205
A value was not found when doing a look up for it (in a TTHash, TTList, or other class).
Definition: TTBase.h:352
TTErr clear()
Remove all items from the hash table.
Definition: TTHash.cpp:117
void clear()
Clear all values from the vector, leaving with size of 0.
Definition: TTValue.h:131
long TTPtrSizedInt
An integer that is the same size as a pointer.
Definition: TTBase.h:240
TTErr remove(const TTSymbol key)
Remove an item from the hash table.
Definition: TTHash.cpp:108
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
std::uint32_t TTUInt32
32 bit unsigned integer
Definition: TTBase.h:178
No Error.
Definition: TTBase.h:343
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34