From: Finn B. <bc...@us...> - 2001-02-01 13:08:13
|
Update of /cvsroot/jython/jython/org/python/core In directory usw-pr-cvs1:/tmp/cvs-serv24998 Modified Files: PyDictionary.java PyStringMap.java Log Message: Added .popitem() to dictionaries. Index: PyDictionary.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PyDictionary.java,v retrieving revision 2.10 retrieving revision 2.11 diff -C2 -r2.10 -r2.11 *** PyDictionary.java 2000/11/19 22:32:45 2.10 --- PyDictionary.java 2001/02/01 13:08:13 2.11 *************** *** 34,37 **** --- 34,39 ---- case 7: return dict.values(); + case 8: + return dict.popitem(); default: throw argCountError(0); *************** *** 115,123 **** dict.__setitem__("keys", new DictFuncs("keys", 6, 0)); dict.__setitem__("values", new DictFuncs("values", 7, 0)); dict.__setitem__("__cmp__", new DictFuncs("__cmp__", 11, 1)); dict.__setitem__("has_key", new DictFuncs("has_key", 12, 1)); dict.__setitem__("get", new DictFuncs("get", 13, 1, 2)); dict.__setitem__("update", new DictFuncs("update", 14, 1)); ! // Hide thse from Python dict.__setitem__("__finditem__", null); dict.__setitem__("__setitem__", null); --- 117,126 ---- dict.__setitem__("keys", new DictFuncs("keys", 6, 0)); dict.__setitem__("values", new DictFuncs("values", 7, 0)); + dict.__setitem__("popitem", new DictFuncs("popitem", 8, 0)); dict.__setitem__("__cmp__", new DictFuncs("__cmp__", 11, 1)); dict.__setitem__("has_key", new DictFuncs("has_key", 12, 1)); dict.__setitem__("get", new DictFuncs("get", 13, 1, 2)); dict.__setitem__("update", new DictFuncs("update", 14, 1)); ! // Hide these from Python dict.__setitem__("__finditem__", null); dict.__setitem__("__setitem__", null); *************** *** 276,279 **** --- 279,292 ---- __setitem__(key, o = failobj); return o; + } + + public PyObject popitem() { + java.util.Enumeration keys = table.keys(); + if (!keys.hasMoreElements()) + throw Py.KeyError("popitem(): dictionary is empty"); + PyObject key = (PyObject) keys.nextElement(); + PyObject val = (PyObject) table.get(key); + table.remove(key); + return new PyTuple(new PyObject[] { key, val }); } Index: PyStringMap.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PyStringMap.java,v retrieving revision 2.6 retrieving revision 2.7 diff -C2 -r2.6 -r2.7 *** PyStringMap.java 2000/09/28 13:38:38 2.6 --- PyStringMap.java 2001/02/01 13:08:13 2.7 *************** *** 17,20 **** --- 17,21 ---- private transient int filled; private transient int prime; + private transient int popfinger; /* Override serialization behavior */ *************** *** 376,379 **** --- 377,411 ---- __setitem__(key, o = failobj); return o; + } + + + public synchronized PyObject popitem() { + if (size == 0) + throw Py.KeyError("popitem(): dictionary is empty"); + + String[] table = keys; + int maxindex = table.length; + int index = popfinger; + + if (index >= maxindex || index < 0) + index = 1; + while (true) { + String tKey = table[index]; + if (tKey != null && tKey != "<deleted key>") + break; + index++; + if (index >= maxindex) + index = 0; + } + + popfinger = index + 1; + PyObject key = Py.newString(table[index]); + PyObject val = (PyObject) values[index]; + + table[index] = "<deleted key>"; + values[index] = null; + size--; + + return new PyTuple(new PyObject[] { key, val }); } |