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