commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Joel Shellman <j...@ikestrel.com>
Subject Re: [primitives] Looking for a primitive hashtable
Date Wed, 31 Mar 2004 11:43:42 GMT
Not necessarily so. I just started writing a little int keyed Object 
"map" backed by an object array last week.

The key difference is that if it is okay for the map to define the key, 
then it's no problem at all. For each new object, just append it to the 
end of the array. So the only holes that show up are when someone 
removes an object from the "map". And if you like, keep track of the 
holes so you can reuse them later.

One might argue that what I'm talking about is simply a thin layer 
making it easier to work with an extendable Object array, and that's 
fine. It just depends on how you want to look at it.

Funny thing is I just posted to the list last week about this asking if 
anyone thought it would be useful. Only got one reply.

-joel shellman

public class IntMap implements Serializable {
	
	private Object[] objects;
	
	private int cursor = 0;
	
	public IntMap() {
		objects = new Object[7];
		holes = new Stack();
	}
	
	public IntMap(int initialCapacity) {
		objects = new Object[initialCapacity];
	}
	
	public int add(Object value) {
		return append(value);
	}
	
	/**
	 * @param value
	 */
	private int append(Object value) {
		if (cursor == objects.length) {
			expandCapacity();
		}
		objects[cursor] = value;
		return cursor++;
	}

	private void expandCapacity() {
		throw new UnsupportedOperationException("Not done yet");
	}

	public void put(int key, Object value) {
		objects[key] = value;
	}
	
	public Object get(int key) {
		return objects[key];
	}
	
	public void remove(int key) {
		objects[key] = null;
	}
}


Tatu Vanhanen wrote:

> I'm not the original sender, but interesting issue though.
> 
> 
>>From: Rodney Waldhoff [mailto:rwaldhoff@apache.org]
> 
> ...
> 
>>Since the existing commons-primitives component was punted out of
>>commons-collections, commons-primitives is probably the place for it here.
>>
>>Depending upon your density, isn't Object[] essentially an int-keyed map?
> 
> 
> Yes but no ;) Using an integer (or long, short, etc) as an index is quite
> different than using it as a hash key. As an example concider a situation
> where you would like to cache some objects to a map based on the object's id
> (an integer). If there are a lot of objects, it feels bad to allocate a lot
> of Integers to be able to use the id as a key. And it also feels bad to just
> allocate an Object array big enough to handle the biggest possible id and
> somehow manage the array as values are inserted, removed and updated. So:
> map functionality is needed.
> 
> It is true that a primitive-keyed-map probably uses an Object array to store
> the values. But to be useful as a general purpose map, an Object array needs
> some functionality around it. And the result is... an int-keyed map.


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message