18 #ifndef _DECAF_UTIL_STLMAP_H_
19 #define _DECAF_UTIL_STLMAP_H_
46 template <
typename K,
typename V,
typename COMPARATOR = std::less<K> >
50 std::map<K,V,COMPARATOR> valueMap;
56 class AbstractMapIterator {
61 typename std::map<K,V,COMPARATOR>::iterator futureEntry;
62 typename std::map<K,V,COMPARATOR>::iterator currentEntry;
68 AbstractMapIterator(
const AbstractMapIterator&);
69 AbstractMapIterator& operator= (
const AbstractMapIterator&);
73 AbstractMapIterator(
StlMap* parent) : position(0),
74 expectedModCount(parent->modCount),
75 futureEntry(parent->valueMap.begin()),
76 currentEntry(parent->valueMap.end()),
77 associatedMap(parent) {
80 virtual ~AbstractMapIterator() {}
82 virtual bool checkHasNext()
const {
83 if (futureEntry != this->associatedMap->valueMap.end()) {
89 void checkConcurrentMod()
const {
90 if (expectedModCount != this->associatedMap->modCount) {
92 __FILE__, __LINE__,
"StlMap modified outside this iterator");
99 if (!checkHasNext()) {
103 currentEntry = futureEntry;
107 virtual void doRemove() {
109 checkConcurrentMod();
111 if (currentEntry == this->associatedMap->valueMap.end()) {
113 __FILE__, __LINE__,
"Remove called before call to next()");
116 this->associatedMap->valueMap.erase(currentEntry);
117 currentEntry = this->associatedMap->valueMap.end();
120 associatedMap->modCount++;
124 class EntryIterator :
public Iterator< MapEntry<K,V> >,
public AbstractMapIterator {
127 EntryIterator(
const EntryIterator&);
128 EntryIterator& operator= (
const EntryIterator&);
132 EntryIterator(
StlMap* parent) : AbstractMapIterator(parent) {
135 virtual ~EntryIterator() {}
137 virtual bool hasNext()
const {
138 return this->checkHasNext();
143 return MapEntry<K, V>(this->currentEntry->first, this->currentEntry->second);
146 virtual void remove() {
151 class KeyIterator :
public Iterator<K>,
public AbstractMapIterator {
154 KeyIterator(
const KeyIterator&);
155 KeyIterator& operator= (
const KeyIterator&);
159 KeyIterator(
StlMap* parent) : AbstractMapIterator(parent) {
162 virtual ~KeyIterator() {}
164 virtual bool hasNext()
const {
165 return this->checkHasNext();
170 return this->currentEntry->first;
173 virtual void remove() {
178 class ValueIterator :
public Iterator<V>,
public AbstractMapIterator {
181 ValueIterator(
const ValueIterator&);
182 ValueIterator& operator= (
const ValueIterator&);
186 ValueIterator(
StlMap* parent) : AbstractMapIterator(parent) {
189 virtual ~ValueIterator() {}
191 virtual bool hasNext()
const {
192 return this->checkHasNext();
197 return this->currentEntry->second;
200 virtual void remove() {
207 class ConstAbstractMapIterator {
210 mutable int position;
211 int expectedModCount;
212 typename std::map<K,V,COMPARATOR>::const_iterator futureEntry;
213 typename std::map<K,V,COMPARATOR>::const_iterator currentEntry;
215 const StlMap* associatedMap;
219 ConstAbstractMapIterator(
const ConstAbstractMapIterator&);
220 ConstAbstractMapIterator& operator= (
const ConstAbstractMapIterator&);
224 ConstAbstractMapIterator(
const StlMap* parent) : position(0),
225 expectedModCount(parent->modCount),
226 futureEntry(parent->valueMap.begin()),
227 currentEntry(parent->valueMap.end()),
228 associatedMap(parent) {
231 virtual ~ConstAbstractMapIterator() {}
233 virtual bool checkHasNext()
const {
234 if (futureEntry != this->associatedMap->valueMap.end()) {
240 void checkConcurrentMod()
const {
241 if (expectedModCount != this->associatedMap->modCount) {
243 __FILE__, __LINE__,
"StlMap modified outside this iterator");
248 checkConcurrentMod();
250 if (!checkHasNext()) {
254 currentEntry = futureEntry;
259 class ConstEntryIterator :
public Iterator< MapEntry<K,V> >,
public ConstAbstractMapIterator {
262 ConstEntryIterator(
const ConstEntryIterator&);
263 ConstEntryIterator& operator= (
const ConstEntryIterator&);
267 ConstEntryIterator(
const StlMap* parent) : ConstAbstractMapIterator(parent) {
270 virtual ~ConstEntryIterator() {}
272 virtual bool hasNext()
const {
273 return this->checkHasNext();
278 return MapEntry<K, V>(this->currentEntry->first, this->currentEntry->second);
281 virtual void remove() {
283 __FILE__, __LINE__,
"Cannot write to a const Iterator." );
287 class ConstKeyIterator :
public Iterator<K>,
public ConstAbstractMapIterator {
290 ConstKeyIterator(
const ConstKeyIterator&);
291 ConstKeyIterator& operator= (
const ConstKeyIterator&);
295 ConstKeyIterator(
const StlMap* parent) : ConstAbstractMapIterator(parent) {
298 virtual ~ConstKeyIterator() {}
300 virtual bool hasNext()
const {
301 return this->checkHasNext();
306 return this->currentEntry->first;
309 virtual void remove() {
311 __FILE__, __LINE__,
"Cannot write to a const Iterator." );
315 class ConstValueIterator :
public Iterator<V>,
public ConstAbstractMapIterator {
318 ConstValueIterator(
const ConstValueIterator&);
319 ConstValueIterator& operator= (
const ConstValueIterator&);
323 ConstValueIterator(
const StlMap* parent) : ConstAbstractMapIterator(parent) {
326 virtual ~ConstValueIterator() {}
328 virtual bool hasNext()
const {
329 return this->checkHasNext();
334 return this->currentEntry->second;
337 virtual void remove() {
339 __FILE__, __LINE__,
"Cannot write to a const Iterator." );
346 class StlMapEntrySet :
public AbstractSet< MapEntry<K, V> > {
353 StlMapEntrySet(
const StlMapEntrySet&);
354 StlMapEntrySet& operator= (
const StlMapEntrySet&);
361 virtual ~StlMapEntrySet() {}
363 virtual int size()
const {
364 return associatedMap->size();
367 virtual void clear() {
368 associatedMap->clear();
372 if (this->associatedMap->containsKey(entry.getKey()) &&
373 this->associatedMap->get(entry.getKey()) == entry.getValue()) {
374 associatedMap->remove(entry.getKey());
382 if (this->associatedMap->containsKey(entry.
getKey()) &&
390 return new EntryIterator(associatedMap);
394 return new ConstEntryIterator(associatedMap);
399 class ConstStlMapEntrySet :
public AbstractSet< MapEntry<K, V> > {
402 const StlMap* associatedMap;
406 ConstStlMapEntrySet(
const ConstStlMapEntrySet&);
407 ConstStlMapEntrySet& operator= (
const ConstStlMapEntrySet&);
414 virtual ~ConstStlMapEntrySet() {}
416 virtual int size()
const {
417 return associatedMap->size();
420 virtual void clear() {
422 __FILE__, __LINE__,
"Can't clear a const collection");
427 __FILE__, __LINE__,
"Can't remove from const collection");
431 if (this->associatedMap->containsKey(entry.
getKey()) &&
440 __FILE__, __LINE__,
"Can't return a non-const iterator for a const collection");
444 return new ConstEntryIterator(associatedMap);
457 StlMapKeySet(
const StlMapKeySet&);
458 StlMapKeySet& operator= (
const StlMapKeySet&);
465 virtual ~StlMapKeySet() {}
467 virtual bool contains(
const K& key)
const {
468 return this->associatedMap->containsKey(key);
471 virtual int size()
const {
472 return this->associatedMap->size();
475 virtual void clear() {
476 this->associatedMap->clear();
479 virtual bool remove(
const K& key) {
480 if (this->associatedMap->containsKey(key)) {
481 associatedMap->remove(key);
488 return new KeyIterator(this->associatedMap);
492 return new ConstKeyIterator(this->associatedMap);
499 const StlMap* associatedMap;
503 ConstStlMapKeySet(
const ConstStlMapKeySet&);
504 ConstStlMapKeySet& operator= (
const ConstStlMapKeySet&);
511 virtual ~ConstStlMapKeySet() {}
513 virtual bool contains(
const K& key)
const {
514 return this->associatedMap->containsKey(key);
517 virtual int size()
const {
518 return this->associatedMap->size();
521 virtual void clear() {
523 __FILE__, __LINE__,
"Can't modify a const collection");
528 __FILE__, __LINE__,
"Can't modify a const collection");
533 __FILE__, __LINE__,
"Can't return a non-const iterator for a const collection");
537 return new ConstKeyIterator(this->associatedMap);
550 StlMapValueCollection(
const StlMapValueCollection&);
551 StlMapValueCollection& operator= (
const StlMapValueCollection&);
558 virtual ~StlMapValueCollection() {}
560 virtual bool contains(
const V& value)
const {
561 return this->associatedMap->containsValue(value);
564 virtual int size()
const {
565 return this->associatedMap->size();
568 virtual void clear() {
569 this->associatedMap->clear();
573 return new ValueIterator(this->associatedMap);
577 return new ConstValueIterator(this->associatedMap);
584 const StlMap* associatedMap;
588 ConstStlMapValueCollection(
const ConstStlMapValueCollection&);
589 ConstStlMapValueCollection& operator= (
const ConstStlMapValueCollection&);
596 virtual ~ConstStlMapValueCollection() {}
598 virtual bool contains(
const V& value)
const {
599 return this->associatedMap->containsValue(value);
602 virtual int size()
const {
603 return this->associatedMap->size();
606 virtual void clear() {
608 __FILE__, __LINE__,
"Can't modify a const collection");
613 __FILE__, __LINE__,
"Can't return a non-const iterator for a const collection");
617 return new ConstValueIterator(this->associatedMap);
638 StlMap() :
Map<K,V>(), valueMap(), mutex(), modCount(0),
639 cachedEntrySet(), cachedKeySet(), cachedValueCollection(),
640 cachedConstEntrySet(), cachedConstKeySet(), cachedConstValueCollection() {
650 cachedEntrySet(), cachedKeySet(), cachedValueCollection(),
651 cachedConstEntrySet(), cachedConstKeySet(), cachedConstValueCollection() {
662 cachedEntrySet(), cachedKeySet(), cachedValueCollection(),
663 cachedConstEntrySet(), cachedConstKeySet(), cachedConstValueCollection() {
673 return this->valueMap == source.valueMap;
680 typename std::auto_ptr< Iterator<K> > iterator(this->
keySet().iterator());
681 while (iterator->hasNext()) {
682 K key = iterator->next();
687 if (!(this->
get(key) == source.
get(key))) {
699 this->valueMap.clear();
700 this->valueMap.insert( source.valueMap.begin(), source.valueMap.end() );
722 if (valueMap.empty()) {
726 typename std::map<K, V, COMPARATOR>::const_iterator iter;
727 iter = valueMap.find(key);
728 return iter != valueMap.end();
735 if (valueMap.empty()) {
739 typename std::map<K, V, COMPARATOR>::const_iterator iter = valueMap.begin();
740 for (; iter != valueMap.end(); ++iter) {
741 if ((*iter).second == value) {
753 return valueMap.empty();
760 return (
int)valueMap.size();
766 virtual V&
get(
const K& key) {
767 typename std::map<K, V, COMPARATOR>::iterator iter;
768 iter = valueMap.find(key);
769 if (iter == valueMap.end()) {
779 virtual const V&
get(
const K& key)
const {
780 typename std::map<K, V, COMPARATOR>::const_iterator iter;
781 iter = valueMap.find(key);
782 if (iter == valueMap.end()) {
792 virtual bool put(
const K& key,
const V& value) {
797 valueMap[key] = value;
805 virtual bool put(
const K& key,
const V& value, V& oldValue) {
809 oldValue = valueMap[key];
811 valueMap[key] = value;
820 this->valueMap.insert(other.valueMap.begin(), other.valueMap.end());
828 typename std::auto_ptr< Iterator<K> > iterator(other.
keySet().iterator());
829 while (iterator->hasNext()) {
830 K key = iterator->next();
831 this->
put(key, other.
get(key));
838 virtual V
remove(
const K& key) {
840 typename std::map<K, V, COMPARATOR>::iterator iter = valueMap.find(key);
841 if (iter == valueMap.end()) {
843 __FILE__, __LINE__,
"Key is not present in this Map.");
846 V result = iter->second;
847 valueMap.erase(iter);
853 if (this->cachedEntrySet ==
NULL) {
854 this->cachedEntrySet.
reset(
new StlMapEntrySet(
this));
856 return *(this->cachedEntrySet);
859 if (this->cachedConstEntrySet ==
NULL) {
860 this->cachedConstEntrySet.
reset(
new ConstStlMapEntrySet(
this));
862 return *(this->cachedConstEntrySet);
866 if (this->cachedKeySet ==
NULL) {
867 this->cachedKeySet.
reset(
new StlMapKeySet(
this));
869 return *(this->cachedKeySet);
873 if (this->cachedConstKeySet ==
NULL) {
874 this->cachedConstKeySet.
reset(
new ConstStlMapKeySet(
this));
876 return *(this->cachedConstKeySet);
880 if (this->cachedValueCollection ==
NULL) {
881 this->cachedValueCollection.
reset(
new StlMapValueCollection(
this));
883 return *(this->cachedValueCollection);
887 if (this->cachedConstValueCollection ==
NULL) {
888 this->cachedConstValueCollection.
reset(
new ConstStlMapValueCollection(
this));
890 return *(this->cachedConstValueCollection);
911 virtual void wait(
long long millisecs ) {
912 mutex.
wait( millisecs );
915 virtual void wait(
long long millisecs,
int nanos ) {
916 mutex.
wait( millisecs, nanos );