51 std::map<K,V,COMPARATOR> valueMap;
57 class AbstractMapIterator {
62 typename std::map<K,V,COMPARATOR>::iterator futureEntry;
63 typename std::map<K,V,COMPARATOR>::iterator currentEntry;
69 AbstractMapIterator(
const AbstractMapIterator&);
70 AbstractMapIterator& operator= (
const AbstractMapIterator&);
74 AbstractMapIterator(
StlMap* parent) : position(0),
75 expectedModCount(parent->modCount),
76 futureEntry(parent->valueMap.begin()),
77 currentEntry(parent->valueMap.end()),
78 associatedMap(parent) {
81 virtual ~AbstractMapIterator() {}
83 virtual bool checkHasNext()
const {
84 if (futureEntry != this->associatedMap->valueMap.end()) {
90 void checkConcurrentMod()
const {
91 if (expectedModCount != this->associatedMap->modCount) {
93 __FILE__, __LINE__,
"StlMap modified outside this iterator");
100 if (!checkHasNext()) {
104 currentEntry = futureEntry;
108 virtual void doRemove() {
110 checkConcurrentMod();
112 if (currentEntry == this->associatedMap->valueMap.end()) {
114 __FILE__, __LINE__,
"Remove called before call to next()");
117 this->associatedMap->valueMap.erase(currentEntry);
118 currentEntry = this->associatedMap->valueMap.end();
121 associatedMap->modCount++;
125 class EntryIterator :
public Iterator< MapEntry<K,V> >,
public AbstractMapIterator {
128 EntryIterator(
const EntryIterator&);
129 EntryIterator& operator= (
const EntryIterator&);
133 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);
151 class KeyIterator :
public Iterator<K>,
public AbstractMapIterator {
154 KeyIterator(
const KeyIterator&);
155 KeyIterator& operator= (
const KeyIterator&);
159 KeyIterator(
StlMap* parent) : AbstractMapIterator(parent) {}
161 virtual ~KeyIterator() {}
163 virtual bool hasNext()
const {
164 return this->checkHasNext();
169 return this->currentEntry->first;
177 class ValueIterator :
public Iterator<V>,
public AbstractMapIterator {
180 ValueIterator(
const ValueIterator&);
181 ValueIterator& operator= (
const ValueIterator&);
185 ValueIterator(
StlMap* parent) : AbstractMapIterator(parent) {
188 virtual ~ValueIterator() {}
190 virtual bool hasNext()
const {
191 return this->checkHasNext();
196 return this->currentEntry->second;
206 class ConstAbstractMapIterator {
209 mutable int position;
210 int expectedModCount;
211 typename std::map<K,V,COMPARATOR>::const_iterator futureEntry;
212 typename std::map<K,V,COMPARATOR>::const_iterator currentEntry;
214 const StlMap* associatedMap;
218 ConstAbstractMapIterator(
const ConstAbstractMapIterator&);
219 ConstAbstractMapIterator& operator= (
const ConstAbstractMapIterator&);
223 ConstAbstractMapIterator(
const StlMap* parent) : position(0),
224 expectedModCount(parent->modCount),
225 futureEntry(parent->valueMap.begin()),
226 currentEntry(parent->valueMap.end()),
227 associatedMap(parent) {
230 virtual ~ConstAbstractMapIterator() {}
232 virtual bool checkHasNext()
const {
233 if (futureEntry != this->associatedMap->valueMap.end()) {
239 void checkConcurrentMod()
const {
240 if (expectedModCount != this->associatedMap->modCount) {
242 __FILE__, __LINE__,
"StlMap modified outside this iterator");
247 checkConcurrentMod();
249 if (!checkHasNext()) {
253 currentEntry = futureEntry;
258 class ConstEntryIterator :
public Iterator< MapEntry<K,V> >,
public ConstAbstractMapIterator {
261 ConstEntryIterator(
const ConstEntryIterator&);
262 ConstEntryIterator& operator= (
const ConstEntryIterator&);
266 ConstEntryIterator(
const StlMap* parent) : ConstAbstractMapIterator(parent) {}
268 virtual ~ConstEntryIterator() {}
270 virtual bool hasNext()
const {
271 return this->checkHasNext();
276 return MapEntry<K, V>(this->currentEntry->first, this->currentEntry->second);
281 __FILE__, __LINE__,
"Cannot write to a const Iterator." );
285 class ConstKeyIterator :
public Iterator<K>,
public ConstAbstractMapIterator {
288 ConstKeyIterator(
const ConstKeyIterator&);
289 ConstKeyIterator& operator= (
const ConstKeyIterator&);
293 ConstKeyIterator(
const StlMap* parent) : ConstAbstractMapIterator(parent) {
296 virtual ~ConstKeyIterator() {}
298 virtual bool hasNext()
const {
299 return this->checkHasNext();
304 return this->currentEntry->first;
309 __FILE__, __LINE__,
"Cannot write to a const Iterator." );
313 class ConstValueIterator :
public Iterator<V>,
public ConstAbstractMapIterator {
316 ConstValueIterator(
const ConstValueIterator&);
317 ConstValueIterator& operator= (
const ConstValueIterator&);
321 ConstValueIterator(
const StlMap* parent) : ConstAbstractMapIterator(parent) {}
323 virtual ~ConstValueIterator() {}
325 virtual bool hasNext()
const {
326 return this->checkHasNext();
331 return this->currentEntry->second;
336 __FILE__, __LINE__,
"Cannot write to a const Iterator." );
343 class StlMapEntrySet :
public AbstractSet< MapEntry<K, V> > {
350 StlMapEntrySet(
const StlMapEntrySet&);
351 StlMapEntrySet& operator= (
const StlMapEntrySet&);
357 virtual ~StlMapEntrySet() {}
359 virtual int size()
const {
360 return associatedMap->
size();
363 virtual void clear() {
364 associatedMap->
clear();
386 return new EntryIterator(associatedMap);
390 return new ConstEntryIterator(associatedMap);
395 class ConstStlMapEntrySet :
public AbstractSet< MapEntry<K, V> > {
398 const StlMap* associatedMap;
402 ConstStlMapEntrySet(
const ConstStlMapEntrySet&);
403 ConstStlMapEntrySet& operator= (
const ConstStlMapEntrySet&);
410 virtual ~ConstStlMapEntrySet() {}
412 virtual int size()
const {
413 return associatedMap->
size();
416 virtual void clear() {
418 __FILE__, __LINE__,
"Can't clear a const collection");
423 __FILE__, __LINE__,
"Can't remove from const collection");
436 __FILE__, __LINE__,
"Can't return a non-const iterator for a const collection");
440 return new ConstEntryIterator(associatedMap);
453 StlMapKeySet(
const StlMapKeySet&);
454 StlMapKeySet& operator= (
const StlMapKeySet&);
460 virtual ~StlMapKeySet() {}
462 virtual bool contains(
const K& key)
const {
466 virtual int size()
const {
467 return this->associatedMap->
size();
470 virtual void clear() {
471 this->associatedMap->
clear();
474 virtual bool remove(
const K& key) {
476 associatedMap->
remove(key);
483 return new KeyIterator(this->associatedMap);
487 return new ConstKeyIterator(this->associatedMap);
494 const StlMap* associatedMap;
498 ConstStlMapKeySet(
const ConstStlMapKeySet&);
499 ConstStlMapKeySet& operator= (
const ConstStlMapKeySet&);
505 virtual ~ConstStlMapKeySet() {}
507 virtual bool contains(
const K& key)
const {
511 virtual int size()
const {
512 return this->associatedMap->
size();
515 virtual void clear() {
517 __FILE__, __LINE__,
"Can't modify a const collection");
520 virtual bool remove(
const K& key DECAF_UNUSED) {
522 __FILE__, __LINE__,
"Can't modify a const collection");
527 __FILE__, __LINE__,
"Can't return a non-const iterator for a const collection");
531 return new ConstKeyIterator(this->associatedMap);
544 StlMapValueCollection(
const StlMapValueCollection&);
545 StlMapValueCollection& operator= (
const StlMapValueCollection&);
551 virtual ~StlMapValueCollection() {}
553 virtual bool contains(
const V& value)
const {
557 virtual int size()
const {
558 return this->associatedMap->
size();
561 virtual void clear() {
562 this->associatedMap->
clear();
566 return new ValueIterator(this->associatedMap);
570 return new ConstValueIterator(this->associatedMap);
577 const StlMap* associatedMap;
581 ConstStlMapValueCollection(
const ConstStlMapValueCollection&);
582 ConstStlMapValueCollection& operator= (
const ConstStlMapValueCollection&);
588 virtual ~ConstStlMapValueCollection() {}
590 virtual bool contains(
const V& value)
const {
594 virtual int size()
const {
595 return this->associatedMap->
size();
598 virtual void clear() {
600 __FILE__, __LINE__,
"Can't modify a const collection");
605 __FILE__, __LINE__,
"Can't return a non-const iterator for a const collection");
609 return new ConstValueIterator(this->associatedMap);
630 StlMap() :
Map<K,V>(), valueMap(), mutex(), modCount(0),
631 cachedEntrySet(), cachedKeySet(), cachedValueCollection(),
632 cachedConstEntrySet(), cachedConstKeySet(), cachedConstValueCollection() {
642 cachedEntrySet(), cachedKeySet(), cachedValueCollection(),
643 cachedConstEntrySet(), cachedConstKeySet(), cachedConstValueCollection() {
654 cachedEntrySet(), cachedKeySet(), cachedValueCollection(),
655 cachedConstEntrySet(), cachedConstKeySet(), cachedConstValueCollection() {
665 return this->valueMap == source.valueMap;
672 typename std::auto_ptr< Iterator<K> > iterator(this->
keySet().iterator());
673 while (iterator->hasNext()) {
674 K key = iterator->next();
679 if (!(this->
get(key) == source.
get(key))) {
691 this->valueMap.clear();
692 this->valueMap.insert( source.valueMap.begin(), source.valueMap.end() );
714 if (valueMap.empty()) {
718 typename std::map<K, V, COMPARATOR>::const_iterator iter;
719 iter = valueMap.find(key);
720 return iter != valueMap.end();
727 if (valueMap.empty()) {
731 typename std::map<K, V, COMPARATOR>::const_iterator iter = valueMap.begin();
732 for (; iter != valueMap.end(); ++iter) {
733 if ((*iter).second == value) {
745 return valueMap.empty();
752 return (
int)valueMap.size();
758 virtual V&
get(
const K& key) {
759 typename std::map<K, V, COMPARATOR>::iterator iter;
760 iter = valueMap.find(key);
761 if (iter == valueMap.end()) {
771 virtual const V&
get(
const K& key)
const {
772 typename std::map<K, V, COMPARATOR>::const_iterator iter;
773 iter = valueMap.find(key);
774 if (iter == valueMap.end()) {
784 virtual bool put(
const K& key,
const V& value) {
789 valueMap[key] = value;
797 virtual bool put(
const K& key,
const V& value, V& oldValue) {
801 oldValue = valueMap[key];
803 valueMap[key] = value;
812 this->valueMap.insert(other.valueMap.begin(), other.valueMap.end());
820 typename std::auto_ptr< Iterator<K> > iterator(other.
keySet().
iterator());
821 while (iterator->hasNext()) {
822 K key = iterator->next();
823 this->
put(key, other.
get(key));
832 typename std::map<K, V, COMPARATOR>::iterator iter = valueMap.find(key);
833 if (iter == valueMap.end()) {
835 __FILE__, __LINE__,
"Key is not present in this Map.");
838 V result = iter->second;
839 valueMap.erase(iter);
845 if (this->cachedEntrySet ==
NULL) {
846 this->cachedEntrySet.
reset(
new StlMapEntrySet(
this));
848 return *(this->cachedEntrySet);
851 if (this->cachedConstEntrySet ==
NULL) {
852 this->cachedConstEntrySet.
reset(
new ConstStlMapEntrySet(
this));
854 return *(this->cachedConstEntrySet);
858 if (this->cachedKeySet ==
NULL) {
859 this->cachedKeySet.
reset(
new StlMapKeySet(
this));
861 return *(this->cachedKeySet);
865 if (this->cachedConstKeySet ==
NULL) {
866 this->cachedConstKeySet.
reset(
new ConstStlMapKeySet(
this));
868 return *(this->cachedConstKeySet);
872 if (this->cachedValueCollection ==
NULL) {
873 this->cachedValueCollection.
reset(
new StlMapValueCollection(
this));
875 return *(this->cachedValueCollection);
879 if (this->cachedConstValueCollection ==
NULL) {
880 this->cachedConstValueCollection.
reset(
new ConstStlMapValueCollection(
this));
882 return *(this->cachedConstValueCollection);
892 return mutex.tryLock();
903 virtual void wait(
long long millisecs ) {
904 mutex.wait( millisecs );
907 virtual void wait(
long long millisecs,
int nanos ) {
908 mutex.wait( millisecs, nanos );