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