Class IntFloatOrderedMap

java.lang.Object
com.github.tommyettinger.ds.IntFloatMap
com.github.tommyettinger.ds.IntFloatOrderedMap
All Implemented Interfaces:
Arrangeable, Ordered.OfInt, Iterable<IntFloatMap.Entry>

public class IntFloatOrderedMap extends IntFloatMap implements Ordered.OfInt
An IntFloatMap that also stores keys in an IntList using the insertion order. No allocation is done except when growing the table size.

Iteration over the entrySet(), keySet(), and values() is ordered and faster than an unordered map. Keys can also be accessed and the order changed using order(). There is some additional overhead for put and remove.

This class performs fast contains (typically O(1), worst case O(n) but that is rare in practice). Remove is somewhat slower due to order(). Add may be slightly slower, depending on hash collisions. Hashcodes are rehashed to reduce collisions and the need to resize. Load factors greater than 0.91 greatly increase the chances to resize to the next higher POT size.

Unordered sets and maps are not designed to provide especially fast iteration. Iteration is faster with Ordered types like ObjectOrderedSet and IntFloatOrderedMap.

You can customize most behavior of this map by extending it. IntFloatMap.place(int) can be overridden to change how hashCodes are calculated (which can be useful for types like StringBuilder that don't implement hashCode()), and IntFloatMap.locateKey(int) can be overridden to change how equality is calculated.

This implementation uses linear probing with the backward shift algorithm for removal. It tries different hashes from a simple family, with the hash changing on resize. Linear probing continues to work even when all hashCodes collide, just more slowly.

  • Field Details

    • keys

      protected final IntList keys
  • Constructor Details

    • IntFloatOrderedMap

      public IntFloatOrderedMap(OrderType ordering)
      Creates a new map with an initial capacity of Utilities.getDefaultTableCapacity() and a load factor of Utilities.getDefaultLoadFactor().
      Parameters:
      ordering - determines what implementation order() will use
    • IntFloatOrderedMap

      public IntFloatOrderedMap(int initialCapacity, OrderType ordering)
      Creates a new map with the given starting capacity and a load factor of Utilities.getDefaultLoadFactor().
      Parameters:
      initialCapacity - If not a power of two, it is increased to the next nearest power of two.
      ordering - determines what implementation order() will use
    • IntFloatOrderedMap

      public IntFloatOrderedMap(int initialCapacity, float loadFactor, OrderType ordering)
      Creates a new map with the specified initial capacity and load factor. This map will hold initialCapacity items before growing the backing table.
      Parameters:
      initialCapacity - If not a power of two, it is increased to the next nearest power of two.
      loadFactor - what fraction of the capacity can be filled before this has to resize; 0 < loadFactor <= 1
      ordering - determines what implementation order() will use
    • IntFloatOrderedMap

      public IntFloatOrderedMap(IntFloatOrderedMap map)
      Creates a new map identical to the specified map.
      Parameters:
      map - the map to copy
    • IntFloatOrderedMap

      public IntFloatOrderedMap(IntFloatMap map, OrderType ordering)
      Creates a new map identical to the specified map.
      Parameters:
      map - the map to copy
      ordering - determines what implementation order() will use
    • IntFloatOrderedMap

      public IntFloatOrderedMap(int[] keys, float[] values, OrderType ordering)
      Given two side-by-side arrays, one of keys, one of values, this constructs a map and inserts each pair of key and value into it. If keys and values have different lengths, this only uses the length of the smaller array.
      Parameters:
      keys - an array of keys
      values - an array of values
      ordering - determines what implementation order() will use
    • IntFloatOrderedMap

      public IntFloatOrderedMap(PrimitiveCollection.OfInt keys, PrimitiveCollection.OfFloat values, OrderType ordering)
      Given two side-by-side collections, one of keys, one of values, this constructs a map and inserts each pair of key and value into it. If keys and values have different lengths, this only uses the length of the smaller collection.
      Parameters:
      keys - a PrimitiveCollection of keys
      values - a PrimitiveCollection of values
      ordering - determines what implementation order() will use
    • IntFloatOrderedMap

      public IntFloatOrderedMap(IntFloatOrderedMap other, int offset, int count, OrderType ordering)
      Creates a new set by copying count items from the given IntFloatOrderedMap, starting at offset in that Map, into this.
      Parameters:
      other - another IntFloatOrderedMap of the same type
      offset - the first index in other's ordering to draw an item from
      count - how many items to copy from other
      ordering - determines what implementation order() will use
    • IntFloatOrderedMap

      public IntFloatOrderedMap()
      Creates a new map with an initial capacity of Utilities.getDefaultTableCapacity() and a load factor of Utilities.getDefaultLoadFactor().
    • IntFloatOrderedMap

      public IntFloatOrderedMap(int initialCapacity)
      Creates a new map with the given starting capacity and a load factor of Utilities.getDefaultLoadFactor().
      Parameters:
      initialCapacity - If not a power of two, it is increased to the next nearest power of two.
    • IntFloatOrderedMap

      public IntFloatOrderedMap(int initialCapacity, float loadFactor)
      Creates a new map with the specified initial capacity and load factor. This map will hold initialCapacity items before growing the backing table.
      Parameters:
      initialCapacity - If not a power of two, it is increased to the next nearest power of two.
      loadFactor - what fraction of the capacity can be filled before this has to resize; 0 < loadFactor <= 1
    • IntFloatOrderedMap

      public IntFloatOrderedMap(IntFloatMap map)
      Creates a new map identical to the specified map.
      Parameters:
      map - the map to copy
    • IntFloatOrderedMap

      public IntFloatOrderedMap(int[] keys, float[] values)
      Given two side-by-side arrays, one of keys, one of values, this constructs a map and inserts each pair of key and value into it. If keys and values have different lengths, this only uses the length of the smaller array.
      Parameters:
      keys - an array of keys
      values - an array of values
    • IntFloatOrderedMap

      public IntFloatOrderedMap(PrimitiveCollection.OfInt keys, PrimitiveCollection.OfFloat values)
      Given two side-by-side collections, one of keys, one of values, this constructs a map and inserts each pair of key and value into it. If keys and values have different lengths, this only uses the length of the smaller collection.
      Parameters:
      keys - a PrimitiveCollection of keys
      values - a PrimitiveCollection of values
    • IntFloatOrderedMap

      public IntFloatOrderedMap(IntFloatOrderedMap other, int offset, int count)
      Creates a new set by copying count items from the given IntFloatOrderedMap, starting at offset in that Map, into this.
      Parameters:
      other - another IntFloatOrderedMap of the same type
      offset - the first index in other's ordering to draw an item from
      count - how many items to copy from other
  • Method Details

    • put

      public float put(int key, float value)
      Description copied from class: IntFloatMap
      Returns the old value associated with the specified key, or this map's IntFloatMap.defaultValue if there was no prior value.
      Overrides:
      put in class IntFloatMap
    • put

      public float put(int key, float value, int index)
      Puts the given key and value into this map at the given index in its order. If the key is already present at a different index, it is moved to the given index and its value is set to the given value.
      Parameters:
      key - an int key
      value - a float value
      index - the index in the order to place the given key and value; must be non-negative and less than IntFloatMap.size()
      Returns:
      the previous value associated with key, if there was one, or IntFloatMap.defaultValue otherwise
    • putOrDefault

      public float putOrDefault(int key, float value, float defaultValue)
      Description copied from class: IntFloatMap
      Returns the old value associated with the specified key, or the given defaultValue if there was no prior value.
      Overrides:
      putOrDefault in class IntFloatMap
    • putAll

      public void putAll(IntFloatOrderedMap map)
      Puts every key-value pair in the given map into this, with the values from the given map overwriting the previous values if two keys are identical. This will put keys in the order of the given map.
      Parameters:
      map - a map with compatible key and value types; will not be modified
    • putAll

      public void putAll(IntFloatOrderedMap other, int offset, int count)
      Adds up to count entries, starting from offset, in the map other to this set, inserting at the end of the iteration order.
      Parameters:
      other - a non-null ordered map with the same type
      offset - the first index in other to use
      count - how many indices in other to use
    • putAll

      public void putAll(int insertionIndex, IntFloatOrderedMap other, int offset, int count)
      Adds up to count entries, starting from offset, in the map other to this set, inserting starting at insertionIndex in the iteration order.
      Parameters:
      insertionIndex - where to insert into the iteration order
      other - a non-null ordered map with the same type
      offset - the first index in other to use
      count - how many indices in other to use
    • remove

      public float remove(int key)
      Overrides:
      remove in class IntFloatMap
    • removeAt

      public float removeAt(int index)
      Removes the entry at the given index in the order, returning the value of that entry.
      Parameters:
      index - the index of the entry to remove; must be at least 0 and less than IntFloatMap.size()
      Returns:
      the value of the removed entry
    • removeRange

      public void removeRange(int start, int end)
      Removes the items between the specified start index, inclusive, and end index, exclusive. Note that this takes different arguments than some other range-related methods; this needs a start index and an end index, rather than a count of items. This matches the behavior in the JDK collections.
      Specified by:
      removeRange in interface Ordered.OfInt
      Parameters:
      start - the first index to remove, inclusive
      end - the last index (after what should be removed), exclusive
    • truncate

      public void truncate(int newSize)
      Reduces the size of the map to the specified size. If the map is already smaller than the specified size, no action is taken.
      Overrides:
      truncate in class IntFloatMap
      Parameters:
      newSize - the target size to try to reach by removing items, if smaller than the current size
    • ensureCapacity

      public void ensureCapacity(int additionalCapacity)
      Increases the size of the backing array to accommodate the specified number of additional items / loadFactor. Useful before adding many items to avoid multiple backing array resizes.
      Overrides:
      ensureCapacity in class IntFloatMap
      Parameters:
      additionalCapacity - how many additional items this should be able to hold without resizing (probably)
    • getAndIncrement

      public float getAndIncrement(int key, float defaultValue, float increment)
      Description copied from class: IntFloatMap
      Returns the key's current value and increments the stored value. If the key is not in the map, defaultValue + increment is put into the map and defaultValue is returned.
      Overrides:
      getAndIncrement in class IntFloatMap
    • alter

      public boolean alter(int before, int after)
      Changes the key before to after without changing its position in the order or its value. Returns true if after has been added to the IntFloatOrderedMap and before has been removed; returns false if after is already present or before is not present. If you are iterating over an IntFloatOrderedMap and have an index, you should prefer alterAt(int, int), which doesn't need to search for an index like this does and so can be faster.
      Parameters:
      before - a key that must be present for this to succeed
      after - a key that must not be in this map for this to succeed
      Returns:
      true if before was removed and after was added, false otherwise
    • alterAt

      public boolean alterAt(int index, int after)
      Changes the key at the given index in the order to after, without changing the ordering of other entries or any values. If after is already present, this returns false; it will also return false if index is invalid for the size of this map. Otherwise, it returns true. Unlike alter(int, int), this operates in constant time.
      Parameters:
      index - the index in the order of the key to change; must be non-negative and less than IntFloatMap.size
      after - the key that will replace the contents at index; this key must not be present for this to succeed
      Returns:
      true if after successfully replaced the key at index, false otherwise
    • setAt

      public float setAt(int index, float v)
      Changes the value at a specified index in the iteration order to v, without changing keys at all. If index isn't currently a valid index in the iteration order, this returns IntFloatMap.defaultValue. Otherwise, it returns the value that was previously held at index, which may be equal to IntFloatMap.defaultValue.
      Parameters:
      v - the new float value to assign
      index - the index in the iteration order to set v at
      Returns:
      the previous value held at index in the iteration order, which may be null if the value was null or if index was invalid
    • getAt

      public float getAt(int index)
      Gets the float value at the given index in the insertion order. The index should be between 0 (inclusive) and IntFloatMap.size() (exclusive).
      Parameters:
      index - an index in the insertion order, between 0 (inclusive) and IntFloatMap.size() (exclusive)
      Returns:
      the value at the given index
    • keyAt

      public int keyAt(int index)
      Gets the int key at the given index in the insertion order. The index should be between 0 (inclusive) and IntFloatMap.size() (exclusive).
      Parameters:
      index - an index in the insertion order, between 0 (inclusive) and IntFloatMap.size() (exclusive)
      Returns:
      the key at the given index
    • clear

      public void clear(int maximumCapacity)
      Description copied from class: IntFloatMap
      Clears the map and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger.
      Overrides:
      clear in class IntFloatMap
    • clear

      public void clear()
      Overrides:
      clear in class IntFloatMap
    • order

      public IntList order()
      Gets the IntList of keys in the order this class will iterate through them. Returns a direct reference to the same IntList this uses, so changes to the returned list will also change the iteration order here.
      Specified by:
      order in interface Ordered.OfInt
      Returns:
      the IntList of keys, in iteration order (usually insertion-order), that this uses
    • sort

      public void sort()
      Sorts this IntFloatOrderedMap in-place by the keys' natural ordering.
    • sort

      public void sort(IntComparator comp)
      Sorts this IntFloatOrderedMap in-place by the given IntComparator used on the keys. If comp is null, then this will sort by the natural ordering of the keys.
      Specified by:
      sort in interface Ordered.OfInt
      Parameters:
      comp - a IntComparator, such as one from IntComparators, or null to use the keys' natural ordering
    • sortByValue

      public void sortByValue(FloatComparator comp)
      Sorts this IntFloatOrderedMap in-place by the given FloatComparator used on the values. comp must not be null. You can use FloatComparators.NATURAL_COMPARATOR to do what sort() does (just sorting values in this case instead of keys).
      Parameters:
      comp - a non-null FloatComparator, such as one from FloatComparators
    • keySet

      public IntFloatMap.Keys keySet()
      Returns a PrimitiveCollection.OfInt view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

      Note that the same Collection instance is returned each time this method is called. Use the OrderedMapKeys(IntFloatOrderedMap) constructor for nested or multithreaded iteration.

      Overrides:
      keySet in class IntFloatMap
      Returns:
      a set view of the keys contained in this map
    • values

      public IntFloatMap.Values values()
      Returns a PrimitiveCollection.OfFloat for the values in the map. Remove is supported by the Collection's iterator.

      Note that the same Collection instance is returned each time this method is called. Use the OrderedMapValues(IntFloatOrderedMap) constructor for nested or multithreaded iteration.

      Overrides:
      values in class IntFloatMap
      Returns:
      a PrimitiveCollection.OfFloat backed by this map
    • entrySet

      public IntFloatMap.Entries entrySet()
      Returns a PrimitiveCollection.OfFloat of IntFloatMap.Entry, containing the entries in the map. Remove is supported by the Set's iterator.

      Note that the same iterator instance is returned each time this method is called. Use the OrderedMapEntries(IntFloatOrderedMap) constructor for nested or multithreaded iteration.

      Overrides:
      entrySet in class IntFloatMap
      Returns:
      a PrimitiveCollection.OfFloat of IntFloatMap.Entry key-value pairs
    • iterator

      public IntFloatMap.EntryIterator iterator()
      Reuses the iterator of the reused IntFloatMap.Entries produced by entrySet(); does not permit nested iteration. Iterate over OrderedMapEntries(IntFloatOrderedMap) if you need nested or multithreaded iteration. You can remove an Entry from this IntFloatOrderedMap using this Iterator.
      Specified by:
      iterator in interface Iterable<IntFloatMap.Entry>
      Overrides:
      iterator in class IntFloatMap
      Returns:
      an Iterator over key-value pairs as Map.Entry values
    • appendTo

      public StringBuilder appendTo(StringBuilder sb, String entrySeparator, String keyValueSeparator, boolean braces, IntAppender keyAppender, FloatAppender valueAppender)
      Appends to a StringBuilder from the contents of this IntFloatOrderedMap, but uses the given IntAppender and FloatAppender to convert each key and each value to a customizable representation and append them to a StringBuilder. These functions are often method references to methods in Base, such as Base.appendUnsigned(CharSequence, int) and Base.appendFriendly(CharSequence, float). To use the default String representation, you can use IntAppender.DEFAULT or FloatAppender.DEFAULT as an appender. To write values so that they can be read back as Java source code, use IntAppender.READABLE or FloatAppender.READABLE for each appender.
      Using READABLE appenders, if you separate keys from values with ", " and also separate entries with ", ", that allows the output to be copied into source code that calls with(Number, Number, Number...) (if braces is false).
      Overrides:
      appendTo in class IntFloatMap
      Parameters:
      sb - a StringBuilder that this can append to
      entrySeparator - how to separate entries, such as ", "
      keyValueSeparator - how to separate each key from its value, such as "=" or ":"
      braces - true to wrap the output in curly braces, or false to omit them
      keyAppender - a function that takes a StringBuilder and an int, and returns the modified StringBuilder
      valueAppender - a function that takes a StringBuilder and a float, and returns the modified StringBuilder
      Returns:
      sb, with the appended keys and values of this map
    • with

      public static IntFloatOrderedMap with()
      Constructs an empty map. This is usually less useful than just using the constructor, but can be handy in some code-generation scenarios when you don't know how many arguments you will have.
      Returns:
      a new map containing nothing
    • with

      public static IntFloatOrderedMap with(Number key0, Number value0)
      Constructs a single-entry map given one key and one value. This is mostly useful as an optimization for with(Number, Number, Number...) when there's no "rest" of the keys or values. Like the more-argument with(), this will convert its Number keys and values to primitive int and float, regardless of which Number type was used.
      Parameters:
      key0 - the first and only key; will be converted to primitive int
      value0 - the first and only value; will be converted to primitive float
      Returns:
      a new map containing just the entry mapping key0 to value0
    • with

      public static IntFloatOrderedMap with(Number key0, Number value0, Number key1, Number value1)
      Constructs a map given alternating keys and values. This is mostly useful as an optimization for with(Number, Number, Number...) when there's no "rest" of the keys or values. Like the more-argument with(), this will convert its Number keys and values to primitive int and float, regardless of which Number type was used.
      Parameters:
      key0 - a Number key; will be converted to primitive int
      value0 - a Number for a value; will be converted to primitive float
      key1 - a Number key; will be converted to primitive int
      value1 - a Number for a value; will be converted to primitive float
      Returns:
      a new map containing the given key-value pairs
    • with

      public static IntFloatOrderedMap with(Number key0, Number value0, Number key1, Number value1, Number key2, Number value2)
      Constructs a map given alternating keys and values. This is mostly useful as an optimization for with(Number, Number, Number...) when there's no "rest" of the keys or values. Like the more-argument with(), this will convert its Number keys and values to primitive int and float, regardless of which Number type was used.
      Parameters:
      key0 - a Number key; will be converted to primitive int
      value0 - a Number for a value; will be converted to primitive float
      key1 - a Number key; will be converted to primitive int
      value1 - a Number for a value; will be converted to primitive float
      key2 - a Number key; will be converted to primitive int
      value2 - a Number for a value; will be converted to primitive float
      Returns:
      a new map containing the given key-value pairs
    • with

      public static IntFloatOrderedMap with(Number key0, Number value0, Number key1, Number value1, Number key2, Number value2, Number key3, Number value3)
      Constructs a map given alternating keys and values. This is mostly useful as an optimization for with(Number, Number, Number...) when there's no "rest" of the keys or values. Like the more-argument with(), this will convert its Number keys and values to primitive int and float, regardless of which Number type was used.
      Parameters:
      key0 - a Number key; will be converted to primitive int
      value0 - a Number for a value; will be converted to primitive float
      key1 - a Number key; will be converted to primitive int
      value1 - a Number for a value; will be converted to primitive float
      key2 - a Number key; will be converted to primitive int
      value2 - a Number for a value; will be converted to primitive float
      key3 - a Number key; will be converted to primitive int
      value3 - a Number for a value; will be converted to primitive float
      Returns:
      a new map containing the given key-value pairs
    • with

      public static IntFloatOrderedMap with(Number key0, Number value0, Number... rest)
      Constructs a map given alternating keys and values. This can be useful in some code-generation scenarios, or when you want to make a map conveniently by-hand and have it populated at the start. You can also use IntFloatOrderedMap(int[], float[]), which takes all keys and then all values. This needs all keys to be some kind of (boxed) Number, and converts them to primitive ints. It also needs all values to be a (boxed) Number, and converts them to primitive floats. Any keys or values that aren't Numbers have that entry skipped.
      Parameters:
      key0 - the first key; will be converted to a primitive int
      value0 - the first value; will be converted to a primitive float
      rest - an array or varargs of Number elements
      Returns:
      a new map containing the given key-value pairs
    • withPrimitive

      public static IntFloatOrderedMap withPrimitive()
      Constructs an empty map. This is usually less useful than just using the constructor, but can be handy in some code-generation scenarios when you don't know how many arguments you will have.
      Returns:
      a new map containing nothing
    • withPrimitive

      public static IntFloatOrderedMap withPrimitive(int key0, float value0)
      Constructs a single-entry map given one key and one value. This is mostly useful as an optimization for with(Number, Number, Number...) when there's no "rest" of the keys or values. Unlike the vararg with(), this doesn't box its arguments into Number items.
      Parameters:
      key0 - the first and only key
      value0 - the first and only value
      Returns:
      a new map containing just the entry mapping key0 to value0
    • withPrimitive

      public static IntFloatOrderedMap withPrimitive(int key0, float value0, int key1, float value1)
      Constructs a map given alternating keys and values. This is mostly useful as an optimization for with(Number, Number, Number...) when there's no "rest" of the keys or values. Unlike the vararg with(), this doesn't box its arguments into Number items.
      Parameters:
      key0 - a int key
      value0 - a float value
      key1 - a int key
      value1 - a float value
      Returns:
      a new map containing the given key-value pairs
    • withPrimitive

      public static IntFloatOrderedMap withPrimitive(int key0, float value0, int key1, float value1, int key2, float value2)
      Constructs a map given alternating keys and values. This is mostly useful as an optimization for with(Number, Number, Number...) when there's no "rest" of the keys or values. Unlike the vararg with(), this doesn't box its arguments into Number items.
      Parameters:
      key0 - a int key
      value0 - a float value
      key1 - a int key
      value1 - a float value
      key2 - a int key
      value2 - a float value
      Returns:
      a new map containing the given key-value pairs
    • withPrimitive

      public static IntFloatOrderedMap withPrimitive(int key0, float value0, int key1, float value1, int key2, float value2, int key3, float value3)
      Constructs a map given alternating keys and values. This is mostly useful as an optimization for with(Number, Number, Number...) when there's no "rest" of the keys or values. Unlike the vararg with(), this doesn't box its arguments into Number items.
      Parameters:
      key0 - a int key
      value0 - a float value
      key1 - a int key
      value1 - a float value
      key2 - a int key
      value2 - a float value
      key3 - a int key
      value3 - a float value
      Returns:
      a new map containing the given key-value pairs
    • parse

      public static IntFloatOrderedMap parse(String str, String entrySeparator, String keyValueSeparator)
      Creates a new map by parsing all of str, with entries separated by entrySeparator, such as ", " and the keys separated from values by keyValueSeparator, such as "=".
      Parameters:
      str - a String containing parseable text
      entrySeparator - the String separating every key-value pair
      keyValueSeparator - the String separating every key from its corresponding value
    • parse

      public static IntFloatOrderedMap parse(String str, String entrySeparator, String keyValueSeparator, boolean brackets)
      Creates a new map by parsing all of str (or if brackets is true, all but the first and last chars), with entries separated by entrySeparator, such as ", " and the keys separated from values by keyValueSeparator, such as "=".
      Parameters:
      str - a String containing parseable text
      entrySeparator - the String separating every key-value pair
      keyValueSeparator - the String separating every key from its corresponding value
      brackets - if true, the first and last chars in str will be ignored
    • parse

      public static IntFloatOrderedMap parse(String str, String entrySeparator, String keyValueSeparator, int offset, int length)
      Creates a new map by parsing the given subrange of str, with entries separated by entrySeparator, such as ", " and the keys separated from values by keyValueSeparator, such as "=".
      Parameters:
      str - a String containing parseable text
      entrySeparator - the String separating every key-value pair
      keyValueSeparator - the String separating every key from its corresponding value
      offset - the first position to read parseable text from in str
      length - how many chars to read; -1 is treated as maximum length