1 21 package com.db4o; 22 23 import com.db4o.foundation.*; 24 25 30 public class TreeInt extends Tree implements ReadWriteable { 31 32 public static TreeInt add(TreeInt tree, int value) { 33 return (TreeInt) Tree.add(tree, new TreeInt(value)); 34 } 35 36 public static TreeInt removeLike(TreeInt tree, int value) { 37 return (TreeInt) Tree.removeLike(tree, new TreeInt(value)); 38 } 39 40 public static Tree addAll(Tree tree, IntIterator4 iter){ 41 if(! iter.moveNext()){ 42 return tree; 43 } 44 TreeInt firstAdded = new TreeInt(iter.currentInt()); 45 tree = Tree.add(tree, firstAdded); 46 while(iter.moveNext()){ 47 tree = tree.add( new TreeInt(iter.currentInt())); 48 } 49 return tree; 50 } 51 52 public int _key; 53 54 public TreeInt(int a_key) { 55 this._key = a_key; 56 } 57 58 public int compare(Tree a_to) { 59 return _key - ((TreeInt) a_to)._key; 60 } 61 62 Tree deepClone() { 63 return new TreeInt(_key); 64 } 65 66 public boolean duplicates() { 67 return false; 68 } 69 70 public static final TreeInt find(Tree a_in, int a_key) { 71 if (a_in == null) { 72 return null; 73 } 74 return ((TreeInt) a_in).find(a_key); 75 } 76 77 final TreeInt find(int a_key) { 78 int cmp = _key - a_key; 79 if (cmp < 0) { 80 if (_subsequent != null) { 81 return ((TreeInt) _subsequent).find(a_key); 82 } 83 } else { 84 if (cmp > 0) { 85 if (_preceding != null) { 86 return ((TreeInt) _preceding).find(a_key); 87 } 88 } else { 89 return this; 90 } 91 } 92 return null; 93 } 94 95 public Object read(YapReader a_bytes) { 96 return new TreeInt(a_bytes.readInt()); 97 } 98 99 public void write(YapReader a_writer) { 100 a_writer.writeInt(_key); 101 } 102 103 public static void write(final YapReader a_writer, TreeInt a_tree){ 104 write(a_writer, a_tree, a_tree == null ? 0 : a_tree.size()); 105 } 106 107 public static void write(final YapReader a_writer, TreeInt a_tree, int size){ 108 if(a_tree == null){ 109 a_writer.writeInt(0); 110 return; 111 } 112 a_writer.writeInt(size); 113 a_tree.traverse(new Visitor4() { 114 public void visit(Object a_object) { 115 ((TreeInt)a_object).write(a_writer); 116 } 117 }); 118 } 119 120 public int ownLength() { 121 return YapConst.INT_LENGTH; 122 } 123 124 boolean variableLength() { 125 return false; 126 } 127 128 QCandidate toQCandidate(QCandidates candidates) { 129 QCandidate qc = new QCandidate(candidates, null, _key, true); 130 qc._preceding = toQCandidate((TreeInt) _preceding, candidates); 131 qc._subsequent = toQCandidate((TreeInt) _subsequent, candidates); 132 qc._size = _size; 133 return qc; 134 } 135 136 public static QCandidate toQCandidate(TreeInt tree, QCandidates candidates) { 137 if (tree == null) { 138 return null; 139 } 140 return tree.toQCandidate(candidates); 141 } 142 143 public String toString() { 144 return "" + _key; 145 } 146 147 protected Tree shallowCloneInternal(Tree tree) { 148 TreeInt treeint=(TreeInt)super.shallowCloneInternal(tree); 149 treeint._key=_key; 150 return treeint; 151 } 152 153 public Object shallowClone() { 154 TreeInt treeint= new TreeInt(_key); 155 return shallowCloneInternal(treeint); 156 } 157 158 public static int byteCount(TreeInt a_tree){ 159 if(a_tree == null){ 160 return YapConst.INT_LENGTH; 161 } 162 return a_tree.byteCount(); 163 } 164 165 public final int byteCount(){ 166 if(variableLength()){ 167 final int[] length = new int[]{YapConst.INT_LENGTH}; 168 traverse(new Visitor4(){ 169 public void visit(Object obj){ 170 length[0] += ((TreeInt)obj).ownLength(); 171 } 172 }); 173 return length[0]; 174 } 175 return YapConst.INT_LENGTH + (size() * ownLength()); 176 } 177 178 public Object key(){ 179 return new Integer (_key); 180 } 181 182 183 } 184 | Popular Tags |