1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/* Glazed Lists (c) 2003-2006 */
/* http://publicobject.com/glazedlists/ publicobject.com,*/
/* O'Dell Engineering Ltd.*/
package ca.odell.glazedlists.impl.adt.barcode2;
import java.util.Arrays;
import java.util.List;
/*
# some M4 Macros that make it easy to use m4 with Java
M4 Macros
# define a function NODE_WIDTH(boolean) to get the node's size for this color
# define a function NODE_SIZE(node, colors) to no node.nodeSize()
# define a function to refresh counts
# multiple values
*/
/*[ BEGIN_M4_JAVA ]*/
/**
* A node in a tree which supports both a value and compressed nodes that
* contain a size, useful for index offsetting.
*
* <p>Note that the <code>counts</code> summary member is created lazily when
* this node is given children. This causes the code to be less easy to read,
* but it means we can put off about a huge number of object allocations since
* 50% of the nodes in an arbitrary tree are leaf nodes, and these leaf nodes
* now don't have counts.
*
* @author <a href="mailto:jesse@swank.ca">Jesse Wilson</a>
*/
class SimpleNode < T0> implements Element<T0> {
/** the number of elements of each color in this subtree */
int count1;
/** the node's value */
T0 t0;
/** values for managing the node within the tree */
byte height;
SimpleNode < T0> left, right, parent;
/** whether this node is consistent in the sorting order */
int sorted = SORTED;
/**
* Create a new node.
*
* @param color a bitmask value such as 1, 2, 4, 8 or 16.
* @param size the size of the node
* @param value the value of the node
* @param parent the parent node in the tree, or <code>null</code> for the
* root node.
*/
public SimpleNode/**/( int size, T0 value, SimpleNode < T0> parent) {
assert(size == 1);
this.t0 = value;
this.height = 1;
this.parent = parent;
count1 += size;
}
/**
* Get the value of this element.
*/
public T0 get() {
return t0;
}
/**
* Set the value of this element.
*/
public void set(T0 value) {
this.t0 = value;
}
/** access the node's values */
public T0 get0() { return t0; }
public void set0(T0 value) { this.t0 = value; }
/**
* Get the color of this element.
*/
public byte getColor() {
return 1 ;
}
/**
* The size of this entire node, including the left child, this node
* and the right child.
*/
final int size(byte colors) {
// total the values of the specified array for the specified colors.
int result = 0;
if((colors & 1) != 0) result += count1;
return result;
}
/**
* Update the counts member variable by examining the counts of
* the child nodes and the size member variable.
*/
final void refreshCounts( boolean countSelf ) {
count1 = 0;
// left child
if(left != null) {
count1 += left.count1;
}
// right child
if(right != null) {
count1 += right.count1;
}
// this node
count1 += countSelf ? 1 : 0;
}
/** {@inheritDoc} */
@Override
public String toString() {
return toString(Arrays.asList(new String[] { "A", "B", "C", "D", "E", "F", "G", "H"}));
}
/**
* Write this node out as a String, using the specified colors to write
* each of the node values.
*/
String toString(List colors) {
StringBuffer result = new StringBuffer();
asTree(0, result, colors);
return result.toString();
}
/**
* Dump this node as a String for diagnostic and debugging purposes.
*/
void asTree(int indentation, StringBuffer out, List colors) {
// write the left subtree
if(left != null) left.asTree(indentation + 1, out, colors);
// write this node
for(int i = 0; i < indentation; i++) {
out.append(" ");
}
if(t0 != null) {
out.append(": ");
if(t0 instanceof SimpleNode) {
out.append("<Node>");
} else {
out.append(t0);
}
}
out.append("\n");
// write the right subtree
if(right != null) right.asTree(indentation + 1, out, colors);
}
/**
* Toggle whether this node is sorted.
*/
public void setSorted(int sorted) {
this.sorted = sorted;
}
/**
* Get whether the value of this node is greater than the previous node
* and less than the next node. This is useful to have occasional unsorted
* elements in an otherwise sorted collection, such as what happens when the
* user expects order to be both sorted and stable during edits which would
* otherwise change the sorting order.
*/
public int getSorted() {
return sorted;
}
/** {@inheritDoc} */
public Element<T0> next() {
return SimpleTree.next(this);
}
/** {@inheritDoc} */
public Element<T0> previous() {
return SimpleTree.previous(this);
}
}
/*[ END_M4_JAVA ]*/
|