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() {}
238 return this->checkHasNext();
243 return *(this->currentEntry);
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() {}
265 return this->checkHasNext();
270 return this->currentEntry->
getKey();
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() {}
292 return this->checkHasNext();
297 return this->currentEntry->
getValue();
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() {}
366 return this->checkHasNext();
371 return *(this->currentEntry);
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() {}
394 return this->checkHasNext();
399 return this->currentEntry->
getKey();
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() {}
422 return this->checkHasNext();
427 return this->currentEntry->
getValue();
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() {}
542 using HashMap<K, V, HASHCODE>::ConstHashMapKeySet::iterator;
545 return new ConstKeyIterator(this->associatedMap);
558 LinkedHashMapValueCollection(
const LinkedHashMapValueCollection&);
559 LinkedHashMapValueCollection& operator= (
const LinkedHashMapValueCollection&);
567 virtual ~LinkedHashMapValueCollection() {}
570 return new ValueIterator(this->associatedMap);
574 return new ConstValueIterator(this->associatedMap);
585 ConstLinkedHashMapValueCollection(
const ConstLinkedHashMapValueCollection&);
586 ConstLinkedHashMapValueCollection& operator= (
const ConstLinkedHashMapValueCollection&);
590 ConstLinkedHashMapValueCollection(
const LinkedHashMap* parent) :
594 virtual ~ConstLinkedHashMapValueCollection() {}
597 return new ConstValueIterator(this->associatedMap);
600 using HashMap<K, V, HASHCODE>::ConstHashMapValueCollection::iterator;
636 HashMap<K, V, HASHCODE>(capacity, load), accessOrder(false), head(), tail() {
657 HashMap<K, V, HASHCODE>(capacity, load), accessOrder(order), head(), tail() {
703 LinkedHashMapEntry* entry = head;
704 while (entry !=
NULL) {
708 entry = entry->chainForward;
719 virtual bool put(
const K& key,
const V& value) {
720 bool result = this->
putImpl(key, value);
730 virtual bool put(
const K& key,
const V& value, V& oldValue) {
731 bool result = this->
putImpl(key, value, oldValue);
743 LinkedHashMapEntry* toRemove = (LinkedHashMapEntry*) this->
removeEntry(key);
744 if (toRemove !=
NULL) {
745 LinkedHashMapEntry* prev = toRemove->chainBackward;
746 LinkedHashMapEntry* next = toRemove->chainForward;
748 prev->chainForward = next;
753 next->chainBackward = prev;
764 __FILE__, __LINE__,
"Specified key not present in the Map.");
783 this->
cachedKeySet.reset(
new LinkedHashMapKeySet(
this));
810 return "LinkedHashMap";
815 virtual LinkedHashMapEntry*
getEntry(
const K& key)
const {
816 LinkedHashMapEntry* result =
NULL;
819 int index = hash & (this->
elementData.length() - 1);
820 result = (LinkedHashMapEntry*) this->
findKeyEntry(key, index, hash);
822 if (result !=
NULL && accessOrder && tail != result) {
823 LinkedHashMapEntry* prev = result->chainBackward;
824 LinkedHashMapEntry* next = result->chainForward;
825 next->chainBackward = prev;
827 prev->chainForward = next;
831 result->chainForward =
NULL;
832 result->chainBackward = tail;
833 tail->chainForward = result;
841 LinkedHashMapEntry* entry =
new LinkedHashMapEntry(key, value);
849 LinkedHashMapEntry* entry =
new LinkedHashMapEntry(key, V(), hash);
869 LinkedHashMapEntry* prev = entry->chainBackward;
870 LinkedHashMapEntry* next = entry->chainForward;
876 next->chainBackward =
NULL;
877 entry->chainBackward = tail;
878 entry->chainForward =
NULL;
879 tail->chainForward = entry;
884 entry->chainBackward = tail;
885 entry->chainForward =
NULL;
886 tail->chainForward = entry;
899 prev->chainForward = next;
900 next->chainBackward = prev;
901 entry->chainForward =
NULL;
902 entry->chainBackward = tail;
903 tail->chainForward = entry;
908 virtual bool putImpl(
const K& key,
const V& value) {
910 return putImpl(key, value, oldValue);
913 virtual bool putImpl(
const K& key,
const V& value, V& oldValue) {
915 LinkedHashMapEntry* entry;
920 bool replaced =
true;
922 int index = hash & (this->
elementData.length() - 1);
924 entry = (LinkedHashMapEntry*) this->
findKeyEntry(key, index, hash);
Definition IllegalStateException.h:32
Definition UnsupportedOperationException.h:32
The root interface in the collection hierarchy.
Definition Collection.h:69
Definition ConcurrentModificationException.h:28
virtual Iterator< MapEntry< K, V > > * iterator()
Definition HashMap.h:550
virtual Iterator< K > * iterator()
Definition HashMap.h:644
virtual Iterator< V > * iterator()
Definition HashMap.h:724
HashMapEntry * next
Definition HashMap.h:108
virtual Iterator< MapEntry< K, V > > * iterator()
Definition HashMap.h:501
virtual Iterator< K > * iterator()
Definition HashMap.h:600
virtual Iterator< V > * iterator()
Definition HashMap.h:685
decaf::lang::ArrayPointer< HashMapEntry * > elementData
Definition HashMap.h:749
decaf::lang::Pointer< HashMapKeySet > cachedKeySet
Definition HashMap.h:769
int threshold
Definition HashMap.h:765
decaf::lang::Pointer< HashMapEntrySet > cachedEntrySet
Definition HashMap.h:768
virtual void clear()
Removes all of the mappings from this map (optional operation).
Definition HashMap.h:933
void rehash()
Definition HashMap.h:1208
decaf::lang::Pointer< ConstHashMapKeySet > cachedConstKeySet
Definition HashMap.h:774
HASHCODE hashFunc
The Hash Code generator for this map's keys.
Definition HashMap.h:739
HashMapEntry * findKeyEntry(const K &key, int index, int keyHash) const
Definition HashMap.h:1181
decaf::lang::Pointer< HashMapValueCollection > cachedValueCollection
Definition HashMap.h:770
HashMap()
Creates a new empty HashMap with default configuration settings.
Definition HashMap.h:805
int elementCount
Definition HashMap.h:744
void removeEntry(HashMapEntry *entry)
Definition HashMap.h:1213
decaf::lang::Pointer< ConstHashMapValueCollection > cachedConstValueCollection
Definition HashMap.h:775
int modCount
Definition HashMap.h:755
decaf::lang::Pointer< ConstHashMapEntrySet > cachedConstEntrySet
Definition HashMap.h:773
Defines an object that can be used to iterate over the elements of a collection.
Definition Iterator.h:34
virtual bool hasNext() const=0
virtual MapEntry< K, V > next()=0
virtual LinkedHashMapEntry * getEntry(const K &key) const
Definition LinkedHashMap.h:815
virtual std::string toString() const
Definition LinkedHashMap.h:809
virtual HashMap< K, V, HASHCODE >::HashMapEntry * createHashedEntry(const K &key, int index, int hash)
Definition LinkedHashMap.h:848
virtual const Collection< V > & values() const
Definition LinkedHashMap.h:802
virtual Set< MapEntry< K, V > > & entrySet()
Returns a Set view of the mappings contained in this map.
Definition LinkedHashMap.h:767
virtual bool putImpl(const K &key, const V &value)
Definition LinkedHashMap.h:908
LinkedHashMap(int capacity, float load)
Constructs a new LinkedHashMap instance with the specified capacity and load factor.
Definition LinkedHashMap.h:635
void linkEntry(LinkedHashMapEntry *entry)
Definition LinkedHashMap.h:856
virtual bool putImpl(const K &key, const V &value, V &oldValue)
Definition LinkedHashMap.h:913
LinkedHashMap(int capacity, float load, bool order)
Constructs a new LinkedHashMap instance with the specified capacity, load factor and a flag specifyin...
Definition LinkedHashMap.h:656
virtual void clear()
Removes all of the mappings from this map (optional operation).
Definition LinkedHashMap.h:713
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:719
LinkedHashMap()
Constructs an empty insertion-ordered LinkedHashMap instance with the default initial capacity (16) a...
Definition LinkedHashMap.h:609
virtual const Set< K > & keySet() const
Definition LinkedHashMap.h:788
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 LinkedHashMap.h:741
virtual const Set< MapEntry< K, V > > & entrySet() const
Definition LinkedHashMap.h:774
virtual Collection< V > & values()
Returns a Collection view of the values contained in this map.
Definition LinkedHashMap.h:795
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:730
virtual Set< K > & keySet()
Returns a Set view of the keys contained in this map.
Definition LinkedHashMap.h:781
LinkedHashMap(const HashMap< K, V > &map)
Constructs a new LinkedHashMap instance containing the mappings from the specified map.
Definition LinkedHashMap.h:667
virtual bool containsValue(const V &value) const
Returns true if this map maps one or more keys to the specified value.
Definition LinkedHashMap.h:702
LinkedHashMap(int capacity)
Constructs a new LinkedHashMap instance with the specified capacity.
Definition LinkedHashMap.h:620
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:698
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:685
virtual ~LinkedHashMap()
Definition LinkedHashMap.h:670
virtual HashMap< K, V, HASHCODE >::HashMapEntry * createEntry(const K &key, int index, const V &value)
Definition LinkedHashMap.h:840
virtual void setValue(const V &value)
Definition MapEntry.h:65
virtual K & getKey()
Definition MapEntry.h:57
virtual V & getValue()
Definition MapEntry.h:69
Definition NoSuchElementException.h:31
A collection that contains no duplicate elements.
Definition Set.h:45
#define NULL
Definition Config.h:33
Definition AbstractCollection.h:33
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
Definition AprPool.h:25