Browse code

Extracted binary tree library from asma

Andrew Alderwick authored on 06/10/2021 05:41:23
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,73 @@
1
+(
2
+
3
+binary tree node layout:
4
+
5
++--+--+
6
+|  '  | incoming-ptr*
7
++--+--+                 key: null   optional
8
+   v       left  right  terminated   binary
9
+   |        ptr   ptr     string      data
10
+    \     +--+--+--+--+---------+--+----- - -
11
+     ---> |  '  |  '  |  U x n  .00|
12
+          +--+--+--+--+---------+--+----- - -
13
+
14
+All of the pointers (ptr) are shorts that have the value of the memory
15
+location of the next node, or 0000 to mean that pointer is empty. The very
16
+simplest tree is one where the incoming-ptr* is empty:
17
+
18
++--+--+
19
+|00'00| incoming-ptr*
20
++--+--+
21
+
22
+traverse-tree does two jobs at once, depending on whether the search-key is
23
+found:
24
+
25
+* if the search-key exists in the tree, return a pointer to the binary data
26
+  that follows that node's key string;
27
+
28
+* if the search-key is not present in the key, return the incoming-ptr* that
29
+  should be written when adding this node yourself.
30
+
31
+)
32
+
33
+@traverse-tree ( incoming-ptr* search-key* -- binary-ptr* 00 if key found
34
+                                           OR node-incoming-ptr* 01 if key not found )
35
+	STH2
36
+	&loop ( incoming-ptr* / search-key* )
37
+	LDA2k ORA ,&valid-node JCN
38
+	POP2r #01 JMP2r
39
+
40
+	&valid-node ( incoming-ptr* / search-key* )
41
+	LDA2 ( node* / search-key* )
42
+	DUP2 #0004 ADD2 ( node* node-key* / search-key* )
43
+	STH2kr ( node* node-key* search-key* / search-key* )
44
+	,strcmp JSR ( node* node-end* search-end* order nomatch / search-key* )
45
+	,&nomatch JCN ( node* node-end* search-end* order / search-key* )
46
+	POP POP2 ( node* node-end* / search-key* )
47
+	INC2 NIP2 ( binary-ptr* / search-key* )
48
+	POP2r #00 ( binary-ptr* 00 )
49
+	JMP2r
50
+
51
+	&nomatch ( node* node-end* search-end* order / search-key* )
52
+	#80 AND #06 SFT #00 SWP STH2 ( node* node-end* search-end* / search-key* node-offset^ )
53
+	POP2 POP2 ( node* / search-key* node-offset^ )
54
+	STH2r ADD2 ( incoming-ptr* / search-key* )
55
+	,&loop JMP
56
+
57
+@strcmp ( a* b* -- a-end* b-end* order nonzero if strings differ
58
+                OR a-end* b-end* 00    00      if strings match )
59
+	STH2
60
+	,&entry JMP
61
+
62
+	&loop ( a* a b / b* )
63
+	SUB ,&nomatch JCNk ( a* a-b nonzero / b* )
64
+	POP2 ( a* / b* )
65
+	INC2 INC2r
66
+	&entry ( a* / b* )
67
+	LDAk LDAkr STHr ( a* a b / b* )
68
+	ORAk ,&loop JCN
69
+
70
+	&nomatch ( a* a-b flag / b* )
71
+	STH2r SWP2 ( a* b* a-b flag )
72
+	JMP2r
73
+
... ...
@@ -583,45 +583,10 @@ include projects/library/file-read-chunks.tal
583 583
 
584 584
 @asma-traverse-tree ( incoming-ptr* -- binary-ptr* 00 if key found
585 585
                                     OR node-incoming-ptr* 01 if key not found )
586
-	&loop ( incoming-ptr* )
587
-	LDA2k ORA ,&valid-node JCN
588
-	#01 JMP2r
589
-
590
-	&valid-node
591
-	LDA2 STH2k
592
-	#0004 ADD2 ,asma-strcmp-tree JSR
593
-	DUP ,&nomatch JCN
594
-	POP2r JMP2r
595
-
596
-	&nomatch
597
-	#06 SFT #02 AND #00 SWP
598
-	STH2r ADD2
599
-	,&loop JMP
600
-
601
-	( &help-str "Looking 20 "up 20 00 )
602
-
603
-@asma-strcmp-tree ( node-key* -- order if strings differ
604
-                              OR after-node-key* 00 if strings match )
605
-	;asma/token LDA2 STH2
606
-
607
-	&loop ( node-key* / token* )
608
-	DUP2 INC2 SWP2 LDA LDAkr STHr
609
-	ORAk ,&not-end JCN
610
-
611
-	( end of C strings, match found )
612
-	POP2r POP
613
-	JMP2r
614
-
615
-	&not-end
616
-	SUB
617
-	DUP ,&nomatch JCN
618
-	POP
619
-	LIT2r 0001 ADD2r
620
-	,&loop JMP
586
+	;asma/token LDA2
587
+	( fall through to traverse-tree )
621 588
 
622
-	&nomatch
623
-	POP2r ROT ROT POP2
624
-	JMP2r
589
+include projects/library/binary-tree.tal
625 590
 
626 591
 (
627 592
 	First character routines.
... ...
@@ -878,8 +843,8 @@ include projects/library/file-read-chunks.tal
878 843
 
879 844
 	&not-macro
880 845
 	POP2
881
-	;&include-string ;asma-strcmp-tree JSR2 ,&not-include JCN
882
-	POP2 ( discard dummy after-node-key* )
846
+	;&include-string ;asma/token LDA2
847
+		;strcmp JSR2 NIP2 NIP2 NIP ,&not-include JCN
883 848
 	#08 asma-STATE-SET
884 849
 	JMP2r
885 850