File: BciiTreeIterator.java

package info (click to toggle)
libglazedlists-java 1.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,024 kB
  • ctags: 4,252
  • sloc: java: 22,561; xml: 818; sh: 51; makefile: 5
file content (318 lines) | stat: -rw-r--r-- 12,832 bytes parent folder | download | duplicates (3)
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* 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.NoSuchElementException;

/*
 m4_include(source/ca/odell/glazedlists/impl/adt/barcode2/JavaMacros.m4)
 m4_include(source/ca/odell/glazedlists/impl/adt/barcode2/TreeMacros.m4)
*/
/*[ BEGIN_M4_JAVA ]*/

/**
 * Iterate through a {@link BciiTree}, one element at a time.
 *
 * <p>We should consider adding the following enhancements to this class:
 * <li>writing methods, such as <code>set()</code> and <code>remove()</code>.
 * <li>a default color, specified at construction time, that shall always be
 *     used as the implicit parameter to overloaded versions of {@link #hasNext}
 *     and {@link #next}.
 *
 * @author <a href="mailto:jesse@swank.ca">Jesse Wilson</a>
 */
public class BciiTreeIterator/*[ TYPELIST_START ]*/ <T0,T1> /*[ TYPELIST_END ]*/ {

    /*[ GENERATED_CODE_START
    forloop(`i', 0, VAR_LAST_COLOR_INDEX, `int counti(i);
    ')
    GENERATED_CODE_END
    EXAMPLE_START ]*/
    int count1;
    int count2;
    int count4;
    /*[ EXAMPLE_END ]*/

    private BciiTree/*[ TYPELIST_START ]*/ <T0,T1> /*[ TYPELIST_END ]*/ tree;
    private BciiNode/*[ TYPELIST_START ]*/ <T0,T1> /*[ TYPELIST_END ]*/ node;
    private int index;

    public BciiTreeIterator/**/(BciiTree/*[ TYPELIST_START ]*/ <T0,T1> /*[ TYPELIST_END ]*/ tree) {
        this(tree, 0, (byte)0);
    }

    /**
     * Create an iterator starting at the specified index.
     *
     * @param tree the tree to iterate
     * @param nextIndex the index to be returned after calling {@link #next next()}.
     * @param nextIndexColors the colors to interpret nextIndex in terms of
     */
    public BciiTreeIterator/**/(BciiTree/*[ TYPELIST_START ]*/ <T0,T1> /*[ TYPELIST_END ]*/ tree, int nextIndex, byte nextIndexColors) {
        this.tree = tree;

        // if the start is, we need to find the node in the tree
        if(nextIndex != 0) {
            int currentIndex = nextIndex - 1;
            this.node = (/*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/)tree.get(currentIndex /*[ COLORED_START ]*/, nextIndexColors /*[ COLORED_END ]*/);

            // find the counts
            /*[ GENERATED_CODE_START
            forloop(`i', 0, VAR_LAST_COLOR_INDEX, `m4_ifelse(VAR_COLOUR_COUNT,`1',`count1 = currentIndex;
            ', counti(i)` = tree.convertIndexColor(currentIndex, nextIndexColors, (byte)'indexToBit(i)`) + (node.color == 'indexToBit(i)` ? 0 : 1);
            ')')
            GENERATED_CODE_END
            EXAMPLE_START ]*/
            count1 = tree.convertIndexColor(currentIndex, nextIndexColors, (byte)1) + (node.color == 1 ? 0 : 1);
            count2 = tree.convertIndexColor(currentIndex, nextIndexColors, (byte)2) + (node.color == 2 ? 0 : 1);
            count4 = tree.convertIndexColor(currentIndex, nextIndexColors, (byte)4) + (node.color == 4 ? 0 : 1);
            /*[ EXAMPLE_END ]*/

            // find out the index in the node
            /*[ GENERATED_CODE_START
            forloop(`i', 0, VAR_LAST_COLOR_INDEX, `m4_ifelse(VAR_COLOUR_COUNT,`1',`this.index = count1 - tree.indexOfNode(this.node, (byte)1);
            ',`if(node.color == 'indexToBit(i)`) this.index = 'counti(i)` - tree.indexOfNode(this.node, '(byte)indexToBit(i)`);
            ')')
            GENERATED_CODE_END
            EXAMPLE_START ]*/
            if(node.color == 1) this.index = count1 - tree.indexOfNode(this.node, (byte)1);
            if(node.color == 2) this.index = count2 - tree.indexOfNode(this.node, (byte)2);
            if(node.color == 4) this.index = count4 - tree.indexOfNode(this.node, (byte)4);
            /*[ EXAMPLE_END ]*/

        // just start before the beginning of the tree
        } else {
            this.node = null;
            this.index = 0;
        }
    }

