4
4
import java .util .ArrayList ;
5
5
import java .util .Arrays ;
6
6
import java .util .Collection ;
7
+ import java .util .Collections ;
7
8
import java .util .Comparator ;
8
9
import java .util .Formatter ;
9
10
import java .util .Locale ;
23
24
import com .jwetherell .algorithms .data_structures .IntervalTree ;
24
25
import com .jwetherell .algorithms .data_structures .KdTree ;
25
26
import com .jwetherell .algorithms .data_structures .PatriciaTrie ;
27
+ import com .jwetherell .algorithms .data_structures .QuadTree ;
26
28
import com .jwetherell .algorithms .data_structures .RadixTrie ;
27
29
import com .jwetherell .algorithms .data_structures .RedBlackTree ;
28
30
import com .jwetherell .algorithms .data_structures .SegmentTree ;
50
52
51
53
public class DataStructures {
52
54
53
- private static final int NUMBER_OF_TESTS = 3 ;
55
+ private static final int NUMBER_OF_TESTS = 1 ;
54
56
private static final Random RANDOM = new Random ();
55
- private static final int ARRAY_SIZE = 1000 ;
57
+ private static final int ARRAY_SIZE = 100000 ;
56
58
private static final int RANDOM_SIZE = 1000 * ARRAY_SIZE ;
57
59
private static final Integer INVALID = RANDOM_SIZE + 10 ;
58
60
private static final DecimalFormat FORMAT = new DecimalFormat ("0.##" );
@@ -61,19 +63,19 @@ public class DataStructures {
61
63
private static Integer [] sorted = null ;
62
64
private static String string = null ;
63
65
64
- private static int debug = 0 ; // Debug level. 0=None, 1=Time and Memory (if
66
+ private static int debug = 1 ; // Debug level. 0=None, 1=Time and Memory (if
65
67
// enabled), 2=Time, Memory, data structure
66
68
// debug
67
- private static boolean debugTime = false ; // How much time to: add all,
69
+ private static boolean debugTime = true ; // How much time to: add all,
68
70
// remove all, add all items in
69
71
// reverse order, remove all
70
- private static boolean debugMemory = false ; // How much memory is used by the
72
+ private static boolean debugMemory = true ; // How much memory is used by the
71
73
// data structure
72
- private static boolean validateStructure = true ; // Is the data structure
74
+ private static boolean validateStructure = false ; // Is the data structure
73
75
// valid (passed
74
76
// invariants) and proper
75
77
// size
76
- private static boolean validateContents = true ; // Was the item
78
+ private static boolean validateContents = false ; // Was the item
77
79
// added/removed really
78
80
// added/removed from the
79
81
// structure
@@ -330,6 +332,12 @@ private static boolean runTests() {
330
332
return false ;
331
333
}
332
334
335
+ passed = testQuadTree ();
336
+ if (!passed ) {
337
+ System .err .println ("QuadTree failed." );
338
+ return false ;
339
+ }
340
+
333
341
passed = testSegmentTree ();
334
342
if (!passed ) {
335
343
System .err .println ("Segment Tree failed." );
@@ -7314,6 +7322,164 @@ private static boolean testPatriciaTrie() {
7314
7322
return true ;
7315
7323
}
7316
7324
7325
+ @ SuppressWarnings ("unchecked" )
7326
+ private static boolean testQuadTree () {
7327
+ int listSize = 10 ;
7328
+ int size = 10000 ;
7329
+ java .util .List <QuadTree .XYPoint >[] lists = new java .util .List [listSize ];
7330
+ for (int i =0 ; i <listSize ; i ++) {
7331
+ java .util .List <QuadTree .XYPoint > list = new java .util .LinkedList <QuadTree .XYPoint >();
7332
+ for (int j =0 ; j <size ; j ++) {
7333
+ float x = RANDOM .nextInt (size );
7334
+ float y = RANDOM .nextInt (size );
7335
+ QuadTree .XYPoint xyPoint = new QuadTree .XYPoint (x ,y );
7336
+ list .add (xyPoint );
7337
+ }
7338
+ lists [i ] = list ;
7339
+ }
7340
+
7341
+ java .util .List <QuadTree .XYPoint >[] queries = new java .util .List [listSize ];
7342
+
7343
+ for (int i =0 ; i <listSize ; i ++) {
7344
+ java .util .List <QuadTree .XYPoint > query = new java .util .LinkedList <QuadTree .XYPoint >();
7345
+ for (int j =0 ; j <size ; j ++) {
7346
+ float x = RANDOM .nextInt (size );
7347
+ float y = RANDOM .nextInt (size );
7348
+ QuadTree .XYPoint xyPoint = new QuadTree .XYPoint (x ,y );
7349
+ query .add (xyPoint );
7350
+ }
7351
+ queries [i ] = query ;
7352
+ }
7353
+
7354
+ long beforeInsert ;
7355
+ long beforeQuery ;
7356
+ long beforeMemory ;
7357
+ long beforeTreeQuery ;
7358
+ long afterInsert ;
7359
+ long afterQuery ;
7360
+ long afterMemory ;
7361
+ long afterTreeQuery ;
7362
+ long insertTime ;
7363
+ long queryTime ;
7364
+ long treeMemory ;
7365
+ long treeQuery ;
7366
+
7367
+ // Point based quad tree
7368
+ {
7369
+ QuadTree .PointQuadTree <QuadTree .XYPoint > tree = new QuadTree .PointQuadTree <QuadTree .XYPoint >(0 ,0 ,size ,size );
7370
+ beforeMemory = DataStructures .getMemoryUse ();
7371
+ for (int i =0 ; i <listSize ; i ++) {
7372
+ java .util .List <QuadTree .XYPoint > list = lists [i ];
7373
+ beforeInsert = System .currentTimeMillis ();
7374
+ for (QuadTree .XYPoint p : list ) {
7375
+ tree .insert (p .getX (), p .getY ());
7376
+ }
7377
+ afterInsert = System .currentTimeMillis ();
7378
+
7379
+ insertTime = afterInsert - beforeInsert ;
7380
+ System .out .println ("insertTime=" +insertTime );
7381
+ }
7382
+ afterMemory = DataStructures .getMemoryUse ();
7383
+ treeMemory = afterMemory - beforeMemory ;
7384
+ System .out .println ("treeMemory=" +treeMemory );
7385
+
7386
+ //System.out.println(tree);
7387
+
7388
+ // We should find all points here
7389
+ for (int i =0 ; i <listSize ; i ++) {
7390
+ java .util .List <QuadTree .XYPoint > list = lists [i ];
7391
+ for (QuadTree .XYPoint p : list ) {
7392
+ java .util .List <QuadTree .XYPoint > result = tree .queryRange (p .getX (), p .getY (), 1 , 1 );
7393
+ if (result .size ()<=0 ) return false ;
7394
+ }
7395
+ }
7396
+
7397
+ // We should find all points here
7398
+ for (int i =0 ; i <listSize ; i ++) {
7399
+ java .util .List <QuadTree .XYPoint > query = queries [i ];
7400
+ for (int j =0 ; j <listSize ; j ++) {
7401
+ beforeQuery = System .currentTimeMillis ();
7402
+ for (QuadTree .XYPoint p : query ) {
7403
+ tree .queryRange (p .getX (), p .getY (), 1 , 1 );
7404
+ }
7405
+ afterQuery = System .currentTimeMillis ();
7406
+
7407
+ queryTime = afterQuery - beforeQuery ;
7408
+ System .out .println ("queryTime=" +queryTime );
7409
+ }
7410
+ }
7411
+
7412
+ // Result set should not contain duplicates
7413
+ beforeTreeQuery = System .currentTimeMillis ();
7414
+ java .util .List <QuadTree .XYPoint > result = tree .queryRange (0 , 0 , size , size );
7415
+ afterTreeQuery = System .currentTimeMillis ();
7416
+ treeQuery = afterTreeQuery - beforeTreeQuery ;
7417
+ System .out .println ("treeQuery=" +treeQuery );
7418
+ Collections .sort (result );
7419
+ QuadTree .XYPoint prev = null ;
7420
+ for (QuadTree .XYPoint p : result ) {
7421
+ if (prev !=null && prev .equals (p )) return false ;
7422
+ prev = p ;
7423
+ }
7424
+ }
7425
+
7426
+ // Rectangle base quadtree
7427
+ {
7428
+ QuadTree .RectangleQuadTree <QuadTree .AxisAlignedBoundingBox > tree = new QuadTree .RectangleQuadTree <QuadTree .AxisAlignedBoundingBox >(0 ,0 ,size ,size );
7429
+ beforeMemory = DataStructures .getMemoryUse ();
7430
+ for (int i =0 ; i <listSize ; i ++) {
7431
+ java .util .List <QuadTree .XYPoint > list = lists [i ];
7432
+ beforeInsert = System .currentTimeMillis ();
7433
+ for (QuadTree .XYPoint p : list ) {
7434
+ tree .insert (p .getX (), p .getY (), 1 , 1 );
7435
+ }
7436
+ afterInsert = System .currentTimeMillis ();
7437
+
7438
+ insertTime = afterInsert - beforeInsert ;
7439
+ System .out .println ("insertTime=" +insertTime );
7440
+
7441
+ }
7442
+ afterMemory = DataStructures .getMemoryUse ();
7443
+ treeMemory = afterMemory - beforeMemory ;
7444
+ System .out .println ("treeMemory=" +treeMemory );
7445
+
7446
+ //System.out.println(tree);
7447
+
7448
+ // We should find all points here
7449
+ for (int i =0 ; i <listSize ; i ++) {
7450
+ java .util .List <QuadTree .XYPoint > list = lists [i ];
7451
+ for (QuadTree .XYPoint p : list ) {
7452
+ java .util .List <QuadTree .AxisAlignedBoundingBox > result = tree .queryRange (p .getX (), p .getY (), 1 , 1 );
7453
+ if (result .size ()<=0 ) return false ;
7454
+ }
7455
+ }
7456
+
7457
+ // We should find all points here
7458
+ for (int i =0 ; i <listSize ; i ++) {
7459
+ java .util .List <QuadTree .XYPoint > query = queries [i ];
7460
+ for (int j =0 ; j <listSize ; j ++) {
7461
+ beforeQuery = System .currentTimeMillis ();
7462
+ for (QuadTree .XYPoint p : query ) {
7463
+ tree .queryRange (p .getX (), p .getY (), 1 , 1 );
7464
+ }
7465
+ afterQuery = System .currentTimeMillis ();
7466
+
7467
+ queryTime = afterQuery - beforeQuery ;
7468
+ System .out .println ("queryTime=" +queryTime );
7469
+ }
7470
+ }
7471
+
7472
+ // Result set should not contain duplicates
7473
+ beforeTreeQuery = System .currentTimeMillis ();
7474
+ tree .queryRange (0 , 0 , size , size );
7475
+ afterTreeQuery = System .currentTimeMillis ();
7476
+ treeQuery = afterTreeQuery - beforeTreeQuery ;
7477
+ System .out .println ("treeQuery=" +treeQuery );
7478
+ }
7479
+
7480
+ return true ;
7481
+ }
7482
+
7317
7483
private static boolean testQueue () {
7318
7484
{
7319
7485
long count = 0 ;
@@ -11345,8 +11511,7 @@ private static final String getTestResults(int testNumber, String[] names, long[
11345
11511
StringBuilder resultsBuilder = new StringBuilder ();
11346
11512
String format = "%-25s %-10s %-15s %-15s %-20s %-15s %-15s\n " ;
11347
11513
Formatter formatter = new Formatter (resultsBuilder , Locale .US );
11348
- formatter = formatter .format (format , "Data Structure" , "Add time" , "Remove time" , "Sorted add time" ,
11349
- "Sorted remove time" , "Lookup time" , "Size" );
11514
+ formatter = formatter .format (format , "Data Structure" , "Add time" , "Remove time" , "Sorted add time" , "Sorted remove time" , "Lookup time" , "Size" );
11350
11515
11351
11516
double KB = 1000 ;
11352
11517
double MB = 1000 * KB ;
@@ -11445,8 +11610,7 @@ private static final String getTestResults(int testNumber, String[] names, long[
11445
11610
return resultsBuilder .toString ();
11446
11611
}
11447
11612
11448
- private static final String getPathMapString (Graph .Vertex <Integer > start ,
11449
- Map <Graph .Vertex <Integer >, Graph .CostPathPair <Integer >> map ) {
11613
+ private static final String getPathMapString (Graph .Vertex <Integer > start , Map <Graph .Vertex <Integer >, Graph .CostPathPair <Integer >> map ) {
11450
11614
StringBuilder builder = new StringBuilder ();
11451
11615
for (Graph .Vertex <Integer > v : map .keySet ()) {
11452
11616
Graph .CostPathPair <Integer > pair = map .get (v );
0 commit comments