KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > metanotion > util > skiplist > SkipIterator


1 /*
2 Copyright (c) 2006, Matthew Estes
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8     * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10     * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13     * Neither the name of Metanotion Software nor the names of its
14 contributors may be used to endorse or promote products derived from this
15 software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */

29 package net.metanotion.util.skiplist;
30
31 import java.util.ListIterator JavaDoc;
32 import java.util.NoSuchElementException JavaDoc;
33
34 /** A basic iterator for a skip list.
35     This is not a complete ListIterator, in particular, since the
36     skip list is a map and is therefore indexed by Comparable objects instead
37     of int's, the nextIndex and previousIndex methods are not really relevant.
38 */

39 public class SkipIterator implements ListIterator JavaDoc {
40     SkipSpan ss;
41     int index;
42
43     protected SkipIterator() { }
44     public SkipIterator(SkipSpan ss, int index) {
45         if(ss==null) { throw new NullPointerException JavaDoc(); }
46         this.ss = ss;
47         this.index = index;
48     }
49
50     public boolean hasNext() {
51         if(index < ss.nKeys) { return true; }
52         return false;
53     }
54
55     public Object JavaDoc next() {
56         Object JavaDoc o;
57         if(index < ss.nKeys) {
58             o = ss.vals[index];
59         } else {
60             throw new NoSuchElementException JavaDoc();
61         }
62
63         if(index < (ss.nKeys-1)) {
64             index++;
65         } else if(ss.next != null) {
66             ss = ss.next;
67             index = 0;
68         } else {
69             index = ss.nKeys;
70         }
71         return o;
72     }
73
74     public Comparable JavaDoc nextKey() {
75         Comparable JavaDoc c;
76         if(index < ss.nKeys) { return ss.keys[index]; }
77         throw new NoSuchElementException JavaDoc();
78     }
79
80     public boolean hasPrevious() {
81         if(index > 0) { return true; }
82         if((ss.prev != null) && (ss.prev.nKeys > 0)) { return true; }
83         return false;
84     }
85
86     public Object JavaDoc previous() {
87         if(index > 0) {
88             index--;
89         } else if(ss.prev != null) {
90             ss = ss.prev;
91             if(ss.nKeys <= 0) { throw new NoSuchElementException JavaDoc(); }
92             index = (ss.nKeys - 1);
93         }
94         return ss.vals[index];
95     }
96
97
98     // Optional methods
99
public void add(Object JavaDoc o) { throw new UnsupportedOperationException JavaDoc(); }
100     public void remove() { throw new UnsupportedOperationException JavaDoc(); }
101     public void set(Object JavaDoc o) { throw new UnsupportedOperationException JavaDoc(); }
102     public int nextIndex() { throw new UnsupportedOperationException JavaDoc(); }
103     public int previousIndex() { throw new UnsupportedOperationException JavaDoc(); }
104
105 }
106
Popular Tags