2283
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
3712
|
3 |
"{ NameSpace: Smalltalk }"
|
|
4 |
|
2283
|
5 |
Collection subclass:#TreeSet
|
|
6 |
instanceVariableNames:'tree sortKey'
|
|
7 |
classVariableNames:''
|
|
8 |
poolDictionaries:''
|
2767
|
9 |
category:'Collections-Ordered-Trees'
|
2283
|
10 |
!
|
|
11 |
|
|
12 |
!TreeSet class methodsFor:'documentation'!
|
|
13 |
|
|
14 |
documentation
|
|
15 |
"
|
4122
|
16 |
BTree and TSTree
|
|
17 |
|
4211
|
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 |
I'm also going to be rolling some code in here from Benjamin Pollack specifically aimed at indexing
|
|
25 |
by date ranges, which lets you do quick queries of all the events that overlap with a specific week,
|
|
26 |
for instance.
|
2283
|
27 |
|
|
28 |
[author:]
|
|
29 |
Avi Bryant
|
|
30 |
|
|
31 |
[license:]
|
4211
|
32 |
Dual licensed under both SqueakL and MIT.
|
2283
|
33 |
This enables both base Squeak inclusion and 100% reuse.
|
|
34 |
"
|
|
35 |
! !
|
|
36 |
|
4211
|
37 |
!TreeSet class methodsFor:'instance creation'!
|
2283
|
38 |
|
|
39 |
keys: aBtreeKeys sortSelector: aSymbol
|
|
40 |
^ self basicNew initializeWithKeys: aBtreeKeys sortSelector: aSymbol
|
|
41 |
!
|
|
42 |
|
|
43 |
new
|
|
44 |
^ self sortBy: #hash
|
|
45 |
!
|
|
46 |
|
|
47 |
sortBy: aSymbol
|
|
48 |
^ self keys: (BTreeKeysArray new: 64) sortSelector: aSymbol
|
|
49 |
! !
|
|
50 |
|
|
51 |
!TreeSet methodsFor:'initialize-release'!
|
|
52 |
|
|
53 |
initializeWithKeys: aBtreeKeys sortSelector: aSymbol
|
|
54 |
tree _ BTree keys: aBtreeKeys.
|
|
55 |
sortKey _ aSymbol
|
|
56 |
! !
|
|
57 |
|
|
58 |
!TreeSet methodsFor:'plugs'!
|
|
59 |
|
|
60 |
keyForValue: anObject
|
|
61 |
^ anObject perform: sortKey
|
|
62 |
!
|
|
63 |
|
|
64 |
value: anObject matches: otherObject
|
|
65 |
^ anObject = otherObject
|
|
66 |
! !
|
|
67 |
|
|
68 |
!TreeSet methodsFor:'private'!
|
|
69 |
|
|
70 |
bucket: anArray includes: anObject
|
|
71 |
^ anArray anySatisfy: [:ea | (self value: anObject matches: ea)]
|
|
72 |
!
|
|
73 |
|
|
74 |
tree
|
|
75 |
^ tree
|
|
76 |
! !
|
|
77 |
|
|
78 |
!TreeSet methodsFor:'public'!
|
|
79 |
|
|
80 |
add: anObject
|
|
81 |
| key bucket |
|
|
82 |
key _ self keyForValue: anObject.
|
|
83 |
bucket _ tree at: key ifAbsent: [#()].
|
|
84 |
(self bucket: bucket includes: anObject) ifFalse:
|
|
85 |
[tree at: key put: (bucket copyWith: anObject)].
|
|
86 |
!
|
|
87 |
|
|
88 |
do: aBlock
|
|
89 |
tree do: [:bucket | bucket do: aBlock]
|
|
90 |
!
|
|
91 |
|
|
92 |
includes: anObject
|
|
93 |
| bucket |
|
|
94 |
bucket _ tree at: (self keyForValue: anObject) ifAbsent: [^ false].
|
|
95 |
^ self bucket: bucket includes: anObject
|
|
96 |
!
|
|
97 |
|
4269
|
98 |
intersect:aCollection
|
|
99 |
aCollection species ~~ self species ifTrue:[
|
|
100 |
^ super intersect:aCollection
|
|
101 |
].
|
|
102 |
aCollection sortSelector ~= self sortSelector ifTrue:[
|
|
103 |
^ super intersect:aCollection
|
|
104 |
].
|
|
105 |
^ Array
|
|
106 |
streamContents:[:s |
|
|
107 |
tree commonKeysWith:aCollection tree
|
|
108 |
keysAndValuesDo:[:key :left :right | s nextPutAll:(left intersect:right)]
|
|
109 |
]
|
|
110 |
|
|
111 |
"Created: / 20-01-2017 / 19:23:01 / stefan"
|
2283
|
112 |
!
|
|
113 |
|
|
114 |
remove: anObject
|
3712
|
115 |
"remove all occurrences of anObject
|
|
116 |
Warning:
|
|
117 |
This is inconsistent with the inherited collection>>remove:,
|
|
118 |
which only remove the first occurrence.
|
|
119 |
Should this be fixed?"
|
4211
|
120 |
|
3712
|
121 |
| key bucket |
|
4211
|
122 |
|
3712
|
123 |
key := self keyForValue: anObject.
|
|
124 |
bucket := tree at: key ifAbsent: [^ self].
|
|
125 |
(self bucket: bucket includes: anObject) ifTrue:[
|
|
126 |
"/ cg: wrong code here: removes all, not only the first match
|
|
127 |
bucket := bucket reject: [:ea | self value: anObject matches: ea].
|
|
128 |
bucket isEmpty
|
|
129 |
ifTrue: [tree removeKey: key]
|
|
130 |
ifFalse: [tree at: key put: bucket]
|
|
131 |
]
|
2283
|
132 |
!
|
|
133 |
|
|
134 |
sortSelector
|
|
135 |
^ sortKey
|
|
136 |
! !
|
|
137 |
|
3011
|
138 |
!TreeSet methodsFor:'testing'!
|
|
139 |
|
|
140 |
isFixedSize
|
|
141 |
"return true if the receiver cannot grow"
|
|
142 |
|
|
143 |
^ false
|
|
144 |
! !
|
|
145 |
|
2283
|
146 |
!TreeSet class methodsFor:'documentation'!
|
|
147 |
|
|
148 |
version_CVS
|
3712
|
149 |
^ '$Header$'
|
2283
|
150 |
! !
|
3011
|
151 |
|