-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0003-Basic-Data-Structures.st
More file actions
1336 lines (1223 loc) · 39.1 KB
/
Copy path0003-Basic-Data-Structures.st
File metadata and controls
1336 lines (1223 loc) · 39.1 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
'From Smalltalk 5.5k XM November 24 on 22 November 1980 at 2:57:08 am.'
"Array"
Class new title: 'Array'
subclassof: Object
fields: ''
declare: '';
asFollows
Array is an abstract class in the sense that it has no state, and instantiation is consequently not meaningful. However it defines the default message set inherited by its subclasses, notably String, Vector, and UniqueString. Notice that subscripting is not done here, except to handle the exceptional cases such as subscripting by other types as in a◦(1 to: 3).
Reading and Writing
◦ x
[⇑x subscripts: self]
◦ x ← val
[⇑x subscripts: self ← val]
< v "for sorting vectors by first element"
[⇑(self◦1)<(v◦1)]
= arg | x
[arg isArray⇒
[self length ≠ arg length⇒ [⇑false]
for⦂ x to: self length do⦂
[(self◦x) = (arg◦x)⇒ [] ⇑false]
⇑true]
⇑false]
> v "for sorting vectors by first element"
[⇑(self◦1)>(v◦1)]
all ← val | i
[for⦂ i to: self length do⦂
[self◦i ← val]]
last
[⇑self◦self length]
last ← val
[⇑self◦self length ← val]
length [user notify: 'message not understood.']
Copying and Altering
+ arg [⇑self concat: arg]
concat: arg | x s [
x ← self species new: self length + arg length.
self copyto: (s ← x asStream).
arg copyto: s.
⇑x]
copy
[⇑self copy: 1 to: self length]
copy: a to: b
[⇑self copy: a to: b to: (self species new: b-a+1)]
copy: a to: b to: t | i s me
[s ← t asStream.
me ← Stream new of: self from: a to: b.
for⦂ i from: a to: b do⦂ "general code wont stop at false"
[s next ← me next]
⇑t]
copyto: t
[⇑self copy: 1 to: self length to: t]
delete: obj | s each
[s ← (self species new: self length) asStream.
for⦂ each from: self do⦂
[obj=each⇒[] s next← each]
⇑ s contents]
grow [⇑self growto: (4 max: self length*2)]
growby: n [⇑self growto: self length + n]
growto: n [⇑self "copyto:" copy: 1 to: self length to: (self species new: n)]
insertNonDescending: x "self is assumed to be sorted"
[⇑self insertSorted: x]
insertSorted: x | a c i "self is assumed to be sorted"
[i ← self findSorted: x.
c ← (a ← self species new: self length+1) asStream.
self◦(1 to: i) copyto: c. c next ← x. self◦(i+1 to: self length) copyto: c.
⇑a]
notNil | t i "copy self (which contains no falses) removing all nils"
[t ← (self species new: (self length-(self count: nil))) asStream.
for⦂ i from: self do⦂ [i≡nil ⇒[] t next← i].
⇑t asArray]
replace: a to: b by: s | x xs
[x ← self species new: self length+s length -(1+b-a).
xs ← x asStream.
self copy: 1 to: a-1 to: xs.
s copy: 1 to: s length to: xs.
self copy: b+1 to: self length to: xs.
⇑x]
without: index | s me i "if index in range, return self without ◦index"
[index cansubscript: self⇒
[s ← (self species new: self length-1) asStream.
me ← self asStream.
for⦂ i to: self length do⦂ [i=index⇒ [me next] s next ← me next].
⇑s asArray]]
Searching
all⦂ variable suchThat⦂ expr | s i x "a copy of some of me"
[s ← (self species new: self length) asStream.
for⦂ i to: self length do⦂
[x ← self◦i. variable value ← x.
expr eval⇒ [s next ← x]].
⇑s contents]
count: x | i n
[n←0.
for⦂ i to: self length do⦂
[x=(self◦i)⇒ [n←n+1]].
⇑n]
find⦂ x suchThat⦂ predicate | i
[for⦂ i to: self length do⦂
[x value ← self◦i. predicate eval⇒ [⇑i]].
⇑0]
find: x | i
[for⦂ i to: self length do⦂
[self◦i=x⇒ [⇑i]].
⇑0]
findnon: x | i
[for⦂ i to: self length do⦂
[self◦i≠x⇒ [⇑i]].
⇑0]
findSorted: x | lo mid hi " returns index of largest element ≤ x "
[hi ← self length+1. lo ← 1.
while⦂ lo < hi do⦂ "binary search; self must be sorted"
[self◦(mid←lo+hi/2) > x⇒[hi ← mid] lo ← mid+1].
⇑hi-1] " 0≤result≤length "
first⦂ x suchThat⦂ predicate | i
[for⦂ i to: self length do⦂
[x value ← self◦i. predicate eval⇒ [⇑self◦i]].
⇑false]
has: x [⇑(self find: x)≠0]
Permutation
permutationToSort
["Return a Vector, permutation, such that self◦permutation is sorted nondescending. Do not alter self."
⇑((self◦((1 to: self length) copy)) sort: 1 to: self length) map.]
promote: t | n
[n ← self find: t. n=0⇒ []
self◦(n to: 2 by: ¬1) ← self◦(n-1 to: 1 by: ¬1).
self◦1 ← t]
reverse
[⇑Substring new data: self map: (self length to: 1 by: ¬1)]
sort
["Permute my elements so they are sorted nondescending. Note: if I am a substring, only my map will be permuted. In certain situations, this may not be what you expect."
self sort: 1 to: self length.]
sort: i to: j | di dij dj tt ij k l n
["Sort elements i through j of self to be nondescending."
"The prefix d means the data at."
(n←j+1-i)≤1⇒ ["Nothing to sort."]
"Sort di,dj."
di ← self◦i. dj ← self◦j.
[di>dj⇒ [self swap: i with: j. tt←di. di←dj. dj←tt]].
n=2⇒ ["They are the only two elements."]
ij ← (i+j) lshift: ¬1. "ij is the midpoint of i and j."
"Sort di,dij,dj. Make dij be their median."
dij ← self◦ij.
[di>dij⇒ [self swap: i with: ij. dij←di] dj<dij⇒ [self swap: j with: ij. dij←dj]].
n=3⇒ ["They are the only three elements."]
"Find k>i and l<j such that dk,dij,dl are in reverse order. Swap k and l. Repeat this procedure until j and k pass each other."
k ← i. l ← j.
while⦂
[
while⦂ self◦(l←l-1) > dij do⦂ [].
while⦂ self◦(k←k+1) < dij do⦂ [].
k≤l
]
do⦂
[self swap: k with: l].
"Now l<k (either 1 or 2 less), and di through dl are all less than dk through dj. Sort those two segments."
self sort: i to: l.
self sort: k to: j.]
swap: i with: j | t
[t ← self◦i. self◦i ← self◦j. self◦j ← t]
Conversion
asSet [⇑Set new of: self to: self length]
asStream
[⇑Stream new of: self]
frequencies | d x "return a sorted vector ((freq item) (freq item) ...)"
[d ← Dictionary new init: 64.
for⦂ x from: self do⦂
[d tally: x].
⇑d asInvertedVector sort]
sum [⇑self sumTo: 0]
sumTo: subTotal | x "add all my elements to this subTotal (usually 0 or 0.0)"
[for⦂ x from: self do⦂
[subTotal← subTotal+x].
⇑subTotal]
transform⦂ each to⦂ expr | s i "a copy of me with each element transformed"
[s ← (self species new: self length) asStream.
for⦂ i to: self length do⦂
[each value ← self◦i. s next ← expr eval].
⇑s asArray]
viewer [⇑SetReader new of: self]
Mapping
cansubscript: a | i
[for⦂ i from: self do⦂ [i cansubscript: a⇒ [] ⇑false]]
subscripts: x "subarrays"
[⇑Substring new data: x map: self]
subscripts: x ← val "subrange replacement"
[self length≠val length⇒
[user notify: 'lengths not commensurate']
val copyto: (Substring new data: x map: self).
⇑val]
Compatibility
isArray
isIntervalBy1
[⇑false]
species
[⇑Vector]
Comparing
hash "make sure = arrays hash =ly"
[self length=0⇒[⇑17171]
⇑(self◦1) hash + (self◦self length) hash]
SystemOrganization classify: ↪Array under: 'Basic Data Structures'.
"FieldReference"
Class new title: 'FieldReference'
subclassof: Object
fields: 'object offset'
declare: '';
asFollows
I reference a field of an instance
Initialization
object: object offset: offset
Indirection
eval
[⇑object instfield: offset]
value [⇑object instfield: offset]
value ← value
[object instfield: offset ← value. ⇑value]
SystemOrganization classify: ↪FieldReference under: 'Basic Data Structures'.
"Interval"
Class new title: 'Interval'
subclassof: Array
fields: 'start stop step length'
declare: '';
asFollows
I am an arithmetic progression from start in steps of step, not exceeding stop
Initialization
from: start to: stop by: step
[length ← 1+(stop-start/step).
step<0⇒[start<stop⇒[length← 0]]
stop<start⇒[length← 0]
]
Reading and Writing
◦ x
[x is: Integer⇒[x<1⇒ [⇑nil]
x>length⇒ [⇑nil]
⇑start+(step*(x-1))]
⇑super◦x]
◦ x ← val
[user notify: 'Intervals are not for writing into']
length [⇑length]
start [⇑start]
stop [⇑stop]
Compatibility
= int [⇑start = int start and⦂ (stop = int stop and⦂ length = int length)]
cansubscript: a
[⇑length≤0 or⦂ ((start cansubscript: a) and⦂ (length-1*step+start cansubscript: a))]
hash [⇑(((start lshift: 2) lxor: stop) lshift: 1) lxor: length]
isIntervalBy1
[⇑step=1]
Random Numbers
random "See Lehmers linear congruential method, Knuth Vol. 1:
modulus m=2^16
a=27181 odd, and 5 = a mod 8
c=13849 odd, and c/m around 0.21132"
[step← (13849 + (27181*step)) asSmall.
⇑(start + ((length asFloat*(32768.0+step))/65536.0)) asSmall]
randomInit [self randomInit: mem◦0430]
randomInit: x "Call with const to get repeatable sequence"
[step← x. "step holds the current state"
start is: Float⇒[length←stop-start] "for Float intervals"]
SystemOrganization classify: ↪Interval under: 'Basic Data Structures'.
"ObjectReference"
Class new title: 'ObjectReference'
subclassof: Object
fields: 'object'
declare: '';
asFollows
I am an indirect reference
Initialization
object: object
Indirection
eval
[⇑object]
value [⇑object]
value ← object
[⇑object]
Conversion
printon: strm
[strm append: '->'; print: object]
SystemOrganization classify: ↪ObjectReference under: 'Basic Data Structures'.
"RunVector"
Class new title: 'RunVector'
subclassof: Array
fields: ' min max starts values offset'
declare: '';
asFollows
RunVectors compactly store data which tends to be constant over much
of its domain. They may have any range of subscript, but must be stored
into consecutively.
Reading and writing
◦i | index
[index← starts findSorted: i.
offset← i-(starts◦index). "distance into run"
⇑values◦index]
◦i← val
[offset← 0.
min≡nil⇒[min← max← i. starts← i inVector. values← val inVector]
i-1≠max⇒[user notify: 'RunVectors must be loaded sequentially'. ⇑val]
max← i.
val=values last⇒[offset← i-starts last. ⇑val]
starts← starts , i.
values← values , val. ⇑val]
length [max≡nil⇒[⇑0]
⇑max-min+1]
max [⇑max]
min [⇑min]
SystemOrganization classify: ↪RunVector under: 'Basic Data Structures'.
"Stream"
Class new title: 'Stream'
subclassof: Object
fields: 'array position limit'
declare: '';
asFollows
Streams provide fast sequential access to arrays (implemented in microcode for Strings and Vectors). A subclass can handle end conditions if desired (disk files do this).
Initialization
close
[limit← position. position← 0]
default
[self of: (String new: 16)]
of: array
[position ← 0. limit ← array length]
of: array from: pos to: lim | len
[limit ← [lim > (len ← array length)⇒ [len] lim].
position ← [pos≤1⇒ [0] pos-1]]
release [array ← nil]
Sequential reading and writing
∢ x | y
[y← self next⇒ "peek for matching element"
[x=y⇒ [⇑y] "gobble it if found"
position ← position-1. ⇑false]
⇑false]
append: x | i "Array arg"
[for⦂ i from: x do⦂
[self next ← i].
⇑x]
dequeue "use it as a FIFO"
[⇑self dequeue: 1]
dequeue: n | t
[position<n⇒ [⇑false]
t ← (array◦(1 to: n)) copy.
array◦(1 to: position-n) ← array◦(n+1 to: position).
position ← position-n. ⇑t]
integerScan | sign base maxdigit c val [
"get the next Integer or LargeInteger (Float?) from a Stream.
copied from String asInteger"
sign← [self∢025⇒[¬1] 1].
base← [self∢060⇒[8] 10].
maxdigit← 060+base.
val← 0.
while⦂ ((c ← self next) and⦂ (c ≥ 060 and⦂ c < maxdigit)) do⦂ [
val← val*base+(c-060)].
[c⇒ [self skip: ¬1]].
"Some special maneuvering to keep 01ddddd and ¬32768 (and nothing else)
from overflowing."
base=8 and⦂ (val>077777 and⦂ (sign=1 and⦂ val<65536))⇒[⇑val asSmall]
⇑(val*sign) asInteger]
into: x [
"generate an error if the Stream is exhausted before x is filled"
⇑self into: x endError: true]
into: x endError: err | i t len ["Array result"
i ← 0.
len ← x length.
"read until count or stream is exhausted"
while⦂ (i < len and⦂ (t ← self next)) do⦂ [x◦(i←i+1) ←t].
err⇒ [
t⇒ [⇑x]
user notify: 'only read first ' + i asString]
"return number that were read"
⇑i]
next "simple result"
[self myend⇒ [⇑self pastend]
⇑array◦(position ← position+1)] primitive: 17
next: n [⇑self into: (array species new: n) endError: true]
next: n from: strm [for⦂ n to: n do⦂ [self next ← strm next]]
next: n ← v [for⦂ n to: n do⦂ [self next ← v]]
nextNumber: n | i s t [
"return next n characters s as a positive Integer or LargeInteger"
s ← false.
"scan for first non-zero byte, then collect rest appropriately"
for⦂ i to: n do⦂ [
t ← self next.
s⇒ ["more LargeInteger: reverse order of significance" s◦(n+1-i) ← t]
i=n⇒ [⇑t]
t=0⇒ []
i ≤ (n-2) or⦂ "i=n-1" (t land: 0200) ≠ 0⇒ [
"LargeInteger of 2 or more bytes"
s ← Natural new: n+1-i.
s last ← t]
"positive Integer"
⇑(t lshift: 8) + self next].
⇑LargeInteger new bytes: s neg: false]
nextNumber: n ← v | vlen [
"write a positive Integer or LargeInteger as n characters"
v ← v bytes.
vlen ← v length.
[n < vlen⇒ [user notify: 'number too big'];
> vlen⇒ [
"pad beginning with 0's"
self next: n - vlen ← 0]].
vlen = 1⇒ [self next ← v]
vlen = 2 and⦂ (v is: Integer)⇒ [self nextword ← v]
"LargeInteger (assume pos, no negative convention)"
self append: v reverse]
nextPoint | x [
x ← self nextword.
⇑Point new x: x y: self nextword]
nextPoint←p [
self nextword ← p x;
nextword ← p y]
nextString | len [
⇑self into: (String new: [
(len ← self next)
<192⇒[len] "up to 191 chars (BCPL compat)"
len-192*256 + self next]) endError: true] "up to 16383 chars"
nextString← s | len [
[(len ← s length) < 192⇒[self next← len]
self next← len/256+192; next← len\256].
self append: s.
⇑s]
nextword | hi lo
[hi ← self next⇒
[lo ← self next⇒
[⇑(hi lshift: 8)+lo]
⇑false]
⇑false]
nextword← val
[self next← val lshift: ¬8.
self next← val land: 0377. ⇑val]
next ← x "simple arg"
[self myend⇒ [⇑self pastend ← x]
⇑array◦(position ← position+1) ← x] primitive: 18
padNext ["make position even (on word boundary), returning padding character if any"
position even⇒ [⇑false]
⇑self next]
padNext← c [
position even⇒ [⇑false]
⇑self next← c]
peek | x
[x← self next⇒ [position ← position-1. ⇑x] "peek at next element"
⇑false]
pop "use it as a LIFO"
[position<1⇒ [⇑false]
position ← position-1. ⇑array◦(position+1)]
pop: n | t
[position<n⇒ [⇑false]
t ← self last: n.
position ← position-n. ⇑t]
upto: x | y s
[s ← (String new: 250) asStream.
for⦂ y from: self do⦂
[y=x⇒[⇑s contents]
s next ← y].
⇑s contents]
Test and alter position
empty "for"
[⇑position=0]
end
[⇑position≥limit]
limit
[⇑limit]
loc "synonym for compiler"
[⇑position]
myend
[⇑position≥limit]
pastend
[⇑false]
pastend ← x
[array ← array grow. limit ← array length.
⇑self next ← x]
position
[⇑position]
position← position
reset
[position ← 0]
settoend [position← limit]
skip: x
[position ← position+x]
skipTo: x | y [
for⦂ y from: self do⦂ [y=x⇒[⇑true]].
⇑false]
skipwords: w [self skip: 2*w]
wordposition [⇑self position/2]
wordposition← w [self position← w*2]
Static reading and writing
◦ x
[⇑array◦x]
◦ x ← val
[⇑array◦x ← val]
contents [⇑array copy: 1 to: position]
first
[position ≠ 0 ⇒ [⇑array◦1] ⇑nil]
insert: x | i "treat as LIFO queue, insert in front"
["grow array if necessary"
[position=limit⇒
[array←array grow.
limit←array length]].
for⦂ i to: position do⦂
[array◦(position-i+2) ← array◦(position-i+1)].
array◦1 ← x.
position←position+1]
last
[position≠ 0 ⇒ [⇑array◦position] ⇑nil]
last: n
[⇑(array◦(position-n+1 to: position)) copy]
rest [⇑array copy: position+1 to: limit]
Character printing
cr
[self next ← 015]
crtab: n | i
[self next←13.
for⦂ i to: n do⦂ [self next←9]]
print: obj
[obj printon: self]
semicrtab
[self append: ';
']
space
[self next ← 040]
tab
[self next ← 011]
Coercions
asArray
[⇑array]
asReadStream [
"an alternative to Set/SetReader.
create another Stream which reads the contents of this one"
⇑Stream new of: array from: 1 to: position]
asStream
asVector "Convert a string to a vector of tokens"
[⇑(Reader new of: self) read]
viewer [⇑SetReader new of: array from: 1 to: position]
Compiler object code
emitLong: jmpOrBfp by: dist
[[dist<0⇒ [dist←dist+1024]; >1023⇒ [dist←¬1] jmpOrBfp←jmpOrBfp+4].
dist<0⇒ [user notify: 'A block compiles more than 1K bytes of code']
self next ← dist/256 + jmpOrBfp. self next ← dist\256]
SystemOrganization classify: ↪Stream under: 'Basic Data Structures'.
"PQueue"
Class new title: 'PQueue'
subclassof: Stream
fields: 'readposition'
declare: '';
asFollows
A PQueue is a First In First Out list of objects implemented as an array and a read pointer and write pointer. PQueue is a subclass of Stream and uses Streamⓢstandard method for inserting a new item (next←, i.e. Streamⓢposition is the write pointer). A PQueue also has a read pointer which it uses for accessing objects with the messages next or dequeue (which are identical). All messages to a PQueue that change its state are declared as critical sections to avoid race conditions
FIFO access
dequeue: num | n
[Top critical⦂
[position-readposition < num ⇒ [n ← false]
n ← (array◦(readposition+1 to: readposition + num)) copy.
readposition ← readposition + num].
⇑n]
length | l
[Top critical⦂ [l ← position-readposition]. ⇑l]
myend [⇑true]
next | n
[Top critical⦂
[readposition≥position⇒ [readposition←position←0. n ← false]
n ← array◦(readposition ← readposition+1)].
⇑n] primitive: 98
pastend ← x | n i "simple arg"
[Top critical⦂
[position≥limit⇒
[readposition=0⇒[super pastend ← x]
n ← position-readposition.
for⦂ i to: n do⦂ [array◦i ← array◦(readposition+i)].
readposition ← 0. position ← n.
self next ← x]
array◦(position ← position+1) ← x].
⇑x]
peek | n
[Top critical⦂
[readposition≥position⇒ [readposition←position←0. n ← false]
n ← array◦(readposition + 1)].
⇑n]
skip: x
[Top critical⦂ [readposition ← readposition+x]]
LIFO access
push: x "treat as LIFO queue"
[Top critical⦂
[readposition > 0 ⇒
[array◦readposition ← x.
readposition ← readposition - 1] "readpositon > 0, just jam it in"
self insert: x]] "otherwise insert on front"
Stream protocol
contents | n
[Top critical⦂ [n ← (array◦(readposition+1 to: position)) copy]. ⇑n]
empty | l
[Top critical⦂ [l ← readposition≥position]. ⇑l] primitive: 99
end | n
[Top critical⦂ [n ← readposition≥position]. ⇑n]
of: array
[Top critical⦂ [position ← 0. readposition ← 0. limit ← array length]]
of: array from: position to: limit
[user notify: 'of:from:to: is not appropriate for PQueues']
reset
[Top critical⦂ [readposition ← position ← 0]]
SystemOrganization classify: ↪PQueue under: 'Basic Data Structures'.
"Queue"
Class new title: 'Queue'
subclassof: Stream
fields: 'readposition'
declare: '';
asFollows
A Queue is a First In First Out list of objects implemented as an array and a read pointer and write pointer. Queue is a subclass of Stream and uses Streamⓢstandard method for inserting a new item (next←, i.e. Streamⓢposition is the write pointer). A Queue also has a read pointer which it uses for accessing objects with the messages next or dequeue (which are identical)
FIFO access
deQ1 | n "A noninterruptable dequeue"
[Top critical⦂ [n ← self dequeue].
⇑n]
dequeue
[readposition≥position⇒ [readposition←position←0. ⇑false]
⇑array◦(readposition ← readposition+1)]
dequeue: num | n
[position-readposition < num ⇒ [⇑false]
n ← (array◦(readposition+1 to: readposition + num)) copy.
readposition ← readposition + num.
⇑n]
enQ1: n "A noninterruptable enqueue"
[Top critical⦂ [super next← n].
⇑n]
length
[⇑position-readposition]
next
[readposition≥position⇒ [readposition←position←0. ⇑false]
⇑array◦(readposition ← readposition+1)]
peek
[readposition≥position⇒ [readposition←position←0. ⇑false]
⇑array◦(readposition + 1)]
skip: x
[readposition ← readposition+x]
LIFO access
push: x "treat as LIFO queue"
[readposition > 0 ⇒
[array◦readposition ← x. readposition ← readposition - 1] "readpositon > 0, just jam it in"
self insert: x] "otherwise insert on front"
Stream protocol
contents
[⇑(array◦(readposition+1 to: position)) copy]
empty
[⇑readposition≥position]
end
[⇑readposition≥position]
of: array
[position ← 0. readposition ← 0. limit ← array length]
of: array from: position to: limit
[user notify: 'of:from:to: is not appropriate for Queues']
pastend ← x | n
[readposition=0⇒[⇑super pastend ← x]
n ← position-readposition.
array◦(1 to: n) ← array◦(readposition+1 to: position).
readposition ← 0.
position ← n.
⇑self next ← x]
reset
[readposition ← position ← 0]
SystemOrganization classify: ↪Queue under: 'Basic Data Structures'.
"Set"
Class new title: 'Set'
subclassof: Stream
fields: 'views'
declare: '';
asFollows
For storing/collecting, read by a SetReader.
Use no messages from Stream except of:, empty, next←, contents, space, nextword←
Initialization
default [self vector: 8]
of: array to: position [limit ← array length]
string: limit [self of: (String new: limit)]
vector: limit [self of: (Vector new: limit)]
Index operations
◦i [⇑array◦("self checkIndex:" i)]
◦i ← val [
position+1 = i⇒ [self next ← val]
⇑array◦("self checkIndex:" i) ← val]
deleteI: i | v j
[
v ← self◦i.
for⦂ j from: i to: position-1 do⦂
[array◦j ← array◦(j+1)
].
array◦position ← nil.
position ← position-1.
⇑v
]
deleteI: i to: j| n k
[
n ← j-i+1.
for⦂ k from: i to: position-n do⦂
[array◦k ← array◦(k+n)
].
for⦂ k from: position-n+1 to: position do⦂
[array◦k ← nil
].
position ← position-n.
]
insertI: i value: v | old j
[i > position ⇒ [ self next ← v ]
old←array.
[position = limit⇒
[limit← limit+(10 max: limit/4).
array ← array species new: limit.
for⦂ j to: i-1 do⦂
[array◦j ← old◦j
]
]
].
for⦂ j from: position to: i by: ¬1 do⦂
[array◦(j+1) ← old◦j
].
array◦i ← v.
position ← position +1
]
Value operations
add: x [self next ← x]
append: x [for⦂ x from: x do⦂ [self next ← x]]
delete: x | i [
for⦂ i to: position do⦂ [
array◦i ≡ x⇒ [⇑self deleteI: i]].
⇑false]
find: v | i [
for⦂ i to: position do⦂ [array◦i = v⇒ [⇑i]].
⇑0]
has: x [⇑(self find: x) > 0]
insert: x [(self find: x) = 0⇒ [self next← x]]
Viewing
asSet
asStream [⇑self viewer]
copy [⇑self viewer copy]
initView: v [⇑v of: array to: position]
length [⇑position]
notViewed: v [
views delete: v;
empty⇒ [views ← nil]]
printon: strm | t [
strm append: 'a Set: '.
array is: String⇒ [strm append: self]
for⦂ t from: self do⦂ [strm space; print: t]]
species [⇑array species]
viewer [
⇑SetReader new of: array from: 1 to: position
"self viewRange: 1 to: position"]
viewer: v [
[views≡nil⇒ [views ← Set default]].
views next ← v]
viewRange: i to: j [
⇑"self viewer:" (
SetReader new of: array from: (i "max: 1") to: (j "min: position"))]
Private
checkIndex: i [
i ≥ 1 and⦂ i ≤ position⇒ [⇑i]
⇑user notify: 'illegal index']
grow [
"self grown and reset. returns another Set with old contents"
⇑self growby: (10 max: limit/4)]
growby: n | old [
"grow and reset self. return old Set for copying"
old ← Set new of: array to: position.
self of: (array species new: limit+n) to: 0.
⇑old]
next [user notify: 'no direct reading of a Set']
pastend ← x [
⇑[self append: self grow; next ← x]]
Arithmetic operations
dotproduct: s | i dotproduct
["dot product of two sets ... sets must be of equal length"
dotproduct ← 0.0.
self length = s length ⇒
[
for⦂ i to: position do⦂ [dotproduct ← dotproduct + ((s◦i)*(self◦i))].
⇑ dotproduct
]
user notify: 'dot product undefined...sets are not of equal length'.
]
product: s | product i
["product of two sets ... sets must be of equal length"
product ← Set new default.
self length = s length ⇒
[
for⦂ i to: position do⦂ [product add: (s◦i)*(self◦i)].
⇑ product
]
user notify: 'product undefined...sets are not of equal length'.
]
summation| i summation
["sum of the values in the set"
summation ← 0.0.
for⦂ i to: position do⦂ [summation ← summation + (self◦i)].
⇑ summation
]
SystemOrganization classify: ↪Set under: 'Basic Data Structures'.
"SetReader"
Class new title: 'SetReader'
subclassof: Stream
fields: ''
declare: '';
asFollows
Read a Set; no edits occur to set. (see Steve for ISetReader (interruptible))
Inherit of:from:to:, next, next:, end, pastend, skip:, ∢, asStream, viewer
Initialization
of: array from: position for: n [
position ← position-1.
limit ← position+n]
Reading
asSet [⇑self copy]
copy "yield contents all at once as a Set" [
⇑[Set new of: (array species new: limit-position); append: self]]
length [
"how much left"
⇑limit-position]
SystemOrganization classify: ↪SetReader under: 'Basic Data Structures'.
"String"
VariableLengthClass new title: 'String'
subclassof: Array
fields: ''
declare: 'StringBlter ';
bytesize: 8;
asFollows
I am an array of bytes, integers between 0 and 255 usually representing ascii characters
Reading and Writing
all← val [self fill: 1 to: self length with: val]
fill: a to: b with: val | i [
"eventually use BitBlt?"
for⦂ i from: a to: b do⦂ [self◦i ← val]]
length [⇑self length "In case this is reached by perform:"]
word: x "read word in String"
[⇑self◦(x+x) + (self◦(x+x-1) lshift: 8)]
word: x ← y "write word in String"
[self◦(x+x-1) ← y lshift: ¬8.
self◦(x+x) ← y land: 0377. ⇑y]
Copying and Altering
concat: s | len [
(len ← self length) + s length > 20 and⦂ (s Is: String)⇒ [
"this concatenates more quickly if BitBlt is used"
⇑self replace: len+1 to: len by: s from: 1 to: s length]
⇑super concat: s]
copy: a to: b [⇑(self species new: 1+b-a) copy: 1 to: 1+b-a with: self from: a to: b]
copy: a to: b with: s from: c to: d | i [
"like replace, except in place. self◦(a to: b) ← s◦(c to: d).
use BitBlt unless size too small, StringBlter≡false, or index/sizes too large"
(b-a > 12 and⦂ StringBlter) and⦂ (
BitBlt new stringCopy: self from: a to: b with: s from: c to: d)⇒ []
self ≡ s and⦂ (c < a and⦂ d ≥ a)⇒ [
"overlap of second range with below first in same string.
copy in reverse order: self◦(b to: a by: ¬1) ← self◦(d to: c by: ¬1)"
for⦂ i from: b-a to: 0 by: ¬1 do⦂ [self◦(a+i) ← self◦(c+i)]]
s copy: c to: d to: (Stream new of: self from: a to: b)]
findString: str startingAt: start | i t
[str length=0⇒[⇑0] t← str◦1.
for⦂ i from: start to: self length-str length+1 do⦂
[self◦i=t⇒[self◦(i to: i+str length-1)=str⇒[⇑i]]]
⇑0]
growto: n | len [
[(len ← self length) ≤ n⇒ [] len ← n].
⇑(self species new: n) copy: 1 to: len with: self from: 1 to: len]
recopy
[⇑self copy]
replace: a to: b by: s
[s Is: String ⇒[
⇑self replace: a to: b by: s from: 1 to: s length]
⇑self replace: a to: b by: s asArray from: 1 to: s position]
replace: a to: b by: r from: c to: d | s t [
s ← self species new: self length + (d-c) - (b-a).
"use BitBlt unless StringBlter≡false or index/sizes too large"
StringBlter and⦂ (
BitBlt new stringReplace: s with: self from: a to: b and: r from: c to: d)⇒ [⇑s]
"see Array concat:"
t ← Stream new of: s.
self copy: 1 to: a-1 to: t.
r copy: c to: d to: t.
self copy: b+1 to: self length to: t.
⇑s]
subst: repl for: key | key1 i nskip result
[nskip ← 0. key1 ← key◦1. result ← Stream default.
for⦂ i to: self length do⦂ " the Boyer Slow string replacement "
[nskip>0⇒ [nskip ← nskip-1]
self◦i = key1⇒
[self◦(i to: (self length min: i+key length-1)) = key⇒
[result append: repl. nskip ← key length-1]
result next← self◦i]
result next← self◦i]
⇑result contents]
Comparison
- s | i c ldiff [
"Return a negative, zero, or positive integer as I compare < = or > s"
"The collation sequence is ascii with case differences ignored."
for⦂ i to: [
(ldiff ← self length-s length) < 0⇒ [self length] s length] do⦂ [
(c← UpperCase◦(self◦i + 1) -(UpperCase◦(s◦i + 1)))
≠0⇒ [⇑c]].
⇑ldiff]
< s
["Return true iff I collate before s. The collation sequence is ascii with case differences ignored."
⇑(self compare: s) = 1]
> s
["Return true iff I collate after s. The collation sequence is ascii with case differences ignored."
⇑(self compare: s) = 3]
compare: s | i len lcomp u1 u2 [
lcomp ← [self length < (len ← s length)⇒ [len ← self length. 1]; =len⇒ [2] 3].
for⦂ i to: len do⦂ [
(u1 ← UpperCase◦(self◦i + 1)) = (u2 ← UpperCase◦(s◦i + 1))⇒ []
u1 < u2⇒ [⇑1]
⇑3]
⇑lcomp]
hash | l m
[[(l← m← self length)≤2⇒
[l=2⇒[m←3]; =1⇒[⇑((self◦1) land: 0177)*0152] ⇑052525]].
⇑(self◦1)*060+(self◦(m-1)+l)]
match: text | star pound pattern scanning p t back [
star ← 052 "*". pound ← 043 "#".
pattern ← self asStream. text ← text asStream.