Documentation Set for Swarm 2.2
Prev | Reference X. Space Library | Next

# Discrete2d

## Name

Discrete2d -- Root class of all 2d discrete spaces.

## Description

A Discrete2d is basically a 2d array of ids. Subclasses add particular space semantics onto this. Currently Discrete2d grids are accessed by integer pairs of X and Y coordinates.

## Protocols adopted by Discrete2d

SwarmObject GridData CREATABLE

## Methods

### Phase: Creating

- - makeOffsets
  Given an array size, compute the offsets array that caches the multiplication by ysize. See the discrete2dSiteAt macro.

- - (id *)allocLattice
  Allocate memory for the lattice.

- - setSizeX: (unsigned)x Y: (unsigned)y
  Set the world size.

- + create: (id <Zone>)aZone setSizeX: (unsigned)x Y: (unsigned)y
  Convenience constructor for Discrete2d lattice

### Phase: Setting

- - setObjectFlag: (BOOL)objectFlag
  When objectFlag is true, indicates that this lattice is intended only for objects.

- - setLattice: (id *)lattice

### Phase: Using

- - copyDiscrete2d: (id <Discrete2d>)a toDiscrete2d: (id <Discrete2d>)b
  This method copies the data in one Discrete2d object to another Discrete2d object. It assumes that both objects already exist.

- - (int)setDiscrete2d: (id <Discrete2d>)a toFile: (const char *)filename
  This method reads a PGM formatted file and pipes the data into a Discrete2d object.

- - fillWithObject: anObj
  Fills the space using putObject.

- - fillWithValue: (long)aValue
  Fills the space using putValue.

- - fastFillWithObject: anObj
  Directly fills the lattice with an object.

- - fastFillWithValue: (long)aValue
  Directly fills the lattice with a value.

- - putValue: (long)v atX: (unsigned)x Y: (unsigned)y
  Put the given integer to (x,y) overwriting whatever was there.

- - putObject: anObject atX: (unsigned)x Y: (unsigned)y
  Put the given pointer to (x,y) overwriting whatever was there.

## Examples

Example space/Discrete2d/1.

     Discrete2d instances can now serialize themselves (without needing
     additional classes such as Int2dFiler).

     The result of value (shallow) Lisp serialization of a 4x3 Discrete2d
     consisting of long values might be:

     (list
       (cons 'myDiscrete2d
         (make-instance 'Discrete2d
          #:xsize 4 #:ysize 3 #:lattice
           (parse
            #2((1000 1000 1000 1000)
               (1000 1000 1000 1000)
               (1000 1000 10 1000))))))

     For object (deep) Lisp serialization of the same 4x3 lattice with
     identical instances of MyClass at each point (except at (2,2) which
     has an instance of MyClassOther) would look like:

     (list
       (cons 'myDiscrete2d
         (make-instance 'Discrete2d
           #:xsize 4 #:ysize 3 #:lattice
            (parse
            (cons '(0 . 0)
             (make-instance 'MyClass #:strVal "Hello World"))
            (cons '(0 . 1)
             (make-instance 'MyClass #:strVal "Hello World"))
            (cons '(0 . 2)
             (make-instance 'MyClass #:strVal "Hello World"))
            (cons '(1 . 0)
             (make-instance 'MyClass #:strVal "Hello World"))
            (cons '(1 . 1)
             (make-instance 'MyClass #:strVal "Hello World"))
            (cons '(1 . 2)
             (make-instance 'MyClass #:strVal "Hello World"))
            (cons '(2 . 0)
             (make-instance 'MyClass #:strVal "Hello World"))
            (cons '(2 . 1)
             (make-instance 'MyClass #:strVal "Hello World"))
            (cons '(2 . 2)
             (make-instance 'MyClassOther #:strVal "Other World"))
            (cons '(3 . 0)
             (make-instance 'MyClass #:strVal "Hello World"))
            (cons '(3 . 1)
             (make-instance 'MyClass #:strVal "Hello World"))
            (cons '(3 . 2)
             (make-instance 'MyClass #:strVal "Hello World"))))))

Prev | Home | Next
Diffuse2d | Up | Grid2d