    /**
     * Create a {@link BciiTreeIterator} exactly the same as this one.
     * The iterators will be backed by the same tree but maintain
     * separate cursors into the tree.
     */
    public BciiTreeIterator/*[ TYPELIST_START ]*/ <T0,T1> /*[ TYPELIST_END ]*/ copy() {
        BciiTreeIterator/*[ TYPELIST_START ]*/ <T0,T1> /*[ TYPELIST_END ]*/ result = new BciiTreeIterator/*[ TYPELIST_START ]*/ <T0,T1> /*[ TYPELIST_END ]*/(tree);

        /*[ GENERATED_CODE_START
        forloop(`i', 0, VAR_LAST_COLOR_INDEX, `result.counti(i) = this.counti(i);
        ')
        GENERATED_CODE_END
        EXAMPLE_START ]*/
        result.count1 = this.count1;
        result.count2 = this.count2;
        result.count4 = this.count4;
        /*[ EXAMPLE_END ]*/

        result.node = node;
        result.index = index;
        return result;
    }

    /**
     * @return <code>true</code> if there's an element of the specified color in
     *     this tree following the current element.
     */
    public boolean hasNext(/*[ COLORED_START ]*/ byte colors /*[ COLORED_END ]*/) {
        if(node == null) {
            return tree.size(/*[ COLORED_START ]*/ colors /*[ COLORED_END ]*/) > 0;
        } else if(/*[ COLORED_START(true) ]*/ (colors & node.color) != 0 /*[ COLORED_END ]*/) {
            return index(/*[ COLORED_START ]*/ colors /*[ COLORED_END ]*/) < tree.size(/*[ COLORED_START ]*/ colors /*[ COLORED_END ]*/) - 1;
        } else {
            return index(/*[ COLORED_START ]*/ colors /*[ COLORED_END ]*/) < tree.size(/*[ COLORED_START ]*/ colors /*[ COLORED_END ]*/);
        }
    }

    /**
     * @return <code>true</code> if there's a node of the specified color in this
     *      tree following the current node.
     */
    public boolean hasNextNode(/*[ COLORED_START ]*/ byte colors /*[ COLORED_END ]*/) {
        if(node == null) {
            return tree.size(/*[ COLORED_START ]*/ colors /*[ COLORED_END ]*/) > 0;
        } else {
            return nodeEndIndex(/*[ COLORED_START ]*/ colors /*[ COLORED_END ]*/) < tree.size(/*[ COLORED_START ]*/ colors /*[ COLORED_END ]*/);
        }
    }

    /**
     * Step to the next element.
     */
    public void next(/*[ COLORED_START ]*/ byte colors /*[ COLORED_END ]*/) {
        if(!hasNext(/*[ COLORED_START ]*/ colors /*[ COLORED_END ]*/)) {
            throw new NoSuchElementException();
        }

        // start at the first node in the tree
        if(node == null) {
            node = tree.firstNode();
            index = 0;
            /*[ COLORED_START ]*/if((node.color & colors) != 0) /*[ COLORED_END ]*/ return;

        // increment within the current node
        } else if(/*[ COLORED_START ]*/ (node.color & colors) != 0 && /*[ COLORED_END ]*/ index < /*[ WIDE_NODES_START(1) ]*/ node.size /*[ WIDE_NODES_END ]*/ - 1) {
            /*[ GENERATED_CODE_START
            forloop(`i', 0, VAR_LAST_COLOR_INDEX, `m4_ifelse(VAR_COLOUR_COUNT,`1',`count1++;
            ', `if(node.color == indexToBit(i)) counti(i)++;
            ')')
            GENERATED_CODE_END
            EXAMPLE_START ]*/
            if(node.color == 1) count1++;
            if(node.color == 2) count2++;
            if(node.color == 4) count4++;
            /*[ EXAMPLE_END ]*/
            index++;
            return;
        }

        // scan through the nodes, looking for the first one of the right color
        while(true) {
            /*[ GENERATED_CODE_START
            forloop(`i', 0, VAR_LAST_COLOR_INDEX, `m4_ifelse(VAR_COLOUR_COUNT,`1',`count1 += NODE_SIZE(node, colors) - index;
            ', `if(node.color == indexToBit(i)) counti(i) += node.size - index;
            ')')
            GENERATED_CODE_END
            EXAMPLE_START ]*/
            if(node.color == 1) count1 += node.size - index;
            if(node.color == 2) count2 += node.size - index;
            if(node.color == 4) count4 += node.size - index;
            /*[ EXAMPLE_END ]*/
            node = BciiTree.next(node);
            index = 0;

            // we've found a node that meet our requirements, so return
            /*[ COLORED_START ]*/ if((node.color & colors) != 0) /*[ COLORED_END ]*/ break;
        }
    }

