author | Claus Gittinger <cg@exept.de> |
Tue, 25 Jun 2019 14:22:47 +0200 | |
changeset 5049 | 0d48cb487e0c |
parent 4734 | 5f69ef333e9d |
permissions | -rw-r--r-- |
2282 | 1 |
"{ Package: 'stx:libbasic2' }" |
2 |
||
3668 | 3 |
"{ NameSpace: Smalltalk }" |
4 |
||
4364 | 5 |
KeyedCollection subclass:#TSTree |
2282 | 6 |
instanceVariableNames:'root' |
7 |
classVariableNames:'' |
|
8 |
poolDictionaries:'' |
|
2762 | 9 |
category:'Collections-Ordered-Trees' |
2282 | 10 |
! |
11 |
||
12 |
!TSTree class methodsFor:'documentation'! |
|
13 |
||
14 |
documentation |
|
15 |
" |
|
4121 | 16 |
BTree and TSTree |
17 |
||
3668 | 18 |
A bunch of collection classes that are useful for building large indices of things. |
19 |
It's especially geared towards people using OODBs like GOODS, but can be used it in the image too: |
|
20 |
the BTree class is great for when you need to select numeric keys by range, |
|
21 |
and TSTree makes a solid basis for full-text search. |
|
22 |
TreeSet has an interesting optimized #intersection: that lets you compare two collections without |
|
23 |
looking at every item of either. |
|
24 |
||
3253
5c005be2975e
Added TSMultiTree, a variant of TSTree that allows for multiple values to be stored
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3042
diff
changeset
|
25 |
TSTree has similar properties as BTree for String keys. |
5c005be2975e
Added TSMultiTree, a variant of TSTree that allows for multiple values to be stored
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3042
diff
changeset
|
26 |
So as well as keeping them sorted, you can do efficient lookups of all |
2282 | 27 |
the keys with a given prefix. One other neat trick TSTree can do is a certain |
28 |
amount of fuzzy matching (eg find all keys with an edit distance of 3 from |
|
29 |
'foo') which makes it especially useful for spell checking and similar |
|
30 |
applications. |
|
31 |
||
32 |
[author:] |
|
33 |
Avi Bryant |
|
34 |
||
35 |
[license:] |
|
36 |
Dual licensed under both SqueakL and MIT. |
|
37 |
This enables both base Squeak inclusion and 100% reuse. |
|
38 |
" |
|
39 |
! ! |
|
40 |
||
3812 | 41 |
!TSTree class methodsFor:'queries'! |
42 |
||
43 |
treeNodeClass |
|
44 |
^ TSTreeNode |
|
45 |
! ! |
|
46 |
||
2282 | 47 |
!TSTree methodsFor:'accessing'! |
48 |
||
49 |
at: aString ifAbsent: exceptionBlock |
|
3842 | 50 |
|ret| |
51 |
||
52 |
aString isEmpty ifTrue: [^ exceptionBlock value]. |
|
53 |
root isNil ifTrue: [^ exceptionBlock value]. |
|
54 |
||
55 |
ret := root lookupString: aString startingAt: 1. |
|
56 |
ret notNil ifTrue:[^ ret]. |
|
57 |
^ exceptionBlock value |
|
2282 | 58 |
! |
59 |
||
60 |
at: aString ifAbsentPut: exceptionBlock |
|
61 |
^ self at: aString ifAbsent: [self at: aString put: exceptionBlock value] |
|
62 |
! |
|
63 |
||
64 |
at: aString put: anObject |
|
3253
5c005be2975e
Added TSMultiTree, a variant of TSTree that allows for multiple values to be stored
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3042
diff
changeset
|
65 |
aString isEmpty ifTrue: [self error: 'Keys cannot be empty']. |
3812 | 66 |
root isNil ifTrue: [root := self class treeNodeClass key: aString first]. |
3253
5c005be2975e
Added TSMultiTree, a variant of TSTree that allows for multiple values to be stored
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3042
diff
changeset
|
67 |
root lookupString: aString startingAt: 1 insert: anObject. |
5c005be2975e
Added TSMultiTree, a variant of TSTree that allows for multiple values to be stored
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3042
diff
changeset
|
68 |
^ anObject |
5c005be2975e
Added TSMultiTree, a variant of TSTree that allows for multiple values to be stored
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3042
diff
changeset
|
69 |
|
5c005be2975e
Added TSMultiTree, a variant of TSTree that allows for multiple values to be stored
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3042
diff
changeset
|
70 |
"Modified: / 26-04-2014 / 11:23:02 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
2282 | 71 |
! |
72 |
||
73 |
removeKey: aString |
|
74 |
^ self removeKey: aString ifAbsent: [self error: 'Could not find key ', aString printString] |
|
75 |
! |
|
76 |
||
77 |
removeKey: aString ifAbsent: errorBlock |
|
3842 | 78 |
| val | |
79 |
val := root removeString:aString startingAt:1. |
|
80 |
root canBeCulled ifTrue: [root := nil]. |
|
81 |
val notNil ifTrue:[^ val]. |
|
82 |
^ errorBlock value |
|
2282 | 83 |
! |
84 |
||
85 |
values |
|
86 |
^ Array streamContents: [:s | self do: [:ea | s nextPut: ea]] |
|
87 |
! ! |
|
88 |
||
3012 | 89 |
!TSTree methodsFor:'enumerating'! |
90 |
||
91 |
do: aBlock |
|
92 |
root notNil ifTrue: [root do: aBlock] |
|
93 |
||
94 |
"Modified: / 08-08-2010 / 15:16:53 / cg" |
|
95 |
! ! |
|
96 |
||
97 |
!TSTree methodsFor:'searching'! |
|
2282 | 98 |
|
99 |
matchesForPrefix: aString |
|
100 |
^ Array streamContents: |
|
101 |
[:s | |
|
102 |
self matchesForPrefix: aString do: |
|
103 |
[:match | |
|
104 |
s nextPut: match]] |
|
105 |
! |
|
106 |
||
107 |
matchesForPrefix: aString do: aBlock |
|
2468 | 108 |
aString isEmpty |
109 |
ifTrue: [self do: aBlock] |
|
110 |
ifFalse: [root notNil ifTrue: [root matchesForPrefix: aString startingAt: 1 do: aBlock]] |
|
111 |
||
112 |
"Modified: / 08-08-2010 / 15:17:01 / cg" |
|
2282 | 113 |
! |
114 |
||
115 |
matchesForString: aString distance: aNumber |
|
116 |
^ Array streamContents: |
|
117 |
[:s | |
|
118 |
self matchesForString: aString distance: aNumber do: |
|
119 |
[:match | |
|
120 |
s nextPut: match]] |
|
121 |
! |
|
122 |
||
123 |
matchesForString: aString distance: aNumber do: aBlock |
|
124 |
self matchesForString: aString distance: aNumber limitNodes: nil do: aBlock |
|
125 |
! |
|
126 |
||
127 |
matchesForString: aString distance: aNumber limitNodes: maxNodes do: aBlock |
|
3842 | 128 |
| nodeCount | |
129 |
nodeCount := 0. |
|
130 |
aString isEmpty ifTrue: [^ self]. |
|
131 |
root isNil ifTrue: [^ self]. |
|
132 |
root matchesForString: aString startingAt: 1 distance: aNumber do: aBlock nodesDo: |
|
133 |
[:ea | |
|
134 |
nodeCount := nodeCount + 1. |
|
135 |
nodeCount = maxNodes ifTrue: [^ self]] |
|
2282 | 136 |
! ! |
137 |
||
3012 | 138 |
!TSTree methodsFor:'testing'! |
2282 | 139 |
|
3012 | 140 |
isFixedSize |
141 |
"return true if the receiver cannot grow" |
|
2468 | 142 |
|
3012 | 143 |
^ false |
2282 | 144 |
! ! |
145 |
||
146 |
!TSTree class methodsFor:'documentation'! |
|
147 |
||
148 |
version_CVS |
|
3668 | 149 |
^ '$Header$' |
2282 | 150 |
! ! |
3012 | 151 |