Class SparseObjectSpace

  • All Implemented Interfaces:
    ObjectSpace

    public class SparseObjectSpace
    extends AbstractSpace<java.lang.Object>
    implements ObjectSpace
    A sparse bidimensional grid containing one object per cell. This object must be used when the ratio between the size of the grid and its average fullness is high. For instance, if you have a grid of 1000 x 1000, with 1000000 of cells and it has to contain 1000 objects, the better solution for memory occupation is to use an ObjVirtualGrid. Obviuosly the ObjVirtualGrid is slower than ObjGrid, but it is much cheaper in memory occupation.

    Title: JAS

    Description: Java Agent-based Simulation library

    Copyright (C) 2002 Michele Sonnessa

    This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
    Author:
    Michele Sonnessa

    • Constructor Detail

      • SparseObjectSpace

        public SparseObjectSpace​(AbstractSpace<java.lang.Object> grid)
        Create a copy of the given grid.
        Parameters:
        grid - The source grid.
      • SparseObjectSpace

        public SparseObjectSpace​(int xSize,
                                 int ySize)
        Create a grid with given size.
        Parameters:
        xSize - The width of the grid.
        ySize - The height of the grid.
    • Method Detail

      • get

        public java.lang.Object get​(int x,
                                    int y)
        Return the object stored at the given position.
        Specified by:
        get in interface ObjectSpace
        Specified by:
        get in class AbstractSpace<java.lang.Object>
        Parameters:
        x - The x coordinate.
        y - The y coordinate.
        Returns:
        The requested object.
      • set

        public void set​(int x,
                        int y,
                        java.lang.Object obj)
        Put the given object at the given position.
        Specified by:
        set in interface ObjectSpace
        Specified by:
        set in class AbstractSpace<java.lang.Object>
        Parameters:
        x - The x coordinate.
        y - The y coordinate.
        obj - The object to be stored at the (x, y) cell.
      • swapPositions

        public void swapPositions​(int x1,
                                  int y1,
                                  int x2,
                                  int y2)
        Swap the content of the (x1, y1) and (x2, y2) cells of the grid.
        Specified by:
        swapPositions in class AbstractSpace<java.lang.Object>
        Parameters:
        x1 - The x coordinate for the first cell.
        y1 - The y coordinate for the first cell.
        x2 - The x coordinate for the second cell.
        y2 - The y coordinate for the second cell.
      • countObjectsAt

        public int countObjectsAt​(int x,
                                  int y)
        Description copied from class: AbstractSpace
        Returns the number of objects allocated in cell (x,y).
        Specified by:
        countObjectsAt in interface ObjectSpace
        Specified by:
        countObjectsAt in class AbstractSpace<java.lang.Object>
        Parameters:
        x - The x coordinate.
        y - The y coordinate.
      • moveGridPosition

        public boolean moveGridPosition​(SpacePosition position,
                                        int destinationX,
                                        int destinationY)
        Move a IGridPosition object from its current position to the specified destination, only if the destination cell is empty.
        Specified by:
        moveGridPosition in interface ObjectSpace
        Parameters:
        destinationX - The x destination coordinate.
        destinationY - The y destination coordinate.
        object - An object implementing IGridPosition interface.
        Returns:
        true if object has been moved, false otherwise.
      • gridIterator

        public ObjectSpaceIterator<java.lang.Object> gridIterator()
        Return a GridIterator storing the position of the read value.
        Returns:
        A GridIterator that scrolls grid from the top-left corner to the bottom-right, reading each line from left to right. It remember the position of the last read value.
      • size

        public int size()
        Return the number of objects stored into the grid.
        Specified by:
        size in class AbstractSpace<java.lang.Object>
        Returns:
        The sum of cells containing an object.
      • contains

        public boolean contains​(java.lang.Object o)
        Test if given object is contained into the grid.
        Parameters:
        o - The object to be tested.
        Returns:
        True if object is present into the grid.
      • iterator

        public java.util.Iterator<java.lang.Object> iterator()
      • toArray

        public java.lang.Object[] toArray()
        Return an array of objects stored into the grid.
        Returns:
        A vector containing only the objects stored into the grid. The empty cells are ignored.
      • add

        public boolean add​(java.lang.Object o)
        Add an object implementing IGridPosition interface to the grid. If object implements IGridPosition it stored in the right position of the grid.
        Parameters:
        o - The IGridPosition object to be added.
        Returns:
        True if object was added. If o does not implement IGridPosition interface it will not be added and method will return false.
      • remove

        public boolean remove​(java.lang.Object o)
        Remove the given object from the grid.
        Parameters:
        o - The object to be removed.
        Returns:
        True if object was found and removed, false otherwise.
        Throws:
        java.lang.ClassCastException - If o does not implement IGridPosition interface.
      • clear

        public void clear()
        Set all cells to the null value.
        Specified by:
        clear in class AbstractSpace<java.lang.Object>
      • equals

        public boolean equals​(java.lang.Object o)
        Test if o is the same of this. There is no content comparing.
        Overrides:
        equals in class AbstractSpace<java.lang.Object>
        Parameters:
        o - The object to be compared.
        Returns:
        True o is this object.
      • addGridPosition

        public boolean addGridPosition​(SpacePosition object)
        Add an object implementing IGridPosition interface to the grid. If the destination cell is already occupied the method return false and the object is not added.
        Specified by:
        addGridPosition in interface ObjectSpace
        Parameters:
        object - The IGridPosition object to be added.
        Returns:
        True if object has been added. False if destination cell is already occupied or if argument object is null.
      • removeGridPosition

        public boolean removeGridPosition​(SpacePosition object)
        Remove the IGridPosition object from the grid.
        Specified by:
        removeGridPosition in interface ObjectSpace
        Parameters:
        object - The IGridPosition object to be removed.
        Returns:
        true if object has been removed. False if object is null or is not present on the grid.