Skip to content

Commit 4464deb

Browse files
author
phishman3579
committed
Updated the Skip List to extend Set instead of List since it doesn't contain duplicates.
git-svn-id: https://java-algorithms-implementation.googlecode.com/svn/trunk@424 032fbc0f-8cab-eb90-e552-f08422b9a96a
1 parent 8b6bea6 commit 4464deb

File tree

3 files changed

+260
-132
lines changed

3 files changed

+260
-132
lines changed

src/com/jwetherell/algorithms/DataStructures.java

+133-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
import java.util.ListIterator;
1212
import java.util.Locale;
1313
import java.util.Map;
14+
import java.util.NavigableSet;
1415
import java.util.NoSuchElementException;
1516
import java.util.Random;
1617
import java.util.Set;
18+
import java.util.concurrent.ConcurrentSkipListSet;
1719

1820
import com.jwetherell.algorithms.data_structures.AVLTree;
1921
import com.jwetherell.algorithms.data_structures.BTree;
@@ -28,6 +30,7 @@
2830
import com.jwetherell.algorithms.data_structures.IList;
2931
import com.jwetherell.algorithms.data_structures.IMap;
3032
import com.jwetherell.algorithms.data_structures.IQueue;
33+
import com.jwetherell.algorithms.data_structures.ISet;
3134
import com.jwetherell.algorithms.data_structures.IStack;
3235
import com.jwetherell.algorithms.data_structures.ITree;
3336
import com.jwetherell.algorithms.data_structures.IntervalTree;
@@ -80,7 +83,7 @@ public class DataStructures {
8083
private static boolean validateContents = true; // Was the item added/removed really added/removed from the structure
8184
private static boolean validateIterator = true; // Does the iterator(s) work
8285

83-
private static final int TESTS = 34; // Max number of dynamic data structures to test
86+
private static final int TESTS = 35; // Max number of dynamic data structures to test
8487
private static final String[] testNames = new String[TESTS]; // Array to hold the test names
8588
private static final long[][] testResults = new long[TESTS][]; // Array to hold the test results
8689
private static int testIndex = 0; // Index into the tests
@@ -254,6 +257,12 @@ private static boolean runTests() {
254257
return false;
255258
}
256259

260+
passed = testJavaSkipList();
261+
if (!passed) {
262+
System.err.println("Java's Skip List failed.");
263+
return false;
264+
}
265+
257266
passed = testSkipList();
258267
if (!passed) {
259268
System.err.println("Skip List failed.");
@@ -1568,12 +1577,20 @@ private static boolean testSegmentTree() {
15681577
return true;
15691578
}
15701579

1580+
private static boolean testJavaSkipList() {
1581+
String sName = "Java's SkipList";
1582+
NavigableSet<Integer> sList = new ConcurrentSkipListSet<Integer>();
1583+
Collection<Integer> lCollection = sList;
1584+
if(!testJavaCollection(lCollection,Type.Integer,sName)) return false;
1585+
return true;
1586+
}
1587+
15711588
private static boolean testSkipList() {
15721589
String sName = "SkipList";
15731590
SkipList<Integer> sList = new SkipList<Integer>();
15741591
Collection<Integer> lCollection = sList.toCollection();
15751592

1576-
if((validateStructure||validateContents) && !testList(sList,sName)) return false;
1593+
if((validateStructure||validateContents) && !testSet(sList,sName)) return false;
15771594
if(!testJavaCollection(lCollection,Type.Integer,sName)) return false;
15781595
return true;
15791596
}
@@ -2586,6 +2603,120 @@ private static <T extends Comparable<T>> boolean testList(IList<T> list, String
25862603
return true;
25872604
}
25882605

2606+
/**
2607+
* Tests the actual list operations
2608+
*
2609+
* @param list to test.
2610+
* @return True is works as a list structure.
2611+
*/
2612+
private static <T extends Comparable<T>> boolean testSet(ISet<T> list, String name) {
2613+
for (int i = 0; i < unsorted.length; i++) {
2614+
T item = (T)unsorted[i];
2615+
list.add(item);
2616+
if (validateStructure && !list.validate() && !(list.size() == i + 1)) {
2617+
System.err.println(name+" YIKES!! " + item + " caused a size mismatch.");
2618+
handleError(list);
2619+
return false;
2620+
}
2621+
if (validateContents && !list.contains(item)) {
2622+
System.err.println(name+" YIKES!! " + item + " doesn't exists but has been added.");
2623+
handleError(list);
2624+
return false;
2625+
}
2626+
}
2627+
2628+
boolean contains = list.contains((T)INVALID);
2629+
if (contains) {
2630+
System.err.println(name+" invalidity check. contains=" + contains);
2631+
handleError(list);
2632+
return false;
2633+
}
2634+
2635+
int size = list.size();
2636+
for (int i = 0; i < size; i++) {
2637+
T item = (T)unsorted[i];
2638+
boolean removed = list.remove(item);
2639+
if (validateStructure && !list.validate() && !(list.size() == unsorted.length - (i + 1))) {
2640+
System.err.println(name+" YIKES!! " + item + " caused a size mismatch.");
2641+
handleError(list);
2642+
return false;
2643+
}
2644+
if (validateContents && removed && list.contains(item)) {
2645+
System.err.println(name+" YIKES!! " + item + " still exists but it has been remove.");
2646+
handleError(list);
2647+
return false;
2648+
}
2649+
}
2650+
2651+
// Add half, remove a quarter, add three-quarters, remove all
2652+
int quarter = unsorted.length/4;
2653+
int half = unsorted.length/2;
2654+
for (int i = 0; i < half; i++) {
2655+
T item = (T)unsorted[i];
2656+
list.add(item);
2657+
if (validateStructure && !list.validate() && !(list.size() == i + 1)) {
2658+
System.err.println(name+" YIKES!! " + item + " caused a size mismatch.");
2659+
handleError(list);
2660+
return false;
2661+
}
2662+
if (validateContents && !list.contains(item)) {
2663+
System.err.println(name+" YIKES!! " + item + " doesn't exists but has been added.");
2664+
handleError(list);
2665+
return false;
2666+
}
2667+
}
2668+
for (int i = (half-1); i >= quarter; i--) {
2669+
T item = (T)unsorted[i];
2670+
boolean removed = list.remove(item);
2671+
if (validateStructure && !list.validate() && !(list.size() == i)) {
2672+
System.err.println(name+" YIKES!! " + item + " caused a size mismatch.");
2673+
handleError(list);
2674+
return false;
2675+
}
2676+
if (validateContents && removed && list.contains(item)) {
2677+
System.err.println(name+" YIKES!! " + item + " still exists but it has been remove.");
2678+
handleError(list);
2679+
return false;
2680+
}
2681+
}
2682+
for (int i = quarter; i < unsorted.length; i++) {
2683+
T item = (T)unsorted[i];
2684+
list.add(item);
2685+
if (validateStructure && !list.validate() && !(list.size() == i + 1)) {
2686+
System.err.println(name+" YIKES!! " + item + " caused a size mismatch.");
2687+
handleError(list);
2688+
return false;
2689+
}
2690+
if (validateContents && !list.contains(item)) {
2691+
System.err.println(name+" YIKES!! " + item + " doesn't exists but has been added.");
2692+
handleError(list);
2693+
return false;
2694+
}
2695+
}
2696+
for (int i = unsorted.length-1; i >= 0; i--) {
2697+
T item = (T)unsorted[i];
2698+
boolean removed = list.remove(item);
2699+
if (validateStructure && !list.validate() && !(list.size() == i)) {
2700+
System.err.println(name+" YIKES!! " + item + " caused a size mismatch.");
2701+
handleError(list);
2702+
return false;
2703+
}
2704+
if (validateContents && removed && list.contains(item)) {
2705+
System.err.println(name+" YIKES!! " + item + " still exists but it has been remove.");
2706+
handleError(list);
2707+
return false;
2708+
}
2709+
}
2710+
2711+
if (validateStructure && (list.size() != 0)) {
2712+
System.err.println(name+" YIKES!! a size mismatch.");
2713+
handleError(list);
2714+
return false;
2715+
}
2716+
2717+
return true;
2718+
}
2719+
25892720
private static <T extends Comparable<T>> boolean testJavaCollection(Collection<T> collection, Type type, String name) {
25902721
// Make sure the collection is empty
25912722
if (!collection.isEmpty()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.jwetherell.algorithms.data_structures;
2+
3+
/**
4+
* In computer science, a set is an abstract data structure that can store certain values, without
5+
* any particular order, and no repeated values. It is a computer implementation of the mathematical
6+
* concept of a finite set. Unlike most other collection types, rather than retrieving a specific
7+
* element from a set, one typically tests a value for membership in a set.
8+
*
9+
* http://en.wikipedia.org/wiki/Set_(abstract_data_type)
10+
*
11+
* @author Justin Wetherell <phishman3579@gmail.com>
12+
*/
13+
public interface ISet<T> {
14+
15+
/**
16+
* Add value to set.
17+
*
18+
* @param value to add.
19+
* @return True if added.
20+
*/
21+
public boolean add(T value);
22+
23+
/**
24+
* Remove value from set.
25+
*
26+
* @param value to remove.
27+
* @return True if removed.
28+
*/
29+
public boolean remove(T value);
30+
31+
/**
32+
* Does the set contain value.
33+
*
34+
* @param value to search set for.
35+
* @return True if set contains value.
36+
*/
37+
public boolean contains(T value);
38+
39+
/**
40+
* Size of the set.
41+
*
42+
* @return size of the set.
43+
*/
44+
public int size();
45+
46+
/**
47+
* Validate the set according to the invariants.
48+
*
49+
* @return True if the set is valid.
50+
*/
51+
public boolean validate();
52+
53+
/**
54+
* Get this Set as a Java compatible Set
55+
*
56+
* @return Java compatible Set
57+
*/
58+
public java.util.Set<T> toSet();
59+
60+
/**
61+
* Get this Set as a Java compatible Collection
62+
*
63+
* @return Java compatible Collection
64+
*/
65+
public java.util.Collection<T> toCollection();
66+
67+
}

0 commit comments

Comments
 (0)