-
Notifications
You must be signed in to change notification settings - Fork 0
/
dt.cpp
executable file
·2127 lines (1888 loc) · 59.4 KB
/
dt.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
/////////////////////////////////////////////////
// Random
const int TRnd::RndSeed=0;
const int TRnd::a=16807;
const int TRnd::m=2147483647;
const int TRnd::q=127773; // m DIV a
const int TRnd::r=2836; // m MOD a
void TRnd::LoadXml(const PXmlTok& XmlTok, const TStr& Nm){
XLoadHd(Nm);
Seed=TXmlObjSer::GetIntArg(XmlTok, "Seed");
}
void TRnd::SaveXml(TSOut& SOut, const TStr& Nm) const {
XSaveBETagArg(Nm, "Seed", TInt::GetStr(Seed));
}
void TRnd::PutSeed(const int& _Seed){
Assert(_Seed>=0);
if (_Seed==0){
//Seed=int(time(NULL));
Seed=abs(int(TSysTm::GetPerfTimerTicks()));
} else {
Seed=_Seed;
//Seed=abs(_Seed*100000)+1;
}
}
void TRnd::Move(const int& Steps){
for (int StepN=0; StepN<Steps; StepN++){GetNextSeed();}
}
bool TRnd::Check(){
int PSeed=Seed; Seed=1;
for (int SeedN=0; SeedN<10000; SeedN++){GetNextSeed();}
bool Ok=Seed==1043618065; Seed=PSeed; return Ok;
}
int TRnd::GetUniDevInt(const int& Range){
int Seed=GetNextSeed();
if (Range==0){return Seed;}
else {return Seed%Range;}
}
uint TRnd::GetUniDevUInt(const uint& Range){
uint Seed=uint(GetNextSeed()%0x10000)*0x10000+uint(GetNextSeed()%0x10000);
if (Range==0){return Seed;}
else {return Seed%Range;}
}
double TRnd::GetNrmDev(){
double v1, v2, rsq;
do {
v1=2.0*GetUniDev()-1.0; // pick two uniform numbers in the square
v2=2.0*GetUniDev()-1.0; // extending from -1 to +1 in each direction
rsq=v1*v1+v2*v2; // see if they are in the unit cicrcle
} while ((rsq>=1.0)||(rsq==0.0)); // and if they are not, try again
double fac=sqrt(-2.0*log(rsq)/rsq); // Box-Muller transformation
return v1*fac;
// return v2*fac; // second deviate
}
double TRnd::GetNrmDev(
const double& Mean, const double& SDev, const double& Mn, const double& Mx){
double Val=Mean+GetNrmDev()*SDev;
if (Val<Mn){Val=Mn;}
if (Val>Mx){Val=Mx;}
return Val;
}
double TRnd::GetExpDev(){
double UniDev;
do {
UniDev=GetUniDev();
} while (UniDev==0.0);
return -log(UniDev);
}
double TRnd::GetExpDev(const double& Lambda) {
return GetExpDev()/Lambda;
}
double TRnd::GetGammaDev(const int& Order){
int j;
double am,e,s,v1,v2,x,y;
if (Order<1){Fail;}
if (Order<6) {
x=1.0;
for (j=1;j<=Order;j++) x *=GetUniDev();
x = -log(x);
} else {
do {
do {
do {
v1=2.0*GetUniDev()-1.0;
v2=2.0*GetUniDev()-1.0;
} while (v1*v1+v2*v2 > 1.0);
y=v2/v1;
am=Order-1;
s=sqrt(2.0*am+1.0);
x=s*y+am;
} while (x <= 0.0);
e=(1.0+y*y)*exp(am*log(x/am)-s*y);
} while (GetUniDev()>e);
}
return x;
}
double TRnd::GetPoissonDev(const double& Mean){
static double sq,alxm,g,oldm=(-1.0);
double em,t,y;
if (Mean < 12.0) {
if (Mean != oldm) {
oldm=Mean;
g=exp(-Mean);
}
em = -1;
t=1.0;
do {
++em;
t *= GetUniDev();
} while (t>g);
} else {
if (Mean != oldm) {
oldm=Mean;
sq=sqrt(2.0*Mean);
alxm=log(Mean);
g=Mean*alxm-TSpecFunc::LnGamma(Mean+1.0);
}
do {
do {
y=tan(TMath::Pi*GetUniDev());
em=sq*y+Mean;
} while (em < 0.0);
em=floor(em);
t=0.9*(1.0+y*y)*exp(em*alxm-TSpecFunc::LnGamma(em+1.0)-g);
} while (GetUniDev()>t);
}
return em;
}
double TRnd::GetBinomialDev(const double& Prb, const int& Trials){
int j;
static int nold=(-1);
double am,em,g,angle,p,bnl,sq,t,y;
static double pold=(-1.0),pc,plog,pclog,en,oldg;
p=(Prb <= 0.5 ? Prb : 1.0-Prb);
am=Trials*p;
if (Trials < 25) {
bnl=0.0;
for (j=1;j<=Trials;j++)
if (GetUniDev() < p) ++bnl;
} else if (am < 1.0) {
g=exp(-am);
t=1.0;
for (j=0;j<=Trials;j++) {
t *= GetUniDev();
if (t < g) break;
}
bnl=(j <= Trials ? j : Trials);
} else {
if (Trials != nold) {
en=Trials;
oldg=TSpecFunc::LnGamma(en+1.0);
nold=Trials;
} if (p != pold) {
pc=1.0-p;
plog=log(p);
pclog=log(pc);
pold=p;
}
sq=sqrt(2.0*am*pc);
do {
do {
angle=TMath::Pi*GetUniDev();
y=tan(angle);
em=sq*y+am;
} while (em < 0.0 || em >= (en+1.0));
em=floor(em);
t=1.2*sq*(1.0+y*y)*exp(oldg-(em+1.0)
-TSpecFunc::LnGamma(en-em+1.0)+em*plog+(en-em)*pclog);
} while (GetUniDev() > t);
bnl=em;
}
if (p != Prb) bnl=Trials-bnl;
return bnl;
}
// sample points from d-dimensional unit sphere
/*void TRnd::GetSphereDev(const int& Dim, TFltV& ValV) {
if (ValV.Len() != Dim) { ValV.Gen(Dim); }
double Length = 0.0;
for (int i = 0; i < Dim; i++) {
ValV[i] = GetNrmDev();
Length += TMath::Sqr(ValV[i]); }
Length = 1.0 / sqrt(Length);
for (int i = 0; i < Dim; i++) {
ValV[i] *= Length;
}
}*/
TRnd TRnd::LoadTxt(TILx& Lx){
return TRnd(Lx.GetInt());
}
void TRnd::SaveTxt(TOLx& Lx) const {
Lx.PutInt(Seed);
}
/////////////////////////////////////////////////
// Memory
void TMem::Resize(const int& _MxBfL){
if (_MxBfL<=MxBfL){return;}
else {if (MxBfL*2<_MxBfL){MxBfL=_MxBfL;} else {MxBfL*=2;}}
char* NewBf=new char[MxBfL]; IAssert(NewBf!=NULL);
if (BfL>0){memcpy(NewBf, Bf, BfL);}
if (Bf!=NULL){delete[] Bf;}
Bf=NewBf;
}
TMem::TMem(const TStr& Str):
MxBfL(Str.Len()), BfL(MxBfL), Bf(NULL){
if (MxBfL>0){
Bf=new char[MxBfL];
if (BfL>0){memcpy(Bf, Str.CStr(), BfL);}
}
}
void TMem::SaveXml(TSOut& SOut, const TStr& Nm) const {
XSaveHdArg(Nm, "BfL", TInt::GetStr(BfL));
SOut.PutStr(TXmlLx::GetXmlStrFromPlainMem(*this));
}
bool TMem::DoFitStr(const TStr& Str) const {
return DoFitLen(Str.Len()+1);
}
TMem& TMem::operator+=(const char& Ch){
if (BfL==MxBfL){Resize(BfL+1);}
Bf[BfL]=Ch; BfL++; return *this;
}
TMem& TMem::operator+=(const TMem& Mem){
int LBfL=Mem.Len(); if (BfL+LBfL>MxBfL){Resize(BfL+LBfL);}
if (LBfL>0){memcpy(&Bf[BfL], Mem(), LBfL);}
BfL+=LBfL; return *this;
}
TMem& TMem::operator+=(const TStr& Str){
int LBfL=Str.Len(); if (BfL+LBfL>MxBfL){Resize(BfL+LBfL);}
if (LBfL>0){memcpy(Bf+BfL, Str.CStr(), LBfL);}
BfL+=LBfL; return *this;
}
TMem& TMem::operator+=(const PSIn& SIn){
int LBfL=SIn->Len(); if (BfL+LBfL>MxBfL){Resize(BfL+LBfL);}
char* LBf=new char[LBfL];
SIn->GetBf(LBf, LBfL);
if (LBfL>0){memcpy(Bf+BfL, LBf, LBfL);}
delete[] LBf;
BfL+=LBfL; return *this;
}
void TMem::Del(const int& BChN, const int& EChN){
if (BChN>EChN){return;}
if ((BChN==0)&&(EChN==BfL-1)){Clr(); return;}
IAssert((0<=BChN)&&(BChN<=EChN)&&(EChN<BfL));
memmove(Bf+BChN, Bf+EChN+1, BfL-EChN-1);
BfL-=(EChN-BChN+1);
}
//int TMem::AddStr(const TStr& Str){
// int LBfL=Str.Len()+1; Resize(BfL+LBfL);
// if (LBfL>0){memcpy(&Bf[BfL], Str.CStr(), LBfL);}
// int FChN=BfL; BfL+=LBfL; return FChN;
//}
void TMem::AddBf(const void* Bf, const int& BfL){
char* ChBf=(char*)Bf;
for (int BfC=0; BfC<BfL; BfC++){
char Ch=ChBf[BfC];
operator+=(Ch);
}
}
TStr TMem::GetAsStr(const char& NewNullCh) const {
if (NewNullCh!='\0'){
TChA ChA(*this);
ChA.ChangeCh('\0', NewNullCh);
return ChA;
} else {
return TStr(*this);
}
}
/////////////////////////////////////////////////
// Input-Memory
TMemIn::TMemIn(const TMem& _Mem, const int& _BfC):
TSBase("Input-Memory"), TSIn("Input-Memory"), Mem(), Bf(_Mem()), BfC(_BfC), BfL(_Mem.Len()){}
int TMemIn::GetBf(const void* LBf, const TSize& LBfL){
Assert(TSize(BfC+LBfL)<=TSize(BfL));
int LBfS=0;
for (TSize LBfC=0; LBfC<LBfL; LBfC++){
LBfS+=(((char*)LBf)[LBfC]=Bf[BfC++]);}
return LBfS;
}
/////////////////////////////////////////////////
// Output-Memory
TMemOut::TMemOut(const PMem& _Mem): TSBase("Output-Memory"), TSOut("Output-Memory"), Mem(_Mem){}
int TMemOut::PutBf(const void* LBf, const TSize& LBfL){
int LBfS=0;
TMem& _Mem=*Mem;
for (TSize LBfC=0; LBfC<LBfL; LBfC++){
char Ch=((char*)LBf)[LBfC];
LBfS+=Ch; _Mem+=Ch;
}
return LBfS;
}
/////////////////////////////////////////////////
// Char-Array
void TChA::Resize(const int& _MxBfL){
if (_MxBfL<=MxBfL){return;}
else {if (MxBfL*2<_MxBfL){MxBfL=_MxBfL;} else {MxBfL*=2;}}
char* NewBf=new char[MxBfL+1]; IAssert(NewBf!=NULL);
strcpy(NewBf, Bf);
delete[] Bf; Bf=NewBf;
}
TChA::TChA(const TStr& Str){
Bf=new char[(MxBfL=BfL=Str.Len())+1];
strcpy(Bf, Str.CStr());
}
void TChA::SaveXml(TSOut& SOut, const TStr& Nm) const {
XSaveHdArg(Nm, "BfL", TInt::GetStr(BfL));
SOut.PutStr(TXmlLx::GetXmlStrFromPlainStr(*this));
}
TChA& TChA::operator=(const TChA& ChA){
if (this!=&ChA){
if (ChA.BfL>MxBfL){delete[] Bf; Bf=new char[(MxBfL=ChA.BfL)+1];}
BfL=ChA.BfL; strcpy(Bf, ChA.CStr());
}
return *this;
}
TChA& TChA::operator=(const TStr& Str){
if (Str.Len()>MxBfL){delete[] Bf; Bf=new char[(MxBfL=Str.Len())+1];}
BfL=Str.Len(); strcpy(Bf, Str.CStr());
return *this;
}
TChA& TChA::operator=(const char* CStr){
int CStrLen=int(strlen(CStr));
if (CStrLen>MxBfL){delete[] Bf; Bf=new char[(MxBfL=CStrLen)+1];}
BfL=CStrLen; strcpy(Bf, CStr);
return *this;
}
TChA& TChA::operator+=(const TChA& ChA){
Resize(BfL+ChA.Len());
strcpy(Bf+BfL, ChA.CStr()); BfL+=ChA.Len(); return *this;
}
TChA& TChA::operator+=(const TStr& Str){
Resize(BfL+Str.Len());
strcpy(Bf+BfL, Str.CStr()); BfL+=Str.Len(); return *this;
}
TChA& TChA::operator+=(const char* CStr){
int CStrLen=(int)strlen(CStr); Resize(BfL+CStrLen);
strcpy(Bf+BfL, CStr); BfL+=CStrLen; return *this;
}
void TChA::Ins(const int& BChN, const char* CStr){
Assert((0<=BChN)&&(BChN<=BfL)); //** ali je <= v (BChN<=BfL) upravicen?
int CStrLen=int(strlen(CStr)); Resize(BfL+CStrLen);
memmove(Bf+BChN+CStrLen, Bf+BChN, BfL-BChN+1);
memmove(Bf+BChN, CStr, CStrLen); BfL+=CStrLen;
}
void TChA::Del(const int& ChN){
Assert((0<=ChN)&&(ChN<BfL));
memmove(Bf+ChN, Bf+ChN+1, BfL-ChN);
BfL--;
}
void TChA::Trunc(){
int BChN=0; while ((BChN<BfL)&&(GetCh(BChN)<=' ')){BChN++;}
int EChN=BfL-1; while ((0<=EChN)&&(GetCh(EChN)<=' ')){EChN--;}
if (BChN<=EChN){
for (int ChN=BChN; ChN<=EChN; ChN++){
PutCh(ChN-BChN, GetCh(ChN));}
Trunc(EChN-BChN+1);
} else {
Clr();
}
/* int BChN=0; while ((BChN<BfL)&&(Bf[BChN]<=' ')){BChN++;}
int EChN=BfL-1; while ((0<=EChN)&&(Bf[EChN]<=' ')){EChN--;}
if (BChN<=EChN){
for (int ChN=BChN; ChN<=EChN; ChN++){Bf[ChN-BChN]=Bf[ChN];}
Bf[BfL=EChN+1]=0;
} else {
Clr();
}*/
}
void TChA::Reverse(){
for (int ChN=0; ChN<BfL/2; ChN++){
char Ch=Bf[ChN];
Bf[ChN]=Bf[BfL-ChN-1];
Bf[BfL-ChN-1]=Ch;
}
}
TChA TChA::GetSubStr(const int& _BChN, const int& _EChN) const {
int BChN=TInt::GetMx(_BChN, 0);
int EChN=TInt::GetMn(_EChN, Len()-1);
int Chs=EChN-BChN+1;
if (Chs<=0){return TStr::GetNullStr();}
else if (Chs==Len()){return *this;}
else {
//char* Bf=new char[Chs+1]; strncpy(Bf, CStr()+BChN, Chs); Bf[Chs]=0;
//TStr Str(Bf); delete[] Bf;
//return Str;
return TChA(CStr()+BChN, Chs);
}
}
int TChA::CountCh(const char& Ch, const int& BChN) const {
int ChN=TInt::GetMx(BChN, 0);
const int ThisLen=Len();
int Cnt = 0;
while (ChN<ThisLen){if (Bf[ChN]==Ch){ Cnt++;} ChN++;}
return Cnt;
}
int TChA::SearchCh(const char& Ch, const int& BChN) const {
int ChN=TInt::GetMx(BChN, 0);
const int ThisLen=Len();
while (ChN<ThisLen){if (Bf[ChN]==Ch){return ChN;} ChN++;}
return -1;
}
int TChA::SearchChBack(const char& Ch, int BChN) const {
if (BChN >= Len() || BChN < 0) { BChN = Len()-1; }
for (int i = BChN; i >= 0; i--) {
if (GetCh(i) == Ch) { return i; }
}
return -1;
}
int TChA::SearchStr(const TChA& Str, const int& BChN) const {
return SearchStr(Str.CStr(), BChN);
}
int TChA::SearchStr(const TStr& Str, const int& BChN) const {
return SearchStr(Str.CStr(), BChN);
}
int TChA::SearchStr(const char* CStr, const int& BChN) const {
const char* BegPos=strstr(Bf+BChN, CStr);
if (BegPos==NULL){return -1;}
else {return int(BegPos-Bf);}
}
bool TChA::IsPrefix(const char* CStr) const { //J:
if ((int)strlen(CStr) > Len()) { return false; }
const char* B = Bf;
const char* C = CStr;
while (*C!=0 && *B==*C) {
B++; C++;
}
if (*C == 0) { return true; }
else { return false; }
}
bool TChA::IsPrefix(const TStr& Str) const {
return IsPrefix(Str.CStr());
}
bool TChA::IsPrefix(const TChA& Str) const {
return IsPrefix(Str.CStr());
}
bool TChA::IsSuffix(const char* CStr) const {
if ((int)strlen(CStr) > Len()) { return false; }
const char* E = Bf+Len()-1;
const char* C = CStr+strlen(CStr)-1;
while (C >= CStr && *E==*C) {
E--; C--;
}
if (C+1 == CStr) { return true; }
else { return false; }
}
bool TChA::IsSuffix(const TStr& Str) const {
return IsSuffix(Str.CStr());
}
bool TChA::IsSuffix(const TChA& Str) const {
return IsSuffix(Str.CStr());
}
void TChA::ChangeCh(const char& SrcCh, const char& DstCh){
int StrLen=Len();
for (int ChN=0; ChN<StrLen; ChN++){if (Bf[ChN]==SrcCh){Bf[ChN]=DstCh;}}
}
/*void TChA::ToUc(){
int StrLen=Len();
for (int ChN=0; ChN<StrLen; ChN++){Bf[ChN]=(char)toupper(Bf[ChN]);}
}
void TChA::ToLc(){
int StrLen=Len();
for (int ChN=0; ChN<StrLen; ChN++){Bf[ChN]=(char)tolower(Bf[ChN]);}
}*/
TChA& TChA::ToLc() {
char *c = Bf;
while (*c) {
*c = (char) tolower(*c); c++;
}
return *this;
}
TChA& TChA::ToUc() {
char *c = Bf;
while (*c) {
*c = (char) toupper(*c); c++;
}
return *this;
}
void TChA::ToTrunc(){
int StrLen=Len(); int BChN=0; int EChN=StrLen-1;
while ((BChN<StrLen)&&TCh::IsWs(GetCh(BChN))){BChN++;}
while ((EChN>=0)&&TCh::IsWs(GetCh(EChN))){EChN--;}
if ((BChN!=0)||(EChN!=StrLen-1)){
int DstChN=0;
for (int SrcChN=BChN; SrcChN<=EChN; SrcChN++){
PutCh(DstChN, GetCh(SrcChN)); DstChN++;}
Trunc(DstChN);
}
}
void TChA::CompressWs(){
int StrLen=Len(); int SrcChN=0; int DstChN=0;
while ((SrcChN<StrLen)&&TCh::IsWs(GetCh(SrcChN))){SrcChN++;}
while (SrcChN<StrLen){
if ((TCh::IsWs(GetCh(SrcChN)))&&(DstChN>0)&&(TCh::IsWs(GetCh(DstChN-1)))){
SrcChN++;
} else {
PutCh(DstChN, GetCh(SrcChN)); SrcChN++; DstChN++;
}
}
if ((DstChN>0)&&(TCh::IsWs(GetCh(DstChN-1)))){DstChN--;}
Trunc(DstChN);
}
void TChA::Swap(const int& ChN1, const int& ChN2){
char Ch=GetCh(ChN1);
PutCh(ChN1, GetCh(ChN2));
PutCh(ChN2, Ch);
}
void TChA::Swap(TChA& ChA) {
::Swap(MxBfL, ChA.MxBfL);
::Swap(BfL, ChA.BfL);
::Swap(Bf, ChA.Bf);
}
int TChA::GetPrimHashCd() const {
return TStrHashF_DJB::GetPrimHashCd(CStr());
}
int TChA::GetSecHashCd() const {
return TStrHashF_DJB::GetSecHashCd(CStr());
}
void TChA::LoadTxt(const PSIn& SIn, TChA& ChA){
delete[] ChA.Bf;
ChA.Bf=new char[(ChA.MxBfL=ChA.BfL=SIn->Len())+1];
SIn->GetBf(ChA.CStr(), SIn->Len()); ChA.Bf[ChA.BfL]=0;
}
void TChA::SaveTxt(const PSOut& SOut) const {
SOut->SaveBf(CStr(), Len());
}
/*TChA operator+(const TChA& LStr, const TChA& RStr){
return LStr+=RStr; }
}
TChA operator+(const TChA& LStr, const TStr& RStr){
return LStr+=RStr.CStr();
}
TChA operator+(const TStr& LStr, const char* RCStr){
return LStr+=RCStr;
}*/
/////////////////////////////////////////////////
// Input-Char-Array
TChAIn::TChAIn(const TChA& ChA, const int& _BfC):
TSBase("Input-Char-Array"), TSIn("Input-Char-Array"), Bf(ChA.CStr()), BfC(_BfC), BfL(ChA.Len()){}
int TChAIn::GetBf(const void* LBf, const TSize& LBfL){
Assert(TSize(BfC+LBfL)<=TSize(BfL));
int LBfS=0;
for (TSize LBfC=0; LBfC<LBfL; LBfC++){
LBfS+=(((char*)LBf)[LBfC]=Bf[BfC++]);}
return LBfS;
}
/////////////////////////////////////////////////
// Ref-String
bool TRStr::IsUc() const {
int StrLen=Len();
for (int ChN=0; ChN<StrLen; ChN++){
if (('a'<=Bf[ChN])&&(Bf[ChN]<='z')){return false;}}
return true;
}
void TRStr::ToUc(){
int StrLen=Len();
for (int ChN=0; ChN<StrLen; ChN++){
Bf[ChN]=(char)toupper(Bf[ChN]);}}
bool TRStr::IsLc() const {
int StrLen=Len();
for (int ChN=0; ChN<StrLen; ChN++){
if (('A'<=Bf[ChN])&&(Bf[ChN]<='Z')){return false;}}
return true;
}
void TRStr::ToLc(){
int StrLen=Len();
for (int ChN=0; ChN<StrLen; ChN++){
Bf[ChN]=(char)tolower(Bf[ChN]);}
}
void TRStr::ToCap(){
int StrLen=Len();
if (StrLen>0){
Bf[0]=(char)toupper(Bf[0]);}
for (int ChN=1; ChN<StrLen; ChN++){
Bf[ChN]=(char)tolower(Bf[ChN]);}
}
void TRStr::ConvUsFromYuAscii(){
int StrLen=Len();
for (int ChN=0; ChN<StrLen; ChN++){
Bf[ChN]=TCh::GetUsFromYuAscii(Bf[ChN]);}
}
int TRStr::CmpI(const char* p, const char* r){
if (!p){return r ? (*r ? -1 : 0) : 0;}
if (!r){return (*p ? 1 : 0);}
while (*p && *r){
int i=int(toupper(*p++))-int(toupper(*r++));
if (i!=0){return i;}
}
return int(toupper(*p++))-int(toupper(*r++));
}
int TRStr::GetPrimHashCd() const {
return TStrHashF_DJB::GetPrimHashCd(Bf);
}
int TRStr::GetSecHashCd() const {
return TStrHashF_DJB::GetSecHashCd(Bf);
}
/////////////////////////////////////////////////
// String
TRStr* TStr::GetRStr(const char* CStr){
int CStrLen;
if (CStr==NULL){CStrLen=0;} else {CStrLen=int(strlen(CStr));}
if (CStrLen==0){return TRStr::GetNullRStr();}
// next lines are not multi-threading safe
//else if (CStrLen==1){return GetChStr(CStr[0]).RStr;}
//else if (CStrLen==2){return GetDChStr(CStr[0], CStr[1]).RStr;}
else {return new TRStr(CStr);}
}
void TStr::Optimize(){
char* CStr=RStr->CStr(); int CStrLen=int(strlen(CStr));
TRStr* NewRStr;
if (CStrLen==0){NewRStr=TRStr::GetNullRStr();}
// next lines are not multi-threading safe
//else if (CStrLen==1){NewRStr=GetChStr(CStr[0]).RStr;}
//else if (CStrLen==2){NewRStr=GetDChStr(CStr[0], CStr[1]).RStr;}
else {NewRStr=RStr;}
NewRStr->MkRef(); RStr->UnRef(); RStr=NewRStr;
}
void TStr::LoadXml(const PXmlTok& XmlTok, const TStr& Nm){
XLoadHd(Nm);
TStr TokStr=XmlTok->GetTokStr(false);
operator=(TokStr);
}
void TStr::SaveXml(TSOut& SOut, const TStr& Nm) const {
TStr XmlStr=TXmlLx::GetXmlStrFromPlainStr(*this);
if (XmlStr.Empty()){XSaveBETag(Nm);}
else {XSaveHd(Nm); SOut.PutStr(XmlStr);}
}
TStr& TStr::ToUc(){
TRStr* NewRStr=new TRStr(RStr->CStr()); NewRStr->ToUc();
RStr->UnRef(); RStr=NewRStr; RStr->MkRef();
Optimize(); return *this;
}
TStr& TStr::ToLc(){
TRStr* NewRStr=new TRStr(RStr->CStr()); NewRStr->ToLc();
RStr->UnRef(); RStr=NewRStr; RStr->MkRef();
Optimize(); return *this;
}
TStr& TStr::ToCap(){
TRStr* NewRStr=new TRStr(RStr->CStr()); NewRStr->ToCap();
RStr->UnRef(); RStr=NewRStr; RStr->MkRef();
Optimize(); return *this;
}
TStr& TStr::ToTrunc(){
int ThisLen=Len(); char* ThisBf=CStr();
int BChN=0; int EChN=ThisLen-1;
while ((BChN<ThisLen)&&TCh::IsWs(ThisBf[BChN])){BChN++;}
while ((EChN>=0)&&TCh::IsWs(ThisBf[EChN])){EChN--;}
*this=GetSubStr(BChN, EChN);
return *this;
}
TStr& TStr::ConvUsFromYuAscii(){
TRStr* NewRStr=new TRStr(RStr->CStr()); NewRStr->ConvUsFromYuAscii();
RStr->UnRef(); RStr=NewRStr; RStr->MkRef();
Optimize(); return *this;
}
TStr& TStr::ToHex(){
TChA ChA;
int StrLen=Len();
for (int ChN=0; ChN<StrLen; ChN++){
uchar Ch=uchar(RStr->Bf[ChN]);
char MshCh=TCh::GetHexCh((Ch/16)%16);
char LshCh=TCh::GetHexCh(Ch%16);
ChA+=MshCh; ChA+=LshCh;
}
*this=ChA;
return *this;
}
TStr& TStr::FromHex(){
int StrLen=Len(); IAssert(StrLen%2==0);
TChA ChA; int ChN=0;
while (ChN<StrLen){
char MshCh=RStr->Bf[ChN]; ChN++;
char LshCh=RStr->Bf[ChN]; ChN++;
uchar Ch=uchar(TCh::GetHex(MshCh)*16+TCh::GetHex(LshCh));
ChA+=Ch;
}
*this=ChA;
return *this;
}
TStr TStr::GetSubStr(const int& _BChN, const int& _EChN) const {
int StrLen=Len();
int BChN=TInt::GetMx(_BChN, 0);
int EChN=TInt::GetMn(_EChN, StrLen-1);
int Chs=EChN-BChN+1;
if (Chs<=0){return TStr();}
else if (Chs==StrLen){return *this;}
else {
char* Bf=new char[Chs+1]; strncpy(Bf, CStr()+BChN, Chs); Bf[Chs]=0;
TStr Str(Bf); delete[] Bf;
return Str;
}
}
void TStr::InsStr(const int& BChN, const TStr& Str){
int ThisLen=Len();
IAssert((0<=BChN)&&(BChN<=ThisLen));
TStr NewStr;
if (BChN==0){
NewStr=Str+*this;
} else
if (BChN==ThisLen){
NewStr=*this+Str;
} else {
NewStr=GetSubStr(0, BChN-1)+Str+GetSubStr(BChN, ThisLen-1);
}
*this=NewStr;
}
void TStr::DelChAll(const char& Ch){
TChA ChA(*this);
int ChN=ChA.SearchCh(Ch);
while (ChN!=-1){
ChA.Del(ChN);
ChN=ChA.SearchCh(Ch);
}
*this=ChA;
}
void TStr::DelSubStr(const int& _BChN, const int& _EChN){
int BChN=TInt::GetMx(_BChN, 0);
int EChN=TInt::GetMn(_EChN, Len()-1);
int Chs=Len()-(EChN-BChN+1);
if (Chs==0){Clr();}
else if (Chs<Len()){
char* Bf=new char[Chs+1]; strncpy(Bf, CStr(), BChN);
strncpy(Bf+BChN, CStr()+EChN+1, Len()-EChN-1); Bf[Chs]=0;
TStr Str(Bf); delete[] Bf;
*this=Str;
}
}
bool TStr::DelStr(const TStr& Str){
int ChN=SearchStr(Str);
if (ChN==-1){
return false;
} else {
DelSubStr(ChN, ChN+Str.Len()-1);
return true;
}
}
void TStr::SplitOnCh(TStr& LStr, const char& SplitCh, TStr& RStr) const {
int ThisLen=Len(); const char* ThisBf=CStr();
int ChN=0;
while ((ChN<ThisLen)&&(ThisBf[ChN]!=SplitCh)){ChN++;}
if (ChN==ThisLen){
LStr=GetSubStr(0, ThisLen-1); RStr="";
} else {
LStr=GetSubStr(0, ChN-1); RStr=GetSubStr(ChN+1, ThisLen-1);
}
}
void TStr::SplitOnLastCh(TStr& LStr, const char& SplitCh, TStr& RStr) const {
int ThisLen=Len(); const char* ThisBf=CStr();
int ChN=Len()-1;
while ((ChN>=0)&&(ThisBf[ChN]!=SplitCh)){ChN--;}
if (ChN==-1){
LStr=""; RStr=*this;
} else
if (ChN==0){
LStr=""; RStr=GetSubStr(1, ThisLen-1);
} else {
LStr=GetSubStr(0, ChN-1); RStr=GetSubStr(ChN+1, ThisLen-1);
}
}
void TStr::SplitOnAllCh(
const char& SplitCh, TStrV& StrV, const bool& SkipEmpty) const {
StrV.Clr();
char* Bf=new char[Len()+1];
strcpy(Bf, CStr());
char* CurStrBf=Bf;
forever{
char* BfC=CurStrBf;
while ((*BfC!=0)&&(*BfC!=SplitCh)){BfC++;}
bool IsEnd=(*BfC=='\0');
*BfC=0;
if ((BfC>CurStrBf)||(!SkipEmpty)){StrV.Add(TStr(CurStrBf));}
if (IsEnd){break;}
CurStrBf=BfC+1;
}
delete[] Bf;
}
void TStr::SplitOnAllAnyCh(
const TStr& SplitChStr, TStrV& StrV, const bool& SkipEmpty) const {
// reset string output-vector
StrV.Clr();
// prepare working-copy of string
char* Bf=new char[Len()+1];
strcpy(Bf, CStr());
char* CurStrBf=Bf; // pointer to current string
// prepare pointer to split-char-string
const char* SplitChBf=SplitChStr.CStr();
forever{
char* BfC=CurStrBf; // set the counter for working-string
while (*BfC!=0){
const char* SplitChBfC=SplitChBf; // set counter for split-char-string
while ((*SplitChBfC!=0)&&(*SplitChBfC!=*BfC)){SplitChBfC++;}
if (*SplitChBfC!=0){break;} // if split-char found
BfC++;
}
bool IsEnd=(*BfC==0);
*BfC=0;
if ((BfC>CurStrBf)||(!SkipEmpty)){StrV.Add(TStr(CurStrBf));}
if (IsEnd){break;}
CurStrBf=BfC+1;
}
// delete working-copy
delete[] Bf;
}
void TStr::SplitOnWs(TStrV& StrV) const {
StrV.Clr();
char* Bf=new char[Len()+1];
strcpy(Bf, CStr());
char* StrBf=Bf;
forever{
while ((*StrBf!=0)&&(isspace(*StrBf))){StrBf++;}
char* BfC=StrBf;
while ((*BfC!=0)&&(!isspace(*BfC))){BfC++;}
bool IsEnd=(*BfC=='\0');
*BfC=0;
if (BfC>StrBf){StrV.Add(TStr(StrBf));}
if (IsEnd){break;}
StrBf=BfC+1;
}
delete[] Bf;
}
void TStr::SplitOnNonAlNum(TStrV& StrV) const {
StrV.Clr();
char* Bf=new char[Len()+1];
strcpy(Bf, CStr());
char* StrBf=Bf;
forever{
while ((*StrBf!=0)&&(!TCh::IsAlNum(*StrBf))){StrBf++;}
char* BfC=StrBf;
while ((*BfC!=0)&&(TCh::IsAlNum(*BfC))){BfC++;}
bool IsEnd=(*BfC=='\0');
*BfC=0;
if (BfC>StrBf){StrV.Add(TStr(StrBf));}
if (IsEnd){break;}
StrBf=BfC+1;
}
delete[] Bf;
}
void TStr::SplitOnStr(const TStr& SplitStr, TStrV& StrV) const {
StrV.Clr();
int SplitStrLen=SplitStr.Len();
int PrevChN=0; int ChN=0;
while ((ChN=SearchStr(SplitStr, ChN))!=-1){
// extract & add string
TStr SubStr=GetSubStr(PrevChN, ChN-1);
StrV.Add(SubStr);
PrevChN=ChN=ChN+SplitStrLen;
}
// add last string
TStr LastSubStr=GetSubStr(PrevChN, Len()-1);
StrV.Add(LastSubStr);
}
void TStr::SplitOnStr(TStr& LeftStr, const TStr& MidStr, TStr& RightStr) const {
const int ChN=SearchStr(MidStr);
if (ChN==-1){
LeftStr=*this; RightStr=GetNullStr();
} else {
LeftStr=GetSubStr(0, ChN-1);
RightStr=GetSubStr(ChN+MidStr.Len(), Len()-1);
}
}
int TStr::CountCh(const char& Ch, const int& BChN) const {
const int ThisLen=Len();
const char* ThisBf=CStr();
int Chs=0;
for (int ChN=TInt::GetMx(BChN, 0); ChN<ThisLen; ChN++){
if (ThisBf[ChN]==Ch){Chs++;}
}
return Chs;
}
int TStr::SearchCh(const char& Ch, const int& BChN) const {
int ThisLen=Len(); const char* ThisBf=CStr();
int ChN=TInt::GetMx(BChN, 0);
while (ChN<ThisLen){
if (ThisBf[ChN]==Ch){return ChN;}
ChN++;
}
return -1;
}
int TStr::SearchChBack(const char& Ch, int BChN) const {
const int StrLen=Len();
if (BChN==-1||BChN>=StrLen){BChN=StrLen-1;}
const char* ThisBf=CStr();
const char* Pt=ThisBf + BChN;
while (Pt>=ThisBf) {
if (*Pt==Ch){return (int)(Pt-ThisBf);}
Pt--;
}
return -1;