Class IdMap
- All Implemented Interfaces:
Serializable
,Cloneable
,Comparator<Object>
With an identifier map a key can be mapped to an identifier
(an integer value) and vice versa. In addition, an optional value
may be associated with each key/identifier. When being added to
the map, the keys are numbered consecutively, starting with 0.
That is, the identifiers of the keys always range from 0 to
size()-1
.
- Since:
- 2006.09.11
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected util.IdMapElem[]
the array of hash binsprotected static final int
the block size for the inverse map arrayprotected Comparator<Object>
the comparator for sorting the mapprotected static final int
a mask to ensure that the hash values are positiveprotected util.IdMapElem[]
the inverse map (identifier to key)protected float
the maximum load factorprotected int
the maximum number of elementsprotected int
the mode for reacting to adding an existing key to the mapstatic final int
when adding an existing key: reject key (return negative id)static final int
when adding an existing key: replace old associated valueprotected int
the current number of elements -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal int
Add a key to an identifier map.final int
Add a key/value pair to an identifier map.final void
clear()
Clear the identifier map, that is, remove all keys/values.final Object
clone()
Clone this identifier map.final int
Compare two identifier map elements.final Object
get
(int id) Look up the key associated with an identifier.final int
Look up the identifier associated with a key.final int
Look up the identifier associated with a key.final Object
getKey
(int id) Look up the key associated with an identifier.final int[]
getMapFrom
(IdMap idmap) Get identifier mapping from another identifier map.final int[]
Get identifier mapping to another identifier map.final int
getSize()
Get the number of mappings in an identifier map.final Object
getValue
(int id) Look up the value associated with an identifier.final Object
Look up the value associated with a key.final void
move
(int src, int dst) Move a key in the identifier map.protected final void
rehash()
Enlarge the hash bin array and rehash the map elements.final int[]
remove
(boolean[] rem) Remove several key/value pair from an identifier map.final void
remove
(int id) Remove a key/value pair from an identifier map.final int[]
remove
(int[] ids) Remove several key/value pair from an identifier map.final void
Remove a key/value pair from an identifier map.protected final void
remove
(util.IdMapElem e) Remove an identifier map element.final void
reorder
(int[] map) Reorder an identifier map.final void
Replace a key in an identifier map.final void
Set the value associated with an identifier.final void
Set the value associated with a key.final int
size()
Get the number of mappings in an identifier map.final int[]
sort
(Comparator<Object> cmp) Reorder an identifier map.Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Comparator
equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
-
Field Details
-
REPLACE
public static final int REPLACEwhen adding an existing key: replace old associated value- See Also:
-
REJECT
public static final int REJECTwhen adding an existing key: reject key (return negative id)- See Also:
-
HASHMASK
protected static final int HASHMASKa mask to ensure that the hash values are positive- See Also:
-
BLKSIZE
protected static final int BLKSIZEthe block size for the inverse map array- See Also:
-
mode
protected int modethe mode for reacting to adding an existing key to the map -
load
protected float loadthe maximum load factor -
max
protected int maxthe maximum number of elements -
size
protected int sizethe current number of elements -
bins
protected util.IdMapElem[] binsthe array of hash bins -
imap
protected util.IdMapElem[] imapthe inverse map (identifier to key) -
cmp
the comparator for sorting the map
-
-
Constructor Details
-
IdMap
public IdMap(int mode, int init, float load) Create an identifier map.- Parameters:
mode
- the mode for reacting to adding an existing keyinit
- the initial size of the map (hash table size)load
- the maximum load factor for the hash table- Since:
- 2006.09.11 (Christian Borgelt)
-
IdMap
public IdMap(int mode, int init) Create an identifier map.- Parameters:
mode
- the mode for reacting to adding an existing keyinit
- the initial size of the map (hash table size)- Since:
- 2006.09.11 (Christian Borgelt)
-
IdMap
public IdMap(int mode) Create an identifier map.- Parameters:
mode
- the mode for reacting to adding an existing key- Since:
- 2006.09.11 (Christian Borgelt)
-
IdMap
public IdMap()Create an identifier map.Equivalent to
IdMap(REPLACE)
.- Since:
- 2007.04.13 (Christian Borgelt)
-
-
Method Details
-
clone
Clone this identifier map.The created clone is a deep copy, with the exception of the keys and values that are associated with identifiers.
-
clear
public final void clear()Clear the identifier map, that is, remove all keys/values.- Since:
- 2007.01.31 (Christian Borgelt)
-
size
public final int size()Get the number of mappings in an identifier map.- Returns:
- the number of key/identifier pairs stored
- Since:
- 2006.09.11 (Christian Borgelt)
-
getSize
public final int getSize()Get the number of mappings in an identifier map.- Returns:
- the number of key/identifier pairs stored
- Since:
- 2017.06.17 (Christian Borgelt)
-
rehash
protected final void rehash()Enlarge the hash bin array and rehash the map elements.- Since:
- 2006.09.11 (Christian Borgelt)
-
add
Add a key to an identifier map.If the key is already present, no new mapping is added, but the identifier already associated with the key is returned, thus automatically avoiding duplicate entries. In addition, a possibly associated value is deleted.
- Parameters:
key
- the key to add to the map- Returns:
- the identifier assigned to the key
- Since:
- 2006.09.11 (Christian Borgelt)
-
add
Add a key/value pair to an identifier map.If the key is already present, no new mapping is added, but the identifier already associated with the key is returned, thus automatically avoiding duplicate entries. In addition, the associated value is replaced.
- Parameters:
key
- the key to add to the mapvalue
- the value associated with the key- Returns:
- the identifier assigned to the key
- Since:
- 2007.01.31 (Christian Borgelt)
-
get
Look up the identifier associated with a key.- Parameters:
key
- the key for which to find the associated identifier- Returns:
- the identifier assigned to the key or
-1
if the key is not contained in the map - Since:
- 2006.09.11 (Christian Borgelt)
-
getId
Look up the identifier associated with a key.- Parameters:
key
- the key for which to find the associated identifier- Returns:
- the identifier assigned to the key or
-1
if the key is not contained in the map - Since:
- 2017.06.14 (Christian Borgelt)
-
getValue
Look up the value associated with a key.- Parameters:
key
- the key for which to find the associated value- Returns:
- the value associated with the identifier
- Since:
- 2007.01.31 (Christian Borgelt)
-
get
Look up the key associated with an identifier.- Parameters:
id
- identifier for which to find the associated key- Returns:
- the key associated with the identifier
- Since:
- 2006.09.11 (Christian Borgelt)
- See Also:
-
getKey
Look up the key associated with an identifier.- Parameters:
id
- identifier for which to find the associated key- Returns:
- the key associated with the identifier
- Since:
- 2006.09.11 (Christian Borgelt)
- See Also:
-
getValue
Look up the value associated with an identifier.- Parameters:
id
- the identifier for which to find the associated value- Returns:
- the value associated with the identifier
- Since:
- 2006.09.11 (Christian Borgelt)
-
setValue
Set the value associated with a key.- Parameters:
key
- the key for which to find the associated valuevalue
- the value to associate with the key- Since:
- 2007.01.31 (Christian Borgelt)
-
setValue
Set the value associated with an identifier.- Parameters:
id
- the identifier for which to set the valuevalue
- the value to associate with the identifier- Since:
- 2007.01.31 (Christian Borgelt)
-
remove
protected final void remove(util.IdMapElem e) Remove an identifier map element.- Parameters:
e
- the identifier map element to remove- Since:
- 2007.07.15 (Christian Borgelt)
-
remove
Remove a key/value pair from an identifier map.Note that by this operation the identifiers of keys with higher identifiers than the one to be removed will be reduced, in order to preserve consecutive identifiers.
- Parameters:
key
- the key to remove- Since:
- 2006.10.17 (Christian Borgelt)
-
remove
public final void remove(int id) Remove a key/value pair from an identifier map.Note that by this operation the identifiers of keys with higher identifiers than the one to be removed will be reduced, in order to preserve consecutive identifiers.
- Parameters:
id
- the identifier of the key to remove- Since:
- 2006.10.17 (Christian Borgelt)
-
remove
public final int[] remove(int[] ids) Remove several key/value pair from an identifier map.- Parameters:
ids
- the array of identifiers of the keys to remove- Returns:
- a map from the old (array indices) to the new key identifiers (content of array elements)
- Since:
- 2017.06.14 (Christian Borgelt)
-
remove
public final int[] remove(boolean[] rem) Remove several key/value pair from an identifier map.- Parameters:
rem
- a flag array indicating the keys to remove (array index corresponds to identifier)- Returns:
- a map from the old (array indices) to the new key identifiers (content of array elements)
- Since:
- 2017.06.14 (Christian Borgelt)
-
replace
Replace a key in an identifier map.It is assumed that a key equal to the replacing key (w.r.t. the function
equals()
) is not associated with another identifier in this map, because otherwise the map gets into an inconsistent state. However, replacing a key with itself or an equal key is harmless and thus permitted.- Parameters:
id
- the identifier of the key to replacekey
- the key to replace it with- Since:
- 2006.10.06 (Christian Borgelt)
-
move
public final void move(int src, int dst) Move a key in the identifier map.- Parameters:
src
- the current identifier of the key to movedst
- the desired new identifier of the key- Since:
- 2007.01.31 (Christian Borgelt)
- See Also:
-
reorder
public final void reorder(int[] map) Reorder an identifier map.Permute the identifiers, thus reordering the keys. The desired reordering has to be stated as a permutation of the integer numbers 0 to
size()-1
, with each entry stating the new value for the identifier that is the array index of the entry (forward map).If the reordering map is not such a permutation, the identifier map will get into an inconsistent state that may lead to serious errors.
- Parameters:
map
- an integer array containing a permutation of the integer numbers 0 tosize()-1
- Since:
- 2006.10.06 (Christian Borgelt)
- See Also:
-
compare
Compare two identifier map elements.- Specified by:
compare
in interfaceComparator<Object>
- Parameters:
o1
- first element to compareo2
- second element to compare- Returns:
- a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second
- Since:
- 2007.07.26 (Christian Borgelt)
-
sort
Reorder an identifier map.- Parameters:
cmp
- the comparator for the items- Returns:
- a map from the old indices (array indices) to the new indices (contents of array elements)
- Since:
- 2007.07.26 (Christian Borgelt)
-
getMapFrom
Get identifier mapping from another identifier map.Identifiers that are present only in the identifier map given as an argument are added to this identifier map.
- Parameters:
idmap
- the other identifier map, from which to get the mapping- Returns:
- an array that is indexed with identifiers referring to the identifier map given as an argument and contains identifiers referring to this identifier map /*------------------------------------------------------------------
-
getMapTo
Get identifier mapping to another identifier map.Identifiers that are present only in this identifier map are added to the identifier map given as an argument.
- Parameters:
idmap
- the other identifier map, to which to get the mapping- Returns:
- an array that is indexed with identifiers referring to this identifier map and contains identifiers referring to the identifier map given as an argument /*------------------------------------------------------------------
-