2283
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
|
3 |
Collection subclass:#TreeSet
|
|
4 |
instanceVariableNames:'tree sortKey'
|
|
5 |
classVariableNames:''
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'Collections-Ordered'
|
|
8 |
!
|
|
9 |
|
|
10 |
!TreeSet class methodsFor:'documentation'!
|
|
11 |
|
|
12 |
documentation
|
|
13 |
"
|
|
14 |
A bunch of collection classes that are useful for building large indices of things.
|
|
15 |
It's especially geared towards people using OODBs like GOODS, but can be used it in the image too:
|
|
16 |
the BTree class is great for when you need to select numeric keys by range,
|
|
17 |
and TSTree makes a solid basis for full-text search.
|
|
18 |
TreeSet has an interesting optimized #intersection: that lets you compare two collections without
|
|
19 |
looking at every item of either.
|
|
20 |
I'm also going to be rolling some code in here from Benjamin Pollack specifically aimed at indexing
|
|
21 |
by date ranges, which lets you do quick queries of all the events that overlap with a specific week,
|
|
22 |
for instance.
|
|
23 |
|
|
24 |
[author:]
|
|
25 |
Avi Bryant
|
|
26 |
|
|
27 |
[license:]
|
|
28 |
Dual licensed under both SqueakL and MIT.
|
|
29 |
This enables both base Squeak inclusion and 100% reuse.
|
|
30 |
"
|
|
31 |
! !
|
|
32 |
|
|
33 |
!TreeSet class methodsFor:'as yet unclassified'!
|
|
34 |
|
|
35 |
keys: aBtreeKeys sortSelector: aSymbol
|
|
36 |
^ self basicNew initializeWithKeys: aBtreeKeys sortSelector: aSymbol
|
|
37 |
!
|
|
38 |
|
|
39 |
new
|
|
40 |
^ self sortBy: #hash
|
|
41 |
!
|
|
42 |
|
|
43 |
sortBy: aSymbol
|
|
44 |
^ self keys: (BTreeKeysArray new: 64) sortSelector: aSymbol
|
|
45 |
! !
|
|
46 |
|
|
47 |
!TreeSet methodsFor:'initialize-release'!
|
|
48 |
|
|
49 |
initializeWithKeys: aBtreeKeys sortSelector: aSymbol
|
|
50 |
tree _ BTree keys: aBtreeKeys.
|
|
51 |
sortKey _ aSymbol
|
|
52 |
! !
|
|
53 |
|
|
54 |
!TreeSet methodsFor:'plugs'!
|
|
55 |
|
|
56 |
keyForValue: anObject
|
|
57 |
^ anObject perform: sortKey
|
|
58 |
!
|
|
59 |
|
|
60 |
value: anObject matches: otherObject
|
|
61 |
^ anObject = otherObject
|
|
62 |
! !
|
|
63 |
|
|
64 |
!TreeSet methodsFor:'private'!
|
|
65 |
|
|
66 |
bucket: anArray includes: anObject
|
|
67 |
^ anArray anySatisfy: [:ea | (self value: anObject matches: ea)]
|
|
68 |
!
|
|
69 |
|
|
70 |
tree
|
|
71 |
^ tree
|
|
72 |
! !
|
|
73 |
|
|
74 |
!TreeSet methodsFor:'public'!
|
|
75 |
|
|
76 |
add: anObject
|
|
77 |
| key bucket |
|
|
78 |
key _ self keyForValue: anObject.
|
|
79 |
bucket _ tree at: key ifAbsent: [#()].
|
|
80 |
(self bucket: bucket includes: anObject) ifFalse:
|
|
81 |
[tree at: key put: (bucket copyWith: anObject)].
|
|
82 |
!
|
|
83 |
|
|
84 |
do: aBlock
|
|
85 |
tree do: [:bucket | bucket do: aBlock]
|
|
86 |
!
|
|
87 |
|
|
88 |
includes: anObject
|
|
89 |
| bucket |
|
|
90 |
bucket _ tree at: (self keyForValue: anObject) ifAbsent: [^ false].
|
|
91 |
^ self bucket: bucket includes: anObject
|
|
92 |
!
|
|
93 |
|
|
94 |
intersection: aCollection
|
|
95 |
aCollection species = self species ifFalse: [^ super intersection: aCollection].
|
|
96 |
aCollection sortSelector = self sortSelector ifFalse: [^ super intersection: aCollection].
|
|
97 |
|
|
98 |
^ Array streamContents:
|
|
99 |
[:s |
|
|
100 |
tree commonKeysWith: aCollection tree keysAndValuesDo:
|
|
101 |
[:key :left :right |
|
|
102 |
s nextPutAll: (left intersection: right)]]
|
|
103 |
!
|
|
104 |
|
|
105 |
remove: anObject
|
|
106 |
| key bucket |
|
|
107 |
key _ self keyForValue: anObject.
|
|
108 |
bucket _ tree at: key ifAbsent: [^ self].
|
|
109 |
(self bucket: bucket includes: anObject) ifTrue:
|
|
110 |
[bucket _ bucket reject: [:ea | self value: anObject matches: ea].
|
|
111 |
bucket isEmpty
|
|
112 |
ifTrue: [tree removeKey: key]
|
|
113 |
ifFalse: [tree at: key put: bucket]]
|
|
114 |
!
|
|
115 |
|
|
116 |
sortSelector
|
|
117 |
^ sortKey
|
|
118 |
! !
|
|
119 |
|
|
120 |
!TreeSet class methodsFor:'documentation'!
|
|
121 |
|
|
122 |
version_CVS
|
|
123 |
^ '$Header: /cvs/stx/stx/libbasic2/TreeSet.st,v 1.1 2009-09-30 13:38:40 cg Exp $'
|
|
124 |
! !
|