| 1 | /* |
|---|
| 2 | * This program is free software; you can redistribute it and/or modify |
|---|
| 3 | * it under the terms of the GNU General Public License as published by |
|---|
| 4 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 5 | * (at your option) any later version. |
|---|
| 6 | * |
|---|
| 7 | * This program is distributed in the hope that it will be useful, |
|---|
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 10 | * GNU General Public License for more details. |
|---|
| 11 | * |
|---|
| 12 | * You should have received a copy of the GNU General Public License |
|---|
| 13 | * along with this program; if not, write to the Free Software |
|---|
| 14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | /* |
|---|
| 18 | * SimpleLinkedList.java |
|---|
| 19 | * Copyright (C) 2003 Peter A. Flach, Nicolas Lachiche |
|---|
| 20 | * |
|---|
| 21 | * Thanks to Amelie Deltour for porting the original C code to Java |
|---|
| 22 | * and integrating it into Weka. |
|---|
| 23 | */ |
|---|
| 24 | |
|---|
| 25 | package weka.associations.tertius; |
|---|
| 26 | |
|---|
| 27 | import weka.core.RevisionHandler; |
|---|
| 28 | import weka.core.RevisionUtils; |
|---|
| 29 | |
|---|
| 30 | import java.io.Serializable; |
|---|
| 31 | import java.util.Comparator; |
|---|
| 32 | import java.util.NoSuchElementException; |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 35 | * @author Peter A. Flach |
|---|
| 36 | * @author Nicolas Lachiche |
|---|
| 37 | * @version $Revision: 1.6 $ |
|---|
| 38 | */ |
|---|
| 39 | public class SimpleLinkedList |
|---|
| 40 | implements Serializable, RevisionHandler { |
|---|
| 41 | |
|---|
| 42 | /** for serialization */ |
|---|
| 43 | private static final long serialVersionUID = -1491148276509976299L; |
|---|
| 44 | |
|---|
| 45 | public class LinkedListIterator |
|---|
| 46 | implements Serializable, RevisionHandler { |
|---|
| 47 | |
|---|
| 48 | /** for serialization */ |
|---|
| 49 | private static final long serialVersionUID = -2448555236100426759L; |
|---|
| 50 | |
|---|
| 51 | Entry current = first; |
|---|
| 52 | Entry lastReturned = null; |
|---|
| 53 | |
|---|
| 54 | public boolean hasNext() { |
|---|
| 55 | return current.next != last; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | public Object next() { |
|---|
| 59 | if (current == last) { |
|---|
| 60 | throw new NoSuchElementException(); |
|---|
| 61 | } |
|---|
| 62 | current = current.next; |
|---|
| 63 | lastReturned = current; |
|---|
| 64 | return current.element; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | public void remove() { |
|---|
| 68 | if (lastReturned == last |
|---|
| 69 | || lastReturned == null) { |
|---|
| 70 | throw new IllegalStateException(); |
|---|
| 71 | } |
|---|
| 72 | lastReturned.previous.next = lastReturned.next; |
|---|
| 73 | lastReturned.next.previous = lastReturned.previous; |
|---|
| 74 | current = lastReturned.previous; |
|---|
| 75 | lastReturned = null; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | public void addBefore(Object o) { |
|---|
| 79 | if (lastReturned == null) { |
|---|
| 80 | throw new IllegalStateException(); |
|---|
| 81 | } |
|---|
| 82 | Entry newEntry = new Entry(o, lastReturned, lastReturned.previous); |
|---|
| 83 | lastReturned.previous.next = newEntry; |
|---|
| 84 | lastReturned.previous = newEntry; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | /** |
|---|
| 88 | * Returns the revision string. |
|---|
| 89 | * |
|---|
| 90 | * @return the revision |
|---|
| 91 | */ |
|---|
| 92 | public String getRevision() { |
|---|
| 93 | return RevisionUtils.extract("$Revision: 1.6 $"); |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | public class LinkedListInverseIterator |
|---|
| 98 | implements Serializable, RevisionHandler { |
|---|
| 99 | |
|---|
| 100 | /** for serialization */ |
|---|
| 101 | private static final long serialVersionUID = 6290379064027832108L; |
|---|
| 102 | |
|---|
| 103 | Entry current = last; |
|---|
| 104 | Entry lastReturned = null; |
|---|
| 105 | |
|---|
| 106 | public boolean hasPrevious() { |
|---|
| 107 | return current.previous != first; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | public Object previous() { |
|---|
| 111 | if (current == first) { |
|---|
| 112 | throw new NoSuchElementException(); |
|---|
| 113 | } |
|---|
| 114 | current = current.previous; |
|---|
| 115 | lastReturned = current; |
|---|
| 116 | return current.element; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | public void remove() { |
|---|
| 120 | if (lastReturned == first |
|---|
| 121 | || lastReturned == null) { |
|---|
| 122 | throw new IllegalStateException(); |
|---|
| 123 | } |
|---|
| 124 | lastReturned.previous.next = lastReturned.next; |
|---|
| 125 | lastReturned.next.previous = lastReturned.previous; |
|---|
| 126 | current = lastReturned.next; |
|---|
| 127 | lastReturned = null; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | /** |
|---|
| 131 | * Returns the revision string. |
|---|
| 132 | * |
|---|
| 133 | * @return the revision |
|---|
| 134 | */ |
|---|
| 135 | public String getRevision() { |
|---|
| 136 | return RevisionUtils.extract("$Revision: 1.6 $"); |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | private static class Entry |
|---|
| 142 | implements Serializable, RevisionHandler { |
|---|
| 143 | |
|---|
| 144 | /** for serialization */ |
|---|
| 145 | private static final long serialVersionUID = 7888492479685339831L; |
|---|
| 146 | |
|---|
| 147 | Object element; |
|---|
| 148 | Entry next; |
|---|
| 149 | Entry previous; |
|---|
| 150 | |
|---|
| 151 | Entry(Object element, Entry next, Entry previous) { |
|---|
| 152 | this.element = element; |
|---|
| 153 | this.next = next; |
|---|
| 154 | this.previous = previous; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | /** |
|---|
| 158 | * Returns the revision string. |
|---|
| 159 | * |
|---|
| 160 | * @return the revision |
|---|
| 161 | */ |
|---|
| 162 | public String getRevision() { |
|---|
| 163 | return RevisionUtils.extract("$Revision: 1.6 $"); |
|---|
| 164 | } |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | private Entry first; |
|---|
| 168 | private Entry last; |
|---|
| 169 | |
|---|
| 170 | public SimpleLinkedList() { |
|---|
| 171 | first = new Entry(null, null, null); |
|---|
| 172 | last = new Entry(null, null, null); |
|---|
| 173 | first.next = last; |
|---|
| 174 | last.previous = first; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | public Object removeFirst() { |
|---|
| 178 | if (first.next == last) { |
|---|
| 179 | throw new NoSuchElementException(); |
|---|
| 180 | } |
|---|
| 181 | Object result = first.next.element; |
|---|
| 182 | first.next.next.previous = first; |
|---|
| 183 | first.next = first.next.next; |
|---|
| 184 | return result; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | public Object getFirst() { |
|---|
| 188 | if (first.next == last) { |
|---|
| 189 | throw new NoSuchElementException(); |
|---|
| 190 | } |
|---|
| 191 | return first.next.element; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | public Object getLast() { |
|---|
| 195 | if (last.previous == first) { |
|---|
| 196 | throw new NoSuchElementException(); |
|---|
| 197 | } |
|---|
| 198 | return last.previous.element; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | public void addFirst(Object o) { |
|---|
| 202 | Entry newEntry = new Entry(o, first.next, first); |
|---|
| 203 | first.next.previous = newEntry; |
|---|
| 204 | first.next = newEntry; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | public void add(Object o) { |
|---|
| 208 | Entry newEntry = new Entry(o, last, last.previous); |
|---|
| 209 | last.previous.next = newEntry; |
|---|
| 210 | last.previous = newEntry; |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | public void addAll(SimpleLinkedList list) { |
|---|
| 214 | last.previous.next = list.first.next; |
|---|
| 215 | list.first.next.previous = last.previous; |
|---|
| 216 | last = list.last; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | public void clear() { |
|---|
| 220 | first.next = last; |
|---|
| 221 | last.previous = first; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | public boolean isEmpty() { |
|---|
| 225 | return first.next == last; |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | public LinkedListIterator iterator() { |
|---|
| 229 | return new LinkedListIterator(); |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | public LinkedListInverseIterator inverseIterator() { |
|---|
| 233 | return new LinkedListInverseIterator(); |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | public int size() { |
|---|
| 237 | int result = 0; |
|---|
| 238 | LinkedListIterator iter = new LinkedListIterator(); |
|---|
| 239 | while (iter.hasNext()) { |
|---|
| 240 | result++; |
|---|
| 241 | iter.next(); |
|---|
| 242 | } |
|---|
| 243 | return result; |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | public void merge(SimpleLinkedList list, Comparator comp) { |
|---|
| 247 | LinkedListIterator iter1 = this.iterator(); |
|---|
| 248 | LinkedListIterator iter2 = list.iterator(); |
|---|
| 249 | Object elem1 = iter1.next(); |
|---|
| 250 | Object elem2 = iter2.next(); |
|---|
| 251 | while (elem2 != null) { |
|---|
| 252 | if ((elem1 == null) |
|---|
| 253 | || (comp.compare(elem2, elem1) < 0)) { |
|---|
| 254 | iter1.addBefore(elem2); |
|---|
| 255 | elem2 = iter2.next(); |
|---|
| 256 | } else { |
|---|
| 257 | elem1 = iter1.next(); |
|---|
| 258 | } |
|---|
| 259 | } |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | public void sort(Comparator comp) { |
|---|
| 263 | LinkedListIterator iter = this.iterator(); |
|---|
| 264 | if (iter.hasNext()) { |
|---|
| 265 | SimpleLinkedList lower = new SimpleLinkedList(); |
|---|
| 266 | SimpleLinkedList upper = new SimpleLinkedList(); |
|---|
| 267 | Object ref = iter.next(); |
|---|
| 268 | Object elem; |
|---|
| 269 | while (iter.hasNext()) { |
|---|
| 270 | elem = iter.next(); |
|---|
| 271 | if (comp.compare(elem, ref) < 0) { |
|---|
| 272 | lower.add(elem); |
|---|
| 273 | } else { |
|---|
| 274 | upper.add(elem); |
|---|
| 275 | } |
|---|
| 276 | } |
|---|
| 277 | lower.sort(comp); |
|---|
| 278 | upper.sort(comp); |
|---|
| 279 | clear(); |
|---|
| 280 | addAll(lower); |
|---|
| 281 | add(ref); |
|---|
| 282 | addAll(upper); |
|---|
| 283 | } |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | public String toString() { |
|---|
| 287 | StringBuffer text = new StringBuffer(); |
|---|
| 288 | LinkedListIterator iter = iterator(); |
|---|
| 289 | text.append("["); |
|---|
| 290 | while (iter.hasNext()) { |
|---|
| 291 | text.append(String.valueOf(iter.next())); |
|---|
| 292 | if (iter.hasNext()) { |
|---|
| 293 | text.append(", "); |
|---|
| 294 | } |
|---|
| 295 | } |
|---|
| 296 | text.append("]"); |
|---|
| 297 | return text.toString(); |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | /** |
|---|
| 301 | * Save the state of this <tt>LinkedList</tt> instance to a stream (that |
|---|
| 302 | * is, serialize it). |
|---|
| 303 | * |
|---|
| 304 | * @serialData The size of the list (the number of elements it |
|---|
| 305 | * contains) is emitted (int), followed by all of its |
|---|
| 306 | * elements (each an Object) in the proper order. */ |
|---|
| 307 | private synchronized void writeObject(java.io.ObjectOutputStream s) |
|---|
| 308 | throws java.io.IOException { |
|---|
| 309 | // Write out any hidden serialization magic |
|---|
| 310 | s.defaultWriteObject(); |
|---|
| 311 | |
|---|
| 312 | // Write out size |
|---|
| 313 | s.writeInt(size()); |
|---|
| 314 | |
|---|
| 315 | // Write out all elements in the proper order. |
|---|
| 316 | for (Entry e = first.next; e != last; e = e.next) |
|---|
| 317 | s.writeObject(e.element); |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | /** |
|---|
| 321 | * Reconstitute this <tt>LinkedList</tt> instance from a stream (that is |
|---|
| 322 | * deserialize it). |
|---|
| 323 | */ |
|---|
| 324 | private synchronized void readObject(java.io.ObjectInputStream s) |
|---|
| 325 | throws java.io.IOException, ClassNotFoundException { |
|---|
| 326 | // Read in any hidden serialization magic |
|---|
| 327 | s.defaultReadObject(); |
|---|
| 328 | |
|---|
| 329 | // Read in size |
|---|
| 330 | int size = s.readInt(); |
|---|
| 331 | |
|---|
| 332 | // Initialize header |
|---|
| 333 | first = new Entry(null, null, null); |
|---|
| 334 | last = new Entry(null, null, null); |
|---|
| 335 | first.next = last; |
|---|
| 336 | last.previous = first; |
|---|
| 337 | |
|---|
| 338 | // Read in all elements in the proper order. |
|---|
| 339 | for (int i=0; i<size; i++) |
|---|
| 340 | add(s.readObject()); |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | /** |
|---|
| 344 | * Returns the revision string. |
|---|
| 345 | * |
|---|
| 346 | * @return the revision |
|---|
| 347 | */ |
|---|
| 348 | public String getRevision() { |
|---|
| 349 | return RevisionUtils.extract("$Revision: 1.6 $"); |
|---|
| 350 | } |
|---|
| 351 | } |
|---|