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());
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");
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 {
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) {
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 {
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 {
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 {
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 );
virtual bool equals(const StlMap &source) const
Definition: StlMap.h:672
void reset(T *value=NULL)
Resets the Pointer to hold the new value.
Definition: Pointer.h:161
Definition: ConcurrentModificationException.h:27
virtual bool tryLock()
Attempts to Lock the object, if the lock is already held by another thread than this method returns f...
Mutex object that offers recursive support on all platforms as well as providing the ability to use t...
Definition: Mutex.h:39
This class provides a skeletal implementation of the Collection interface, to minimize the effort req...
Definition: AbstractCollection.h:58
A collection that contains no duplicate elements.
Definition: Set.h:45
virtual bool equals(const Map< K, V > &source) const
Compares the specified object with this map for equality.Returns true if the two maps represent the s...
Definition: StlMap.h:679
virtual bool containsKey(const K &key) const
Returns true if this map contains a mapping for the specified key.More formally, returns true if and ...
Definition: StlMap.h:721
Map template that wraps around a std::map to provide a more user-friendly interface and to provide co...
Definition: StlMap.h:47
#define NULL
Definition: Config.h:33
virtual bool containsValue(const V &value) const
Returns true if this map maps one or more keys to the specified value.More formally, returns true if and only if this map contains at least one mapping to a value v such that (value==v). This operation will probably require time linear in the map size for most implementations of the Map interface.The Value to look up in this Map.true if this map contains at least one mapping for the value, otherwise false.
Definition: StlMap.h:734
virtual Collection< V > & values()
Returns a Collection view of the values contained in this map.
Definition: StlMap.h:879
virtual void unlock()
Unlocks the object.
Definition: StlMap.h:903
virtual void wait(long long millisecs, int nanos)
Waits on a signal from this object, which is generated by a call to Notify.
Definition: StlMap.h:915
virtual void unlock()
Unlocks the object.
virtual K & getKey()
Definition: MapEntry.h:57
virtual V remove(const K &key)
Removes the value (key/value pair) for the specified key from the map, returns a copy of the value th...
Definition: StlMap.h:838
virtual void wait()
Waits on a signal from this object, which is generated by a call to Notify.
Defines an object that can be used to iterate over the elements of a collection.
Definition: Iterator.h:34
virtual V & getValue()
Definition: MapEntry.h:69
virtual const Set< MapEntry< K, V > > & entrySet() const
Definition: StlMap.h:858
Definition: UnsupportedOperationException.h:32
virtual void copy(const StlMap &source)
Definition: StlMap.h:698
virtual void putAll(const StlMap< K, V, COMPARATOR > &other)
Definition: StlMap.h:819
virtual void wait(long long millisecs)
Waits on a signal from this object, which is generated by a call to Notify.
Definition: StlMap.h:911
virtual ~StlMap()
Definition: StlMap.h:667
virtual const Set< K > & keySet() const
Definition: StlMap.h:872
virtual V & get(const K &key)=0
Gets the value mapped to the specified key in the Map.
virtual int size() const
The number of elements (key/value pairs) in this map.
Definition: StlMap.h:759
virtual void notifyAll()
Signals the waiters on this object that it can now wake up and continue.
virtual decaf::util::Iterator< E > * iterator()=0
virtual void notify()
Signals a waiter on this object that it can now wake up and continue.
virtual void lock()
Locks the object.
An object that maps keys to values.
Definition: Map.h:88
virtual void clear()
Removes all of the mappings from this map (optional operation).The map will be empty after this call ...
Definition: StlMap.h:714
virtual void putAll(const Map< K, V > &other)
Copies all of the mappings from the specified map to this map (optional operation).The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress.A Map instance whose elements are to all be inserted in this Map.If the implementing class does not support the putAll operation.
Definition: StlMap.h:827
Definition: MapEntry.h:27
Definition: IllegalStateException.h:32
virtual Set< K > & keySet()
Returns a Set view of the keys contained in this map.
Definition: StlMap.h:865
virtual Set< K > & keySet()=0
Returns a Set view of the keys contained in this map.
virtual Set< MapEntry< K, V > > & entrySet()
Returns a Set view of the mappings contained in this map.
Definition: StlMap.h:852
virtual bool put(const K &key, const V &value, V &oldValue)
Associates the specified value with the specified key in this map (optional operation).If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.)This method accepts a reference to a value which will be assigned the previous value for the given key (if any). If there was no previous mapping for the given key the out value is not written to. A return of true indicates that a value was replaced by this put operation.The target key. The value to be set. (out) The value previously held in the mapping for this key. .true if the put operation replaced a value that was associated with an existing mapping to the given key or false otherwise.if this map is unmodifiable. if some property of the specified key or value prevents it from being stored in this map
Definition: StlMap.h:805
virtual bool tryLock()
Attempts to Lock the object, if the lock is already held by another thread than this method returns f...
Definition: StlMap.h:899
#define DECAF_UNUSED
Definition: Config.h:160
Definition: NoSuchElementException.h:31
StlMap()
Default constructor - does nothing.
Definition: StlMap.h:638
This class provides a skeletal implementation of the Set interface to minimize the effort required to...
Definition: AbstractSet.h:46
StlMap(const Map< K, V > &source)
Copy constructor - copies the content of the given map into this one.
Definition: StlMap.h:661
virtual V & get(const K &key)
Gets the value mapped to the specified key in the Map.If there is no element in the map whose key is ...
Definition: StlMap.h:766
virtual const Collection< V > & values() const
Definition: StlMap.h:886
virtual void wait()
Waits on a signal from this object, which is generated by a call to Notify.
Definition: StlMap.h:907
virtual void copy(const Map< K, V > &source)
Copies the content of the source map into this map.Erases all existing mappings in this map...
Definition: StlMap.h:706
virtual bool isEmpty() const
if the Map contains any element or not, TRUE or FALSE
Definition: StlMap.h:752
virtual void notify()
Signals a waiter on this object that it can now wake up and continue.
Definition: StlMap.h:919
StlMap(const StlMap &source)
Copy constructor - copies the content of the given map into this one.
Definition: StlMap.h:649
virtual bool put(const K &key, const V &value)
Associates the specified value with the specified key in this map (optional operation).If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.)The target key. The value to be set.true if the put operation replaced a value that was associated with an existing mapping to the given key or false otherwise.if this map is unmodifiable. if some property of the specified key or value prevents it from being stored in this map
Definition: StlMap.h:792
virtual void lock()
Locks the object.
Definition: StlMap.h:895
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: AprPool.h:25
virtual void notifyAll()
Signals the waiters on this object that it can now wake up and continue.
Definition: StlMap.h:923