Class SparseTextMap

java.lang.Object
squidpony.squidgrid.gui.gdx.SparseTextMap
All Implemented Interfaces:
Iterable<SparseTextMap.Entry>

public class SparseTextMap
extends Object
implements Iterable<SparseTextMap.Entry>
An unordered map where the keys are two positive ints up to 16 bits (x and y, between 0 and 65535) and there are multiple kinds of value per key, here just a char and a float for color. Can be rendered if given a running Batch and a TextCellFactory using draw(Batch, TextCellFactory, Frustum).
Author:
Nathan Sweet, Tommy Ettinger
  • Nested Class Summary

    Nested Classes 
    Modifier and Type Class Description
    static class  SparseTextMap.CharValues  
    static class  SparseTextMap.Entries  
    static class  SparseTextMap.Entry  
    static class  SparseTextMap.FloatValues  
    static class  SparseTextMap.Keys  
  • Field Summary

    Fields 
    Modifier and Type Field Description
    int size  
  • Constructor Summary

    Constructors 
    Constructor Description
    SparseTextMap()
    Creates a new map with an initial capacity of 192 and a load factor of 0.75.
    SparseTextMap​(int initialCapacity)
    Creates a new map with a load factor of 0.75.
    SparseTextMap​(int initialCapacity, float loadFactor)
    Creates a new map with the specified initial capacity and load factor.
    SparseTextMap​(SparseTextMap map)
    Creates a new map identical to the specified map.
  • Method Summary

    Modifier and Type Method Description
    SparseTextMap.CharValues charValues()
    Returns an iterator for the values in the map.
    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 containsCharValue​(char value)
    Returns true if the specified char value is in the map.
    boolean containsFloatValue​(float value)
    Returns true if the specified char value is in the map.
    boolean containsKey​(int key)  
    static int decodeX​(int encoded)
    Decodes a packed position and gets the x component from it, if it was produced by encodePosition(int, int) or place(int, int, char, float).
    static int decodeY​(int encoded)
    Decodes a packed position and gets the y component from it, if it was produced by encodePosition(int, int) or place(int, int, char, float).
    void draw​(com.badlogic.gdx.graphics.g2d.Batch batch, TextCellFactory textFactory)
    Draws the contents of this SparseTextMap, using the keys as x,y pairs as they would be entered by calling place(int, int, char, float) and drawing the associated char at that x,y position.
    void draw​(com.badlogic.gdx.graphics.g2d.Batch batch, TextCellFactory textFactory, float screenOffsetX, float screenOffsetY)
    Draws the contents of this SparseTextMap, using the keys as x,y pairs as they would be entered by calling place(int, int, char, float) and drawing the associated char at that x,y position, potentially with an offset on x and/or y.
    void draw​(com.badlogic.gdx.graphics.g2d.Batch batch, TextCellFactory textFactory, com.badlogic.gdx.math.Frustum frustum)
    Draws the contents of this SparseTextMap, using the keys as x,y pairs as they would be entered by calling place(int, int, char, float) and drawing the associated char at that x,y position.
    void draw​(com.badlogic.gdx.graphics.g2d.Batch batch, TextCellFactory textFactory, com.badlogic.gdx.math.Frustum frustum, float screenOffsetX, float screenOffsetY)
    Draws the contents of this SparseTextMap, using the keys as x,y pairs as they would be entered by calling place(int, int, char, float) and drawing the associated char at that x,y position, potentially with an offset on x and/or y.
    void draw​(com.badlogic.gdx.graphics.g2d.Batch batch, TextCellFactory textFactory, com.badlogic.gdx.math.Frustum frustum, float screenOffsetX, float screenOffsetY, char replacement)
    Draws the contents of this SparseTextMap, using the keys as x,y pairs as they would be entered by calling place(int, int, char, float) and drawing the char replacement at that x,y position, potentially with an offset on x and/or y.
    static int encodePosition​(int x, int y)
    Packs the given x,y position into one int in the type that is used elsewhere in this class.
    void ensureCapacity​(int additionalCapacity)
    Increases the size of the backing array to accommodate the specified number of additional items.
    SparseTextMap.Entries entries()
    Returns an iterator for the entries in the map.
    boolean equals​(Object obj)  
    int findKey​(char value, int notFound)
    Returns the key for the specified char value, or notFound if it is not in the map.
    int findKey​(float value, int notFound)
    Returns the key for the specified float value, or notFound if it is not in the map.
    SparseTextMap.FloatValues floatValues()
    Returns an iterator for the values in the map.
    char getChar​(int key, char defaultValue)  
    char getChar​(int x, int y, char defaultValue)  
    float getFloat​(int key, float defaultValue)  
    float getFloat​(int x, int y, float defaultValue)  
    int hashCode()  
    Iterator<SparseTextMap.Entry> iterator()  
    SparseTextMap.Keys keys()
    Returns an iterator for the keys in the map.
    int place​(int x, int y, char charValue, float encodedColor)
    Places a char in the given encoded color at the requested x, y position in grid cells, where x and y must each be between 0 and 65535, both inclusive, and the encoded color is one produced by Color.toFloatBits() or any of several methods in SColor, such as SColor.floatGetI(int, int, int), SColor.floatGetHSV(float, float, float, float), or SColor.lerpFloatColors(float, float, float).
    int place​(int x, int y, char charValue, com.badlogic.gdx.graphics.Color color)
    Places a char in the given libGDX Color at the requested x, y position in grid cells, where x and y must each be between 0 and 65535, both inclusive.
    void put​(int key, char charValue, float floatValue)  
    void putAll​(SparseTextMap map)  
    void remove​(int key)  
    char remove​(int key, char defaultValue)  
    void shrink​(int maximumCapacity)
    Reduces the size of the backing arrays to be the specified capacity or less.
    String toString()  
    void updateChar​(int key, char charValue)
    If and only if key is already present, this changes the char associated with it while leaving the float the same.
    void updateFloat​(int key, float floatValue)
    If and only if key is already present, this changes the float associated with it while leaving the char the same.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Field Details

  • Constructor Details

    • SparseTextMap

      public SparseTextMap()
      Creates a new map with an initial capacity of 192 and a load factor of 0.75.
    • SparseTextMap

      public SparseTextMap​(int initialCapacity)
      Creates a new map with a load factor of 0.75.
      Parameters:
      initialCapacity - If not a power of two, it is increased to the next nearest power of two.
    • SparseTextMap

      public SparseTextMap​(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.
    • SparseTextMap

      public SparseTextMap​(SparseTextMap map)
      Creates a new map identical to the specified map.
  • Method Details

    • draw

      public void draw​(com.badlogic.gdx.graphics.g2d.Batch batch, TextCellFactory textFactory)
      Draws the contents of this SparseTextMap, using the keys as x,y pairs as they would be entered by calling place(int, int, char, float) and drawing the associated char at that x,y position. Uses a Frustum object, usually obtained from the current Camera with Camera.frustum, to cull anything that would be drawn out of view. Treats the float value as a color for the char, using the encoding for colors as floats that Color.toFloatBits() uses. Relies on the sizing information from the given TextCellFactory (its TextCellFactory.actualCellWidth and TextCellFactory.actualCellHeight, which may differ from its width and height if either of TextCellFactory.tweakWidth(float) or TextCellFactory.tweakHeight(float) were called). It also, of course, uses the TextCellFactory to determine what its text will look like (font, size, and so on). The TextCellFactory must have been initialized, probably with TextCellFactory.initBySize() after setting the width and height as desired. This method should be called between batch.begin() and batch.end() with the Batch passed to this.
      Parameters:
      batch - the FilterBatch or other Batch used to draw this; should have already have had begin() called
      textFactory - used to determine the font, size, cell size, and other information; must be initialized
    • draw

      public void draw​(com.badlogic.gdx.graphics.g2d.Batch batch, TextCellFactory textFactory, float screenOffsetX, float screenOffsetY)
      Draws the contents of this SparseTextMap, using the keys as x,y pairs as they would be entered by calling place(int, int, char, float) and drawing the associated char at that x,y position, potentially with an offset on x and/or y. Uses a Frustum object, usually obtained from the current Camera with Camera.frustum, to cull anything that would be drawn out of view. Treats the float value as a color for the char, using the encoding for colors as floats that Color.toFloatBits() uses. Relies on the sizing information from the given TextCellFactory (its TextCellFactory.actualCellWidth and TextCellFactory.actualCellHeight, which may differ from its width and height if either of TextCellFactory.tweakWidth(float) or TextCellFactory.tweakHeight(float) were called). It also, of course, uses the TextCellFactory to determine what its text will look like (font, size, and so on). The TextCellFactory must have been initialized, probably with TextCellFactory.initBySize() after setting the width and height as desired. This method should be called between batch.begin() and batch.end() with the Batch passed to this.
      Parameters:
      batch - the FilterBatch or other Batch used to draw this; should have already have had begin() called
      textFactory - used to determine the font, size, cell size, and other information; must be initialized
      screenOffsetX - offset to apply to the x position of each char rendered; positive moves chars right
      screenOffsetY - offset to apply to the y position of each char rendered; positive moves chars up
    • draw

      public void draw​(com.badlogic.gdx.graphics.g2d.Batch batch, TextCellFactory textFactory, com.badlogic.gdx.math.Frustum frustum)
      Draws the contents of this SparseTextMap, using the keys as x,y pairs as they would be entered by calling place(int, int, char, float) and drawing the associated char at that x,y position. Uses a Frustum object, usually obtained from the current Camera with Camera.frustum, to cull anything that would be drawn out of view. Treats the float value as a color for the char, using the encoding for colors as floats that Color.toFloatBits() uses. Relies on the sizing information from the given TextCellFactory (its TextCellFactory.actualCellWidth and TextCellFactory.actualCellHeight, which may differ from its width and height if either of TextCellFactory.tweakWidth(float) or TextCellFactory.tweakHeight(float) were called). It also, of course, uses the TextCellFactory to determine what its text will look like (font, size, and so on). The TextCellFactory must have been initialized, probably with TextCellFactory.initBySize() after setting the width and height as desired. This method should be called between batch.begin() and batch.end() with the Batch passed to this.
      Parameters:
      batch - the FilterBatch or other Batch used to draw this; should have already have had begin() called
      textFactory - used to determine the font, size, cell size, and other information; must be initialized
      frustum - a Frustum object to determine culling, almost always obtained from Camera.frustum
    • draw

      public void draw​(com.badlogic.gdx.graphics.g2d.Batch batch, TextCellFactory textFactory, com.badlogic.gdx.math.Frustum frustum, float screenOffsetX, float screenOffsetY)
      Draws the contents of this SparseTextMap, using the keys as x,y pairs as they would be entered by calling place(int, int, char, float) and drawing the associated char at that x,y position, potentially with an offset on x and/or y. Uses a Frustum object, usually obtained from the current Camera with Camera.frustum, to cull anything that would be drawn out of view. Treats the float value as a color for the char, using the encoding for colors as floats that Color.toFloatBits() uses. Relies on the sizing information from the given TextCellFactory (its TextCellFactory.actualCellWidth and TextCellFactory.actualCellHeight, which may differ from its width and height if either of TextCellFactory.tweakWidth(float) or TextCellFactory.tweakHeight(float) were called). It also, of course, uses the TextCellFactory to determine what its text will look like (font, size, and so on). The TextCellFactory must have been initialized, probably with TextCellFactory.initBySize() after setting the width and height as desired. This method should be called between batch.begin() and batch.end() with the Batch passed to this.
      Parameters:
      batch - the FilterBatch or other Batch used to draw this; should have already have had begin() called
      textFactory - used to determine the font, size, cell size, and other information; must be initialized
      frustum - a Frustum object to determine culling, almost always obtained from Camera.frustum
      screenOffsetX - offset to apply to the x position of each char rendered; positive moves chars right
      screenOffsetY - offset to apply to the y position of each char rendered; positive moves chars up
    • draw

      public void draw​(com.badlogic.gdx.graphics.g2d.Batch batch, TextCellFactory textFactory, com.badlogic.gdx.math.Frustum frustum, float screenOffsetX, float screenOffsetY, char replacement)
      Draws the contents of this SparseTextMap, using the keys as x,y pairs as they would be entered by calling place(int, int, char, float) and drawing the char replacement at that x,y position, potentially with an offset on x and/or y. Uses a Frustum object, usually obtained from the current Camera with Camera.frustum, to cull anything that would be drawn out of view. Treats the float value as a color for replacement, using the encoding for colors as floats that Color.toFloatBits() uses. Relies on the sizing information from the given TextCellFactory (its TextCellFactory.actualCellWidth and TextCellFactory.actualCellHeight, which may differ from its width and height if either of TextCellFactory.tweakWidth(float) or TextCellFactory.tweakHeight(float) were called). It also, of course, uses the TextCellFactory to determine what its text will look like (font, size, and so on). The TextCellFactory must have been initialized, probably with TextCellFactory.initBySize() after setting the width and height as desired. This method should be called between batch.begin() and batch.end() with the Batch passed to this.
      Parameters:
      batch - the FilterBatch or other Batch used to draw this; should have already have had begin() called
      textFactory - used to determine the font, size, cell size, and other information; must be initialized
      frustum - a Frustum object to determine culling, almost always obtained from Camera.frustum
      screenOffsetX - offset to apply to the x position of each char rendered; positive moves chars right
      screenOffsetY - offset to apply to the y position of each char rendered; positive moves chars up
      replacement - a char that will be used in place of the normal char values stored in this; often the char with Unicode value 0, which renders as a solid block.
    • encodePosition

      public static int encodePosition​(int x, int y)
      Packs the given x,y position into one int in the type that is used elsewhere in this class. This requires x and y to each be between 0 and 65535, both inclusive.
      Parameters:
      x - the x component to encode; this must be between 0 and 65535, both inclusive
      y - the y component to encode; this must be between 0 and 65535, both inclusive
      Returns:
      an encoded form of the x,y pair in the style used elsewhere in this class
    • decodeX

      public static int decodeX​(int encoded)
      Decodes a packed position and gets the x component from it, if it was produced by encodePosition(int, int) or place(int, int, char, float).
      Parameters:
      encoded - a packed position that holds x and y components
      Returns:
      the x component packed in the parameter
    • decodeY

      public static int decodeY​(int encoded)
      Decodes a packed position and gets the y component from it, if it was produced by encodePosition(int, int) or place(int, int, char, float).
      Parameters:
      encoded - a packed position that holds x and y components
      Returns:
      the y component packed in the parameter
    • place

      public int place​(int x, int y, char charValue, com.badlogic.gdx.graphics.Color color)
      Places a char in the given libGDX Color at the requested x, y position in grid cells, where x and y must each be between 0 and 65535, both inclusive. The color can also be an SColor, such as one of the many constants in that class. Returns the int code that can be used to locate the x,y pair as a key in this SparseTextMap.
      Parameters:
      x - the x position of the colorful char; this must be between 0 and 65535, both inclusive
      y - the y position of the colorful char; this must be between 0 and 65535, both inclusive
      charValue - the char to put into the SparseTextMap
      color - the libGDX Color or SColor to use for the char
      Returns:
      the int that the x,y pair will be stored at, and used as the single key associated with the colorful char
    • place

      public int place​(int x, int y, char charValue, float encodedColor)
      Places a char in the given encoded color at the requested x, y position in grid cells, where x and y must each be between 0 and 65535, both inclusive, and the encoded color is one produced by Color.toFloatBits() or any of several methods in SColor, such as SColor.floatGetI(int, int, int), SColor.floatGetHSV(float, float, float, float), or SColor.lerpFloatColors(float, float, float). Returns the int code that can be used to locate the x,y pair as a key in this SparseTextMap.
      Parameters:
      x - the x position of the colorful char; this must be between 0 and 65535, both inclusive
      y - the y position of the colorful char; this must be between 0 and 65535, both inclusive
      charValue - the char to put into the SparseTextMap
      encodedColor - the encoded color to use for the char, as produced by Color.toFloatBits()
      Returns:
      the int that the x,y pair will be stored at, and used as the single key associated with the colorful char
    • put

      public void put​(int key, char charValue, float floatValue)
    • updateFloat

      public void updateFloat​(int key, float floatValue)
      If and only if key is already present, this changes the float associated with it while leaving the char the same.
      Parameters:
      key - the encoded key as produced by encodePosition(int, int)
      floatValue - the float value to set at the given encoded key
    • updateChar

      public void updateChar​(int key, char charValue)
      If and only if key is already present, this changes the char associated with it while leaving the float the same.
      Parameters:
      key - the encoded key as produced by encodePosition(int, int)
      charValue - the char value to set at the given encoded key
    • putAll

      public void putAll​(SparseTextMap map)
    • getChar

      public char getChar​(int x, int y, char defaultValue)
      Parameters:
      x - the x-component of the position to look up
      y - the y-component of the position to look up
      defaultValue - Returned if the key was not associated with a value.
      Returns:
      the char associated with the given position, or defaultValue if no char is associated
    • getChar

      public char getChar​(int key, char defaultValue)
      Parameters:
      key - the encoded key as produced by encodePosition(int, int)
      defaultValue - Returned if the key was not associated with a value.
      Returns:
      the char associated with the given key, or defaultValue if no char is associated
    • getFloat

      public float getFloat​(int x, int y, float defaultValue)
      Parameters:
      x - the x-component of the position to look up
      y - the y-component of the position to look up
      defaultValue - Returned if the key was not associated with a value.
      Returns:
      the float associated with the given position, or defaultValue if no float is associated
    • getFloat

      public float getFloat​(int key, float defaultValue)
      Parameters:
      key - the encoded key as produced by encodePosition(int, int)
      defaultValue - Returned if the key was not associated with a value.
      Returns:
      the float associated with the given key, or defaultValue if no float is associated
    • remove

      public void remove​(int key)
    • remove

      public char remove​(int key, char defaultValue)
    • shrink

      public void shrink​(int maximumCapacity)
      Reduces the size of the backing arrays to be the specified capacity or less. If the capacity is already less, nothing is done. If the map contains more items than the specified capacity, the next highest power of two capacity is used instead.
    • 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()
    • containsCharValue

      public boolean containsCharValue​(char value)
      Returns true if the specified char value is in the map. Note: this traverses the entire map and compares every value, which may be an expensive operation.
    • containsFloatValue

      public boolean containsFloatValue​(float value)
      Returns true if the specified char value is in the map. Note: this traverses the entire map and compares every value, which may be an expensive operation.
    • containsKey

      public boolean containsKey​(int key)
    • findKey

      public int findKey​(char value, int notFound)
      Returns the key for the specified char value, or notFound if it is not in the map. Note this traverses the entire map and compares every value, which may be an expensive operation.
    • findKey

      public int findKey​(float value, int notFound)
      Returns the key for the specified float value, or notFound if it is not in the map. Note this traverses the entire map and compares every value, which may be an expensive operation.
    • ensureCapacity

      public void ensureCapacity​(int additionalCapacity)
      Increases the size of the backing array to accommodate the specified number of additional items. Useful before adding many items to avoid multiple backing array resizes.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals​(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • iterator

      Specified by:
      iterator in interface Iterable<SparseTextMap.Entry>
    • 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 SparseTextMap.Entries constructor for nested or multithreaded iteration.
    • charValues

      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 SparseTextMap.Entries constructor for nested or multithreaded iteration.
    • floatValues

      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 SparseTextMap.Entries constructor for nested or multithreaded iteration.
    • 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 SparseTextMap.Entries constructor for nested or multithreaded iteration.