KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > TreeInt


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o;
22
23 import com.db4o.foundation.*;
24
25 /**
26  * Base class for balanced trees.
27  *
28  * @exclude
29  */

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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc key(){
179         return new Integer JavaDoc(_key);
180     }
181
182
183 }
184
Popular Tags