-
Notifications
You must be signed in to change notification settings - Fork 7
/
util.cpp
1086 lines (863 loc) · 27.9 KB
/
util.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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** Daedalus (Version 3.5) File: util.cpp
** By Walter D. Pullen, Astara@msn.com, http://www.astrolog.org/labyrnth.htm
**
** IMPORTANT NOTICE: Daedalus and all Maze generation and general
** graphics routines used in this program are Copyright (C) 1998-2024 by
** Walter D. Pullen. Permission is granted to freely use, modify, and
** distribute these routines provided these credits and notices remain
** unmodified with any altered or distributed versions of the program.
** The user does have all rights to Mazes and other graphic output
** they make in Daedalus, like a novel created in a word processor.
**
** More formally: 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 and inspiring, 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, a copy of which is in the
** LICENSE.HTM included with Daedalus, and at http://www.gnu.org
**
** This file contains generic utilities. Nothing here is related to graphics
** or any other part of Daedalus.
**
** Created: 6/4/1993.
** Last code change: 10/30/2024.
*/
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <math.h>
#include "util.h"
US us = {fTrue, 0L, 0L, 0L};
/*
******************************************************************************
** General Routines
******************************************************************************
*/
// Global new and delete operator implementations.
void *operator new(size_t cb)
{
void *pv;
pv = PAllocate((long)cb);
return pv;
}
void operator delete(void *pv)
{
DeallocateP(pv);
}
void *operator new(size_t cb, void *pv)
{
return pv;
}
void operator delete(void *pv, void *pv2)
{
}
// Change the size of a memory allocation, containing a list of cElem items of
// cbElem size, to a list of cElemNew items of cbElem size.
void *ReallocateArray(void *rgElem, int cElem, int cbElem, int cElemNew)
{
void *rgElemNew;
rgElemNew = PAllocate(cElemNew * cbElem);
if (rgElemNew == NULL)
return NULL;
ClearPb(rgElemNew, cElemNew * cbElem);
if (rgElem != NULL)
CopyPb(rgElem, rgElemNew, Min(cElem, cElemNew) * cbElem);
return rgElemNew;
}
// Display a string with an integer parameter embedded in it.
int PrintSzNCore(CONST char *sz, int n, int nPriority)
{
char szT[cchSzDef];
sprintf(S(szT), sz, n);
return PrintSzCore(szT, nPriority);
}
// Display a string with two integer parameters embedded in it.
int PrintSzNNCore(CONST char *sz, int n1, int n2, int nPriority)
{
char szT[cchSzDef];
sprintf(S(szT), sz, n1, n2);
return PrintSzCore(szT, nPriority);
}
// Display a string with a long integer parameter embedded in it.
int PrintSzLCore(CONST char *sz, long l, int nPriority)
{
char szT[cchSzDef];
sprintf(S(szT), sz, l);
return PrintSzCore(szT, nPriority);
}
// Return whether n is in the range from n1 to n2, inclusive. If not, display
// a suitable warning message.
flag FErrorRange(CONST char *sz, int n, int n1, int n2)
{
char szT[cchSzDef];
if (n < n1 || n > n2) {
sprintf(S(szT), "%s value %d is out of range from %d to %d.\n",
sz, n, n1, n2);
PrintSz_W(szT);
return fTrue;
}
return fFalse;
}
// Copy bytes from one buffer to another. The buffers shouldn't overlap.
void CopyRgb(CONST char *pbFrom, char *pbTo, long cb)
{
while (cb--)
*pbTo++ = *pbFrom++;
}
// Return length of a zero terminated string, not including the terminator.
int CchSz(CONST char *sz)
{
CONST char *pch = sz;
while (*pch)
pch++;
return PD(pch - sz);
}
// Compare two strings case sensitively. Return 0 if equal, negative number if
// first less than second, and positive number if first greater than second.
int CompareSz(CONST char *sz1, CONST char *sz2)
{
while (*sz1 && (*sz1 == *sz2))
sz1++, sz2++;
return (int)*sz1 - *sz2;
}
// Compare two strings case insensitively. Return 0 if equal, negative number
// if first less than second, and positive if first greater than second.
int CompareSzI(CONST char *sz1, CONST char *sz2)
{
while (*sz1 && (ChCap(*sz1) == ChCap(*sz2)))
sz1++, sz2++;
return (int)ChCap(*sz1) - ChCap(*sz2);
}
// Compare two ranges of characters case sensitively. Return 0 if equal,
// negative if first less than second, and positive if greater than second.
int CompareRgch(CONST char *rgch1, int cch1, CONST char *rgch2, int cch2)
{
while (cch1 > 0 && cch2 > 0 && *rgch1 == *rgch2)
rgch1++, rgch2++, cch1--, cch2--;
return cch1 > 0 ? (cch2 > 0 ? (int)*rgch1 - *rgch2 : (int)*rgch1) :
cch2 == 0 ? 0 : -(int)*rgch2;
}
// Compare two ranges of characters case insensitively. Return 0 if equal,
// negative if first less than second, and positive if greater than second.
int CompareRgchI(CONST char *rgch1, int cch1, CONST char *rgch2, int cch2)
{
while (cch1 > 0 && cch2 > 0 && ChCap(*rgch1) == ChCap(*rgch2))
rgch1++, rgch2++, cch1--, cch2--;
return cch1 > 0 ? (cch2 > 0 ? (int)ChCap(*rgch1) - ChCap(*rgch2) :
(int)ChCap(*rgch1)) : cch2 == 0 ? 0 : -(int)ChCap(*rgch2);
}
// Return whether a zero terminated string is equal to a range of characters,
// case sensitively.
flag FCompareSzRgch(CONST char *sz, CONST char *pch, int cch)
{
if (sz[0] == chNull) // Note the empty string is NOT equal.
return fFalse;
while (cch > 0 && *sz == *pch)
sz++, pch++, cch--;
return cch == 0 && sz[0] == chNull;
}
// Return whether a zero terminated string is equal to a range of characters,
// case insensitively.
flag FCompareSzRgchI(CONST char *sz, CONST char *pch, int cch)
{
if (sz[0] == chNull) // Note the empty string is NOT equal.
return fFalse;
while (cch > 0 && ChCap(*sz) == ChCap(*pch))
sz++, pch++, cch--;
return cch == 0 && sz[0] == chNull;
}
// Search for and return the position of a character within a string.
int FindCh(CONST char *pch, int cch, char ch)
{
int ich;
for (ich = 0; ich < cch; ich++)
if (*pch++ == ch)
return ich;
return -1;
}
// Copy a range of characters and zero terminate it. If there are too many
// characters to fit in the destination buffer, the string is truncated.
void CopyRgchToSz(CONST char *pch, int cch, char *sz, int cchMax)
{
cch = Min(cch, cchMax-1);
CopyRgb(pch, sz, cch);
sz[cch] = chNull;
}
// Convert a range of characters to upper case.
void UpperRgch(char *rgch, int cch)
{
while (cch > 0) {
*rgch = ChCap(*rgch);
rgch++, cch--;
}
}
// Parse and return a number contained in a range of characters.
long LFromRgch(CONST char *rgch, int cch)
{
char sz[cchSzDef], *pch;
long l;
flag fBinary;
CopyRgchToSz(rgch, cch, sz, cchSzDef);
if (*sz != '#')
return atol(sz);
fBinary = (*(sz+1) == '#');
// Strings starting with "#" are considered hexadecimal numbers.
// Strings starting with "##" are considered binary numbers.
l = 0;
for (pch = sz+1+fBinary; *pch; pch++)
l = fBinary ? ((l << 1) | (*pch == '1')) :
((l << 4) | NHex2(ChCap(*pch)));
return l;
}
// Parse a real number contained in a range of characters, returning it as an
// fixed point integer with the decimal point at the specified power of ten.
long LFromRgch2(CONST char *rgch, int cch, int nDecimal)
{
char sz[cchSzDef];
real r;
long l;
CopyRgchToSz(rgch, cch, sz, cchSzDef);
r = atof(sz);
l = LPower(10, NAbs(nDecimal));
if (nDecimal >= 0)
r *= (real)l;
else
r /= (real)l;
return (long)r;
}
// Return the smaller of two integers.
int NMin(int n1, int n2)
{
return Min(n1, n2);
}
// Return the larger of two integers.
int NMax(int n1, int n2)
{
return Max(n1, n2);
}
// Return the smaller of two floating point numbers.
real RMin(real n1, real n2)
{
return Min(n1, n2);
}
// Return the larger of two floating point numbers.
real RMax(real n1, real n2)
{
return Max(n1, n2);
}
// Ensure n1 < n2. If not, swap them.
void SortN(int *n1, int *n2)
{
if (*n1 > *n2)
SwapN(*n1, *n2);
}
// Open a file.
FILE *FileOpen(CONST char *sz, CONST char *szMode)
{
#ifdef SECURECRT
FILE *file;
if (fopen_s(&file, sz, szMode))
file = NULL;
return file;
#else
return fopen(sz, szMode);
#endif
}
// Write a string to a file.
void WriteSz(FILE *file, CONST char *sz)
{
CONST char *pch;
for (pch = sz; *pch; pch++) {
if (*pch == '\n') // Translate LF to CRLF for PC's
putc('\r', file);
putc(*pch, file);
}
}
// Read a 16 bit word from a file.
word WRead(FILE *file)
{
byte b1, b2;
b1 = getbyte(); b2 = getbyte();
return WFromBB(b1, b2);
}
// Read a 32 bit long from a file.
dword LRead(FILE *file)
{
byte b1, b2, b3, b4;
b1 = getbyte(); b2 = getbyte(); b3 = getbyte(); b4 = getbyte();
return LFromWW(WFromBB(b1, b2), WFromBB(b3, b4));
}
// Multiplication function. Return x*y, checking for overflow.
long LMul(long x, long y)
{
long z = x * y;
if (y != 0 && z / y != x)
return 0x80000000;
return z;
}
// Division function. Return x/y, checking for illegal parameters.
long LDiv(long x, long y)
{
if (y == 0) // Dividing by 0 is assumed to be 0.
return 0;
else if (x == 0x80000000 && y == -1) // This will crash on x86 too.
return x;
return x / y;
}
// Modulus function. Return remainder of x/y.
long LMod(long x, long y)
{
if (y == 0) // Remainder when dividing by 0 is assumed to be 0.
return 0;
else if (x == 0x80000000 && y == -1) // This will crash on x86 too.
return 0;
return x % y;
}
// Integer power raising function. Return x^y.
long LPower(long x, long y)
{
long pow;
if (y < 1)
return 1;
if (y == 1 || x == 0 || x == 1)
return x;
if (x == -1)
return FOdd(y) ? -1 : 1;
for (pow = x; y > 1; y--) {
if (pow * x / x != pow) // Return 0 if overflow 32 bits.
return 0;
pow *= x;
}
return pow;
}
// Division function. Return x/y, checking for illegal parameters.
real RDiv(real x, real y)
{
if (y >= 0.0) {
if (y < rMinute)
y = rMinute;
} else {
if (y > -rMinute)
y = -rMinute;
}
return x / y;
}
// Calculate x and y coordinates along a circle or ellipse, given a center
// point, radius, and angle.
void AngleR(int *h, int *v, int x, int y, real rx, real ry, real d)
{
*h = x + (int)(rx*RCosD(d));
*v = y + (int)(ry*RSinD(d));
}
// Return the angle of the line between two points.
real GetAngle(int x1, int y1, int x2, int y2)
{
real d;
if (x1 != x2) {
d = RAtnD((real)(y2 - y1) / (real)(x2 - x1));
if (d < 0.0)
d += rDegQuad;
} else
d = 0.0;
if (x2 <= x1 && y2 > y1)
d += rDegQuad;
else if (x2 < x1 && y2 <= y1)
d += rDegHalf;
else if (x2 >= x1 && y2 < y1)
d += rDeg34;
Assert(FBetween(d, 0.0, rDegMax));
return d;
}
// Rotate a point around the origin by the given number of degrees.
void RotateR(real *h, real *v, real d)
{
real m = *h, n = *v, rS, rC;
rS = RSinD(d); rC = RCosD(d);
*h = m*rC - n*rS;
*v = n*rC + m*rS;
}
// Fast version of RotateR that assumes the slow trigonometry values have
// already been computed. Useful when rotating many points by the same angle.
void RotateR2(real *h, real *v, real rS, real rC)
{
real m = *h, n = *v;
*h = m*rC - n*rS;
*v = n*rC + m*rS;
}
/*
******************************************************************************
** Vector Routines
******************************************************************************
*/
// Assign the value of a vector given its components along each axis.
void CVector::Set(real x, real y, real z)
{
m_x = x;
m_y = y;
m_z = z;
}
// Multiply or stretch a vector by a factor.
void CVector::Mul(real len)
{
m_x *= len;
m_y *= len;
m_z *= len;
}
// Return the length of a vector.
real CVector::Length() CONST
{
return RSqr(m_x*m_x + m_y*m_y + m_z*m_z);
}
// Create a unit length vector given spherical coordinate angles.
void CVector::Sphere(real theta, real phi)
{
real r;
m_x = RCosD(theta);
m_y = RSinD(theta);
r = RCosD(phi);
m_z = RSinD(phi);
m_x *= r; m_y *= r;
}
// Return the dot product of two vectors.
real CVector::Dot(CONST CVector &v2) CONST
{
return m_x*v2.m_x + m_y*v2.m_y + m_z*v2.m_z;
}
// Determine the cross product of two vectors, i.e. create a new vector that's
// perpendicular to both.
void CVector::Cross(CONST CVector &v1, CONST CVector &v2)
{
Set(v1.m_y*v2.m_z - v1.m_z*v2.m_y, v1.m_z*v2.m_x - v1.m_x*v2.m_z,
v1.m_x*v2.m_y - v1.m_y*v2.m_x);
}
// Return the angle between two vectors.
real CVector::Angle(CONST CVector &v2) CONST
{
real angle, len1, len2;
len1 = Length();
len2 = v2.Length();
if (len1 != 0.0 && len2 != 0.0) {
angle = Dot(v2)/len1/len2;
if (angle == 0.0)
return rPiHalf;
else if (angle <= -1.0)
return rPi;
angle = RAtn(RSqr(1.0 - Sq(angle)) / angle);
if (angle >= 0.0)
return angle;
else
return angle + rPi;
} else
return rPiHalf;
}
// Create a normal vector to a plane, i.e. a vector perpendicular to the plane
// passing through three coordinates.
void CVector::Normal(real x1, real y1, real z1, real x2, real y2, real z2,
real x3, real y3, real z3)
{
CVector v1, v2;
v1.Set(x2 - x1, y2 - y1, z2 - z1);
v2.Set(x3 - x1, y3 - y1, z3 - z1);
Cross(v1, v2);
}
/*
******************************************************************************
** Trie Tree Routines
******************************************************************************
*/
// Return whether two ranges of characters are equal. Either string ending
// prematurely with a zero terminator makes the strings not equal.
flag FEqRgch(CONST char *rgch1, CONST char *rgch2, int cch, flag fInsensitive)
{
int ich;
if (!fInsensitive) {
for (ich = 0; ich < cch; ich++) {
if (rgch1[ich] == '\0' || rgch1[ich] != rgch2[ich])
return fFalse;
}
} else {
for (ich = 0; ich < cch; ich++) {
if (rgch1[ich] == '\0' || ChCap(rgch1[ich]) != ChCap(rgch2[ich]))
return fFalse;
}
}
return fTrue;
}
// Create a trie tree within the buffer rgsOut of size csTrieMax. Returns the
// size of the trie in shorts, or -1 on failure. The input list of strings is
// used to create the trie. It does not have to be in sorted order. Trie
// lookups on strings will return the index they are in the original list. No
// two strings should be the same. Individual strings shouldn't be longer than
// 255 characters, or rather trailing unique substrings can't be longer than
// 255. With signed 16 bit indexes, these tries can't be larger than 32K
// shorts, or 64K bytes. That can store about 2500 strings.
//
// The trie format is a sequence of nodes. "Pointers" to other nodes are
// indexes into the array of shorts. There are two types of nodes: Standard
// nodes which handle all the branches from a particular leading substring,
// and leaf nodes for unique trailing substrings. Standard node format:
//
// Short 0: 0 = No string ends at this point. Non-zero = Payload + 1 for
// substring leading up to this node.
// Short 1: High byte = Highest character covered by this node. Low byte =
// Lowest character covered. If high character is 0, this is a leaf node.
// Short 2+: Pointers to nodes for the range of low through high characters.
// 0 = null pointer. Less than 0 means a string ends with this character,
// where value contains -(Payload + 1).
//
// Leaf node format:
// Short 0: Same as standard node.
// Short 1: Length of trailing substring.
// Short 2: Payload for the string ending after this point.
// Short 3+: Range of characters storing final substring to compare against.
int CsCreateTrie(CONST uchar *rgszIn[], int cszIn, TRIE rgsOut, int csTrieMax,
flag fInsensitive)
{
uchar rgchStack[cchSzMax];
int rgisStack[cchSzMax];
long rgchUsed[256], iUsed = 0;
int iStack, csOut = 0, isz, ich, chLo, chHi, chT, is, cch, isT,
isRemember = 0, csz, iszSav;
for (ich = 0; ich < 256; ich++)
rgchUsed[ich] = 0;
for (iStack = 0;; iStack++) {
if (iStack >= cchSzMax) {
Assert(fFalse);
return -1;
}
rgisStack[iStack] = csOut;
chLo = 255, chHi = 0, iUsed++, csz = 0;
// Count how many strings match the current leading substring. Also
// get the low and high character for these strings.
for (isz = 0; isz < cszIn; isz++) {
if (FEqRgch((CONST char *)rgszIn[isz], (CONST char *)rgchStack, iStack,
fInsensitive)) {
chT = rgszIn[isz][iStack];
if (chT != 0) {
if (fInsensitive)
chT = ChCap(chT);
csz++;
iszSav = isz;
rgchUsed[chT] = iUsed;
if (chT < chLo)
chLo = chT;
if (chT > chHi)
chHi = chT;
}
}
}
// If no strings match, back up to an earlier node.
if (csz <= 0) {
LPop:
loop {
// Pop the stack to the parent node.
iStack--;
if (iStack < 0)
goto LDone;
is = rgisStack[iStack];
chLo = (word)rgsOut[is + 1] & 255;
chHi = (word)rgsOut[is + 1] >> 8;
// Scan for a pointer that hasn't been filled out yet.
for (ich = chLo + 1; ich <= chHi; ich++) {
if (rgsOut[is + 2 + (ich - chLo)] == 1) {
chT = ich;
goto LPush;
}
}
}
}
// Since there's at least one string, there will be a new node. Set the
// pointer in the parent node to here.
rgsOut[isRemember] = csOut;
if (csOut >= csTrieMax - 1) {
Assert(fFalse);
return -1;
}
rgsOut[csOut++] = 0; // Short 0
// If exactly one string matches, create a leaf node.
if (csz == 1) {
for (ich = iStack; rgszIn[iszSav][ich] != 0; ich++)
;
cch = ich - iStack;
if (cch > 255) {
Assert(fFalse);
return -1;
}
if (csOut >= csTrieMax - 2 - ((cch + 1) >> 1)) {
Assert(fFalse);
return -1;
}
rgsOut[csOut++] = cch; // Short 1
rgsOut[csOut++] = 0; // Short 2
CopyRgb((char *)&rgszIn[iszSav][iStack], (char *)&rgsOut[csOut],
(cch + 1) & ~1);
if (fInsensitive)
UpperRgch((char *)&rgsOut[csOut], cch);
csOut += (cch + 1) >> 1;
goto LPop;
}
// Create a standard node.
if (csOut >= csTrieMax - 1 - (chHi - chLo + 1)) {
Assert(fFalse);
return -1;
}
rgsOut[csOut++] = (chHi << 8) | chLo; // Short 1
// Set those characters in use to the temporary pointer value 1, which
// will be filled out later.
for (ich = chLo; ich <= chHi; ich++)
rgsOut[csOut++] = (rgchUsed[ich] == iUsed);
chT = chLo;
LPush:
rgchStack[iStack] = chT;
isRemember = rgisStack[iStack] + 2 + (chT - chLo);
rgsOut[isRemember] = -1;
}
LDone:
// Fill out payloads. For each string in the input list, walk the trie and
// set the appropriate point in it to the string's index in the input list.
Assert(csOut != 0);
for (isz = 0; isz < cszIn; isz++) {
is = 0;
for (ich = 0;; ich++) {
if (rgszIn[isz][ich] == '\0') {
// Handle the substring case (short 0).
Assert(rgsOut[is] == 0 || !fInsensitive);
rgsOut[is] = isz+1;
break;
}
chLo = (word)rgsOut[is + 1] & 255;
chHi = (word)rgsOut[is + 1] >> 8;
if (chHi == 0) {
// Handle the leaf node case (short 2).
Assert(rgsOut[is + 2] == 0 && FEqRgch((char *)&rgsOut[is+3],
(char *)&rgszIn[isz][ich], chLo, fInsensitive));
rgsOut[is + 2] = isz;
break;
}
chT = rgszIn[isz][ich];
if (fInsensitive)
chT = ChCap(chT);
Assert(chT >= chLo && chT <= chHi);
isT = rgsOut[is + 2 + (chT - chLo)];
Assert(isT != 0);
if (isT <= 0) {
// Handle the payload pointer within standard node case.
Assert(rgszIn[isz][ich + 1] == '\0');
rgsOut[is + 2 + (chT - chLo)] = -(isz+1);
break;
}
is = isT;
}
}
return csOut;
}
// Lookup a string in a trie created with CsCreateTrie. Return -1 if string
// not found, otherwise return the index of the string in the original list
// used to create the trie. This lookup is very fast, and not much slower than
// a single string compare. For strings not in the list, usually don't even
// have to look at all of its characters before knowing it's not in the trie.
int ILookupTrie(CONST TRIE rgsIn, CONST char *rgch, int cch,
flag fInsensitive)
{
int is = 0, chLo, chHi, ch, cchT;
CONST char *pchEnd = rgch + cch, *pch, *pch1, *pch2;
// Walk the input string, while going from node to node in the trie.
for (pch = rgch;; pch++) {
if (pch >= pchEnd) {
// At end of input string. Check whether current node has a payload.
if (rgsIn[is] != 0)
return rgsIn[is]-1;
else
return -1;
}
chLo = (word)rgsIn[is + 1];
chHi = chLo >> 8;
if (chHi == 0) {
// Leaf node. Compare rest of input string with substring in node.
pch1 = (char *)&rgsIn[is + 3], pch2 = pch, cchT = chLo;
if (!fInsensitive) {
while (cchT > 0 && *pch1 == *pch2) {
Assert(*pch1 != '\0');
pch1++, pch2++, cchT--;
}
} else {
while (cchT > 0 && *pch1 == ChCap(*pch2)) {
Assert(*pch1 != '\0');
pch1++, pch2++, cchT--;
}
}
if (cchT > 0 || pch + chLo < pchEnd)
return -1;
else
return rgsIn[is + 2];
}
// Standard node. Get pointer to appropriate child node.
chLo &= 255;
ch = *pch;
if (fInsensitive)
ch = ChCap(ch);
if (ch < chLo || ch > chHi)
return -1;
is = rgsIn[is + 2 + (ch - chLo)];
if (is < 0) {
if (pch + 1 == pchEnd)
return -is-1;
else
return -1;
} else if (is == 0)
return -1;
}
}
/*
******************************************************************************
** Random Number Routines
******************************************************************************
*/
// C code for MT19937, with initialization improved Jan 26, 2002.
// Coded by Takuji Nishimura and Makoto Matsumoto.
// http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
// Copyright (C) 1997-2002 by Makoto Matsumoto and Takuji Nishimura,
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1) Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2) Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3) The names of its contributors may not be used to endorse or promote
// products derived from this software without specific prior written
// permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// Period parameters
#define N 624
#define M 397
#define MATRIX_A 0x9908b0dfUL // Constant vector a
#define UPPER_MASK 0x80000000UL // Most significant w-r bits
#define LOWER_MASK 0x7fffffffUL // Least significant r bits
ulong mt[N]; // The array for the state vector
int imt = N+1; // imt == N+1 means mt[N] is not initialized
// Initialize mt[N] with a seed.
void InitRndL(ulong l)
{
if (us.fRndOld) {
srand(l);
return;
}
mt[0] = l & 0xffffffffUL;
for (imt = 1; imt < N; imt++) {
mt[imt] = 1812433253UL * (mt[imt-1] ^ (mt[imt-1] >> 30)) + imt;
// See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. In the previous
// versions, MSBs of the seed affect only MSBs of the array mt[].
// Modified by Makoto Matsumoto, Jan 9, 2002.
}
}
// Initialize by an array with array-length; rgl is the array for
// initializing keys; cl is its length. Slight change for C++, Feb 26, 2004.
void InitRndRgl(ulong rgl[], int cl)
{
int i = 1, j = 0, c;
InitRndL(19650218UL);
c = (N > cl ? N : cl);
for (; c; c--) {
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL)) +
rgl[j] + j; // Non-linear
i++, j++;
if (i >= N) {
mt[0] = mt[N-1]; i=1;
}
if (j >= cl)
j = 0;
}
for (c = N-1; c; c--) {
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL)) -
i; // Non-linear
i++;
if (i >= N) {