KickJava   Java API By Example, From Geeks To Geeks.

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


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 public class SkipLevels {
32     /* "Next" pointers
33         The highest indexed level is the "highest" level in the list.
34         The "bottom" level is the direct pointer to a SkipSpan.
35     */

36     public SkipLevels[] levels;
37     public SkipSpan bottom;
38
39     public SkipLevels newInstance(int levels, SkipSpan ss, SkipList sl) { return new SkipLevels(levels, ss); }
40     public void killInstance() { }
41     public void flush() { }
42
43     protected SkipLevels() { }
44     public SkipLevels(int size, SkipSpan span) {
45         if(size < 1) { throw new Error JavaDoc("Invalid Level Skip size"); }
46         levels = new SkipLevels[size];
47         bottom = span;
48     }
49
50     public void print() {
51         SkipLevels prev = null;
52         SkipLevels max = null;
53         System.out.print("SL:" + key() + "::");
54         for(int i=0;i<levels.length;i++) {
55             if(levels[i] != null) {
56                 max = levels[i];
57                 System.out.print(i + "->" + levels[i].key() + " ");
58             } else {
59                 System.out.print(i + "->() ");
60             }
61         }
62         System.out.print("\n");
63         if(levels[0] != null) {
64             levels[0].print();
65         }
66     }
67
68     public SkipSpan getEnd() {
69         for(int i=(levels.length - 1);i>=0;i--) {
70             if(levels[i] != null) { return levels[i].getEnd(); }
71         }
72         return bottom.getEnd();
73     }
74
75     public SkipSpan getSpan(int start, Comparable JavaDoc key, int[] search) {
76         for(int i=Math.min(start, levels.length - 1);i>=0;i--) {
77             if((levels[i] != null) && (levels[i].key().compareTo(key) <= 0)) {
78                 return levels[i].getSpan(i,key,search);
79             }
80         }
81         return bottom.getSpan(key, search);
82     }
83
84     public Comparable JavaDoc key() { return bottom.keys[0]; }
85
86     public Object JavaDoc get(int start, Comparable JavaDoc key) {
87         for(int i=Math.min(start, levels.length - 1);i>=0;i--) {
88             if((levels[i] != null) && (levels[i].key().compareTo(key) <= 0)) {
89                 return levels[i].get(i,key);
90             }
91         }
92         return bottom.get(key);
93     }
94
95     public Object JavaDoc[] remove(int start, Comparable JavaDoc key, SkipList sl) {
96         SkipSpan ss = null;
97         Object JavaDoc[] res = null;
98         SkipLevels slvls = null;
99         for(int i=Math.min(start, levels.length - 1);i>=0;i--) {
100             if(levels[i] != null) {
101                 int cmp = levels[i].key().compareTo(key);
102                 if((cmp < 0) || ((i==0) && (cmp <= 0))) {
103                     res = levels[i].remove(i, key, sl);
104                     if((res != null) && (res[1] != null)) {
105                         slvls = (SkipLevels) res[1];
106                         if(levels.length >= slvls.levels.length) { res[1] = null; }
107                         for(int j=0;j<(Math.min(slvls.levels.length,levels.length));j++) {
108                             if(levels[j] == slvls) {
109                                 levels[j] = slvls.levels[j];
110                             }
111                         }
112                         this.flush();
113                     }
114                     return res;
115                 }
116             }
117         }
118         res = bottom.remove(key, sl);
119         if((res!=null) && (res[1] != null)) {
120             if(res[1] == bottom) {
121                 res[1] = this;
122             } else {
123                 res[1] = null;
124             }
125         }
126         if((bottom.nKeys == 0) && (sl.first != bottom)) { this.killInstance(); }
127         return res;
128     }
129
130     public SkipLevels put(int start, Comparable JavaDoc key, Object JavaDoc val, SkipList sl) {
131         SkipSpan ss = null;
132         SkipLevels slvls = null;
133         for(int i=Math.min(start, levels.length - 1);i>=0;i--) {
134             if((levels[i] != null) && (levels[i].key().compareTo(key) <= 0)) {
135                 slvls = levels[i].put(i, key, val, sl);
136                 if(slvls != null) {
137                     for(int j=i+1;j<(Math.min(slvls.levels.length,levels.length));j++) {
138                         slvls.levels[j] = levels[j];
139                         levels[j] = slvls;
140                     }
141                     if(levels.length < slvls.levels.length) {
142                         this.flush();
143                         return slvls;
144                     }
145                 }
146                 this.flush();
147                 return null;
148             }
149         }
150         ss = bottom.put(key,val,sl);
151         if(ss!=null) {
152             int height = sl.generateColHeight();
153             if(height != 0) {
154                 slvls = this.newInstance(height, ss, sl);
155                 for(int i=0;i<(Math.min(height,levels.length));i++) {
156                     slvls.levels[i] = levels[i];
157                     levels[i] = slvls;
158                 }
159             }
160             this.flush();
161             if(levels.length >= height) { return null; }
162             return slvls;
163         } else {
164             return null;
165         }
166     }
167 }
168
169
Popular Tags