18 #ifndef _DECAF_UTIL_LINKEDHASHMAP_H_ 19 #define _DECAF_UTIL_LINKEDHASHMAP_H_ 110 template<
typename K,
typename V,
typename HASHCODE = HashCode<K> >
117 LinkedHashMapEntry(
const LinkedHashMapEntry&);
118 LinkedHashMapEntry& operator= (
const LinkedHashMapEntry&);
122 LinkedHashMapEntry* chainForward;
123 LinkedHashMapEntry* chainBackward;
127 LinkedHashMapEntry(
const K& key,
const V& value,
int hash) :
131 LinkedHashMapEntry(
const K& key,
const V& value) :
139 mutable LinkedHashMapEntry* head;
140 mutable LinkedHashMapEntry* tail;
144 class AbstractMapIterator {
147 int expectedModCount;
148 LinkedHashMapEntry* futureEntry;
149 LinkedHashMapEntry* currentEntry;
154 AbstractMapIterator(
const AbstractMapIterator&);
155 AbstractMapIterator& operator= (
const AbstractMapIterator&);
160 futureEntry(parent->head),
162 associatedMap(parent) {
165 virtual ~AbstractMapIterator() {}
167 void checkConcurrentMod()
const {
168 if (expectedModCount != associatedMap->
modCount) {
170 __FILE__, __LINE__,
"LinkedHashMap modified outside this iterator");
174 virtual bool checkHasNext()
const {
175 return (futureEntry !=
NULL);
179 checkConcurrentMod();
180 if (!checkHasNext()) {
182 __FILE__, __LINE__,
"No next element");
184 currentEntry = futureEntry;
185 futureEntry = futureEntry->chainForward;
188 virtual void doRemove() {
189 checkConcurrentMod();
190 if (currentEntry ==
NULL) {
192 __FILE__, __LINE__,
"Remove called before call to next()");
195 LinkedHashMapEntry* entry = currentEntry;
196 LinkedHashMapEntry* prev = entry->chainBackward;
197 LinkedHashMapEntry* next = entry->chainForward;
205 prev->chainForward = next;
207 next->chainBackward = prev;
214 next->chainBackward =
NULL;
224 class EntryIterator :
public Iterator< MapEntry<K,V> >,
public AbstractMapIterator {
227 EntryIterator(
const EntryIterator&);
228 EntryIterator& operator= (
const EntryIterator&);
232 EntryIterator(
LinkedHashMap* parent) : AbstractMapIterator(parent) {
235 virtual ~EntryIterator() {}
237 virtual bool hasNext()
const {
238 return this->checkHasNext();
243 return *(this->currentEntry);
246 virtual void remove() {
251 class KeyIterator :
public Iterator<K>,
public AbstractMapIterator {
254 KeyIterator(
const KeyIterator&);
255 KeyIterator& operator= (
const KeyIterator&);
259 KeyIterator(
LinkedHashMap* parent) : AbstractMapIterator(parent) {
262 virtual ~KeyIterator() {}
264 virtual bool hasNext()
const {
265 return this->checkHasNext();
270 return this->currentEntry->getKey();
273 virtual void remove() {
278 class ValueIterator :
public Iterator<V>,
public AbstractMapIterator {
281 ValueIterator(
const ValueIterator&);
282 ValueIterator& operator= (
const ValueIterator&);
286 ValueIterator(
LinkedHashMap* parent) : AbstractMapIterator(parent) {
289 virtual ~ValueIterator() {}
291 virtual bool hasNext()
const {
292 return this->checkHasNext();
297 return this->currentEntry->getValue();
300 virtual void remove() {
307 class ConstAbstractMapIterator {
310 int expectedModCount;
311 const LinkedHashMapEntry* futureEntry;
312 const LinkedHashMapEntry* currentEntry;
317 ConstAbstractMapIterator(
const ConstAbstractMapIterator&);
318 ConstAbstractMapIterator& operator= (
const ConstAbstractMapIterator&);
323 futureEntry(parent->head),
325 associatedMap(parent) {
328 virtual ~ConstAbstractMapIterator() {}
330 virtual bool checkHasNext()
const {
331 return (futureEntry !=
NULL);
334 void checkConcurrentMod()
const {
335 if (expectedModCount != associatedMap->
modCount) {
337 __FILE__, __LINE__,
"LinkedHashMap modified outside this iterator");
342 checkConcurrentMod();
343 if (!checkHasNext()) {
345 __FILE__, __LINE__,
"No next element");
347 currentEntry = futureEntry;
348 futureEntry = futureEntry->chainForward;
352 class ConstEntryIterator :
public Iterator< MapEntry<K,V> >,
public ConstAbstractMapIterator {
355 ConstEntryIterator(
const ConstEntryIterator&);
356 ConstEntryIterator& operator= (
const ConstEntryIterator&);
360 ConstEntryIterator(
const LinkedHashMap* parent) : ConstAbstractMapIterator(parent) {
363 virtual ~ConstEntryIterator() {}
365 virtual bool hasNext()
const {
366 return this->checkHasNext();
371 return *(this->currentEntry);
374 virtual void remove() {
376 __FILE__, __LINE__,
"Cannot write to a const Iterator." );
380 class ConstKeyIterator :
public Iterator<K>,
public ConstAbstractMapIterator {
383 ConstKeyIterator(
const ConstKeyIterator&);
384 ConstKeyIterator& operator= (
const ConstKeyIterator&);
388 ConstKeyIterator(
const LinkedHashMap* parent) : ConstAbstractMapIterator(parent) {
391 virtual ~ConstKeyIterator() {}
393 virtual bool hasNext()
const {
394 return this->checkHasNext();
399 return this->currentEntry->getKey();
402 virtual void remove() {
404 __FILE__, __LINE__,
"Cannot write to a const Iterator." );
408 class ConstValueIterator :
public Iterator<V>,
public ConstAbstractMapIterator {
411 ConstValueIterator(
const ConstValueIterator&);
412 ConstValueIterator& operator= (
const ConstValueIterator&);
416 ConstValueIterator(
const LinkedHashMap* parent) : ConstAbstractMapIterator(parent) {
419 virtual ~ConstValueIterator() {}
421 virtual bool hasNext()
const {
422 return this->checkHasNext();
427 return this->currentEntry->getValue();
430 virtual void remove() {
432 __FILE__, __LINE__,
"Cannot write to a const Iterator." );
446 LinkedHashMapEntrySet(
const LinkedHashMapEntrySet&);
447 LinkedHashMapEntrySet& operator= (
const LinkedHashMapEntrySet&);
455 virtual ~LinkedHashMapEntrySet() {}
458 return new EntryIterator(associatedMap);
462 return new ConstEntryIterator(associatedMap);
474 ConstLinkedHashMapEntrySet(
const ConstLinkedHashMapEntrySet&);
475 ConstLinkedHashMapEntrySet& operator= (
const ConstLinkedHashMapEntrySet&);
483 virtual ~ConstLinkedHashMapEntrySet() {}
487 __FILE__, __LINE__,
"Can't return a non-const iterator for a const collection");
491 return new ConstEntryIterator(associatedMap);
504 LinkedHashMapKeySet(
const LinkedHashMapKeySet&);
505 LinkedHashMapKeySet& operator= (
const LinkedHashMapKeySet&);
513 virtual ~LinkedHashMapKeySet() {}
516 return new KeyIterator(this->associatedMap);
520 return new ConstKeyIterator(this->associatedMap);
531 ConstLinkedHashMapKeySet(
const ConstLinkedHashMapKeySet&);
532 ConstLinkedHashMapKeySet& operator= (
const ConstLinkedHashMapKeySet&);
540 virtual ~ConstLinkedHashMapKeySet() {}
543 return new ConstKeyIterator(this->associatedMap);
556 LinkedHashMapValueCollection(
const LinkedHashMapValueCollection&);
557 LinkedHashMapValueCollection& operator= (
const LinkedHashMapValueCollection&);
565 virtual ~LinkedHashMapValueCollection() {}
568 return new ValueIterator(this->associatedMap);
572 return new ConstValueIterator(this->associatedMap);
583 ConstLinkedHashMapValueCollection(
const ConstLinkedHashMapValueCollection&);
584 ConstLinkedHashMapValueCollection& operator= (
const ConstLinkedHashMapValueCollection&);
588 ConstLinkedHashMapValueCollection(
const LinkedHashMap* parent) :
592 virtual ~ConstLinkedHashMapValueCollection() {}
595 return new ConstValueIterator(this->associatedMap);
632 HashMap<K, V, HASHCODE>(capacity, load), accessOrder(false), head(), tail() {
653 HashMap<K, V, HASHCODE>(capacity, load), accessOrder(order), head(), tail() {
699 LinkedHashMapEntry* entry = head;
700 while (entry !=
NULL) {
701 if (value == entry->getValue()) {
704 entry = entry->chainForward;
715 virtual bool put(
const K& key,
const V& value) {
716 bool result = this->
putImpl(key, value);
720 this->
remove(head->getKey());
726 virtual bool put(
const K& key,
const V& value, V& oldValue) {
727 bool result = this->
putImpl(key, value, oldValue);
731 this->
remove(head->getKey());
737 virtual V
remove(
const K& key) {
739 LinkedHashMapEntry* toRemove = (LinkedHashMapEntry*) this->
removeEntry(key);
740 if (toRemove !=
NULL) {
741 LinkedHashMapEntry* prev = toRemove->chainBackward;
742 LinkedHashMapEntry* next = toRemove->chainForward;
744 prev->chainForward = next;
749 next->chainBackward = prev;
754 V oldValue = toRemove->getValue();
760 __FILE__, __LINE__,
"Specified key not present in the Map.");
779 this->
cachedKeySet.reset(
new LinkedHashMapKeySet(
this));
806 return "LinkedHashMap";
811 virtual LinkedHashMapEntry*
getEntry(
const K& key)
const {
812 LinkedHashMapEntry* result =
NULL;
815 int index = hash & (this->
elementData.length() - 1);
816 result = (LinkedHashMapEntry*) this->
findKeyEntry(key, index, hash);
818 if (result !=
NULL && accessOrder && tail != result) {
819 LinkedHashMapEntry* prev = result->chainBackward;
820 LinkedHashMapEntry* next = result->chainForward;
821 next->chainBackward = prev;
823 prev->chainForward = next;
827 result->chainForward =
NULL;
828 result->chainBackward = tail;
829 tail->chainForward = result;
837 LinkedHashMapEntry* entry =
new LinkedHashMapEntry(key, value);
845 LinkedHashMapEntry* entry =
new LinkedHashMapEntry(key, V(), hash);
865 LinkedHashMapEntry* prev = entry->chainBackward;
866 LinkedHashMapEntry* next = entry->chainForward;
872 next->chainBackward =
NULL;
873 entry->chainBackward = tail;
874 entry->chainForward =
NULL;
875 tail->chainForward = entry;
880 entry->chainBackward = tail;
881 entry->chainForward =
NULL;
882 tail->chainForward = entry;
895 prev->chainForward = next;
896 next->chainBackward = prev;
897 entry->chainForward =
NULL;
898 entry->chainBackward = tail;
899 tail->chainForward = entry;
904 virtual bool putImpl(
const K& key,
const V& value) {
906 return putImpl(key, value, oldValue);
909 virtual bool putImpl(
const K& key,
const V& value, V& oldValue) {
911 LinkedHashMapEntry* entry;
916 bool replaced =
true;
918 int index = hash & (this->
elementData.length() - 1);
920 entry = (LinkedHashMapEntry*) this->
findKeyEntry(key, index, hash);
931 oldValue = entry->getValue();
934 entry->setValue(value);
void removeEntry(HashMapEntry *entry)
Definition: HashMap.h:1203
virtual bool removeEldestEntry(const MapEntry< K, V > &eldest DECAF_UNUSED)
This method is queried from the put and putAll methods to check if the eldest member of the map shoul...
Definition: LinkedHashMap.h:681
virtual const Set< K > & keySet() const
Definition: LinkedHashMap.h:784
Definition: ConcurrentModificationException.h:27
decaf::lang::Pointer< HashMapKeySet > cachedKeySet
Definition: HashMap.h:769
decaf::lang::Pointer< HashMapEntrySet > cachedEntrySet
Definition: HashMap.h:768
virtual const Set< MapEntry< K, V > > & entrySet() const
Definition: LinkedHashMap.h:770
LinkedHashMap(int capacity)
Constructs a new LinkedHashMap instance with the specified capacity.
Definition: LinkedHashMap.h:616
int modCount
Definition: HashMap.h:755
decaf::lang::Pointer< ConstHashMapValueCollection > cachedConstValueCollection
Definition: HashMap.h:775
A collection that contains no duplicate elements.
Definition: Set.h:45
virtual bool putImpl(const K &key, const V &value)
Definition: LinkedHashMap.h:904
Definition: HashMap.h:562
Definition: HashMap.h:511
LinkedHashMap()
Constructs an empty insertion-ordered LinkedHashMap instance with the default initial capacity (16) a...
Definition: LinkedHashMap.h:605
Definition: HashMap.h:656
virtual LinkedHashMapEntry * getEntry(const K &key) const
Definition: LinkedHashMap.h:811
LinkedHashMap(int capacity, float load)
Constructs a new.
Definition: LinkedHashMap.h:631
virtual void onEviction(const MapEntry< K, V > &eldest DECAF_UNUSED)
This method is called when the removeEldestEntry has returned true and a MapEntry is about to be remo...
Definition: LinkedHashMap.h:694
#define NULL
Definition: Config.h:33
virtual std::string toString() const
Definition: LinkedHashMap.h:805
Definition: HashMap.h:609
LinkedHashMap(const HashMap< K, V > &map)
Constructs a new LinkedHashMap instance containing the mappings from the specified map...
Definition: LinkedHashMap.h:663
int elementCount
Definition: HashMap.h:744
virtual void clear()
Removes all of the mappings from this map (optional operation).
Definition: HashMap.h:923
virtual const Collection< V > & values() const
Definition: LinkedHashMap.h:798
Defines an object that can be used to iterate over the elements of a collection.
Definition: Iterator.h:34
Definition: UnsupportedOperationException.h:32
virtual HashMap< K, V, HASHCODE >::HashMapEntry * createHashedEntry(const K &key, int index, int hash)
Definition: LinkedHashMap.h:844
Definition: HashMap.h:694
void linkEntry(LinkedHashMapEntry *entry)
Definition: LinkedHashMap.h:852
LinkedHashMap(int capacity, float load, bool order)
Constructs a new.
Definition: LinkedHashMap.h:652
virtual bool put(const K &key, const V &value)
Associates the specified value with the specified key in this map (optional operation).
Definition: LinkedHashMap.h:715
decaf::lang::Pointer< ConstHashMapEntrySet > cachedConstEntrySet
Definition: HashMap.h:773
Hashed and linked list implementation of the Map interface, with predictable iteration order...
Definition: LinkedHashMap.h:111
decaf::lang::ArrayPointer< HashMapEntry * > elementData
Definition: HashMap.h:749
decaf::lang::Pointer< ConstHashMapKeySet > cachedConstKeySet
Definition: HashMap.h:774
virtual void clear()
Removes all of the mappings from this map (optional operation).
Definition: LinkedHashMap.h:709
Definition: MapEntry.h:27
Definition: IllegalStateException.h:32
Hash table based implementation of the Map interface.
Definition: HashMap.h:95
HashMapEntry * findKeyEntry(const K &key, int index, int keyHash) const
Definition: HashMap.h:1171
Definition: HashMap.h:458
#define DECAF_UNUSED
Definition: Config.h:160
Definition: NoSuchElementException.h:31
virtual HashMap< K, V, HASHCODE >::HashMapEntry * createEntry(const K &key, int index, const V &value)
Definition: LinkedHashMap.h:836
virtual Collection< V > & values()
Returns a Collection view of the values contained in this map.
Definition: LinkedHashMap.h:791
virtual ~LinkedHashMap()
Definition: LinkedHashMap.h:666
HASHCODE hashFunc
The Hash Code generator for this map's keys.
Definition: HashMap.h:739
virtual bool containsValue(const V &value) const
Returns true if this map maps one or more keys to the specified value.
Definition: LinkedHashMap.h:698
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: LinkedHashMap.h:726
int threshold
Definition: HashMap.h:765
virtual Set< K > & keySet()
Returns a Set view of the keys contained in this map.
Definition: LinkedHashMap.h:777
void rehash()
Definition: HashMap.h:1198
virtual bool putImpl(const K &key, const V &value, V &oldValue)
Definition: LinkedHashMap.h:909
decaf::lang::Pointer< HashMapValueCollection > cachedValueCollection
Definition: HashMap.h:770
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: AprPool.h:25
virtual Set< MapEntry< K, V > > & entrySet()
Returns a Set view of the mappings contained in this map.
Definition: LinkedHashMap.h:763