    /**
     * Step to the next node.
     */
    public void nextNode(/*[ COLORED_START ]*/ byte colors /*[ COLORED_END ]*/) {
        if(!hasNextNode(/*[ COLORED_START ]*/ colors /*[ COLORED_END ]*/)) {
            throw new NoSuchElementException();
        }

        // start at the first node in the tree
        if(node == null) {
            node = tree.firstNode();
            index = 0;
            /*[ COLORED_START ]*/if((node.color & colors) != 0) /*[ COLORED_END ]*/ return;
        }

        // scan through the nodes, looking for the first one of the right color
        while(true) {
            /*[ GENERATED_CODE_START
            forloop(`i', 0, VAR_LAST_COLOR_INDEX, `m4_ifelse(VAR_COLOUR_COUNT,`1',`count1 += NODE_SIZE(node, colors) - index;
            ', `if(node.color == indexToBit(i)) counti(i) += node.size - index;
            ')')
            GENERATED_CODE_END
            EXAMPLE_START ]*/
            if(node.color == 1) count1 += node.size - index;
            if(node.color == 2) count2 += node.size - index;
            if(node.color == 4) count4 += node.size - index;
            /*[ EXAMPLE_END ]*/
            node = BciiTree.next(node);
            index = 0;

            // we've found a node that meet our requirements, so return
            /*[ COLORED_START ]*/ if((node.color & colors) != 0) /*[ COLORED_END ]*/ break;
        }
    }


    /**
     * Get the size of the current node, or 0 if it's color doesn't match those
     * specified.
     */
    public int nodeSize(/*[ COLORED_START ]*/ byte colors /*[ COLORED_END ]*/) {
        if(/*[ COLORED_START(true) ]*/ (node.color & colors) != 0 /*[ COLORED_END ]*/) {
            return /*[ WIDE_NODES_START(1) ]*/ node.size /*[ WIDE_NODES_END ]*/;
        } else {
            return 0;
        }
    }

    /*[ COLORED_START ]*/
    /**
     * The color of the current element.
     */
    public byte color() {
        if(node == null) throw new IllegalStateException();
        return node.color;
    }
    /*[ COLORED_END ]*/

    /**
     * Expected values for index should be in the range  ( 0, size() - 1 )
     */
    public int index(/*[ COLORED_START ]*/ byte colors /*[ COLORED_END ]*/) {
        if(node == null) throw new NoSuchElementException();

        // total the values of the specified array for the specified colors.
        int result = 0;

        /*[ GENERATED_CODE_START
        forloop(`i', 0, VAR_LAST_COLOR_INDEX, `m4_ifelse(VAR_COLOUR_COUNT,`1',`result += count1;
        ', `if((colors & indexToBit(i)) != 0) result += counti(i);
        ')')
        GENERATED_CODE_END
        EXAMPLE_START ]*/
        if((colors & 1) != 0) result += count1;
        if((colors & 2) != 0) result += count2;
        if((colors & 4) != 0) result += count4;
        /*[ EXAMPLE_END ]*/
        return result;
    }
    /**
     * Get the index of the current node's start.
     */
    public int nodeStartIndex(/*[ COLORED_START ]*/ byte colors /*[ COLORED_END ]*/) {
        if(node == null) throw new NoSuchElementException();

        // the count of all nodes prior to this one
        int result = 0;

        // this should merely be the sum of each count
        /*[ GENERATED_CODE_START
        forloop(`i', 0, VAR_LAST_COLOR_INDEX, `m4_ifelse(VAR_COLOUR_COUNT,`1',`result += count1;
        ', `if((colors & indexToBit(i)) != 0) result += counti(i);
        ')')
        GENERATED_CODE_END
        EXAMPLE_START ]*/
        if((colors & 1) != 0) result += count1;
        if((colors & 2) != 0) result += count2;
        if((colors & 4) != 0) result += count4;
        /*[ EXAMPLE_END ]*/

        // subtract the count of anything in the current node which we may
        // have included inadvertently
        if(/*[ COLORED_START(true) ]*/ (node.color & colors) != 0 /*[ COLORED_END ]*/) {
            result -= index;
        }

        return result;
    }
    /**
     * Get the index of the node immediately following the current. Expected
     * values are in the range ( 1, size() )
     */
    public int nodeEndIndex(/*[ COLORED_START ]*/ byte colors /*[ COLORED_END ]*/) {
        if(node == null) throw new NoSuchElementException();

        // the count of all nodes previous
        return nodeStartIndex(/*[ COLORED_START ]*/ colors /*[ COLORED_END ]*/)
                + nodeSize(/*[ COLORED_START ]*/ colors /*[ COLORED_END ]*/);
    }
    public T0 value() {
        if(node == null) throw new IllegalStateException();
        return node.get();
    }
    public Element<T0> node() {
        if(node == null) throw new IllegalStateException();
        return node;
    }
}
/*[ END_M4_JAVA ]*/