-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStuckPA.java
1445 lines (1285 loc) · 57.3 KB
/
StuckPA.java
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
//------------------------------------------------------------------------------
// StuckSA on a bit machine - completed on Christmas Eve!
// Philip R Brenan at appaapps dot com, Appa Apps Ltd Inc., 2024
//------------------------------------------------------------------------------
package com.AppaApps.Silicon; // Btree in a block on the surface of a silicon chip.
abstract class StuckPA extends Test // A fixed size stack of ordered key, data pairs
{abstract int maxSize(); // The maximum number of entries in the stuck.
abstract int bitsPerKey(); // The number of bits needed to define a key
abstract int bitsPerData(); // The number of bits needed to define a data field
abstract int bitsPerSize(); // The number of bits needed to define the size field
final String name; // Name of the stuck
final MemoryLayoutPA M; // Memory for stuck
final MemoryLayoutPA C; // Temporary storage containing a copy of parts of the stuck to allow shifts to occur in parallel
final MemoryLayoutPA T; // Memory for transaction intermediates
ProgramPA P = new ProgramPA(); // The program to be written to to describe the actions on the stuck. The caller can provide a different one as this field is not final
Layout.Variable sKey; // Key in a stuck
Layout.Array Keys; // Array of keys
Layout.Variable sData; // Data associated with a key
Layout.Array Data; // Array of data associated with the array of keys
Layout.Variable currentSize; // Current size of stuck
Layout.Structure stuck; // The stuck itself
Layout.Variable search; // Search key
Layout.Variable limit; // Limit of search
Layout.Bit isFull; // Whether the stuck is currently full
Layout.Bit isEmpty; // Whether the stuck is currently empty
Layout.Bit found; // Whether a matching element was found
Layout.Variable index; // The index from which the key, data pair were retrieved
Layout.Variable tKey; // The retrieved key
Layout.Variable tData; // The retrieved data
Layout.Variable size; // The current size of the stuck
Layout.Variable full; // Used by isFull
Layout.Bit equal; // The result of an equal operation
Layout.Variable copyCount; // Number of elements to copy in a copy operation
Layout.Variable copyBits; // Number of bits to copy in a copy operation
Layout.Structure temp; // Transaction intermediate fields
String action; // Last action performed
static boolean debug; // Debug when true
//D1 Construction // Create a stuck
StuckPA(String Name) // Create the stuck with a maximum number of the specified elements
{this(Name, null);
}
StuckPA(String Name, MemoryLayoutPA based) // Create the stuck with a maximum number of the specified elements
{z();
name = Name;
final Layout layout = layout(), tl = transactionLayout(); // Layout out the stuck
M = new MemoryLayoutPA(layout, name+"_StuckSA_Memory", based); // Memory for stuck
C = new MemoryLayoutPA(layout, name+"_StuckSA_Copy"); // Temporary storage containing a copy of parts of the stuck to allow shifts to occur in parallel
T = new MemoryLayoutPA(tl, name+"_StuckSA_Transaction"); // Memory for transaction intermediates
program(M.P);
}
void base(int Base) // Set the base address of the stuck in the memory layout containing the stuck
{z(); M.base(Base);
}
void base(MemoryLayoutPA.At Base) // Set the base address of the stuck in the memory layout containing the stuck
{z();
P.new I()
{void a() {M.base(Base.setOff().result);}
String v() {return M.baseName() + " <= " + Base.verilogLoad()+";";} // Set the base for this based data structure
};
}
void program(ProgramPA program) // Set the program in which the various components should generate code
{z(); P = program;
M.program(P);
C.program(P);
T.program(P);
}
StuckPA copy() // Copy a stuck definition
{z();
final StuckPA parent = this;
final StuckPA child = new StuckPA(parent.name, parent.M)
{int maxSize () {return parent.maxSize ();}
int bitsPerKey () {return parent.bitsPerKey ();}
int bitsPerData() {return parent.bitsPerData();}
int bitsPerSize() {return parent.bitsPerSize();}
};
child.program(parent.P);
return child;
}
Layout layout() // Layout describing stuck
{z();
final Layout l = Layout.layout();
sKey = l.variable ("key" , bitsPerKey());
Keys = l.array ("Keys" , sKey, maxSize());
sData = l.variable ("data" , bitsPerData());
Data = l.array ("Data" , sData, maxSize());
currentSize = l.variable ("currentSize", bitsPerSize());
stuck = l.structure("stuck" , currentSize, Keys, Data);
return l.compile();
}
Layout transactionLayout() // Layout of temporary memory used by a transaction
{z();
final Layout l = Layout.layout();
search = l.variable ( "search", bitsPerKey());
limit = l.variable ( "limit", bitsPerSize());
isFull = l.bit ( "isFull");
isEmpty = l.bit ( "isEmpty");
found = l.bit ( "found");
index = l.variable ( "index", bitsPerSize());
tKey = l.variable ( "key", bitsPerKey());
tData = l.variable ( "data", bitsPerData());
size = l.variable ( "size", bitsPerSize());
full = l.variable ( "full", bitsPerSize());
equal = l.bit ( "equal");
copyCount = l.variable ("copyCount", bitsPerSize());
copyBits = l.variable ( "copyBits", bitsPerSize() + max(bitsPerKey(), bitsPerData()));
temp = l.structure("temp", search, limit, isFull, isEmpty, found, index,
tKey, tData, size, full, equal, copyCount);
return l.compile();
}
//D1 Transactions // Transactions on the stuck
void size() // The current number of elements in the stuck
{z();
T.at(size).move(M.at(currentSize));
}
void setSize() // Set the current number of elements in the stuck
{z();
M.at(currentSize).move(T.at(size));
}
void isFull() // Check the stuck is full
{z();
T.setIntInstruction(full, maxSize());
T.at(size).greaterThanOrEqual(T.at(full), T.at(isFull));
}
void isEmpty() // Check the stuck is empty
{z();
T.setIntInstruction(full, 0);
T.at(size).equal(T.at(full), T.at(isEmpty));
}
void assertNotFull() // Assert the stuck is not full
{z();
final StuckPA t = this;
P.new If(T.at(isFull))
{void Then()
{P.halt("Stuck full");
}
};
}
void assertNotEmpty() // Assert the stuck is not empty
{z();
P.new If(T.at(isEmpty))
{void Then() {P.halt("Empty");}
};
}
void assertInNormal() // Check that the index would yield a valid element
{z();
P.new I()
{void a()
{final int i = T.at(index).getInt();
final int n = T.at(size) .getInt();
if (i < 0 || i >= n) stop("Out of normal range", i, "for size", n, traceBack);
}
String v()
{return "/* assertInNormal */";
}
};
}
void copyKeys(StuckPA source) // Copy the specified number of elements from the source array of keys at the specified index into the target array of keys at the specified target index
{P.new I() {void a() {T.at(copyBits).setInt(T.at(copyCount).getInt()*bitsPerKey());}};
final MemoryLayoutPA.At ti = T.at(index);
final MemoryLayoutPA.At si = source.T.at(source.index);
M.at(sKey, ti).copy(source.M.at(source.sKey, si), T.at(copyBits));
}
void copyData(StuckPA source) // Copy the specified number of elements from the source array of data at the specified index into the target array of data at the specified target index
{P.new I() {void a() {T.at(copyBits).setInt(T.at(copyCount).getInt()*bitsPerKey());}};
final MemoryLayoutPA.At ti = T.at(index);
final MemoryLayoutPA.At si = source.T.at(source.index);
M.at(sData, ti).copy(source.M.at(source.sData, si), T.at(copyBits));
}
void assertInExtended() // Check that the index would yield a valid element
{z();
P.new I()
{void a()
{final int i = T.at(index).getInt();
final int n = T.at(size) .getInt();
if (i < 0 || i > n) stop("Out of extended range", i, "for size", n, traceBack);
}
String v()
{return "/* assertInEXtended */";
}
};
}
void inc() // Increment the current size
{z();
assertNotFull();
M.at(currentSize).inc();
}
void dec() // Decrement the current size
{z();
assertNotEmpty();
M.at(currentSize).dec();
}
void clear() // Zero the current size to clear the stuck
{z();
M.setIntInstruction(currentSize, 0);
sizeFullEmpty();
}
MemoryLayoutPA.At key() // Refer to key
{z();
return M.at(sKey, T.at(index));
}
MemoryLayoutPA.At data() // Refer to data
{z(); return M.at(sData, T.at(index));
}
void moveKey() // Move a key from the stuck to this transaction
{z(); T.at(tKey ).move(key ());
}
void moveData() // Move a key from the stuck to this transaction
{z(); T.at(tData).move(data());
}
void setKey () // Set the indexed key
{z(); key().move(T.at(tKey));
}
void setData () // Set the indexed data
{z(); data().move(T.at(tData));
}
void setKeyData() {z(); setKey(); setData();} // Set a key, data element in the stuck
void sizeFullEmpty() {z(); size(); isFull(); isEmpty();} // Status
void setFound() {z(); T.setIntInstruction(found, 1);} // Set found to true
void push() // Push an element onto the stuck
{z(); action = "push";
size();
isFull();
assertNotFull();
size();
T.at(index).move(T.at(size));
setKeyData();
inc();
sizeFullEmpty();
}
void unshift() // Unshift an element onto the stuck
{z(); action = "unshift";
size();
isFull();
assertNotFull();
setFound();
T.setIntInstruction(index, 0);
M.at(Keys).moveUp(T.at(index), C.at(Keys));
//T.setIntInstruction(index, 0);
M.at(Data).moveUp(T.at(index), C.at(Data));
inc();
setKeyData();
sizeFullEmpty();
}
void pop() // Pop an element from the stuck
{z(); action = "pop";
size();
isEmpty();
assertNotEmpty();
dec();
setFound();
T.at(size).dec();
T.at(index).move(T.at(size));
moveKey(); moveData();
sizeFullEmpty();
}
void shift() // Shift an element from the stuck
{z(); action = "shift";
size();
isEmpty();
assertNotEmpty();
setFound();
T.setIntInstruction(index, 0);
moveKey(); moveData();
T.zero();
M.at(Keys).moveDown(T.at(index), C.at(Keys));
M.at(Data).moveDown(T.at(index), C.at(Data));
dec();
sizeFullEmpty();
}
void elementAt() // Look up key and data associated with the index in the stuck at the specified base offset in memory
{z(); action = "elementAt";
size();
assertInNormal();
setFound();
moveKey();
moveData();
}
void setElementAt() // Set an element either in range or one above the current range
{z(); action = "setElementAt";
size();
T.at(index).equal(T.at(size), T.at(equal)); // Extended range
P.new If(T.at(equal))
{void Then()
{setKeyData();
inc();
T.at(size).inc();
}
void Else() // In range
{assertInNormal(); setKeyData();
}
};
setFound();
}
void insertElementAt() // Insert an element at the indicated location shifting all the remaining elements up one
{z(); action = "insertElementAt";
size();
isFull();
assertInExtended();
T.zero();
M.at(Keys).moveUp(T.at(index), C.at(Keys));
M.at(Data).moveUp(T.at(index), C.at(Data));
M.at(currentSize).inc();
setKeyData();
sizeFullEmpty();
}
void removeElementAt() // Remove an element at the indicated location from the stuck
{z(); action = "removeElementAt";
size();
assertInNormal();
setFound();
moveKey();
moveData();
T.zero();
M.at(Keys).moveDown(T.at(index), C.at(Keys));
M.at(Data).moveDown(T.at(index), C.at(Data));
M.at(currentSize).dec();
sizeFullEmpty();
}
void firstElement() // First element
{z(); action = "firstElement";
size();
isEmpty();
assertNotEmpty();
setFound();
T.setIntInstruction(index, 0);
moveKey();
moveData();
}
void lastElement() // Last element
{z(); action = "lastElement";
size();
isEmpty();
assertNotEmpty();
setFound();
T.at(index).move(M.at(currentSize));
T.at(index).dec();
moveKey();
moveData();
}
void search() // Search for an element within all elements of the stuck
{z(); action = "search";
size();
P.new If(T.at(limit)) // Better if we had a separate routine for search a branch versus a leaf
{void Then()
{T.at(size).dec();
}
};
T.setIntInstruction(found, 0, index, 0); // Assume we will not find a match
P.new Block()
{void code()
{for (int I = 0; I < maxSize(); I++) // Search
{final int i = I;
//T.setIntInstruction(index, i);
T.at(index).equal(T.at(size), T.at(equal));
P.GoOn(end, T.at(equal)); // Reached the upper limit of the stuck
moveKey();
T.at(tKey).equal(T.at(search), T.at(equal));
P.new If (T.at(equal)) // Found an equal key
{void Then()
{setFound();
moveData();
if (i != maxSize()-1) P.Goto(end); // Goto superfluous on last iteration
}
};
if (i != maxSize()-1) T.setIntInstruction(index, i+1); // Not needed on last iteration
}
}
};
}
void searchFirstGreaterThanOrEqual() // Search for first greater than or equal
{z(); action = "searchFirstGreaterThanOrEqual";
size();
P.new If(T.at(limit)) // Better if we had a separate routine for search a branch versus a leaf
{void Then()
{T.at(size).dec();
}
};
//T.at(index).move(T.at(size)); // Index top if no match found
T.setIntInstruction(found, 0, index, 0); // Assume we will not find a match
P.new Block()
{void code()
{for (int I = 0; I < maxSize(); I++) // Search
{final int i = I;
//T.setIntInstruction(index, i);
T.at(index).equal(T.at(size), T.at(equal));
P.GoOn(end, T.at(equal)); // Reached the upper limit of the stuck
moveKey();
T.at(tKey).greaterThanOrEqual(T.at(search), T.at(equal));
P.new If (T.at(equal)) // Found an equal key or greater
{void Then()
{setFound();
moveKey();
moveData();
if (i != maxSize()-1) P.Goto(end); // Goto superfluous on last iteration
}
};
if (i != maxSize()-1) T.setIntInstruction(index, i+1); // Not needed on last iteration
}
}
};
}
//D1 Merge and Split // Merge and split stucks
void concatenate(StuckPA Source) // Concatenate two stucks placing the source at the end of the target while not modifying the source
{z(); action = "concatenate";
final StuckPA Target = this;
size(); Source.size(); // Get the size of the stucks
Source.T.at(Source.index ).setInt(0); // Start at start of source
Target.T.at(Target.index ).move(Target.T.at(Target.size)); // Extend target
Target.T.at(Target.copyCount).move(Source.T.at(Source.size)); // Number of elements to copy into the target
Target.copyKeys(Source); // Copy keys
Target.copyData(Source); // Copy data
P.new I() // Update size of target
{void a()
{Target.T.at(Target.size).setInt
(Target.T.at(Target.size).getInt() +
Source.T.at(Source.size).getInt());
}
};
Target.setSize();
}
void prepend(StuckPA Source) // Concatenate two stucks placing the source at the start of the target while (apparently) not modifying the target
{z(); action = "prepend";
final StuckPA Target = this;
size(); Source.size(); // Get the size of the stucks
Target.T.at(Target.index ).setInt(0); // Concatenate the target to the source
Source.T.at(Source.index ).move(Source.T.at(Source.size)); // Extend source
Source.T.at(Source.copyCount).move(Target.T.at(Target.size)); // Number of elements to copy into the target
Source.copyKeys(Target); // Copy keys
Source.copyData(Target); // Copy data
Target.T.at(Target.index ).setInt(0); // Copy the source to the target leaving the lenmgth of the source unchanged so it looks as if it has not been changed
Source.T.at(Source.index ).setInt(0); // Extend source
Source.T.at(Source.copyCount).move(Target.T.at(Target.size)); // Number of elements to copy into the target
Source.copyKeys(Target); // Copy keys
Source.copyData(Target); // Copy data
P.new I() // Update size of target
{void a()
{Target.T.at(Target.size).setInt
(Target.T.at(Target.size).getInt() +
Source.T.at(Source.size).getInt());
}
};
Target.setSize();
}
//D1 Print // Print a stuck
public String print()
{final StringBuilder s = new StringBuilder();
s.append("Transaction(");
s.append( "action:"+action);
s.append( " search:"+T.at(search) .getInt());
s.append( " limit:"+T.at(limit) .getInt());
s.append( " found:"+T.at(found) .getInt());
s.append( " index:"+T.at(index) .getInt());
s.append( " key:"+T.at(tKey) .getInt());
s.append( " data:"+T.at(tData) .getInt());
s.append( " size:"+T.at(size) .getInt());
s.append( " isFull:"+T.at(isFull) .getInt());
s.append(" isEmpty:"+T.at(isEmpty).getInt());
s.append(")\n");
return s.toString();
}
public String toString() // Print a stuck
{final StuckPA s = this;
final StuckSML t = new StuckSML()
{int maxSize () {return s.maxSize ();}; // The maximum number of entries in the stuck.
int bitsPerKey () {return s.bitsPerKey ();}; // The number of bits per key
int bitsPerData() {return s.bitsPerData();}; // The number of bits per data
int bitsPerSize() {return s.bitsPerSize();}; // The number of bits in size field
};
t.M.memory(s.M.memory());
t.base(s.M.base());
return t.toString();
}
//D1 Testing // Test the stuck
static StuckPA stuckPA() // Create a sample stuck
{z();
final int offset = 16; // To make testing more relevant
final StuckPA S = new StuckPA("original")
{int maxSize () {return 8;}
int bitsPerKey () {return 16;}
int bitsPerData () {return 16;}
int bitsPerSize () {return 16;}
};
Layout l = Layout.layout();
Layout.Variable a = l.variable ("a", S.M.layout.size()+offset);
l.compile();
MemoryLayoutPA m = new MemoryLayoutPA(l.compile(), "M");
final StuckPA T = new StuckPA("copyable", m)
{int maxSize () {return 8;}
int bitsPerKey () {return 16;}
int bitsPerData () {return 16;}
int bitsPerSize () {return 16;}
};
final StuckPA t = T.copy();
t.base(offset);
return t;
}
public void ok(String expected) {ok(toString(), expected);} // Check the stuck
//D0 Tests // Test stuck
static StuckPA test_load()
{StuckPA s = stuckPA();
for (int I = 0; I < 4; I++)
{final int i = I;
s.P.new I() {void a()
{s.T.at(s.tKey ).setInt(2 + 2 * i);
}};
s.P.new I() {void a() {s.T.at(s.tData).setInt(1 + 1 * i);}};
s.push();
}
s.P.run(); s.P.clear();
//stop(s);
ok(s, """
StuckSML(maxSize:8 size:4)
0 key:2 data:1
1 key:4 data:2
2 key:6 data:3
3 key:8 data:4
""");
return s;
}
static void test_clear()
{StuckPA s = test_load();
s.size();
ok(s.T.at(s.size).getInt(), 4);
s.clear();
s.P.run();
ok(s.M.at(s.currentSize).setOff(false).getInt(), 0);
ok(s.T.at(s.size).getInt(), 0);
}
static void test_push()
{StuckPA s = stuckPA();
s.P.new I() {void a() {s.T.at(s.tKey).setInt(15); s.T.at(s.tData).setInt( 9);}}; s.push();
s.P.new I() {void a() {s.T.at(s.tKey).setInt(14); s.T.at(s.tData).setInt(10);}}; s.push();
s.P.new I() {void a() {s.T.at(s.tKey).setInt(13); s.T.at(s.tData).setInt(11);}}; s.push();
s.P.new I() {void a() {s.T.at(s.tKey).setInt(12); s.T.at(s.tData).setInt(12);}}; s.push();
s.P.run();
//stop(s.M);
ok(""+s.M, """
MemoryLayout: copyable_StuckSA_Memory
Memory : M
Line T At Wide Size Indices Value Name
1 S 16 272 stuck
2 V 16 16 4 currentSize
3 A 32 128 8 Keys
4 V 32 16 0 15 key
5 V 48 16 1 14 key
6 V 64 16 2 13 key
7 V 80 16 3 12 key
8 V 96 16 4 0 key
9 V 112 16 5 0 key
10 V 128 16 6 0 key
11 V 144 16 7 0 key
12 A 160 128 8 Data
13 V 160 16 0 9 data
14 V 176 16 1 10 data
15 V 192 16 2 11 data
16 V 208 16 3 12 data
17 V 224 16 4 0 data
18 V 240 16 5 0 data
19 V 256 16 6 0 data
20 V 272 16 7 0 data
""");
//stop(s.memoryLayout.memory);
ok(s.M.memory(), """
Memory: M
4... 4... 4... 4... 3... 3... 3... 3... 2... 2... 2... 2... 1... 1... 1... 1...
Line FEDC BA98 7654 3210 FEDC BA98 7654 3210 FEDC BA98 7654 3210 FEDC BA98 7654 3210
0 0000 0000 000c 000b 000a 0009 0000 0000 0000 0000 000c 000d 000e 000f 0004 0000
1 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
""");
//stop(s);
ok(s, """
StuckSML(maxSize:8 size:4)
0 key:15 data:9
1 key:14 data:10
2 key:13 data:11
3 key:12 data:12
""");
s.P.clear();
s.T.at(s.tKey).setInt(11); s.T.at(s.tData ).setInt(11); s.push();
s.P.new I() {void a() {ok(s.T.at(s.isFull ).getInt() == 0);}};
s.P.new I() {void a() {ok(s.T.at(s.isEmpty).getInt() == 0);}};
s.T.at(s.tKey).setInt(10); s.T.at(s.tData ).setInt(10); s.push();
s.T.at(s.tKey).setInt( 9); s.T.at(s.tData ).setInt( 9); s.push();
s.T.at(s.tKey).setInt( 8); s.T.at(s.tData ).setInt( 8); s.push();
s.P.new I() {void a() {ok(s.T.at(s.isFull ).getInt() == 1);}};
s.P.new I() {void a() {sayThisOrStop("Stuck full");}};
s.T.at(s.tKey).setInt(7); s.T.at(s.tData).setInt(7); s.push();
try {s.P.run();} catch(RuntimeException e) {}
}
static void test_pop()
{StuckPA s = test_load();
s.pop();
s.P.run();
//stop(s.print());
ok(s.print(), """
Transaction(action:pop search:0 limit:0 found:1 index:3 key:8 data:4 size:3 isFull:0 isEmpty:0)
""");
//stop(s);
ok(s, """
StuckSML(maxSize:8 size:3)
0 key:2 data:1
1 key:4 data:2
2 key:6 data:3
""");
s.P.clear();
s.P.new I() {void a() {ok(s.T.at(s.isEmpty).getInt() == 0);}};
s.clear();
s.P.new I() {void a() {ok(s.T.at(s.isEmpty).getInt() == 1);}};
s.P.new I() {void a() {sayThisOrStop("Empty");}};
s.pop();
try {s.P.run();} catch(RuntimeException e) {}
}
static void test_shift()
{StuckPA s = test_load();
s.shift();
s.P.run(); s.P.clear();
ok(s.print(), """
Transaction(action:shift search:0 limit:0 found:1 index:0 key:2 data:1 size:3 isFull:0 isEmpty:0)
""");
//stop(s);
ok(s, """
StuckSML(maxSize:8 size:3)
0 key:4 data:2
1 key:6 data:3
2 key:8 data:4
""");
ok(s.T.at(s.isEmpty).getInt() == 0);
s.clear();
s.P.run(); s.P.clear();
ok(s.T.at(s.isEmpty).getInt() == 1);
sayThisOrStop("Empty");
s.pop();
try {s.P.run();} catch(RuntimeException e) {}
}
static void test_unshift()
{StuckPA s = test_load();
final MemoryLayoutPA m = s.T;
s.T.at(s.tKey).setInt(9); s.T.at(s.tData).setInt(9);
s.unshift();
s.P.run(); s.P.clear();
//stop(s);
ok(s.print(), """
Transaction(action:unshift search:0 limit:0 found:1 index:0 key:9 data:9 size:5 isFull:0 isEmpty:0)
""");
//stop(s);
ok(s, """
StuckSML(maxSize:8 size:5)
0 key:9 data:9
1 key:2 data:1
2 key:4 data:2
3 key:6 data:3
4 key:8 data:4
""");
ok(s.T.at(s.isFull).getInt() == 0);
s.unshift(); s.unshift(); s.unshift();
s.P.run(); s.P.clear();
ok(s.T.at(s.isFull).getInt() == 1);
sayThisOrStop("Stuck full");
s.unshift();
try {s.P.run();} catch(RuntimeException e) {}
}
static void test_elementAt()
{StuckPA s = test_load();
s.T.at(s.index).setInt(2);
s.elementAt();
s.P.run(); s.P.clear();
//stop(s);
ok(s.print(), """
Transaction(action:elementAt search:0 limit:0 found:1 index:2 key:6 data:3 size:4 isFull:0 isEmpty:0)
""");
s.T.at(s.index).setInt(-2);
sayThisOrStop("Out of normal range 65534 for size 4");
s.elementAt();
try {s.P.run();} catch(RuntimeException e) {} finally {s.P.clear();}
s.T.at(s.index).setInt(4);
sayThisOrStop("Out of normal range 4 for size 4");
try {s.P.run();} catch(RuntimeException e) {} finally {s.P.clear();}
}
static void test_set_element_at()
{StuckPA s = test_load();
s.P.new I() {void a() {s.T.at(s.tKey ).setInt(22);}};
s.P.new I() {void a() {s.T.at(s.tData).setInt(33);}};
s.P.new I() {void a() {s.T.at(s.index).setInt( 2);}};
s.setElementAt();
s.P.run(); s.P.clear();
//stop(s);
ok(s, """
StuckSML(maxSize:8 size:4)
0 key:2 data:1
1 key:4 data:2
2 key:22 data:33
3 key:8 data:4
""");
s.P.new I() {void a() {s.T.at(s.tKey ).setInt(88);}};
s.P.new I() {void a() {s.T.at(s.tData).setInt(99);}};
s.P.new I() {void a() {s.T.at(s.index).setInt( 4);}};
s.setElementAt();
s.P.run(); s.P.clear();
//stop(s);
ok(s, """
StuckSML(maxSize:8 size:5)
0 key:2 data:1
1 key:4 data:2
2 key:22 data:33
3 key:8 data:4
4 key:88 data:99
""");
s.P.new I() {void a() {s.T.at(s.index).setInt(-2);}};
sayThisOrStop("Out of normal range 65534 for size 5");
s.setElementAt();
try {s.P.run();} catch(RuntimeException e) {} finally {s.P.clear();}
s.P.new I() {void a() {s.T.at(s.index).setInt(6);}};
sayThisOrStop("Out of normal range 6 for size 5");
s.setElementAt();
try {s.P.run();} catch(RuntimeException e) {} finally {s.P.clear();}
}
static void test_insert_element_at()
{StuckPA s = test_load();
s.P.new I() {void a() {s.T.at(s.tKey ).setInt(9);}};
s.P.new I() {void a() {s.T.at(s.tData).setInt(9);}};
s.P.new I() {void a() {s.T.at(s.index).setInt(2);}};
s.insertElementAt();
s.P.run(); s.P.clear();
//stop(s);
ok(s, """
StuckSML(maxSize:8 size:5)
0 key:2 data:1
1 key:4 data:2
2 key:9 data:9
3 key:6 data:3
4 key:8 data:4
""");
s.P.new I() {void a() {s.T.at(s.tKey ).setInt(7);}};
s.P.new I() {void a() {s.T.at(s.tData).setInt(7);}};
s.P.new I() {void a() {s.T.at(s.index).setInt(5);}};
s.insertElementAt();
s.P.run(); s.P.clear();
//stop(s);
ok(s, """
StuckSML(maxSize:8 size:6)
0 key:2 data:1
1 key:4 data:2
2 key:9 data:9
3 key:6 data:3
4 key:8 data:4
5 key:7 data:7
""");
s.P.new I() {void a() {s.T.at(s.index).setInt(7);}};
sayThisOrStop("Out of extended range 7 for size 6");
s.insertElementAt();
try {s.P.run();} catch(RuntimeException e) {} finally {s.P.clear();}
}
static void test_remove_element_at()
{StuckPA s = test_load();
s.P.new I() {void a() {s.T.at(s.index).setInt(2);}};
s.removeElementAt();
s.P.run(); s.P.clear();
//stop(t);
ok(s.print(), """
Transaction(action:removeElementAt search:0 limit:0 found:1 index:2 key:6 data:3 size:3 isFull:0 isEmpty:0)
""");
//stop(s);
ok(s, """
StuckSML(maxSize:8 size:3)
0 key:2 data:1
1 key:4 data:2
2 key:8 data:4
""");
s.P.new I() {void a() {s.T.at(s.index).setInt(3);}};
sayThisOrStop("Out of normal range 3 for size 3");
s.removeElementAt();
try {s.P.run();} catch(RuntimeException e) {} finally {s.P.clear();}
}
static void test_first_last()
{StuckPA s = test_load();
s.firstElement();
s.P.run(); s.P.clear();
//stop(t);
ok(s.print(), """
Transaction(action:firstElement search:0 limit:0 found:1 index:0 key:2 data:1 size:4 isFull:0 isEmpty:0)
""");
s.lastElement();
s.P.run(); s.P.clear();
//stop(s);
ok(s.print(), """
Transaction(action:lastElement search:0 limit:0 found:1 index:3 key:8 data:4 size:4 isFull:0 isEmpty:0)
""");
s.clear();
sayThisOrStop("Empty");
s.firstElement();
try {s.P.run();} catch(RuntimeException e) {} finally {s.P.clear();}
}
static void test_search()
{StuckPA s = test_load();
s.P.new I() {void a() {s.T.at(s.search).setInt(2);}};
s.search();
s.P.run(); s.P.clear();
//stop(s);
ok(s.print(), """
Transaction(action:search search:2 limit:0 found:1 index:0 key:2 data:1 size:4 isFull:0 isEmpty:0)
""");
s.P.new I() {void a() {s.T.at(s.search).setInt(3);}};
s.search();
s.P.run(); s.P.clear();
//stop(s);
ok(s.print(), """
Transaction(action:search search:3 limit:0 found:0 index:4 key:8 data:1 size:4 isFull:0 isEmpty:0)
""");
s.P.new I() {void a() {s.T.at(s.search).setInt(8); }};
s.search();
s.P.run(); s.P.clear();
//stop(s);
ok(s.print(), """
Transaction(action:search search:8 limit:0 found:1 index:3 key:8 data:4 size:4 isFull:0 isEmpty:0)
""");
}
static void test_search_except_last()
{StuckPA s = test_load();
s.P.new I() {void a() {s.T.at(s.limit ).setInt(1);}};
s.P.new I() {void a() {s.T.at(s.search).setInt(4);}};
s.search();
s.P.run(); s.P.clear();
//stop(s);
ok(s.print(), """
Transaction(action:search search:4 limit:1 found:1 index:1 key:4 data:2 size:3 isFull:0 isEmpty:0)
""");
s.P.new I() {void a() {s.T.at(s.search).setInt(8);}};
s.search();
s.P.run(); s.P.clear();
//stop(t);
ok(s.print(), """
Transaction(action:search search:8 limit:1 found:0 index:3 key:6 data:2 size:3 isFull:0 isEmpty:0)
""");
}
static void test_search_first_greater_than_or_equal()
{StuckPA s = test_load();
s.P.new I() {void a() {s.T.at(s.search).setInt(5);}};
s.searchFirstGreaterThanOrEqual();
s.P.run(); s.P.clear();
//stop(t);
ok(s.print(), """
Transaction(action:searchFirstGreaterThanOrEqual search:5 limit:0 found:1 index:2 key:6 data:3 size:4 isFull:0 isEmpty:0)
""");
s.P.new I() {void a() {s.T.at(s.search).setInt(7);}};
s.searchFirstGreaterThanOrEqual();
s.P.run(); s.P.clear();
//stop(t);
ok(s.print(), """
Transaction(action:searchFirstGreaterThanOrEqual search:7 limit:0 found:1 index:3 key:8 data:4 size:4 isFull:0 isEmpty:0)
""");
}
static void test_search_first_greater_than_or_equal_except_last()
{StuckPA s = test_load();
s.P.new I() {void a() {s.T.at(s.limit).setInt(1);}};
s.P.new I() {void a() {s.T.at(s.search).setInt(5);}};
s.searchFirstGreaterThanOrEqual();
s.P.run(); s.P.clear();
//stop(t);
ok(s.print(), """
Transaction(action:searchFirstGreaterThanOrEqual search:5 limit:1 found:1 index:2 key:6 data:3 size:3 isFull:0 isEmpty:0)
""");
s.P.new I() {void a() {s.T.at(s.search).setInt(7);}};
s.searchFirstGreaterThanOrEqual();
s.P.run(); s.P.clear();
//stop(t);
ok(s.print(), """
Transaction(action:searchFirstGreaterThanOrEqual search:7 limit:1 found:0 index:3 key:6 data:3 size:3 isFull:0 isEmpty:0)
""");
}
static void test_at()
{final int N = 16;
final StuckPA D = new StuckPA("test")
{int maxSize () {return 4;}
int bitsPerKey () {return 8;}
int bitsPerData () {return 8;}
int bitsPerSize () {return 8;}