📄 pystringmap.java
字号:
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; i<bn; i++) { PyObject akey = akeys.get(i); PyObject bkey = bkeys.get(i); int c = akey._cmp(bkey); if (c != 0) return c; PyObject avalue = __finditem__(akey); PyObject bvalue = other.__finditem__(bkey); c = avalue._cmp(bvalue); if (c != 0) return c; } return 0; } /** * Return true if the key exist in the dictionary. */ public boolean has_key(PyObject key) { return __finditem__(key) != null; } /** * Return this[key] if the key exists in the mapping, default_object * is returned otherwise. * * @param key the key to lookup in the mapping. * @param default_object the value to return if the key does not * exists in the mapping. */ public PyObject get(PyObject key, PyObject default_object) { PyObject o = __finditem__(key); if (o == null) return default_object; else return o; } /** * Return this[key] if the key exists in the mapping, None * is returned otherwise. * * @param key the key to lookup in the mapping. */ public PyObject get(PyObject key) { return get(key, Py.None); } /** * Return a shallow copy of the dictionary. */ public synchronized PyStringMap copy() { int n = keys.length; PyStringMap map = new PyStringMap(n); System.arraycopy(keys, 0, map.keys, 0, n); System.arraycopy(values, 0, map.values, 0, n); map.filled = filled; map.size = size; map.prime = prime; return map; } /** * Insert all the key:value pairs from <code>map</code> 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<n; i++) { String key = keyTable[i]; if (key == null || key == "<deleted key>") continue; insertkey(key, valueTable[i]); } } /** * Insert all the key:value pairs from <code>dict</code> 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<n; i++) { __setitem__((PyObject)ek.nextElement(), (PyObject)ev.nextElement()); } } /** * Return this[key] if the key exist, otherwise insert key with * a None value and return None. * * @param key the key to lookup in the mapping. */ public PyObject setdefault(PyObject key) { return setdefault(key, Py.None); } /** * Return this[key] if the key exist, otherwise insert key with * the value of failobj and return failobj * * @param key the key to lookup in the mapping. * @param failobj the default value to insert in the mapping * if key does not already exist. */ public PyObject setdefault(PyObject key, PyObject failobj) { PyObject o = __finditem__(key); if (o == null) __setitem__(key, o = failobj); return o; } /** * Return a random (key, value) tuple pair and remove the pair * from the mapping. */ 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 }); } /** * 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<n; i++) { String key = keyTable[i]; if (key == null || key == "<deleted key>" || 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<n; i++) { String key = keyTable[i]; if (key == null || key == "<deleted key>") 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<n; i++) { String key = keyTable[i]; if (key == null || key == "<deleted key>" || 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<n; i++) { PyObject value = valueTable[i]; if (value == null) continue; l.append(value); } return l; }}class PyStringMapIter extends PyIterator { String[] keyTable; PyObject[] valTable; private int idx; public PyStringMapIter(String[] keys, PyObject[] values) { this.keyTable = keys; this.valTable = values; this.idx = 0; } public PyObject __iternext__() { int n = keyTable.length; for (; idx < n; idx++) { String key = keyTable[idx]; if (key == null || key == "<deleted key>" || 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__; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -