001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.internal;
022:
023: import com.db4o.foundation.*;
024: import com.db4o.internal.query.processor.*;
025:
026: /**
027: * Base class for balanced trees.
028: *
029: * @exclude
030: */
031: public class TreeInt extends Tree implements ReadWriteable {
032:
033: public static TreeInt add(TreeInt tree, int value) {
034: return (TreeInt) Tree.add(tree, new TreeInt(value));
035: }
036:
037: public static TreeInt removeLike(TreeInt tree, int value) {
038: return (TreeInt) Tree.removeLike(tree, new TreeInt(value));
039: }
040:
041: public static Tree addAll(Tree tree, IntIterator4 iter) {
042: if (!iter.moveNext()) {
043: return tree;
044: }
045: TreeInt firstAdded = new TreeInt(iter.currentInt());
046: tree = Tree.add(tree, firstAdded);
047: while (iter.moveNext()) {
048: tree = tree.add(new TreeInt(iter.currentInt()));
049: }
050: return tree;
051: }
052:
053: public int _key;
054:
055: public TreeInt(int a_key) {
056: this ._key = a_key;
057: }
058:
059: public int compare(Tree a_to) {
060: return _key - ((TreeInt) a_to)._key;
061: }
062:
063: Tree deepClone() {
064: return new TreeInt(_key);
065: }
066:
067: public boolean duplicates() {
068: return false;
069: }
070:
071: public static final TreeInt find(Tree a_in, int a_key) {
072: if (a_in == null) {
073: return null;
074: }
075: return ((TreeInt) a_in).find(a_key);
076: }
077:
078: public final TreeInt find(int a_key) {
079: int cmp = _key - a_key;
080: if (cmp < 0) {
081: if (_subsequent != null) {
082: return ((TreeInt) _subsequent).find(a_key);
083: }
084: } else {
085: if (cmp > 0) {
086: if (_preceding != null) {
087: return ((TreeInt) _preceding).find(a_key);
088: }
089: } else {
090: return this ;
091: }
092: }
093: return null;
094: }
095:
096: public Object read(Buffer a_bytes) {
097: return new TreeInt(a_bytes.readInt());
098: }
099:
100: public void write(Buffer a_writer) {
101: a_writer.writeInt(_key);
102: }
103:
104: public static void write(final Buffer a_writer, TreeInt a_tree) {
105: write(a_writer, a_tree, a_tree == null ? 0 : a_tree.size());
106: }
107:
108: public static void write(final Buffer a_writer, TreeInt a_tree,
109: int size) {
110: if (a_tree == null) {
111: a_writer.writeInt(0);
112: return;
113: }
114: a_writer.writeInt(size);
115: a_tree.traverse(new Visitor4() {
116: public void visit(Object a_object) {
117: ((TreeInt) a_object).write(a_writer);
118: }
119: });
120: }
121:
122: public int ownLength() {
123: return Const4.INT_LENGTH;
124: }
125:
126: boolean variableLength() {
127: return false;
128: }
129:
130: QCandidate toQCandidate(QCandidates candidates) {
131: QCandidate qc = new QCandidate(candidates, null, _key, true);
132: qc._preceding = toQCandidate((TreeInt) _preceding, candidates);
133: qc._subsequent = toQCandidate((TreeInt) _subsequent, candidates);
134: qc._size = _size;
135: return qc;
136: }
137:
138: public static QCandidate toQCandidate(TreeInt tree,
139: QCandidates candidates) {
140: if (tree == null) {
141: return null;
142: }
143: return tree.toQCandidate(candidates);
144: }
145:
146: public String toString() {
147: return "" + _key;
148: }
149:
150: protected Tree shallowCloneInternal(Tree tree) {
151: TreeInt treeint = (TreeInt) super .shallowCloneInternal(tree);
152: treeint._key = _key;
153: return treeint;
154: }
155:
156: public Object shallowClone() {
157: TreeInt treeint = new TreeInt(_key);
158: return shallowCloneInternal(treeint);
159: }
160:
161: public static int marshalledLength(TreeInt a_tree) {
162: if (a_tree == null) {
163: return Const4.INT_LENGTH;
164: }
165: return a_tree.marshalledLength();
166: }
167:
168: public final int marshalledLength() {
169: if (variableLength()) {
170: final IntByRef mint = new IntByRef(Const4.INT_LENGTH);
171: traverse(new Visitor4() {
172: public void visit(Object obj) {
173: mint.value += ((TreeInt) obj).ownLength();
174: }
175: });
176: return mint.value;
177: }
178: return Const4.INT_LENGTH + (size() * ownLength());
179: }
180:
181: public Object key() {
182: return new Integer(_key);
183: }
184:
185: }
|