edu.vub.at.objects.natives
Class FieldMap

java.lang.Object
  extended byedu.vub.at.objects.natives.FieldMap
All Implemented Interfaces:
java.io.Serializable

public final class FieldMap
extends java.lang.Object
implements java.io.Serializable

Instances of this class implement a so-called 'map' for an AmbientTalk object's fields. The terminology stems from the language Self. A 'map' maps variable names to an integer index. This index can be used to index an object's state vector to retrieve the value of a slot. The advantage of maps lies in their space effiency: clones can share maps, thereby enabling the sharing of variable names (which are immutable). We implement a custom map from symbols to Java ints ourselves, because the java.util.HashMap requires explicit boxing and unboxing of integers as java.lang.Integers, which is wasteful.

Author:
tvc
See Also:
Serialized Form

Field Summary
private static int _DEFAULT_SIZE_
           
private  int free_
           
private  FieldMap origin_
           
private  ATSymbol[] varNames_
           
 
Constructor Summary
  FieldMap()
           
private FieldMap(ATSymbol[] copiedNames, int copiedFree, FieldMap origin)
           
 
Method Summary
(package private)  FieldMap copy()
          Creates a shallow copy of the field map.
private  int findName(ATSymbol nam)
          Searches for the name of a field in the varNames_ array.
 int get(ATSymbol nam)
          Retrieve the index of a field given its name.
 boolean isDerivedFrom(FieldMap aFieldMap)
          Checks whether both FieldMaps are equal or whether the passed object is a FieldMap from which this one (indirectly) originates.
 ATSymbol[] listFields()
          Returns all field names.
 boolean put(ATSymbol nam)
          Add a new field to the field map.
private  void reAlloc()
          Doubles the size of the map to make room for extra fields.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_DEFAULT_SIZE_

private static final int _DEFAULT_SIZE_
See Also:
Constant Field Values

varNames_

private ATSymbol[] varNames_

free_

private int free_

origin_

private FieldMap origin_
Constructor Detail

FieldMap

public FieldMap()

FieldMap

private FieldMap(ATSymbol[] copiedNames,
                 int copiedFree,
                 FieldMap origin)
Method Detail

put

public boolean put(ATSymbol nam)
Add a new field to the field map. The field will be assigned the index of the map's current size. If the field already exists, it will not be added twice.

Returns:
true if the field was added successfully, false if the field already existed

get

public int get(ATSymbol nam)
Retrieve the index of a field given its name.

Parameters:
nam - the name of the field
Returns:
the index of the field or -1 if the field cannot be found.

listFields

public ATSymbol[] listFields()
Returns all field names.

Returns:
an array of the field names stored in the map.

copy

FieldMap copy()
Creates a shallow copy of the field map. This operation is performed when a new field is added in one object which shares its field map with others. To allow tracking the origins of the new fieldMap, we need to store this as its progenitor.


isDerivedFrom

public boolean isDerivedFrom(FieldMap aFieldMap)
Checks whether both FieldMaps are equal or whether the passed object is a FieldMap from which this one (indirectly) originates.


reAlloc

private void reAlloc()
Doubles the size of the map to make room for extra fields.


findName

private int findName(ATSymbol nam)
Searches for the name of a field in the varNames_ array. Performs a simple sequential search, which is usually not a bottleneck as maps are supposed to be rather small (< 50 elements).

Returns:
the index of the field or -1 if not found.