com.badlogic.gdx.utils
Class ArrayMap<K,V>

java.lang.Object
  extended by com.badlogic.gdx.utils.ArrayMap<K,V>

public class ArrayMap<K,V>
extends Object

An ordered or unordered map of objects. This implementation uses arrays to store the keys and values, which means gets do a comparison for each key in the map. This is slower than a typical hash map implementation, but may be acceptable for small maps and has the benefits that keys and values can be accessed by index, which makes iteration fast. Like Array, if ordered is false, * this class avoids a memory copy when removing elements (the last element is moved to the removed element's position).

Author:
Nathan Sweet

Nested Class Summary
static class ArrayMap.Entries<K,V>
           
static class ArrayMap.Keys<K>
           
static class ArrayMap.Values<V>
           
 
Field Summary
 K[] keys
           
 boolean ordered
           
 int size
           
 V[] values
           
 
Constructor Summary
ArrayMap()
          Creates an ordered map with a capacity of 16.
ArrayMap(ArrayMap array)
          Creates a new map containing the elements in the specified map.
ArrayMap(boolean ordered, int capacity)
           
ArrayMap(boolean ordered, int capacity, Class keyArrayType, Class valueArrayType)
          Creates a new map with keys and values of the specified type.
ArrayMap(Class keyArrayType, Class valueArrayType)
          Creates an ordered map with keys and values of the specified type and a capacity of 16.
ArrayMap(int capacity)
          Creates an ordered map with the specified capacity.
 
Method Summary
 void clear()
           
 void clear(int maximumCapacity)
          Clears the map and reduces the size of the backing arrays to be the specified capacity if they are larger.
 boolean containsKey(K key)
           
 boolean containsValue(V value, boolean identity)
           
 void ensureCapacity(int additionalCapacity)
          Increases the size of the backing arrays to acommodate the specified number of additional entries.
 ArrayMap.Entries<K,V> entries()
          Returns an iterator for the entries in the map.
 K firstKey()
           
 V firstValue()
           
 V get(K key)
          Returns the value for the specified key.
 K getKey(V value, boolean identity)
          Returns the key for the specified value.
 K getKeyAt(int index)
           
 V getValueAt(int index)
           
 int indexOfKey(K key)
           
 int indexOfValue(V value, boolean identity)
           
 void insert(int index, K key, V value)
           
 ArrayMap.Keys<K> keys()
          Returns an iterator for the keys in the map.
 K peekKey()
          Returns the last key.
 V peekValue()
          Returns the last value.
 void put(K key, V value)
           
 void put(K key, V value, int index)
           
 void putAll(ArrayMap map)
           
 void putAll(ArrayMap map, int offset, int length)
           
 void removeIndex(int index)
          Removes and returns the key/values pair at the specified index.
 V removeKey(K key)
           
 boolean removeValue(V value, boolean identity)
           
 void reverse()
           
 void setKey(int index, K key)
           
 void setValue(int index, V value)
           
 void shrink()
          Reduces the size of the backing arrays to the size of the actual number of entries.
 void shuffle()
           
 String toString()
           
 void truncate(int newSize)
          Reduces the size of the arrays to the specified size.
 ArrayMap.Values<V> values()
          Returns an iterator for the values in the map.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

keys

public K[] keys

values

public V[] values

size

public int size

ordered

public boolean ordered
Constructor Detail

ArrayMap

public ArrayMap()
Creates an ordered map with a capacity of 16.


ArrayMap

public ArrayMap(int capacity)
Creates an ordered map with the specified capacity.


ArrayMap

public ArrayMap(boolean ordered,
                int capacity)
Parameters:
ordered - If false, methods that remove elements may change the order of other elements in the arrays, which avoids a memory copy.
capacity - Any elements added beyond this will cause the backing arrays to be grown.

ArrayMap

public ArrayMap(boolean ordered,
                int capacity,
                Class keyArrayType,
                Class valueArrayType)
Creates a new map with keys and values of the specified type.

Parameters:
ordered - If false, methods that remove elements may change the order of other elements in the arrays, which avoids a memory copy.
capacity - Any elements added beyond this will cause the backing arrays to be grown.

ArrayMap

public ArrayMap(Class keyArrayType,
                Class valueArrayType)
Creates an ordered map with keys and values of the specified type and a capacity of 16.


ArrayMap

public ArrayMap(ArrayMap array)
Creates a new map containing the elements in the specified map. The new map will have the same type of backing arrays and will be ordered if the specified map is ordered. The capacity is set to the number of elements, so any subsequent elements added will cause the backing arrays to be grown.

Method Detail

put

public void put(K key,
                V value)

put

public void put(K key,
                V value,
                int index)

putAll

public void putAll(ArrayMap map)

putAll

public void putAll(ArrayMap map,
                   int offset,
                   int length)

get

public V get(K key)
Returns the value for the specified key. Note this does a .equals() comparison of each key in reverse order until the specified key is found.


getKey

public K getKey(V value,
                boolean identity)
Returns the key for the specified value. Note this does a comparison of each value in reverse order until the specified value is found.

Parameters:
identity - If true, == comparison will be used. If false, .equals() comaparison will be used.

getKeyAt

public K getKeyAt(int index)

getValueAt

public V getValueAt(int index)

firstKey

public K firstKey()

firstValue

public V firstValue()

setKey

public void setKey(int index,
                   K key)

setValue

public void setValue(int index,
                     V value)

insert

public void insert(int index,
                   K key,
                   V value)

containsKey

public boolean containsKey(K key)

containsValue

public boolean containsValue(V value,
                             boolean identity)
Parameters:
identity - If true, == comparison will be used. If false, .equals() comaparison will be used.

indexOfKey

public int indexOfKey(K key)

indexOfValue

public int indexOfValue(V value,
                        boolean identity)

removeKey

public V removeKey(K key)

removeValue

public boolean removeValue(V value,
                           boolean identity)

removeIndex

public void removeIndex(int index)
Removes and returns the key/values pair at the specified index.


peekKey

public K peekKey()
Returns the last key.


peekValue

public V peekValue()
Returns the last value.


clear

public void clear(int maximumCapacity)
Clears the map and reduces the size of the backing arrays to be the specified capacity if they are larger.


clear

public void clear()

shrink

public void shrink()
Reduces the size of the backing arrays to the size of the actual number of entries. This is useful to release memory when many items have been removed, or if it is known that more entries will not be added.


ensureCapacity

public void ensureCapacity(int additionalCapacity)
Increases the size of the backing arrays to acommodate the specified number of additional entries. Useful before adding many entries to avoid multiple backing array resizes.


reverse

public void reverse()

shuffle

public void shuffle()

truncate

public void truncate(int newSize)
Reduces the size of the arrays to the specified size. If the arrays are already smaller than the specified size, no action is taken.


toString

public String toString()
Overrides:
toString in class Object

entries

public ArrayMap.Entries<K,V> entries()
Returns an iterator for the entries in the map. Remove is supported. Note that the same iterator instance is returned each time this method is called. Use the ArrayMap.Entries constructor for nested or multithreaded iteration.


values

public ArrayMap.Values<V> values()
Returns an iterator for the values in the map. Remove is supported. Note that the same iterator instance is returned each time this method is called. Use the ArrayMap.Entries constructor for nested or multithreaded iteration.


keys

public ArrayMap.Keys<K> keys()
Returns an iterator for the keys in the map. Remove is supported. Note that the same iterator instance is returned each time this method is called. Use the ArrayMap.Entries constructor for nested or multithreaded iteration.



Copyright © 2013. All Rights Reserved.