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