// Copyright (c) Corporation for National Research Initiatives
// Modified for tinyos by Michael Demmer (demmer@cs.berkeley.edu) to
// use java.lang.String hashCode() and equals() methods, not to use
// System.identityHashCode() and the == operator.
package org.python.core;
/**
* A faster Dictionary where the keys have to be strings.
*
* This is the default for all __dict__ instances.
*/
public class PyStringMap extends PyObject
{
//Table of primes to cycle through
private static final int[] primes = {
7, 13, 31, 61, 127, 251, 509, 1021, 2017, 4093,
5987, 9551, 15683, 19609, 31397,
65521, 131071, 262139, 524287, 1048573, 2097143,
4194301, 8388593, 16777213, 33554393, 67108859,
134217689, 268435399, 536870909, 1073741789,};
private transient String[] keys;
private transient PyObject[] values;
private int size;
private transient int filled;
private transient int prime;
private transient int popfinger;
/* Override serialization behavior */
private void writeObject(java.io.ObjectOutputStream out)
throws java.io.IOException
{
out.defaultWriteObject();
String[] keyTable = keys;
PyObject[] valueTable = values;
int n = keyTable.length;
for (int i=0; i 0) {
// System.err.println("key: "+key+", "+collisions+", "+
// maxindex+", "+System.identityHashCode(key));
//}
return values[index];
}
//collisions++;
index = (index+stepsize) % maxindex;
}
}
public PyObject __finditem__(PyObject key) {
//System.err.println("oops: "+key);
if (key instanceof PyString) {
return __finditem__(((PyString)key).internedString());
} else {
return null;
}
}
public PyObject __iter__() {
return new PyStringMapIter(keys, values);
}
private final void insertkey(String key, PyObject value) {
String[] table = keys;
int maxindex = table.length;
int index = (myHashCode(key) & 0x7fffffff) % maxindex;
int orig = index;
// Fairly aribtrary choice for stepsize...
int stepsize = maxindex / 5;
int free_index = -1;
// Cycle through possible positions for the key;
while (true) {
String tkey = table[index];
if (tkey == null) {
if (free_index == -1 ) {
filled++;
free_index = index;
}
break;
} else if (tkey.equals(key)) {
values[index] = value;
return;
} else if (tkey == "" && free_index == -1) {
free_index = index;
}
index = (index+stepsize) % maxindex;
}
table[free_index] = key;
values[free_index] = value;
size++;
return;
}
private synchronized final void resize(int capacity) {
int p = prime;
for (; p= capacity)
break;
}
if (primes[p] < capacity) {
throw Py.ValueError("can't make hashtable of size: "+capacity);
}
//System.err.println("resize: "+(keys != null ? keys.length : -1)+
// ", "+primes[p]);
capacity = primes[p];
prime = p;
String[] oldKeys = keys;
PyObject[] oldValues = values;
keys = new String[capacity];
values = new PyObject[capacity];
size = 0;
filled = 0;
if (oldValues != null) {
int n = oldValues.length;
for (int i=0; i keys.length)
resize(keys.length+1);
insertkey(key, value);
}
public void __setitem__(PyObject key, PyObject value) {
if (key instanceof PyString) {
__setitem__(((PyString)key).internedString(), value);
} else {
throw Py.TypeError("keys in namespace must be strings");
}
}
public synchronized void __delitem__(String key) {
String[] table = keys;
int maxindex = table.length;
int index = (myHashCode(key) & 0x7fffffff) % maxindex;
// Fairly aribtrary choice for stepsize...
int stepsize = maxindex / 5;
// Cycle through possible positions for the key;
while (true) {
String tkey = table[index];
if (tkey == null) {
throw Py.KeyError(key);
}
if (tkey.equals(key)) {
table[index] = "";
values[index] = null;
size--;
break;
}
index = (index+stepsize) % maxindex;
}
}
public void __delitem__(PyObject key) {
if (key instanceof PyString) {
__delitem__(((PyString)key).internedString());
} else {
throw Py.KeyError(key.toString());
}
}
/**
* Remove all items from the dictionary.
*/
public synchronized void clear() {
for (int i=0; i 4) {
buf.setLength(len-2);
}
buf.append("}");
ts.exitRepr(this);
return buf.toString();
}
public synchronized int __cmp__(PyObject other) {
if (!(other instanceof PyStringMap ||
other instanceof PyDictionary)) {
return -2;
}
int an = __len__();
int bn = other.__len__();
if (an < bn) return -1;
if (an > bn) return 1;
PyList akeys = keys();
PyList bkeys = null;
if (other instanceof PyStringMap) {
bkeys = ((PyStringMap)other).keys();
} else {
bkeys = ((PyDictionary)other).keys();
}
akeys.sort();
bkeys.sort();
for (int i=0; imap into
* this mapping.
*/
public synchronized void update(PyStringMap map) {
String[] keyTable = map.keys;
PyObject[] valueTable = map.values;
int n = keyTable.length;
if (2*filled+n > keys.length)
resize(2*filled+n);
for (int i=0; i")
continue;
insertkey(key, valueTable[i]);
}
}
/**
* Insert all the key:value pairs from dict
into
* this mapping.
*/
public void update(PyDictionary dict) {
java.util.Hashtable table = dict.table;
java.util.Enumeration ek = table.keys();
java.util.Enumeration ev = table.elements();
int n = table.size();
for(int i=0; i= maxindex || index < 0)
index = 1;
while (true) {
String tKey = table[index];
if (tKey != null && tKey != "")
break;
index++;
if (index >= maxindex)
index = 0;
}
popfinger = index + 1;
PyObject key = Py.newString(table[index]);
PyObject val = (PyObject) values[index];
table[index] = "";
values[index] = null;
size--;
return new PyTuple(new PyObject[] { key, val });
}
/**
* Return a copy of the mappings list of (key, value) tuple
* pairs.
*/
public synchronized PyList items() {
String[] keyTable = keys;
PyObject[] valueTable = values;
int n = keyTable.length;
PyList l = new PyList();
for (int i=0; i" || values[i] == null)
continue;
l.append(new PyTuple(new PyObject[] {
new PyString(key), valueTable[i]
}));
}
return l;
}
synchronized String[] jkeys() {
String[] keyTable = keys;
//PyObject[] valueTable = values;
int n = keyTable.length;
String[] newKeys = new String[size];
int j=0;
for (int i=0; i")
continue;
newKeys[j++] = key;
}
return newKeys;
}
/**
* Return a copy of the mappings list of keys.
*/
public synchronized PyList keys() {
String[] keyTable = keys;
//PyObject[] valueTable = values;
int n = keyTable.length;
PyList l = new PyList();
for (int i=0; i" || values[i] == null)
continue;
l.append(new PyString(key));
}
return l;
}
/**
* Return a copy of the mappings list of values.
*/
public synchronized PyList values() {
PyObject[] valueTable = values;
int n = valueTable.length;
PyList l = new PyList();
for (int i=0; i" || valTable[idx] == null)
continue;
idx++;
return Py.newString(key);
}
return null;
}
// __class__ boilerplate -- see PyObject for details
public static PyClass __class__;
protected PyClass getPyClass() { return __class__; }
}