-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinterface.cpp
More file actions
1969 lines (1493 loc) · 46.2 KB
/
interface.cpp
File metadata and controls
1969 lines (1493 loc) · 46.2 KB
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
/*
This is interface.cpp
Coxeter version 3.0 Copyright (C) 2002 Fokko du Cloux
See file main.cpp for full copyright notice
*/
#include "interface.h"
#include <ctype.h>
#include "bits.h"
#include "error.h"
namespace interface {
using namespace error;
};
/******** local declarations *************************************************/
namespace {
using namespace interface;
const char *alphabet = "abcdefghijklmnopqrstuvwxyz";
const char *affine = "abcdefg";
const Token not_token = RANK_MAX+1;
const Token prefix_token = RANK_MAX+2;
const Token postfix_token = RANK_MAX+3;
const Token separator_token = RANK_MAX+4;
const Token begingroup_token = RANK_MAX+5;
const Token endgroup_token = RANK_MAX+6;
const Token longest_token = RANK_MAX+7;
const Token inverse_token = RANK_MAX+8;
const Token power_token = RANK_MAX+9;
const Token contextnbr_token = RANK_MAX+10;
const Token densearray_token = RANK_MAX+11;
const unsigned prefix_bit = 0;
const unsigned postfix_bit = 1;
const unsigned separator_bit = 2;
};
namespace {
using namespace interface;
void makeSymbols(List<String>& list, const String* const symbol, Ulong n);
CoxNbr toCoxNbr(char c);
Automaton *tokenAutomaton(LFlags f);
Automaton *tokenAut0();
Automaton *tokenAut1();
Automaton *tokenAut2();
Automaton *tokenAut3();
Automaton *tokenAut4();
Automaton *tokenAut5();
Automaton *tokenAut6();
Automaton *tokenAut7();
};
/****************************************************************************
This module defines the interface class, which takes care of input/output
of elements in the group.
We are careful to distinguish between external and internal representation
of our elements. The internal representation is always the same : the
default one is as reduced coxwords (arrays for finite groups, numbers for
small groups.) We do not in fact insist that our reduced word be normalized;
so equality will be a relatively expensive test.
Externally, the user has two levels of flexibility. First of all, to each
integer in {1,...,n}, where n is the rank, there is associated a symbol
(in fact two symbols, an input symbol --- some non-empty string --- and
an output symbol, an arbitrary string). The default is input = output =
the decimal representation of the number. Furthermore, there are three
arbitrary strings : prefix, postfix and separator, also with
an input and an output version. Default is prefix and postfix empty,
separator empty if the rank is <= 9, "." otherwise.
The second level is the possibility to define an arbitrary ordering on
the generators. Each group has a "standard" ordering, built-in in the
case of finite or affine groups, and implicitly defined by the datum
of the Coxeter matrix for general groups. However, the user may change
this ordering if he wishes.
This setup is easy to implement, and gives more than enough flexibility
to read from and write to programs like Gap, Magma or Maple, or even TeX,
and to perform some other nifty output tricks (outputting a k-l basis
element in Gap format, say, is a breeze.) Also the program can read from
one program and write to another, functioning as a pipe.
****************************************************************************/
/****************************************************************************
Chapter I -- The Interface class.
This class regroups the facilities through which a CoxGroup communicates
with the outside world.
The following functions are defined :
constructors :
- Interface(x,l) : constructs the standard interface in type x and rank l;
- ~Interface() : (not implemented yet);
manipulators :
- readSymbols() : updates the symbol tree for the group;
- setAutomaton() : resets the value of the automaton;
- setIn(i) : resets the input interface to i;
- setInPostfix(a) : changes the input postfix; (inlined)
- setInPrefix(a) : changes the input prefix; (inlined)
- setInSeparator(a) : changes the input separator; (inlined)
- setInSymbol(s,a) : changes the input symbol; (inlined)
- setOrder(gen_order) : changes the perceived ordering of the generators;
- setOut(i) : resets the output interface to i;
- setOutPrefix(a) : changes the output prefix; (inlined)
- setOutPostfix(a) : changes the output postfix; (inlined)
- setOutSeparator(a) : changes the output separator; (inlined)
- setOutSymbol(s,a) : changes the output symbol; (inlined)
accessors :
- in(s) : returns the internal number of the generator placed in s-th
position by the user (inlined);
- out(s) : returns the user-number of the s-th internal generator (inlined);
- outOrder() : returns the array [out(s), 0 <= s < rank] (inlined);
- rank() : returns the rank (inlined);
input/output functions :
- parse(T,g,line,r,x) : parses a CoxWord from line;
- print(file,g) : prints the coxword g;
- print(file,f) : prints a list of the generators flagged by f;
****************************************************************************/
namespace interface {
Interface::Interface(const Type& x, const Rank& l)
:d_order(l),
d_beginGroup("("),
d_endGroup(")"),
d_longest("*"),
d_inverse("!"),
d_power("^"),
d_contextNbr("%"),
d_denseArray("#"),
d_parseEscape("?"),
d_reserved(0),
d_rank(l)
/*
Constructs the default interface (see the introduction.)
*/
{
d_order = identityOrder(l);
d_in = new GroupEltInterface(l);
d_out = new GroupEltInterface(l);
d_descent = new DescentSetInterface;
insert(d_reserved,d_beginGroup);
insert(d_reserved,d_endGroup);
insert(d_reserved,d_longest);
insert(d_reserved,d_inverse);
insert(d_reserved,d_power);
insert(d_reserved,d_contextNbr);
insert(d_reserved,d_denseArray);
insert(d_reserved,d_parseEscape);
readSymbols();
setAutomaton();
}
Interface::~Interface()
/*
We just have to delete d_in and d_out.
*/
{
delete d_out;
delete d_in;
}
/******** manipulators ******************************************************/
void Interface::readSymbols()
/*
Reads the input symbols into the symbol tree. This should be used each time
the input symbols are reset. The safest way to avoid running into trouble
is simply building the tree again. It is important that no meaningless
symbols remain on the tree.
(an alternative would have been to mark them as meaningless, through a
special token.)
*/
{
d_symbolTree.~TokenTree();
new(&d_symbolTree) TokenTree;
if (inPrefix().length())
d_symbolTree.insert(inPrefix(),prefix_token);
if (inSeparator().length())
d_symbolTree.insert(inSeparator(),separator_token);
if (inPostfix().length())
d_symbolTree.insert(inPostfix(),postfix_token);
for (Generator s = 0; s < rank(); ++s) {
d_symbolTree.insert(inSymbol(s),s+1);
}
d_symbolTree.insert(d_beginGroup,begingroup_token);
d_symbolTree.insert(d_endGroup,endgroup_token);
d_symbolTree.insert(d_longest,longest_token);
d_symbolTree.insert(d_inverse,inverse_token);
d_symbolTree.insert(d_power,power_token);
d_symbolTree.insert(d_contextNbr,contextnbr_token);
d_symbolTree.insert(d_denseArray,densearray_token);
return;
}
void Interface::setAutomaton()
{
LFlags f = 0;
using constants::lmask;
if (d_in->prefix.length())
f |= lmask[prefix_bit];
if (d_in->postfix.length())
f |= lmask[postfix_bit];
if (d_in->separator.length())
f |= lmask[separator_bit];
d_tokenAut = tokenAutomaton(f);
return;
}
void Interface::setDescent(Default)
/*
Resets the DescentSetInterface to the default parameters.
*/
{
new(d_descent) DescentSetInterface();
return;
}
void Interface::setDescent(GAP)
/*
Resets the DescentSetInterface to GAP parameters.
*/
{
new(d_descent) DescentSetInterface(GAP());
return;
}
void Interface::setIn(const GroupEltInterface& i)
/*
Resets d_in to i.
*/
{
delete d_in;
d_in = new GroupEltInterface(i);
readSymbols();
setAutomaton();
return;
}
void Interface::setOrder(const Permutation& order)
/*
Resets the numbering of the generators. The given ordering is the
ordering of our generators as the user wants them. What we record
in d_order is rather the inverse permutation : d_order[i] is the
number in the user's order of our generator i.
*/
{
for (Generator s = 0; s < rank(); ++s) {
d_order[order[s]] = s;
}
return;
}
void Interface::setOut(const GroupEltInterface& i)
/* Resets d_out to i */
{
delete d_out;
d_out = new GroupEltInterface(i);
return;
}
/******** input-output *******************************************************/
bool Interface::parseCoxWord(ParseInterface& P, const MinTable& T) const
/*
This function parses a CoxWord from the line, starting at position r, and
increments the CoxWord g with it. The syntax for a group element is as
follows :
prefix [generator [separator generator]*] postfix
Here prefix, postfix, and separator, and the symbols representing the
generators, are a priori arbitrary strings without whitespace, subject
only to the condition that they be distinct. Tokens are taken from the
input string, after skipping over leading whitespaces (so tokens could
actually contain embedded whitespace, but no leading whitespace.)
The symbols have been put on a tree, which makes it easy to read them
and to take care of the (very real!) possibility of embedded symbols.
The tokens are then processed by a little finite state automaton which
checks syntactical correctness; in case of failure, the error PARSE_ERROR
is set. The return value is the number of characters successfully read,
enabling the caller to position the cursor just after the whitespace
following the last token successfully read.
An additional complication comes from the fact that we wish to allow
the strings prefix, postfix and separator to be empty --- so in fact
there are eight variants of the syntactical automaton (this seemed
easier to do than to try to cover all cases at once.) The appropriate
automaton is loaded when the interface is created, or modified.
*/
{
Token tok = 0;
while (Ulong p = getToken(P,tok)) {
Letter tok_type = tokenType(tok);
if (tok_type > separator_type) /* end of coxword */
break;
State y = d_tokenAut->act(P.x,tok_type);
if (d_tokenAut->isFailure(y)) /* end of coxword */
break;
P.x = y;
if (tok_type == generator_type) {
Generator s = tok-1;
T.prod(P.c,s);
}
P.offset += p;
}
if (d_tokenAut->isAccept(P.x)) /* correct input */
P.x = 0;
else /* incomplete input */
ERRNO = PARSE_ERROR;
return true;
}
bool Interface::readCoxElt(ParseInterface& P) const
/*
This function attempts to read a Coxeter element from P. It does not
have to worry about word reduction because all the generators read have
to be distinct.
NOTE : it is assumed that the rank is at most MEDRANK_MAX.
*/
{
Token tok = 0;
LFlags f = 0;
// in case this is a second attempt after an incomplete read, make
// f hold the part already read
for (Ulong j = 0; j < P.c.length(); ++j)
f |= lmask[P.c[j]-1];
// read new part
while (Ulong p = getToken(P,tok)) {
Letter tok_type = tokenType(tok);
if (tok_type > separator_type) /* end of coxword */
break;
State y = d_tokenAut->act(P.x,tok_type);
if (d_tokenAut->isFailure(y)) /* end of coxword */
break;
P.x = y;
if (tok_type == generator_type) {
if (f & lmask[tok-1]) { // generator already appeared
ERRNO = NOT_COXELT;
return true;
}
f |= lmask[tok-1];
P.c.append(tok);
}
P.offset += p;
}
if (d_tokenAut->isAccept(P.x)) { /* input is subword of coxelt */
if ((f != 0) && (f != leqmask[rank()-1]))
ERRNO = NOT_COXELT;
else
P.x = 0;
}
else { /* incomplete input */
ERRNO = PARSE_ERROR;
}
return true;
}
};
/****************************************************************************
Chapter II --- The DescentSetInterface class.
This is the part of the interface used to write out descent sets. The
meaning of the fields is as follows :
- prefix : opening string;
- postfix : closing string;
- separator : separation between two entries;
- twosidedPrefix : opening string for two-sided descent sets;
- twosidedPostfix : closing string for two-sided descent sets;
- twosidedSeparator : used to separate the left from the right part
in the printout of two-sided descent sets.
The symbols used for the generators are the ones from the current output
GroupEltInterface.
The following functions are defined :
- constructors and destructors :
- DescentSetInterface() : the default constructor;
- DescentSetInterface(GAP) : constructor for GAP output;
- ~DescentSetInterface() : destructor;
- modifiers :
- setPrefix(str) : sets the prefix to str;
- setPostfix(str) : sets the postfix to str;
- setSeparator(str) : sets the separator to str;
- setTwosidedSeparator(str) : sets the 2-sided separator to str;
*****************************************************************************/
namespace interface {
DescentSetInterface::DescentSetInterface()
:prefix("{"),postfix("}"),separator(","),twosidedPrefix("{"),
twosidedPostfix("}"),twosidedSeparator(";")
/*
Sets the default values for the interface.
*/
{}
DescentSetInterface::DescentSetInterface(GAP)
:prefix("["),postfix("]"),separator(","),twosidedPrefix("[["),
twosidedPostfix("]]"),twosidedSeparator("],[")
{}
DescentSetInterface::~DescentSetInterface()
{}
void DescentSetInterface::setPrefix(const String& str)
{
prefix = str;
return;
}
void DescentSetInterface::setPostfix(const String& str)
{
postfix = str;
return;
}
void DescentSetInterface::setSeparator(const String& str)
{
separator = str;
return;
}
void DescentSetInterface::setTwosidedPrefix(const String& str)
{
twosidedPrefix = str;
return;
}
void DescentSetInterface::setTwosidedPostfix(const String& str)
{
twosidedPostfix = str;
return;
}
void DescentSetInterface::setTwosidedSeparator(const String& str)
{
twosidedSeparator = str;
return;
}
};
/****************************************************************************
Chapter III --- The GroupEltInterface class.
This is the part of the interface used to read and write group elements in
word form. The following functions are defined :
- constructors and destructors :
- GroupEltInterface();
- GroupEltInterface(l);
- GroupEltInterface(l,Gap);
- ~GroupEltInterface();
- accessors :
- print(file);
- manipulators :
- setPostfix(a) : sets the postfix to a;
- setPrefix(a) : sets the prefix to a;
- setSeparator(a) : sets the separator to a;
- setSymbol(s,a) : sets symbol # s to a;
****************************************************************************/
namespace interface {
GroupEltInterface::GroupEltInterface()
:symbol(0),prefix(String::undefined()),postfix(String::undefined()),
separator(String::undefined())
/*
We use the default constructor to construct an interface where the symbol
table is empty, and the prefix,postfix and separator strings are all
undefined. It is not intended to be used other than in very special cases.
NOTE : This is pretty dangerous, and should be much more hidden!
*/
{}
GroupEltInterface::GroupEltInterface(const Rank& l)
:symbol(l),prefix(0),postfix(0),separator(0)
/*
Constructs the default interface in rank l.
*/
{
symbol.setSize(l);
makeSymbols(symbol,decimalSymbols(l),l);
if (l > 9) { /* need separators */
new(&separator) String(".");
}
}
GroupEltInterface::GroupEltInterface(const Rank& l, Alphabetic)
:symbol(l),prefix(""),postfix(""),separator("")
/*
Constructs the GAP interface in rank l. This represents Coxeter words
as lists, with decimal symbols : for instance, the element 12321 in
rank 3 would be represented as [1,2,3,2,1].
*/
{
symbol.setSize(l);
makeSymbols(symbol,alphabeticSymbols(l),l);
if (l > 26)
separator = ".";
}
GroupEltInterface::GroupEltInterface(const Rank& l, Decimal)
:symbol(l),prefix(""),postfix(""),separator("")
/*
Constructs the GAP interface in rank l. This represents Coxeter words
as lists, with decimal symbols : for instance, the element 12321 in
rank 3 would be represented as [1,2,3,2,1].
*/
{
symbol.setSize(l);
makeSymbols(symbol,decimalSymbols(l),l);
if (l > 9)
separator = ".";
}
GroupEltInterface::GroupEltInterface(const Rank& l, GAP)
:symbol(l),prefix("["),postfix("]"),separator(",")
/*
Constructs the GAP interface in rank l. This represents Coxeter words
as lists, with decimal symbols : for instance, the element 12321 in
rank 3 would be represented as [1,2,3,2,1].
*/
{
symbol.setSize(l);
makeSymbols(symbol,decimalSymbols(l),l);
}
GroupEltInterface::GroupEltInterface(const Rank& l, Hexadecimal)
:symbol(l),prefix(""),postfix(""),separator("")
/*
Constructs the hexadecimal interface in rank l. This represents Coxeter
words as strings of hex digits if the rank is <= 15, dot-separated hex
numbers otherwise. The symbol 0 is not used.
*/
{
symbol.setSize(l);
makeSymbols(symbol,hexSymbols(l),l);
if (l > 15)
separator = ".";
}
GroupEltInterface::GroupEltInterface(const Rank& l, HexadecimalFromZero)
:symbol(l),prefix(""),postfix(""),separator("")
/*
Constructs the hexadecimal interface in rank l. This represents Coxeter
words as strings of hex digits if the rank is <= 16, dot-separated hex
numbers otherwise. The symbol 0 is used for the first generator.
*/
{
symbol.setSize(l);
makeSymbols(symbol,hexSymbolsFromZero(l),l);
if (l > 16)
separator = ".";
}
GroupEltInterface::~GroupEltInterface()
/*
Automatic destruction is enough.
*/
{}
/******** accessors *********************************************************/
void GroupEltInterface::print(FILE* file) const
{
fprintf(file,"prefix: ");
io::print(file,prefix);
fprintf(file,"\n");
fprintf(file,"separator: ");
io::print(file,separator);
fprintf(file,"\n");
fprintf(file,"postfix: ");
io::print(file,postfix);
fprintf(file,"\n");
for (Generator s = 0; s < symbol.size(); ++s) {
fprintf(file,"symbol #%d: ",s+1);
io::print(file,symbol[s]);
fprintf(file,"\n");
}
return;
}
/******** manipulators ******************************************************/
void GroupEltInterface::setPostfix(const String& a)
{
postfix = a;
return;
}
void GroupEltInterface::setPrefix(const String& a)
{
prefix = a;
return;
}
void GroupEltInterface::setSeparator(const String& a)
{
separator = a;
return;
}
void GroupEltInterface::setSymbol(const Generator& s, const String& a)
/*
Sets the symbol for generator s to a. Remember that the relation between
symbols and numbers is not affected by the ordering of the generators; if
the user changes the ordering, so that a generator previously numbered i is
now numbered j, that generator will be represented by symbol j instead of
symbol i; this is the expected behaviour, I think.
*/
{
symbol[s] = a;
return;
}
};
/****************************************************************************
Chapter IV -- The ReservedSymbol class.
This is just a little structure to hold the symbols reserved for some
basic operations. These cannot be used in the input-interface for group
elements.
Their redefinition should be possible, but is not implemented currently.
NOTE : I'm having a little trouble using this --- not used currently.
****************************************************************************/
namespace interface {
ReservedSymbols::ReservedSymbols()
:beginGroup(0),endGroup(0),longest(0),inverse(0),power(0),contextnbr(0),
densearray(0)
{}
ReservedSymbols::ReservedSymbols(Default)
:beginGroup("("),endGroup(")"),longest("*"),inverse("!"),power("^"),
contextnbr("%"),densearray("#")
{}
ReservedSymbols::~ReservedSymbols()
{}
};
/****************************************************************************
Chapter V -- Standard interfaces.
This section defines some functions which facilitate the definiton of
standard interfaces. The following functions are defined :
- gapInput(I) : sets the input to GAP input (not implemented yet);
- gapOutput(I) : sets the output to GAP output (not implemented yet);
( ... maple ? magma ? mathematica ?)
- decimalSymbols() : returns a pointer to the list of decimal integers;
- hexSymbols() : returns a pointer to the list of hexadecimal integers;
- hexSymbolsFromZero() : returns a pointer to the list of hexadecimal
integers, starting from zero;
- twohexSymbols() : returns a pointer to the list of two-digit hex symbols;
- alphabeticSymbols() : returns a pointer to the list of alphabetic symbols;
****************************************************************************/
namespace interface {
const String* alphabeticSymbols(Ulong n)
/*
Produces an alphabetic representation of the numbers in {1,...,n}.
In fact, the numbers from 0 are represented by strings of the form
"", "a", ... , "z", "aa", ... , "az", "ba", ... through the following
algorithm : for n > 0 the representation of n is the concatenation
of rep((n-1)/b) with the letter (n-1)%b, where b is the base, i.e.,
the number of letters in the alphabet. In our case the role of
n-1 is actually played by n.
*/
{
static List<String> list(0);
static bool first = true;
if (first) {
first = false;
list.setSize(1);
new(list.ptr()) String("");
}
if (n+1 > list.size()) { /* enlarge the list */
Ulong prev = list.size()-1;
list.setSize(n+1);
for (Ulong j = prev; j < n; ++j) { /* write symbol */
list[j+1].assign(list[j/26]);
append(list[j+1],alphabet[j%26]);
}
}
return list.ptr()+1;
}
const String* decimalSymbols(Ulong n)
/*
Returns a pointer to a list of strings, the first n of which contain
the decimal string representations of the first n natural numbers.
*/
{
static List<String> list(0);
if (n > list.size()) { /* enlarge the list */
Ulong prev_size = list.size();
list.setSize(n);
for (Ulong j = prev_size; j < n; ++j) { /* write symbol */
list[j].setLength(io::digits(j+1,10));
sprintf(list[j].ptr(),"%lu",j+1);
}
}
return list.ptr();
}
const String* hexSymbolsFromZero(Ulong n)
/*
Returns a pointer to a list of strings, the first n of which contain
the hexadecimal string representations of the first n integers,
including zero.
*/
{
static List<String> list;
if (n > list.size()) { /* enlarge the list */
Ulong prev_size = list.size();
list.setSize(n);
for (Ulong j = prev_size; j < n; ++j) { /* write symbol */
list[j].setLength(io::digits(j,16));
sprintf(list[j].ptr(),"%lx",j);
}
}
return list.ptr();
}
const String* hexSymbols(Ulong n)
/*
Returns a pointer to a list of strings, the first n of which contain
the hexadecimal string representations of the first n natural numbers.
*/
{
static List<String> list;
if (n > list.size()) { /* enlarge the list */
Ulong prev_size = list.size();
list.setSize(n);
for (Ulong j = prev_size; j < n; ++j) { /* write symbol */
list[j].setLength(io::digits(j+1,16));
sprintf(list[j].ptr(),"%lx",j+1);
}
}
return list.ptr();
}
const String* twohexSymbols(Ulong n)
/*
Returns a pointer to a list of strings, the first n of which contain
the hexadecimal string representations of the first n natural numbers
--- here we impose the condition that the width is at least two, with
0 as a padding character. For the range of possible ranks, this leads
to constant-width symbols.
*/
{
static List<String> list;
if (n > list.size()) { /* enlarge the list */
Ulong prev_size = list.size();
list.setSize(n);
for (Ulong j = prev_size; j < n; ++j) { /* write symbol */
list[j].setLength(2*io::digits(j+1,256));
sprintf(list[j].ptr(),"%0*lx",2*io::digits(j+1,256),j+1);
}
}
return list.ptr();
}
const Permutation& identityOrder(Ulong n)
{
static Permutation list(0);
static Ulong valid_range = 0;
if (n > valid_range) { /* enlarge the list */
Ulong prev_size = valid_range;
list.setSize(n);
for (Ulong j = prev_size; j < n; ++j)
list[j] = j;
valid_range = n;
}
list.setSize(n);
return list;
}
};
/****************************************************************************
Chapter VI -- The ParseInterface class.
This class provides a convenient interface for the delicate operation of
parsing. The point is that we wish to be able to parse interactively, and
in particular give the user a chance to correct his mistakes; therefore
the "state" of the parsing has to be recorded somewhere.
The following functions are defined :
- ParseInterface();
- ~ParseInterface();
- reset();
****************************************************************************/
namespace interface {
ParseInterface::ParseInterface()
:str(0),nestlevel(0),a(1),c(0),x(0)
{
a.setSize(1);
a[0].reset();
}
ParseInterface::~ParseInterface()
/*
Automatic destruction is enough.
*/
{}
void ParseInterface::reset()
{
str.setLength(0);
nestlevel = 0;
a.setSize(1);
a[0].reset();
c.reset();
x = 0;
offset = 0;
}
};
/****************************************************************************
Chapter VII -- The TokenTree class.
This class is an auxiliary for the parsing of input, which we found a
non-trivial business! Probably this could be done in terms of the
Dictionary template.