18#ifndef _DECAF_UTIL_STLMAP_H_
19#define _DECAF_UTIL_STLMAP_H_
47 template <
typename K,
typename V,
typename COMPARATOR = decaf::util::comparators::Less<K> >
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);
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) {}
161 virtual ~KeyIterator() {}
163 virtual bool hasNext()
const {
164 return this->checkHasNext();
169 return this->currentEntry->first;
172 virtual void remove() {
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;
199 virtual void remove() {
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);
279 virtual void remove() {
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;
307 virtual void remove() {
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;
334 virtual void remove() {
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");
421 virtual bool remove(
const MapEntry<K,V>& entry DECAF_UNUSED) {
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);
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 );
virtual decaf::util::Iterator< E > * iterator()=0
Decaf's implementation of a Smart Pointer that is a template on a Type and is Thread Safe if the defa...
Definition: Pointer.h:53
void reset(T *value=NULL)
Resets the Pointer to hold the new value.
Definition: Pointer.h:161
Definition: IllegalStateException.h:32
Definition: UnsupportedOperationException.h:32
This class provides a skeletal implementation of the Collection interface, to minimize the effort req...
Definition: AbstractCollection.h:58
This class provides a skeletal implementation of the Set interface to minimize the effort required to...
Definition: AbstractSet.h:47
The root interface in the collection hierarchy.
Definition: Collection.h:69
Definition: ConcurrentModificationException.h:28
Defines an object that can be used to iterate over the elements of a collection.
Definition: Iterator.h:34
Definition: MapEntry.h:27
virtual K & getKey()
Definition: MapEntry.h:57
virtual V & getValue()
Definition: MapEntry.h:69
An object that maps keys to values.
Definition: Map.h:88
virtual V & get(const K &key)=0
Gets the value mapped to the specified key in the Map.
virtual Set< K > & keySet()=0
Returns a Set view of the keys contained in this map.
Definition: NoSuchElementException.h:31
A collection that contains no duplicate elements.
Definition: Set.h:45
Map template that wraps around a std::map to provide a more user-friendly interface and to provide co...
Definition: StlMap.h:48
StlMap(const StlMap &source)
Copy constructor - copies the content of the given map into this one.
Definition: StlMap.h:641
virtual void lock()
Locks the object.
Definition: StlMap.h:887
virtual void putAll(const Map< K, V > &other)
Copies all of the mappings from the specified map to this map (optional operation)....
Definition: StlMap.h:819
virtual const V & get(const K &key) const
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:771
virtual bool equals(const StlMap &source) const
Definition: StlMap.h:664
virtual int size() const
The number of elements (key/value pairs) in this map.
Definition: StlMap.h:751
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:891
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:758
virtual Set< MapEntry< K, V > > & entrySet()
Returns a Set view of the mappings contained in this map.
Definition: StlMap.h:844
StlMap()
Default constructor - does nothing.
Definition: StlMap.h:630
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:830
StlMap(const Map< K, V > &source)
Copy constructor - copies the content of the given map into this one.
Definition: StlMap.h:653
virtual Collection< V > & values()
Returns a Collection view of the values contained in this map.
Definition: StlMap.h:871
virtual void notifyAll()
Signals the waiters on this object that it can now wake up and continue.
Definition: StlMap.h:915
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:698
virtual Set< K > & keySet()
Returns a Set view of the keys contained in this map.
Definition: StlMap.h:857
virtual bool put(const K &key, const V &value)
Associates the specified value with the specified key in this map (optional operation)....
Definition: StlMap.h:784
virtual ~StlMap()
Definition: StlMap.h:659
virtual void putAll(const StlMap< K, V, COMPARATOR > &other)
Definition: StlMap.h:811
virtual void copy(const StlMap &source)
Definition: StlMap.h:690
virtual const Set< MapEntry< K, V > > & entrySet() const
Definition: StlMap.h:850
virtual const Set< K > & keySet() const
Definition: StlMap.h:864
virtual bool put(const K &key, const V &value, V &oldValue)
Associates the specified value with the specified key in this map (optional operation)....
Definition: StlMap.h:797
virtual void notify()
Signals a waiter on this object that it can now wake up and continue.
Definition: StlMap.h:911
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:907
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:713
virtual bool isEmpty() const
if the Map contains any element or not, TRUE or FALSE
Definition: StlMap.h:744
virtual void unlock()
Unlocks the object.
Definition: StlMap.h:895
virtual void wait(long long millisecs)
Waits on a signal from this object, which is generated by a call to Notify.
Definition: StlMap.h:903
virtual bool containsValue(const V &value) const
Returns true if this map maps one or more keys to the specified value.More formally,...
Definition: StlMap.h:726
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:671
virtual void clear()
Removes all of the mappings from this map (optional operation).The map will be empty after this call ...
Definition: StlMap.h:706
virtual const Collection< V > & values() const
Definition: StlMap.h:878
virtual void wait()
Waits on a signal from this object, which is generated by a call to Notify.
Definition: StlMap.h:899
Mutex object that offers recursive support on all platforms as well as providing the ability to use t...
Definition: Mutex.h:39
virtual void lock()
Locks the object.
virtual void notifyAll()
Signals the waiters on this object that it can now wake up and continue.
virtual bool tryLock()
Attempts to Lock the object, if the lock is already held by another thread than this method returns f...
virtual void unlock()
Unlocks the object.
virtual void wait()
Waits on a signal from this object, which is generated by a call to Notify.
virtual void notify()
Signals a waiter on this object that it can now wake up and continue.
#define NULL
Definition: Config.h:33
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
Definition: AprPool.h:25