| 1 |
package structures; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.Arrays; |
| 5 |
import java.util.HashSet; |
| 6 |
import java.util.Random; |
| 7 |
|
| 8 |
import shared.Shared; |
| 9 |
import shared.Timer; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
public class IntHashSetList extends IntHashSet{ |
| 19 |
|
| 20 |
public static void main(String[] args){ |
| 21 |
Random randy2=Shared.threadLocalRandom(); |
| 22 |
IntHashSetList set=new IntHashSetList(20, 0.7f); |
| 23 |
HashSet<Integer> set2=new HashSet<Integer>(20, 0.7f); |
| 24 |
ArrayList<Integer> list=new ArrayList<Integer>(); |
| 25 |
ArrayList<Integer> list2=new ArrayList<Integer>(); |
| 26 |
for(int i=0; i<1000; i++){ |
| 27 |
assert(!set.contains(i)); |
| 28 |
assert(!set2.contains(i)); |
| 29 |
list.add(Integer.valueOf(i)); |
| 30 |
} |
| 31 |
for(int i=0; i<1000; i++){ |
| 32 |
int r=randy2.nextInt(); |
| 33 |
list2.add(r); |
| 34 |
} |
| 35 |
|
| 36 |
for(int x : list){ |
| 37 |
set.add(x); |
| 38 |
set2.add(x); |
| 39 |
assert(set.contains(x)); |
| 40 |
assert(set2.contains(x)); |
| 41 |
} |
| 42 |
|
| 43 |
for(int x : list){ |
| 44 |
assert(set.contains(x)); |
| 45 |
assert(set2.contains(x)); |
| 46 |
set.remove(x); |
| 47 |
set2.remove(x); |
| 48 |
assert(!set.contains(x)); |
| 49 |
assert(!set2.contains(x)); |
| 50 |
} |
| 51 |
assert(set.isEmpty()); |
| 52 |
assert(set2.isEmpty()); |
| 53 |
|
| 54 |
for(int x : list2){ |
| 55 |
set.add(x); |
| 56 |
set2.add(x); |
| 57 |
assert(set.contains(x)); |
| 58 |
assert(set2.contains(x)); |
| 59 |
} |
| 60 |
|
| 61 |
for(int x : list2){ |
| 62 |
assert(set.contains(x)); |
| 63 |
assert(set2.contains(x)); |
| 64 |
set.remove(x); |
| 65 |
set2.remove(x); |
| 66 |
assert(!set.contains(x)); |
| 67 |
assert(!set2.contains(x)); |
| 68 |
} |
| 69 |
assert(set.isEmpty()); |
| 70 |
assert(set2.isEmpty()); |
| 71 |
|
| 72 |
int count=4000000; |
| 73 |
int runs=32; |
| 74 |
IntList ll=new IntList(count); |
| 75 |
for(int i=0; i<count; i++){ll.add(randy2.nextInt());} |
| 76 |
|
| 77 |
Shared.printMemory(); |
| 78 |
Timer t=new Timer(); |
| 79 |
for(int k=0; k<2; k++){ |
| 80 |
System.err.println("IntHashSetList:"); |
| 81 |
t.start(); |
| 82 |
for(int i=0; i<runs; i++){ |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
final int[] y=ll.array; |
| 87 |
for(int z=0; z<count; z++){ |
| 88 |
final int value=y[z]; |
| 89 |
set.add(value); |
| 90 |
set.contains(value); |
| 91 |
set.remove(value); |
| 92 |
set.add(value); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
} |
| 101 |
t.stop(); |
| 102 |
System.err.println(t); |
| 103 |
Shared.printMemory(); |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
} |
| 121 |
t.stop(); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public IntHashSetList(){ |
| 129 |
super(); |
| 130 |
list=new IntList(4); |
| 131 |
} |
| 132 |
|
| 133 |
public IntHashSetList(int initialSize){ |
| 134 |
super(initialSize); |
| 135 |
list=new IntList(4); |
| 136 |
} |
| 137 |
|
| 138 |
public IntHashSetList(int initialSize, float loadFactor_){ |
| 139 |
super(initialSize, loadFactor_); |
| 140 |
list=new IntList(4); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
@Override |
| 148 |
public void clear(){ |
| 149 |
assert(size()==list.size) : list.size+", "+size()+"\n"+list.toString()+"\n"+Arrays.toString(toArray())+"\n"+Arrays.toString(super.toArray())+"\n"; |
| 150 |
if(size()<1){return;} |
| 151 |
if(size()>0.25*sizeLimit()){ |
| 152 |
super.clear(); |
| 153 |
}else{ |
| 154 |
for(int i=0; i<list.size; i++){ |
| 155 |
boolean b=remove(list.get(i)); |
| 156 |
assert(b) : list.get(i)+", "+list.size+", "+size()+"\n"+list.toString()+"\n"+Arrays.toString(toArray())+"\n"; |
| 157 |
} |
| 158 |
} |
| 159 |
list.clear(); |
| 160 |
} |
| 161 |
|
| 162 |
@Override |
| 163 |
public boolean add(int value){ |
| 164 |
|
| 165 |
if(super.add(value)){ |
| 166 |
|
| 167 |
|
| 168 |
list.add(value); |
| 169 |
return true; |
| 170 |
} |
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
@Override |
| 175 |
public int[] toArray(){ |
| 176 |
int[] r=list.toArray(); |
| 177 |
return r; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
@Override |
| 185 |
public boolean verify(){ |
| 186 |
if(size()!=list.size){return false;} |
| 187 |
if(list.containsDuplicates()){return false;} |
| 188 |
return super.verify(); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
private IntList list; |
| 196 |
|
| 197 |
} |