18 #ifndef _DECAF_UTIL_CONCURRENTSTLMAP_H_
19 #define _DECAF_UTIL_CONCURRENTSTLMAP_H_
36 namespace concurrent {
50 template <
typename K,
typename V,
typename COMPARATOR = std::less<K> >
54 std::map<K,V,COMPARATOR> valueMap;
60 class AbstractMapIterator {
65 typename std::map<K,V,COMPARATOR>::iterator futureEntry;
66 typename std::map<K,V,COMPARATOR>::iterator currentEntry;
72 AbstractMapIterator(
const AbstractMapIterator&);
73 AbstractMapIterator& operator= (
const AbstractMapIterator&);
78 expectedModCount(parent->modCount),
79 futureEntry(parent->valueMap.begin()),
80 currentEntry(parent->valueMap.end()),
81 associatedMap(parent) {
84 virtual ~AbstractMapIterator() {}
86 virtual bool checkHasNext()
const {
87 synchronized(&this->associatedMap->mutex) {
88 if (futureEntry != this->associatedMap->valueMap.end()) {
95 void checkConcurrentMod()
const {
96 if (expectedModCount != this->associatedMap->modCount) {
98 __FILE__, __LINE__,
"StlMap modified outside this iterator");
103 synchronized(&this->associatedMap->mutex) {
104 checkConcurrentMod();
106 if (!checkHasNext()) {
110 currentEntry = futureEntry;
115 virtual void doRemove() {
116 synchronized(&this->associatedMap->mutex) {
117 checkConcurrentMod();
119 if (currentEntry == this->associatedMap->valueMap.end()) {
121 __FILE__, __LINE__,
"Remove called before call to next()");
124 this->associatedMap->valueMap.erase(currentEntry);
125 currentEntry = this->associatedMap->valueMap.end();
128 associatedMap->modCount++;
133 class EntryIterator :
public Iterator< MapEntry<K,V> >,
public AbstractMapIterator {
136 EntryIterator(
const EntryIterator&);
137 EntryIterator& operator= (
const EntryIterator&);
144 virtual ~EntryIterator() {}
146 virtual bool hasNext()
const {
147 return this->checkHasNext();
151 synchronized(&this->associatedMap->mutex) {
153 return MapEntry<K, V>(this->currentEntry->first, this->currentEntry->second);
159 virtual void remove() {
164 class KeyIterator :
public Iterator<K>,
public AbstractMapIterator {
167 KeyIterator(
const KeyIterator&);
168 KeyIterator& operator= (
const KeyIterator&);
175 virtual ~KeyIterator() {}
177 virtual bool hasNext()
const {
178 return this->checkHasNext();
182 synchronized(&this->associatedMap->mutex) {
184 return this->currentEntry->first;
190 virtual void remove() {
195 class ValueIterator :
public Iterator<V>,
public AbstractMapIterator {
198 ValueIterator(
const ValueIterator&);
199 ValueIterator& operator= (
const ValueIterator&);
206 virtual ~ValueIterator() {}
208 virtual bool hasNext()
const {
209 return this->checkHasNext();
213 synchronized(&this->associatedMap->mutex) {
215 return this->currentEntry->second;
221 virtual void remove() {
228 class ConstAbstractMapIterator {
231 mutable int position;
232 int expectedModCount;
233 typename std::map<K,V,COMPARATOR>::const_iterator futureEntry;
234 typename std::map<K,V,COMPARATOR>::const_iterator currentEntry;
240 ConstAbstractMapIterator(
const ConstAbstractMapIterator&);
241 ConstAbstractMapIterator& operator= (
const ConstAbstractMapIterator&);
246 expectedModCount(parent->modCount),
247 futureEntry(parent->valueMap.begin()),
248 currentEntry(parent->valueMap.end()),
249 associatedMap(parent) {
252 virtual ~ConstAbstractMapIterator() {}
254 virtual bool checkHasNext()
const {
255 synchronized(&this->associatedMap->mutex) {
256 if (futureEntry != this->associatedMap->valueMap.end()) {
263 void checkConcurrentMod()
const {
264 synchronized(&this->associatedMap->mutex) {
265 if (expectedModCount != this->associatedMap->modCount) {
267 __FILE__, __LINE__,
"StlMap modified outside this iterator");
273 synchronized(&this->associatedMap->mutex) {
274 checkConcurrentMod();
276 if (!checkHasNext()) {
280 currentEntry = futureEntry;
286 class ConstEntryIterator :
public Iterator< MapEntry<K,V> >,
public ConstAbstractMapIterator {
289 ConstEntryIterator(
const ConstEntryIterator&);
290 ConstEntryIterator& operator= (
const ConstEntryIterator&);
294 ConstEntryIterator(
const ConcurrentStlMap* parent) : ConstAbstractMapIterator(parent) {
297 virtual ~ConstEntryIterator() {}
299 virtual bool hasNext()
const {
300 return this->checkHasNext();
304 synchronized(&this->associatedMap->mutex) {
306 return MapEntry<K, V>(this->currentEntry->first, this->currentEntry->second);
312 virtual void remove() {
314 __FILE__, __LINE__,
"Cannot write to a const Iterator." );
318 class ConstKeyIterator :
public Iterator<K>,
public ConstAbstractMapIterator {
321 ConstKeyIterator(
const ConstKeyIterator&);
322 ConstKeyIterator& operator= (
const ConstKeyIterator&);
326 ConstKeyIterator(
const ConcurrentStlMap* parent) : ConstAbstractMapIterator(parent) {
329 virtual ~ConstKeyIterator() {}
331 virtual bool hasNext()
const {
332 return this->checkHasNext();
336 synchronized(&this->associatedMap->mutex) {
338 return this->currentEntry->first;
344 virtual void remove() {
346 __FILE__, __LINE__,
"Cannot write to a const Iterator." );
350 class ConstValueIterator :
public Iterator<V>,
public ConstAbstractMapIterator {
353 ConstValueIterator(
const ConstValueIterator&);
354 ConstValueIterator& operator= (
const ConstValueIterator&);
358 ConstValueIterator(
const ConcurrentStlMap* parent) : ConstAbstractMapIterator(parent) {
361 virtual ~ConstValueIterator() {}
363 virtual bool hasNext()
const {
364 return this->checkHasNext();
368 synchronized(&this->associatedMap->mutex) {
370 return this->currentEntry->second;
376 virtual void remove() {
378 __FILE__, __LINE__,
"Cannot write to a const Iterator." );
385 class StlMapEntrySet :
public AbstractSet< MapEntry<K, V> > {
392 StlMapEntrySet(
const StlMapEntrySet&);
393 StlMapEntrySet& operator= (
const StlMapEntrySet&);
400 virtual ~StlMapEntrySet() {}
402 virtual int size()
const {
403 return associatedMap->size();
406 virtual void clear() {
407 associatedMap->clear();
411 synchronized(&this->associatedMap->mutex) {
412 if (this->associatedMap->containsKey(entry.getKey()) &&
413 this->associatedMap->get(entry.getKey()) == entry.getValue()) {
414 associatedMap->remove(entry.getKey());
423 synchronized(&this->associatedMap->mutex) {
424 if (this->associatedMap->containsKey(entry.
getKey()) &&
433 return new EntryIterator(associatedMap);
437 return new ConstEntryIterator(associatedMap);
442 class ConstStlMapEntrySet :
public AbstractSet< MapEntry<K, V> > {
449 ConstStlMapEntrySet(
const ConstStlMapEntrySet&);
450 ConstStlMapEntrySet& operator= (
const ConstStlMapEntrySet&);
457 virtual ~ConstStlMapEntrySet() {}
459 virtual int size()
const {
460 return associatedMap->size();
463 virtual void clear() {
465 __FILE__, __LINE__,
"Can't clear a const collection");
470 __FILE__, __LINE__,
"Can't remove from const collection");
474 synchronized(&this->associatedMap->mutex) {
475 if (this->associatedMap->containsKey(entry.
getKey()) &&
485 __FILE__, __LINE__,
"Can't return a non-const iterator for a const collection");
489 return new ConstEntryIterator(associatedMap);
502 StlMapKeySet(
const StlMapKeySet&);
503 StlMapKeySet& operator= (
const StlMapKeySet&);
510 virtual ~StlMapKeySet() {}
512 virtual bool contains(
const K& key)
const {
513 return this->associatedMap->containsKey(key);
516 virtual int size()
const {
517 return this->associatedMap->size();
520 virtual void clear() {
521 this->associatedMap->clear();
524 virtual bool remove(
const K& key) {
525 synchronized(&this->associatedMap->mutex) {
526 if (this->associatedMap->containsKey(key)) {
527 associatedMap->remove(key);
535 return new KeyIterator(this->associatedMap);
539 return new ConstKeyIterator(this->associatedMap);
550 ConstStlMapKeySet(
const ConstStlMapKeySet&);
551 ConstStlMapKeySet& operator= (
const ConstStlMapKeySet&);
558 virtual ~ConstStlMapKeySet() {}
560 virtual bool contains(
const K& key)
const {
561 return this->associatedMap->containsKey(key);
564 virtual int size()
const {
565 return this->associatedMap->size();
568 virtual void clear() {
570 __FILE__, __LINE__,
"Can't modify a const collection");
575 __FILE__, __LINE__,
"Can't modify a const collection");
580 __FILE__, __LINE__,
"Can't return a non-const iterator for a const collection");
584 return new ConstKeyIterator(this->associatedMap);
597 StlMapValueCollection(
const StlMapValueCollection&);
598 StlMapValueCollection& operator= (
const StlMapValueCollection&);
605 virtual ~StlMapValueCollection() {}
607 virtual bool contains(
const V& value)
const {
608 return this->associatedMap->containsValue(value);
611 virtual int size()
const {
612 return this->associatedMap->size();
615 virtual void clear() {
616 this->associatedMap->clear();
620 return new ValueIterator(this->associatedMap);
624 return new ConstValueIterator(this->associatedMap);
635 ConstStlMapValueCollection(
const ConstStlMapValueCollection&);
636 ConstStlMapValueCollection& operator= (
const ConstStlMapValueCollection&);
643 virtual ~ConstStlMapValueCollection() {}
645 virtual bool contains(
const V& value)
const {
646 return this->associatedMap->containsValue(value);
649 virtual int size()
const {
650 return this->associatedMap->size();
653 virtual void clear() {
655 __FILE__, __LINE__,
"Can't modify a const collection");
660 __FILE__, __LINE__,
"Can't return a non-const iterator for a const collection");
664 return new ConstValueIterator(this->associatedMap);
686 cachedEntrySet(), cachedKeySet(), cachedValueCollection(),
687 cachedConstEntrySet(), cachedConstKeySet(), cachedConstValueCollection() {
697 cachedEntrySet(), cachedKeySet(), cachedValueCollection(),
698 cachedConstEntrySet(), cachedConstKeySet(), cachedConstValueCollection() {
708 cachedEntrySet(), cachedKeySet(), cachedValueCollection(),
709 cachedConstEntrySet(), cachedConstKeySet(), cachedConstValueCollection() {
719 synchronized(&mutex) {
720 return this->valueMap == source.valueMap;
727 synchronized(&mutex) {
728 typename std::auto_ptr< Iterator<K> > iterator(this->
keySet().iterator());
729 while (iterator->hasNext()) {
730 K key = iterator->next();
735 if (!(this->
get(key) == source.
get(key))) {
748 synchronized(&mutex) {
749 this->valueMap.clear();
750 this->valueMap.insert(source.valueMap.begin(), source.valueMap.end());
755 synchronized( &mutex ) {
765 synchronized(&mutex) {
774 typename std::map<K, V, COMPARATOR>::const_iterator iter;
776 synchronized(&mutex) {
777 if (!valueMap.empty()) {
778 iter = valueMap.find(key);
779 return iter != valueMap.end();
790 synchronized(&mutex) {
791 if (valueMap.empty()) {
795 typename std::map<K, V, COMPARATOR>::const_iterator iter = valueMap.begin();
796 for (; iter != valueMap.end(); ++iter) {
797 if ((*iter).second == value) {
810 synchronized(&mutex) {
811 return valueMap.empty();
821 synchronized(&mutex) {
822 return (
int)valueMap.size();
831 virtual V&
get(
const K& key) {
832 typename std::map<K,V,COMPARATOR>::iterator iter;
833 synchronized(&mutex) {
834 if (!valueMap.empty()) {
835 iter = valueMap.find(key);
836 if (iter != valueMap.end()) {
843 __FILE__, __LINE__,
"Key does not exist in map");
849 virtual const V&
get(
const K& key)
const {
850 typename std::map<K,V,COMPARATOR>::const_iterator iter;
851 synchronized(&mutex) {
852 if (!valueMap.empty()) {
853 iter = valueMap.find(key);
854 if (iter != valueMap.end()) {
861 __FILE__, __LINE__,
"Key does not exist in map");
867 virtual bool put(
const K& key,
const V& value) {
869 synchronized(&mutex) {
874 valueMap[key] = value;
882 virtual bool put(
const K& key,
const V& value, V& oldValue) {
884 synchronized(&mutex) {
887 oldValue = valueMap[key];
890 valueMap[key] = value;
899 synchronized(&mutex) {
900 this->valueMap.insert(other.valueMap.begin(), other.valueMap.end());
909 synchronized(&mutex) {
910 typename std::auto_ptr< Iterator<K> > iterator(other.
keySet().
iterator());
911 while (iterator->hasNext()) {
912 K key = iterator->next();
913 this->
put(key, other.
get(key));
922 virtual V
remove(
const K& key) {
924 synchronized(&mutex) {
925 if (!valueMap.empty()) {
926 typename std::map<K, V, COMPARATOR>::iterator iter = valueMap.find(key);
927 if (iter == valueMap.end()) {
930 result = iter->second;
931 valueMap.erase(iter);
963 synchronized(&mutex) {
965 this->
put(key, value);
991 bool remove(
const K& key,
const V& value) {
992 synchronized(&mutex) {
993 if( this->
containsKey( key ) && ( this->
get( key ) == value ) ) {
1021 bool replace(
const K& key,
const V& oldValue,
const V& newValue) {
1022 synchronized(&mutex) {
1023 if (this->
containsKey(key) && (this->
get(key) == oldValue)) {
1024 this->
put(key, newValue);
1053 synchronized(&mutex) {
1055 V result = this->
get(key);
1056 this->
put(key, value);
1062 __FILE__, __LINE__,
"Value to Replace was not in the Map." );
1066 synchronized(&mutex) {
1067 if (this->cachedEntrySet ==
NULL) {
1068 this->cachedEntrySet.
reset(
new StlMapEntrySet(
this));
1071 return *(this->cachedEntrySet);
1074 synchronized(&mutex) {
1075 if (this->cachedConstEntrySet ==
NULL) {
1076 this->cachedConstEntrySet.
reset(
new ConstStlMapEntrySet(
this));
1079 return *(this->cachedConstEntrySet);
1083 synchronized(&mutex) {
1084 if (this->cachedKeySet ==
NULL) {
1085 this->cachedKeySet.
reset(
new StlMapKeySet(
this));
1088 return *(this->cachedKeySet);
1092 synchronized(&mutex) {
1093 if (this->cachedConstKeySet ==
NULL) {
1094 this->cachedConstKeySet.
reset(
new ConstStlMapKeySet(
this));
1097 return *(this->cachedConstKeySet);
1101 synchronized(&mutex) {
1102 if (this->cachedValueCollection ==
NULL) {
1103 this->cachedValueCollection.
reset(
new StlMapValueCollection(
this));
1106 return *(this->cachedValueCollection);
1110 synchronized(&mutex) {
1111 if (this->cachedConstValueCollection ==
NULL) {
1112 this->cachedConstValueCollection.
reset(
new ConstStlMapValueCollection(
this));
1115 return *(this->cachedConstValueCollection);
1136 virtual void wait(
long long millisecs ) {
1137 mutex.
wait( millisecs );
1140 virtual void wait(
long long millisecs,
int nanos ) {
1141 mutex.
wait( millisecs, nanos );
void reset(T *value=NULL)
Resets the Pointer to hold the new value.
Definition: Pointer.h:161
ConcurrentStlMap()
Default constructor - does nothing.
Definition: ConcurrentStlMap.h:685
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...
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: ConcurrentStlMap.h:882
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
ConcurrentStlMap(const ConcurrentStlMap &source)
Copy constructor - copies the content of the given map into this one.
Definition: ConcurrentStlMap.h:696
A collection that contains no duplicate elements.
Definition: Set.h:45
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: ConcurrentStlMap.h:789
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: ConcurrentStlMap.h:908
#define NULL
Definition: Config.h:33
ConcurrentStlMap(const Map< K, V > &source)
Copy constructor - copies the content of the given map into this one.
Definition: ConcurrentStlMap.h:707
virtual void clear()
Removes all of the mappings from this map (optional operation).The map will be empty after this call ...
Definition: ConcurrentStlMap.h:764
virtual Set< MapEntry< K, V > > & entrySet()
Returns a Set view of the mappings contained in this map.
Definition: ConcurrentStlMap.h:1065
virtual void unlock()
Unlocks the object.
virtual void notify()
Signals a waiter on this object that it can now wake up and continue.
Definition: ConcurrentStlMap.h:1144
virtual K & getKey()
Definition: MapEntry.h:57
V replace(const K &key, const V &value)
Replace entry for key only if currently mapped to some value.
Definition: ConcurrentStlMap.h:1052
virtual Collection< V > & values()
Returns a Collection view of the values contained in this map.
Definition: ConcurrentStlMap.h:1100
virtual bool equals(const ConcurrentStlMap &source) const
Definition: ConcurrentStlMap.h:718
virtual ~ConcurrentStlMap()
Definition: ConcurrentStlMap.h:713
virtual void wait()
Waits on a signal from this object, which is generated by a call to Notify.
bool replace(const K &key, const V &oldValue, const V &newValue)
Replace entry for key only if currently mapped to given value.
Definition: ConcurrentStlMap.h:1021
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 void lock()
Locks the object.
Definition: ConcurrentStlMap.h:1120
virtual void putAll(const ConcurrentStlMap< K, V, COMPARATOR > &other)
Definition: ConcurrentStlMap.h:898
bool putIfAbsent(const K &key, const V &value)
If the specified key is not already associated with a value, associate it with the given value...
Definition: ConcurrentStlMap.h:962
Definition: UnsupportedOperationException.h:32
Map template that wraps around a std::map to provide a more user-friendly interface and to provide co...
Definition: ConcurrentStlMap.h:51
virtual bool isEmpty() const
if the Map contains any element or not, TRUE or FALSE
Definition: ConcurrentStlMap.h:809
virtual void copy(const Map< K, V > &source)
Copies the content of the source map into this map.
Definition: ConcurrentStlMap.h:754
virtual bool equals(const Map< K, V > &source) const
Compares the specified object with this map for equality.
Definition: ConcurrentStlMap.h:726
virtual V & get(const K &key)=0
Gets the value mapped to the specified key in the Map.
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.
virtual int size() const
The number of elements (key/value pairs) in this map.
Definition: ConcurrentStlMap.h:820
An object that maps keys to values.
Definition: Map.h:88
virtual void wait()
Waits on a signal from this object, which is generated by a call to Notify.
Definition: ConcurrentStlMap.h:1132
virtual bool tryLock()
Attempts to Lock the object, if the lock is already held by another thread than this method returns f...
Definition: ConcurrentStlMap.h:1124
Definition: MapEntry.h:27
Definition: IllegalStateException.h:32
virtual const Collection< V > & values() const
Definition: ConcurrentStlMap.h:1109
virtual void wait(long long millisecs, int nanos)
Waits on a signal from this object, which is generated by a call to Notify.
Definition: ConcurrentStlMap.h:1140
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: ConcurrentStlMap.h:867
virtual Set< K > & keySet()=0
Returns a Set view of the keys contained in this map.
#define DECAF_UNUSED
Definition: Config.h:160
Definition: NoSuchElementException.h:31
virtual void notifyAll()
Signals the waiters on this object that it can now wake up and continue.
Definition: ConcurrentStlMap.h:1148
This class provides a skeletal implementation of the Set interface to minimize the effort required to...
Definition: AbstractSet.h:46
virtual void unlock()
Unlocks the object.
Definition: ConcurrentStlMap.h:1128
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: ConcurrentStlMap.h:773
virtual const Set< MapEntry< K, V > > & entrySet() const
Definition: ConcurrentStlMap.h:1073
virtual void copy(const ConcurrentStlMap &source)
Definition: ConcurrentStlMap.h:747
virtual void wait(long long millisecs)
Waits on a signal from this object, which is generated by a call to Notify.
Definition: ConcurrentStlMap.h:1136
Interface for a Map type that provides additional atomic putIfAbsent, remove, and replace methods alo...
Definition: ConcurrentMap.h:39
virtual const Set< K > & keySet() const
Definition: ConcurrentStlMap.h:1091
virtual Set< K > & keySet()
Returns a Set view of the keys contained in this map.
Definition: ConcurrentStlMap.h:1082