/
pathtree.cpp
799 lines (695 loc) · 24.3 KB
/
pathtree.cpp
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
/**
* @file pathtree.cpp
* @ingroup base
*
* @authors Copyright © 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2012 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include <QStringList>
#include <de/Error>
#include <de/Log>
#include <de/stringpool.h>
#if _DEBUG
# include "m_misc.h" // For M_NumDigits()
#endif
#include "pathtree.h"
namespace de {
struct PathTree::Instance
{
PathTree& self;
/// Path name fragment intern pool.
StringPool fragments;
/// @see pathTreeFlags
int flags;
/// Total number of unique paths in the directory.
int size;
/// Path node hashes.
PathTree::Nodes leafHash;
PathTree::Nodes branchHash;
Instance(PathTree& d, int _flags)
: self(d), fragments(), flags(_flags), size(0)
{}
~Instance()
{
clear();
}
void clear()
{
clearPathHash(leafHash);
clearPathHash(branchHash);
size = 0;
}
PathTree::FragmentId internFragmentAndUpdateIdHashMap(String fragment, ushort hash)
{
PathTree::FragmentId internId = fragments.intern(fragment);
fragments.setUserValue(internId, hash);
return internId;
}
/**
* @return [ a new | the ] directory node that matches the name and type and
* which has the specified parent node.
*/
PathTree::Node* direcNode(PathTree::Node* parent, PathTree::NodeType nodeType,
String fragment, char delimiter)
{
// Have we already encountered this?
PathTree::FragmentId fragmentId = fragments.isInterned(fragment);
if(fragmentId)
{
// The name is known. Perhaps we have.
PathTree::Nodes& hash = (nodeType == PathTree::Leaf? leafHash : branchHash);
ushort hashKey = fragments.userValue(fragmentId);
for(PathTree::Nodes::const_iterator i = hash.find(hashKey); i != hash.end() && i.key() == hashKey; ++i)
{
PathTree::Node* node = *i;
if(parent != node->parent()) continue;
if(fragmentId != node->fragmentId()) continue;
if(nodeType == PathTree::Branch || !(flags & PATHTREE_MULTI_LEAF))
return node;
}
}
/*
* A new node is needed.
*/
// Do we need a new identifier (and hash)?
ushort hash;
if(!fragmentId)
{
QByteArray fragmentUtf8 = fragment.toUtf8();
hash = hashPathFragment(fragmentUtf8.constData(), fragmentUtf8.length(), delimiter);
fragmentId = internFragmentAndUpdateIdHashMap(fragment, hash);
}
else
{
hash = self.fragmentHash(fragmentId);
}
// Are we out of indices?
if(!fragmentId) return NULL;
PathTree::Node* node = new PathTree::Node(self, nodeType, fragmentId, parent);
// Insert the new node into the hash.
if(nodeType == PathTree::Leaf)
{
leafHash.insert(hash, node);
}
else // Branch
{
branchHash.insert(hash, node);
}
return node;
}
/**
* The path is split into as many nodes as necessary. Parent links are set.
*
* @return The node that identifies the given path.
*/
PathTree::Node* buildDirecNodes(String path, char delimiter)
{
PathTree::Node* node = 0, *parent = 0;
QStringList parts = path.split(delimiter);
for(int i = 0; i < parts.count() - 1; ++i)
{
node = direcNode(parent, PathTree::Branch, parts[i], delimiter);
parent = node;
}
if(!parts.last().isEmpty())
{
node = direcNode(parent, PathTree::Leaf, parts.last(), delimiter);
}
return node;
}
static void clearPathHash(PathTree::Nodes& ph)
{
LOG_AS("PathTree::clearPathHash");
DENG2_FOR_EACH(PathTree::Nodes, i, ph)
{
PathTree::Node* node = *i;
#if _DEBUG
if(node->userPointer())
{
LOG_ERROR("Node %p has non-NULL user data.") << de::dintptr(node);
}
#endif
delete node;
}
ph.clear();
}
};
ushort PathTree::hashPathFragment(const char* fragment, size_t len, char delimiter)
{
ushort key = 0;
DENG2_ASSERT(fragment);
// Skip over any trailing delimiters.
const char* c = fragment + len - 1;
while(c >= fragment && *c && *c == delimiter) c--;
// Compose the hash.
int op = 0;
for(; c >= fragment && *c && *c != delimiter; c--)
{
switch(op)
{
case 0: key ^= tolower(*c); ++op; break;
case 1: key *= tolower(*c); ++op; break;
case 2: key -= tolower(*c); op=0; break;
}
}
return key % PATHTREE_PATHHASH_SIZE;
}
PathTree::Node* PathTree::insert(const char* path, char delimiter)
{
PathTree::Node* node = d->buildDirecNodes(path, delimiter);
if(node)
{
// There is now one more unique path in the directory.
d->size += 1;
}
return node;
}
PathTree::PathTree(int flags)
{
d = new Instance(*this, flags);
}
PathTree::~PathTree()
{
delete d;
}
ddstring_t const* PathTree::nodeTypeName(NodeType type)
{
static Str const nodeNames[] = {
"branch",
"leaf"
};
return nodeNames[type == Branch? 0 : 1];
}
int PathTree::size() const
{
return d->size;
}
bool PathTree::empty() const
{
return size() == 0;
}
void PathTree::clear()
{
d->clear();
}
PathTree::Node& PathTree::find(int flags, char const* searchPath, char delimiter)
{
Node* foundNode = NULL;
if(searchPath && searchPath[0] && d->size)
{
PathMap mappedSearchPath = PathMap(hashPathFragment, searchPath, delimiter);
ushort hash = PathMap_Fragment(&mappedSearchPath, 0)->hash;
if(!(flags & PCF_NO_LEAF))
{
Nodes& nodes = d->leafHash;
Nodes::iterator i = nodes.find(hash);
for(; i != nodes.end() && i.key() == hash; ++i)
{
Node& node = **i;
if(!node.comparePath(mappedSearchPath, flags))
{
// This is the node we're looking for - stop iteration.
foundNode = *i;
break;
}
}
}
if(!foundNode)
if(!(flags & PCF_NO_BRANCH))
{
Nodes& nodes = d->branchHash;
Nodes::iterator i = nodes.find(hash);
for(; i != nodes.end() && i.key() == hash; ++i)
{
Node& node = **i;
if(!node.comparePath(mappedSearchPath, flags))
{
// This is the node we're looking for - stop iteration.
foundNode = *i;
break;
}
}
}
}
if(!foundNode) throw NotFoundError("PathTree::find", String("No paths found matching \"") + searchPath + "\"");
return *foundNode;
}
String const& PathTree::fragmentName(FragmentId fragmentId) const
{
return d->fragments.string(fragmentId);
}
ushort PathTree::fragmentHash(FragmentId fragmentId) const
{
return d->fragments.userValue(fragmentId);
}
PathTree::Nodes const& PathTree::nodes(NodeType type) const
{
return (type == Leaf? d->leafHash : d->branchHash);
}
static void collectPathsInHash(PathTree::FoundPaths& found, PathTree::Nodes const& ph, char delimiter)
{
if(ph.empty()) return;
DENG2_FOR_EACH_CONST(PathTree::Nodes, i, ph)
{
PathTree::Node& node = **i;
found.push_back(node.composePath(delimiter));
}
}
int PathTree::findAllPaths(FoundPaths& found, int flags, char delimiter)
{
int numFoundSoFar = found.count();
if(!(flags & PCF_NO_BRANCH))
{
collectPathsInHash(found, branchNodes(), delimiter);
}
if(!(flags & PCF_NO_LEAF))
{
collectPathsInHash(found, leafNodes(), delimiter);
}
return found.count() - numFoundSoFar;
}
static int iteratePathsInHash(PathTree& pathTree, ushort hash, PathTree::NodeType type, int flags,
PathTree::Node* parent, int (*callback) (PathTree::Node&, void*), void* parameters)
{
int result = 0;
if(hash != PATHTREE_NOHASH && hash >= PATHTREE_PATHHASH_SIZE)
{
throw Error("PathTree::iteratePathsInHash", String("Invalid hash %1 (valid range is [0..%2]).").arg(hash).arg(PATHTREE_PATHHASH_SIZE-1));
}
PathTree::Nodes const& nodes = pathTree.nodes(type);
// Are we iterating nodes with a known hash?
if(hash != PATHTREE_NOHASH)
{
// Yes.
PathTree::Nodes::const_iterator i = nodes.constFind(hash);
for(; i != nodes.end() && i.key() == hash; ++i)
{
if(!((flags & PCF_MATCH_PARENT) && parent != (*i)->parent()))
{
result = callback(**i, parameters);
if(result) break;
}
}
}
else
{
// No - iterate all nodes.
DENG2_FOR_EACH_CONST(PathTree::Nodes, i, nodes)
{
if(!((flags & PCF_MATCH_PARENT) && parent != (*i)->parent()))
{
result = callback(**i, parameters);
if(result) break;
}
}
}
return result;
}
int PathTree::iterate(int flags, PathTree::Node* parent, ushort hash,
int (*callback) (PathTree::Node&, void*), void* parameters)
{
int result = 0;
if(callback)
{
if(!(flags & PCF_NO_LEAF))
result = iteratePathsInHash(*this, hash, Leaf, flags, parent, callback, parameters);
if(!result && !(flags & PCF_NO_BRANCH))
result = iteratePathsInHash(*this, hash, Branch, flags, parent, callback, parameters);
}
return result;
}
#if _DEBUG
void PathTree::debugPrint(PathTree& pt, char delimiter)
{
LOG_AS("PathTree");
LOG_INFO("[%p]:") << de::dintptr(&pt);
FoundPaths found;
if(pt.findAllPaths(found, 0, delimiter))
{
qSort(found.begin(), found.end());
DENG2_FOR_EACH_CONST(FoundPaths, i, found)
{
LOG_INFO(" %s") << *i;
}
}
LOG_INFO(" %i unique %s in the tree.") << found.count() << (found.count() == 1? "path" : "paths");
}
#if 0
static void printDistributionOverviewElement(const int* colWidths, const char* name,
size_t numEmpty, size_t maxHeight, size_t numCollisions, size_t maxCollisions,
size_t sum, size_t total)
{
DENG2_ASSERT(colWidths);
float coverage, collision, variance;
if(0 != total)
{
size_t sumSqr = sum*sum;
float mean = (signed)sum / total;
variance = ((signed)sumSqr - (signed)sum * mean) / (((signed)total)-1);
coverage = 100 / (float)PATHTREE_PATHHASH_SIZE * (PATHTREE_PATHHASH_SIZE - numEmpty);
collision = 100 / (float) total * numCollisions;
}
else
{
variance = coverage = collision = 0;
}
const int* col = colWidths;
Con_Printf("%*s ", *col++, name);
Con_Printf("%*lu ", *col++, (unsigned long)total);
Con_Printf("%*lu", *col++, PATHTREE_PATHHASH_SIZE - (unsigned long)numEmpty);
Con_Printf(":%-*lu ", *col++, (unsigned long)numEmpty);
Con_Printf("%*lu ", *col++, (unsigned long)maxCollisions);
Con_Printf("%*lu ", *col++, (unsigned long)numCollisions);
Con_Printf("%*.2f ", *col++, collision);
Con_Printf("%*.2f ", *col++, coverage);
Con_Printf("%*.2f ", *col++, variance);
Con_Printf("%*lu\n", *col++, (unsigned long)maxHeight);
}
static void printDistributionOverview(PathTree* pt,
size_t nodeCountSum[PATHTREENODE_TYPE_COUNT],
size_t nodeCountTotal[PATHTREENODE_TYPE_COUNT],
size_t nodeBucketCollisions[PATHTREENODE_TYPE_COUNT], size_t nodeBucketCollisionsTotal,
size_t nodeBucketCollisionsMax[PATHTREENODE_TYPE_COUNT], size_t /*nodeBucketCollisionsMaxTotal*/,
size_t nodeBucketEmpty[PATHTREENODE_TYPE_COUNT], size_t nodeBucketEmptyTotal, size_t nodeBucketHeight,
size_t /*nodeCount*/[PATHTREENODE_TYPE_COUNT])
{
#define NUMCOLS 10/*type+count+used:+empty+collideMax+collideCount+collidePercent+coverage+variance+maxheight*/
DENG2_ASSERT(pt);
size_t collisionsMax = 0, countSum = 0, countTotal = 0;
for(int i = 0; i < PATHTREENODE_TYPE_COUNT; ++i)
{
if(nodeBucketCollisionsMax[i] > collisionsMax)
collisionsMax = nodeBucketCollisionsMax[i];
countSum += nodeCountSum[i];
countTotal += nodeCountTotal[i];
}
int nodeCountDigits = M_NumDigits((int)countTotal);
// Calculate minimum field widths:
int colWidths[NUMCOLS];
int* col = colWidths;
*col = 0;
for(int i = 0; i < PATHTREENODE_TYPE_COUNT; ++i)
{
PathTree::NodeType type = PathTree::NodeType(i);
if(Str_Length(PathTreeNode::typeName(type)) > *col)
*col = Str_Length(PathTreeNode::typeName(type));
}
col++;
*col++ = MAX_OF(nodeCountDigits, 1); /*#*/
*col++ = MAX_OF(nodeCountDigits, 4); /*used*/
*col++ = MAX_OF(nodeCountDigits, 5); /*empty*/
*col++ = MAX_OF(nodeCountDigits, 3); /*max*/
*col++ = MAX_OF(nodeCountDigits, 4); /*num*/
*col++ = MAX_OF(3+1+2, 8); /*percent*/
*col++ = MAX_OF(3+1+2, 9); /*coverage*/
*col++ = MAX_OF(nodeCountDigits, 8); /*variance*/
*col = MAX_OF(nodeCountDigits, 9); /*maxheight*/
// Calculate span widths:
int spans[4][2];
spans[0][0] = colWidths[0] + 1/* */ + colWidths[1];
spans[1][0] = colWidths[2] + 1/*:*/ + colWidths[3];
spans[2][0] = colWidths[4] + 1/* */ + colWidths[5] + 1/* */ + colWidths[6];
spans[3][0] = colWidths[7] + 1/* */ + colWidths[8] + 1/* */ + colWidths[9];
for(int i = 0; i < 4; ++i)
{
int remainder = spans[i][0] % 2;
spans[i][1] = remainder + (spans[i][0] /= 2);
}
Con_FPrintf(CPF_YELLOW, "Directory Distribution (p:%p):\n", pt);
// Level1 headings:
int* span = &spans[0][0];
Con_Printf("%*s", *span++ + 5/2, "nodes"); Con_Printf("%-*s|", *span++ - 5/2, "");
Con_Printf("%*s", *span++ + 4/2, "hash"); Con_Printf("%-*s|", *span++ - 4/2, "");
Con_Printf("%*s", *span++ + 10/2, "collisions"); Con_Printf("%-*s|", *span++ - 10/2, "");
Con_Printf("%*s", *span++ + 5/2, "other"); Con_Printf("%-*s\n",*span++ - 5/2, "");
// Level2 headings:
col = colWidths;
Con_FPrintf(CPF_LIGHT, "%*s ", *col++, "type");
Con_FPrintf(CPF_LIGHT, "%-*s|", *col++, "#");
Con_FPrintf(CPF_LIGHT, "%*s:", *col++, "used");
Con_FPrintf(CPF_LIGHT, "%-*s|", *col++, "empty");
Con_FPrintf(CPF_LIGHT, "%*s ", *col++, "max");
Con_FPrintf(CPF_LIGHT, "%*s ", *col++, "num#");
Con_FPrintf(CPF_LIGHT, "%-*s|", *col++, "percent%");
Con_FPrintf(CPF_LIGHT, "%*s ", *col++, "coverage%");
Con_FPrintf(CPF_LIGHT, "%*s ", *col++, "variance");
Con_FPrintf(CPF_LIGHT, "%-*s\n", *col++, "maxheight");
if(countTotal != 0)
{
for(int i = 0; i < PATHTREENODE_TYPE_COUNT; ++i)
{
PathTree::NodeType type = PathTree::NodeType(i);
printDistributionOverviewElement(colWidths, Str_Text(PathTreeNode::typeName(type)),
nodeBucketEmpty[i], (i == PT_LEAF? nodeBucketHeight : 0),
nodeBucketCollisions[i], nodeBucketCollisionsMax[i],
nodeCountSum[i], nodeCountTotal[i]);
}
Con_PrintRuler();
}
printDistributionOverviewElement(colWidths, "total",
nodeBucketEmptyTotal, nodeBucketHeight,
nodeBucketCollisionsTotal, collisionsMax,
countSum / PATHTREENODE_TYPE_COUNT, countTotal);
#undef NUMCOLS
}
#endif
#if 0
static void printDistributionHistogram(PathTree* pt, ushort size,
size_t nodeCountTotal[PATHTREENODE_TYPE_COUNT])
{
#define NUMCOLS 4/*range+total+PATHTREENODE_TYPE_COUNT*/
size_t totalForRange, total, nodeCount[PATHTREENODE_TYPE_COUNT];
int hashIndexDigits, col, colWidths[2+/*range+total*/PATHTREENODE_TYPE_COUNT];
PathTreeNode* node;
int j;
DENG2_ASSERT(pt);
total = 0;
for(int i = 0; i < PATHTREENODE_TYPE_COUNT; ++i)
{
total += nodeCountTotal[i];
}
if(0 == total) return;
// Calculate minimum field widths:
hashIndexDigits = M_NumDigits(PATHTREE_PATHHASH_SIZE);
col = 0;
if(size != 0)
colWidths[col] = 2/*braces*/+hashIndexDigits*2+3/*elipses*/;
else
colWidths[col] = 2/*braces*/+hashIndexDigits;
colWidths[col] = MAX_OF(colWidths[col], 5/*range*/);
++col;
{ size_t max = 0;
for(int i = 0; i < PATHTREENODE_TYPE_COUNT; ++i)
{
if(nodeCountTotal[i] > max)
max = nodeCountTotal[i];
}
colWidths[col++] = MAX_OF(M_NumDigits((int)max), 5/*total*/);
}
for(int i = 0; i < PATHTREENODE_TYPE_COUNT; ++i, ++col)
{
PathTree::NodeType type = PathTree::NodeType(i);
colWidths[col] = Str_Length(PathTreeNode::typeName(type));
}
// Apply formatting:
for(int i = 1; i < NUMCOLS; ++i) { colWidths[i] += 1; }
Con_FPrintf(CPF_YELLOW, "Histogram (p:%p):\n", pt);
// Print heading:
col = 0;
Con_Printf("%*s", colWidths[col++], "range");
Con_Printf("%*s", colWidths[col++], "total");
for(int i = 0; i < PATHTREENODE_TYPE_COUNT; ++i)
{
PathTree::NodeType type = PathTree::NodeType(i);
Con_Printf("%*s", colWidths[col++], Str_Text(PathTreeNode::typeName(type)));
}
Con_Printf("\n");
Con_PrintRuler();
{ ushort from = 0, n = 0, range = (size != 0? PATHTREE_PATHHASH_SIZE / size: 0);
memset(nodeCount, 0, sizeof(nodeCount));
for(ushort i = 0; i < PATHTREE_PATHHASH_SIZE; ++i)
{
pathtree_pathhash_t** phAdr;
phAdr = hashAddressForNodeType(pt, PathTree::Node::Branch);
if(*phAdr)
for(node = (**phAdr)[i].head; node; node = node->next)
++nodeCount[PathTree::Node::Branch];
phAdr = hashAddressForNodeType(pt, PT_LEAF);
if(*phAdr)
for(node = (**phAdr)[i].head; node; node = node->next)
++nodeCount[PT_LEAF];
if(size != 0 && (++n != range && i != PATHTREE_PATHHASH_SIZE-1))
continue;
totalForRange = 0;
for(j = 0; j < PATHTREENODE_TYPE_COUNT; ++j)
totalForRange += nodeCount[j];
col = 0;
if(size != 0)
{
Str range; Str_Init(&range);
Str_Appendf(&range, "%*u...%*u", hashIndexDigits, from, hashIndexDigits, from+n-1);
Con_Printf("[%*s]", colWidths[col++]-2/*braces*/, Str_Text(&range));
Str_Free(&range);
}
else
{
Con_Printf("[%*u]", colWidths[col++]-2/*braces*/, i);
}
Con_Printf("%*lu", colWidths[col++], (unsigned long) totalForRange);
if(0 != totalForRange)
{
for(j = 0; j < PATHTREENODE_TYPE_COUNT; ++j, ++col)
{
if(0 != nodeCount[j])
{
Con_Printf("%*lu", colWidths[col], (unsigned long) nodeCount[j]);
}
else if(j < PATHTREENODE_TYPE_COUNT-1 || 0 == size)
{
Con_Printf("%*s", colWidths[col], "");
}
}
}
// Are we printing a "graphical" representation?
if(0 != totalForRange)
{
size_t max = MAX_OF(1, ROUND(total/(float)size/10));
size_t scale = totalForRange / (float)max;
scale = MAX_OF(scale, 1);
Con_Printf(" ");
for(n = 0; n < scale; ++n)
Con_Printf("*");
}
Con_Printf("\n");
from = i+1;
n = 0;
memset(nodeCount, 0, sizeof(nodeCount));
}}
Con_PrintRuler();
// Sums:
col = 0;
Con_Printf("%*s", colWidths[col++], "Sum");
Con_Printf("%*lu", colWidths[col++], (unsigned long) total);
if(0 != total)
{
int i;
for(i = 0; i < PATHTREENODE_TYPE_COUNT; ++i, ++col)
{
if(0 != nodeCountTotal[i])
{
Con_Printf("%*lu", colWidths[col], (unsigned long) nodeCountTotal[i]);
}
else if(i < PATHTREENODE_TYPE_COUNT-1)
{
Con_Printf("%*s", colWidths[col], "");
}
}
}
Con_Printf("\n");
#undef NUMCOLS
}
#endif
void PathTree::debugPrintHashDistribution(PathTree& /*pt*/)
{
#if 0
size_t nodeCountSum[PATHTREENODE_TYPE_COUNT],
nodeCountTotal[PATHTREENODE_TYPE_COUNT], nodeBucketHeight = 0,
nodeBucketCollisions[PATHTREENODE_TYPE_COUNT], nodeBucketCollisionsTotal = 0,
nodeBucketCollisionsMax[PATHTREENODE_TYPE_COUNT], nodeBucketCollisionsMaxTotal = 0,
nodeBucketEmpty[PATHTREENODE_TYPE_COUNT], nodeBucketEmptyTotal = 0,
nodeCount[PATHTREENODE_TYPE_COUNT];
size_t totalForRange;
PathTreeNode* node;
DENG2_ASSERT(pt);
nodeCountTotal[PathTree::Node::Branch] = countNodesInPathHash(*hashAddressForNodeType(pt, PathTree::Node::Branch));
nodeCountTotal[PT_LEAF] = countNodesInPathHash(*hashAddressForNodeType(pt, PT_LEAF));
memset(nodeCountSum, 0, sizeof(nodeCountSum));
memset(nodeBucketCollisions, 0, sizeof(nodeBucketCollisions));
memset(nodeBucketCollisionsMax, 0, sizeof(nodeBucketCollisionsMax));
memset(nodeBucketEmpty, 0, sizeof(nodeBucketEmpty));
for(ushort i = 0; i < PATHTREE_PATHHASH_SIZE; ++i)
{
pathtree_pathhash_t** phAdr;
phAdr = hashAddressForNodeType(pt, PathTree::Node::Branch);
nodeCount[PathTree::Node::Branch] = 0;
if(*phAdr)
for(node = (**phAdr)[i].head; node; node = node->next)
++nodeCount[PathTree::Node::Branch];
phAdr = hashAddressForNodeType(pt, PT_LEAF);
nodeCount[PT_LEAF] = 0;
if(*phAdr)
{
size_t chainHeight = 0;
for(node = (**phAdr)[i].head; node; node = node->next)
{
size_t height = 0;
PathTreeNode* other = node;
++nodeCount[PT_LEAF];
while((other = other->parent())) { ++height; }
if(height > chainHeight)
chainHeight = height;
}
if(chainHeight > nodeBucketHeight)
nodeBucketHeight = chainHeight;
}
totalForRange = nodeCount[PathTree::Node::Branch] + nodeCount[PT_LEAF];
nodeCountSum[PT_BRANCH] += nodeCount[PathTree::Node::Branch];
nodeCountSum[PT_LEAF] += nodeCount[PT_LEAF];
for(int j = 0; j < PATHTREENODE_TYPE_COUNT; ++j)
{
if(nodeCount[j] != 0)
{
if(nodeCount[j] > 1)
nodeBucketCollisions[j] += nodeCount[j]-1;
}
else
{
++(nodeBucketEmpty[j]);
}
if(nodeCount[j] > nodeBucketCollisionsMax[j])
nodeBucketCollisionsMax[j] = nodeCount[j];
}
size_t max = 0;
for(int j = 0; j < PATHTREENODE_TYPE_COUNT; ++j)
{
max += nodeCount[j];
}
if(max > nodeBucketCollisionsMaxTotal)
nodeBucketCollisionsMaxTotal = max;
if(totalForRange != 0)
{
if(totalForRange > 1)
nodeBucketCollisionsTotal += totalForRange-1;
}
else
{
++nodeBucketEmptyTotal;
}
if(totalForRange > nodeBucketCollisionsMaxTotal)
nodeBucketCollisionsMaxTotal = totalForRange;
}
printDistributionOverview(pt, nodeCountSum, nodeCountTotal,
nodeBucketCollisions, nodeBucketCollisionsTotal,
nodeBucketCollisionsMax, nodeBucketCollisionsMaxTotal,
nodeBucketEmpty, nodeBucketEmptyTotal,
nodeBucketHeight, nodeCount);
Con_Printf("\n");
printDistributionHistogram(pt, 16, nodeCountTotal);
#endif
}
#endif
} // namespace de