Debian Codegraph @

current/structures/IntHashSetList.java

raw symbols SCIP
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 * Extends IntHashSet to track new numbers added for rapid clearing.
13 * This is synchronized with clear, but not remove.
14 * @author Brian Bushnell
15 * @date September 13, 2017
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 // for(int x : ll.array){
84 // set.add(x);
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 // for(int x : ll.array){
95 // set.remove(x);
96 // }
97 // set.clear();
98 // assert(set.isEmpty());
99 // System.err.println("Finished run "+i);
100 }
101 t.stop();
102 System.err.println(t);
103 Shared.printMemory();
104
105 // System.err.println("HashSet:");
106 // t.start();
107 // for(int i=0; i<runs; i++){
108 // for(int x : ll.array){
109 // set2.add(x);
110 // }
111 // for(int x : ll.array){
112 // set2.remove(x);
113 // }
114 // assert(set2.isEmpty());
115 //// System.err.println("Finished run "+i);
116 // }
117 // t.stop();
118 // System.err.println(t);
119 // Shared.printMemory();
120 }
121 t.stop();
122 }
123
124 /*--------------------------------------------------------------*/
125 /*---------------- Initialization ----------------*/
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 /*---------------- Public Methods ----------------*/
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 // boolean b=super.contains(value);
165 if(super.add(value)){
166 // assert(!b);
167 // assert(super.contains(value));
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 /*---------------- Private Methods ----------------*/
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 /*---------------- Fields ----------------*/
193 /*--------------------------------------------------------------*/
194
195 private IntList list;
196
197 }