📄 stringmap.java
字号:
if (node._char[ni]==c || _ignoreCase&&node._ochar[ni]==c) { ni++; if (ni==node._char.length) ni=-1; continue charLoop; } // No char match, so if mid node then no match at all. if (ni>0) return null; // try next in chain node=node._next; } return null; } if (ni>0) return null; if (node!=null && node._key==null) return null; return node; } /* ------------------------------------------------------------ */ /** Get a map entry by byte array key, using as much of the passed key as needed for a match. * A simple 8859-1 byte to char mapping is assumed. * @param key char array containing the key * @param offset Offset of the key within the array. * @param maxLength The length of the key * @return The Map.Entry for the key or null if the key is not in * the map. */ public Map.Entry getBestEntry(byte[] key,int offset, int maxLength) { if (key==null) return _nullEntry; Node node = _root; int ni=-1; // look for best match charLoop: for (int i=0;i<maxLength;i++) { char c=(char)key[offset+i]; // Advance node if (ni==-1) { ni=0; Node child = (node._children==null)?null:node._children[c%_width]; if (child==null && i>0) return node; // This is the best match node=child; } // While we have a node to try while (node!=null) { // If it is a matching node, goto next char if (node._char[ni]==c || _ignoreCase&&node._ochar[ni]==c) { ni++; if (ni==node._char.length) ni=-1; continue charLoop; } // No char match, so if mid node then no match at all. if (ni>0) return null; // try next in chain node=node._next; } return null; } if (ni>0) return null; if (node!=null && node._key==null) return null; return node; } /* ------------------------------------------------------------ */ public Object remove(Object key) { if (key==null) return remove(null); return remove(key.toString()); } /* ------------------------------------------------------------ */ public Object remove(String key) { if (key==null) { Object oldValue=_nullValue; if (_nullEntry!=null) { _entrySet.remove(_nullEntry); _nullEntry=null; _nullValue=null; } return oldValue; } Node node = _root; int ni=-1; // look for best match charLoop: for (int i=0;i<key.length();i++) { char c=key.charAt(i); // Advance node if (ni==-1) { ni=0; node=(node._children==null)?null:node._children[c%_width]; } // While we have a node to try while (node!=null) { // If it is a matching node, goto next char if (node._char[ni]==c || _ignoreCase&&node._ochar[ni]==c) { ni++; if (ni==node._char.length) ni=-1; continue charLoop; } // No char match, so if mid node then no match at all. if (ni>0) return null; // try next in chain node=node._next; } return null; } if (ni>0) return null; if (node!=null && node._key==null) return null; Object old = node._value; _entrySet.remove(node); node._value=null; node._key=null; return old; } /* ------------------------------------------------------------ */ public Set entrySet() { return _umEntrySet; } /* ------------------------------------------------------------ */ public int size() { return _entrySet.size(); } /* ------------------------------------------------------------ */ public boolean isEmpty() { return _entrySet.isEmpty(); } /* ------------------------------------------------------------ */ public boolean containsKey(Object key) { if (key==null) return _nullEntry!=null; return getEntry(key.toString(),0,key==null?0:key.toString().length())!=null; } /* ------------------------------------------------------------ */ public void clear() { _root=new Node(); _nullEntry=null; _nullValue=null; _entrySet.clear(); } /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ private static class Node implements Map.Entry { char[] _char; char[] _ochar; Node _next; Node[] _children; String _key; Object _value; Node(){} Node(boolean ignoreCase,String s, int offset) { int l=s.length()-offset; _char=new char[l]; _ochar=new char[l]; for (int i=0;i<l;i++) { char c=s.charAt(offset+i); _char[i]=c; if (ignoreCase) { char o=c; if (Character.isUpperCase(c)) o=Character.toLowerCase(c); else if (Character.isLowerCase(c)) o=Character.toUpperCase(c); _ochar[i]=o; } } } Node split(StringMap map,int offset) { Node split = new Node(); int sl=_char.length-offset; char[] tmp=this._char; this._char=new char[offset]; split._char = new char[sl]; System.arraycopy(tmp,0,this._char,0,offset); System.arraycopy(tmp,offset,split._char,0,sl); if (this._ochar!=null) { tmp=this._ochar; this._ochar=new char[offset]; split._ochar = new char[sl]; System.arraycopy(tmp,0,this._ochar,0,offset); System.arraycopy(tmp,offset,split._ochar,0,sl); } split._key=this._key; split._value=this._value; this._key=null; this._value=null; if (map._entrySet.remove(this)) map._entrySet.add(split); split._children=this._children; this._children=new Node[map._width]; this._children[split._char[0]%map._width]=split; if (split._ochar!=null && this._children[split._ochar[0]%map._width]!=split) this._children[split._ochar[0]%map._width]=split; return split; } public Object getKey(){return _key;} public Object getValue(){return _value;} public Object setValue(Object o){Object old=_value;_value=o;return old;} public String toString() { StringBuffer buf=new StringBuffer(); synchronized(buf) { toString(buf); } return buf.toString(); } private void toString(StringBuffer buf) { buf.append("{["); if (_char==null) buf.append('-'); else for (int i=0;i<_char.length;i++) buf.append(_char[i]); buf.append(':'); buf.append(_key); buf.append('='); buf.append(_value); buf.append(']'); if (_children!=null) { for (int i=0;i<_children.length;i++) { buf.append('|'); if (_children[i]!=null) _children[i].toString(buf); else buf.append("-"); } } buf.append('}'); if (_next!=null) { buf.append(",\n"); _next.toString(buf); } } } /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ private class NullEntry implements Map.Entry { public Object getKey(){return null;} public Object getValue(){return _nullValue;} public Object setValue(Object o) {Object old=_nullValue;_nullValue=o;return old;} public String toString(){return "[:null="+_nullValue+"]";} } /* ------------------------------------------------------------ */ public void writeExternal(java.io.ObjectOutput out) throws java.io.IOException { HashMap map = new HashMap(this); out.writeBoolean(_ignoreCase); out.writeObject(map); } /* ------------------------------------------------------------ */ public void readExternal(java.io.ObjectInput in) throws java.io.IOException, ClassNotFoundException { boolean ic=in.readBoolean(); HashMap map = (HashMap)in.readObject(); setIgnoreCase(ic); this.putAll(map); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -