Javolution 6.0.0 java
javolution.util.internal.map.FractalMapImpl Class Reference
Collaboration diagram for javolution.util.internal.map.FractalMapImpl:
[legend]

Public Member Functions

 FractalMapImpl ()
 
 FractalMapImpl (int shift)
 
MapEntryImpl addEntry (MapEntryImpl newEntry, Object key, int hash)
 
void clear ()
 
MapEntryImpl getEntry (Object key, int hash)
 
MapEntryImpl removeEntry (Object key, int hash)
 

Static Package Attributes

static final int EMPTINESS_LEVEL = 2
 
static final int INITIAL_BLOCK_CAPACITY = 2 << EMPTINESS_LEVEL
 
static final int SHIFT = 10
 

Private Member Functions

int indexOfKey (Object key, int hash)
 
void resize (int newCapacity)
 

Private Attributes

int count
 
MapEntryImpl[] entries = new MapEntryImpl[INITIAL_BLOCK_CAPACITY]
 
final int shift
 

Detailed Description

A fractal-based map with rehash performed only on limited size maps. It is based on a fractal structure with self-similar patterns at any scale (maps holding submaps). At each depth only a part of the hashcode is used starting by the last bits.

Definition at line 18 of file FractalMapImpl.java.

Constructor & Destructor Documentation

◆ FractalMapImpl() [1/2]

javolution.util.internal.map.FractalMapImpl.FractalMapImpl ( )

Definition at line 28 of file FractalMapImpl.java.

28  {
29  this.shift = 0;
30  }

◆ FractalMapImpl() [2/2]

javolution.util.internal.map.FractalMapImpl.FractalMapImpl ( int  shift)

Definition at line 32 of file FractalMapImpl.java.

32  {
33  this.shift = shift;
34  }

Member Function Documentation

◆ addEntry()

MapEntryImpl javolution.util.internal.map.FractalMapImpl.addEntry ( MapEntryImpl  newEntry,
Object  key,
int  hash 
)

Adds the specified entry if not already present; returns either the specified entry or an existing entry for the specified key.

Definition at line 39 of file FractalMapImpl.java.

39  {
40  int i = indexOfKey(key, hash);
41  MapEntryImpl entry = entries[i];
42  if (entry != null) return entry; // Entry exists
43  entries[i] = newEntry;
44  newEntry.key = key;
45  newEntry.hash = hash;
46  // Check if we need to resize.
47  if ((++count << EMPTINESS_LEVEL) > entries.length) {
48  resize(entries.length << 1);
49  }
50  return newEntry;
51  }

References javolution.util.internal.map.MapEntryImpl< K, V >.key.

Referenced by javolution.util.internal.map.FastMapImpl< K, V >.put(), and javolution.util.internal.map.FastMapImpl< K, V >.putIfAbsent().

Here is the caller graph for this function:

◆ clear()

void javolution.util.internal.map.FractalMapImpl.clear ( )

Definition at line 53 of file FractalMapImpl.java.

53  {
54  entries = new MapEntryImpl[INITIAL_BLOCK_CAPACITY];
55  count = 0;
56  }

◆ getEntry()

MapEntryImpl javolution.util.internal.map.FractalMapImpl.getEntry ( Object  key,
int  hash 
)

Returns null if no entry with specified key

Definition at line 59 of file FractalMapImpl.java.

59  {
60  return entries[indexOfKey(key, hash)];
61  }

Referenced by javolution.util.internal.map.FastMapImpl< K, V >.containsKey(), javolution.util.internal.map.FastMapImpl< K, V >.get(), javolution.util.internal.map.FastMapImpl< K, V >.remove(), and javolution.util.internal.map.FastMapImpl< K, V >.replace().

Here is the caller graph for this function:

◆ indexOfKey()

int javolution.util.internal.map.FractalMapImpl.indexOfKey ( Object  key,
int  hash 
)
private

Returns the index of the specified key in the map (points to a null key if key not present).

Definition at line 91 of file FractalMapImpl.java.

91  {
92  int mask = entries.length - 1;
93  int i = (hash >> shift) & mask;
94  while (true) {
95  MapEntryImpl entry = entries[i];
96  if (entry == null) return i;
97  if ((entry.hash == hash) && key.equals(entry.key)) return i;
98  i = (i + 1) & mask;
99  }
100  }

References javolution.util.internal.map.MapEntryImpl< K, V >.hash, and javolution.util.internal.map.MapEntryImpl< K, V >.key.

◆ removeEntry()

MapEntryImpl javolution.util.internal.map.FractalMapImpl.removeEntry ( Object  key,
int  hash 
)

Returns the entry removed or null if none.

Definition at line 64 of file FractalMapImpl.java.

64  {
65  int i = indexOfKey(key, hash);
66  MapEntryImpl oldEntry = entries[i];
67  if (oldEntry == null) return null; // Entry does not exist.
68  entries[i] = null;
69  // Since we have made a hole, adjacent keys might have to shift.
70  for (;;) {
71  // We use a step of 1 (improve caching through memory locality).
72  i = (i + 1) & (entries.length - 1);
73  MapEntryImpl entry = entries[i];
74  if (entry == null) break; // Done.
75  int correctIndex = indexOfKey(entry.key, entry.hash);
76  if (correctIndex != i) { // Misplaced.
77  entries[correctIndex] = entries[i];
78  entries[i] = null;
79  }
80  }
81  // Check if we need to resize.
82  if (((--count << (EMPTINESS_LEVEL + 1)) <= entries.length)
83  && (entries.length > INITIAL_BLOCK_CAPACITY)) {
84  resize(entries.length >> 1);
85  }
86  return oldEntry;
87  }

References javolution.util.internal.map.MapEntryImpl< K, V >.hash, and javolution.util.internal.map.MapEntryImpl< K, V >.key.

Referenced by javolution.util.internal.map.FastMapImpl< K, V >.iterator(), and javolution.util.internal.map.FastMapImpl< K, V >.remove().

Here is the caller graph for this function:

◆ resize()

void javolution.util.internal.map.FractalMapImpl.resize ( int  newCapacity)
private

Definition at line 105 of file FractalMapImpl.java.

105  {
106  MapEntryImpl[] newEntries = new MapEntryImpl[newCapacity];
107  int newMask = newEntries.length - 1;
108  for (int i = 0, n = entries.length; i < n; i++) {
109  MapEntryImpl entry = entries[i];
110  if (entry == null) continue;
111  int newIndex = entry.hash & newMask;
112  while (newEntries[newIndex] != null) { // Find empty slot.
113  newIndex = (newIndex + 1) & newMask;
114  }
115  newEntries[newIndex] = entry;
116  }
117  entries = newEntries;
118  }

References javolution.util.internal.map.MapEntryImpl< K, V >.hash.

Member Data Documentation

◆ count

int javolution.util.internal.map.FractalMapImpl.count
private

Definition at line 24 of file FractalMapImpl.java.

◆ EMPTINESS_LEVEL

final int javolution.util.internal.map.FractalMapImpl.EMPTINESS_LEVEL = 2
staticpackage

Definition at line 20 of file FractalMapImpl.java.

◆ entries

MapEntryImpl [] javolution.util.internal.map.FractalMapImpl.entries = new MapEntryImpl[INITIAL_BLOCK_CAPACITY]
private

Definition at line 25 of file FractalMapImpl.java.

◆ INITIAL_BLOCK_CAPACITY

final int javolution.util.internal.map.FractalMapImpl.INITIAL_BLOCK_CAPACITY = 2 << EMPTINESS_LEVEL
staticpackage

Definition at line 21 of file FractalMapImpl.java.

◆ SHIFT

final int javolution.util.internal.map.FractalMapImpl.SHIFT = 10
staticpackage

Definition at line 22 of file FractalMapImpl.java.

◆ shift

final int javolution.util.internal.map.FractalMapImpl.shift
private

Definition at line 26 of file FractalMapImpl.java.


The documentation for this class was generated from the following file:
javolution.util.internal.map.FractalMapImpl.shift
final int shift
Definition: FractalMapImpl.java:26
javolution.util.internal.map.FractalMapImpl.INITIAL_BLOCK_CAPACITY
static final int INITIAL_BLOCK_CAPACITY
Definition: FractalMapImpl.java:21
javolution.util.internal.map.FractalMapImpl.indexOfKey
int indexOfKey(Object key, int hash)
Definition: FractalMapImpl.java:91
javolution.util.internal.map.MapEntryImpl.key
K key
Definition: MapEntryImpl.java:19
javolution.util.internal.map.FractalMapImpl.resize
void resize(int newCapacity)
Definition: FractalMapImpl.java:105
javolution.util.internal.map.MapEntryImpl.hash
int hash
Definition: MapEntryImpl.java:18
javolution.util.internal.map.FractalMapImpl.entries
MapEntryImpl[] entries
Definition: FractalMapImpl.java:25
javolution.util.internal.map.FractalMapImpl.count
int count
Definition: FractalMapImpl.java:24
javolution.util.internal.map.FractalMapImpl.EMPTINESS_LEVEL
static final int EMPTINESS_LEVEL
Definition: FractalMapImpl.java:20