TSTree.st
author Claus Gittinger <cg@exept.de>
Tue, 25 Jun 2013 13:22:58 +0200
changeset 3012 bc770831871d
parent 2762 0532634862eb
child 3042 ad61ad4b21ee
permissions -rw-r--r--
added isFixedSize query
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2282
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     1
"{ Package: 'stx:libbasic2' }"
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     3
Collection subclass:#TSTree
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
	instanceVariableNames:'root'
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
	classVariableNames:''
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
	poolDictionaries:''
2762
0532634862eb category changes
Claus Gittinger <cg@exept.de>
parents: 2468
diff changeset
     7
	category:'Collections-Ordered-Trees'
2282
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
Object subclass:#TSTreeNode
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
	instanceVariableNames:'key value low high equal'
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
	classVariableNames:''
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
	poolDictionaries:''
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
	privateIn:TSTree
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
!TSTree class methodsFor:'documentation'!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
documentation
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
"
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
    A bunch of collection classes that are useful for building large indices of things. 
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
    It's especially geared towards people using OODBs like GOODS, but can be used it in the image too: 
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
    the BTree class is great for when you need to select numeric keys by range, 
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
    and TSTree makes a solid basis for full-text search. 
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
    TreeSet has an interesting optimized #intersection: that lets you compare two collections without 
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
    looking at every item of either. 
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
    I'm also going to be rolling some code in here from Benjamin Pollack specifically aimed at indexing 
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
    by date ranges, which lets you do quick queries of all the events that overlap with a specific week, 
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
    for instance. 
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
    Also in the BTree package is a TSTree, which has similar properties for String 
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
    keys.  So as well as keeping them sorted, you can do efficient lookups of all 
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
    the keys with a given prefix.  One other neat trick TSTree can do is a certain 
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
    amount of fuzzy matching (eg find all keys with an edit distance of 3 from 
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
    'foo') which makes it especially useful for spell checking and similar 
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
    applications.
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
    [author:]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
        Avi Bryant
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
    [license:]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
        Dual licensed under both SqueakL and MIT. 
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
        This enables both base Squeak inclusion and 100% reuse.
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
"
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
! !
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
!TSTree methodsFor:'accessing'!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
at: aString
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
	^ self at: aString ifAbsent: [self error: aString printString, ' not found']
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
at: aString ifAbsent: exceptionBlock
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
	aString isEmpty ifTrue: [^ exceptionBlock value].
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
	root ifNil: [^ exceptionBlock value].
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
	
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
	^ (root lookupString: aString startingAt: 1) ifNil: exceptionBlock
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
at: aString ifAbsentPut: exceptionBlock
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
	^ self at: aString ifAbsent: [self at: aString put: exceptionBlock value]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
at: aString put: anObject
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
	aString isEmpty ifTrue: [self error: 'Keys cannot be empty'].
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
	root ifNil: [root _ TSTreeNode key: aString first].
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
	root lookupString: aString startingAt: 1 insert: anObject.
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
	^ anObject
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
removeKey: aString
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
	^ self removeKey: aString ifAbsent: [self error: 'Could not find key ', aString printString]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
removeKey: aString ifAbsent: errorBlock
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
	| val |
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
	val _ root removeString: aString startingAt: 1.
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
	root canBeCulled ifTrue: [root _ nil].
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
	^ val ifNil: errorBlock
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
values
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
	^ Array streamContents: [:s | self do: [:ea | s nextPut: ea]]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
! !
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
3012
bc770831871d added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2762
diff changeset
    86
!TSTree methodsFor:'enumerating'!
bc770831871d added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2762
diff changeset
    87
bc770831871d added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2762
diff changeset
    88
do: aBlock
bc770831871d added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2762
diff changeset
    89
        root notNil ifTrue: [root do: aBlock]
bc770831871d added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2762
diff changeset
    90
bc770831871d added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2762
diff changeset
    91
    "Modified: / 08-08-2010 / 15:16:53 / cg"
bc770831871d added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2762
diff changeset
    92
! !
bc770831871d added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2762
diff changeset
    93
bc770831871d added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2762
diff changeset
    94
!TSTree methodsFor:'searching'!
2282
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
matchesForPrefix: aString
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
	^ Array streamContents:
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
		[:s |
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
		self matchesForPrefix: aString do:
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
			[:match |
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
			s nextPut: match]]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
matchesForPrefix: aString do: aBlock
2468
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   105
        aString isEmpty
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   106
                ifTrue: [self do: aBlock]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   107
                ifFalse: [root notNil ifTrue: [root matchesForPrefix: aString startingAt: 1 do: aBlock]]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   108
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   109
    "Modified: / 08-08-2010 / 15:17:01 / cg"
2282
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
matchesForString: aString distance: aNumber
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
	^ Array streamContents:
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
		[:s |
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
		self matchesForString: aString distance: aNumber do:
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
			[:match |
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
			s nextPut: match]]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
matchesForString: aString distance: aNumber do: aBlock
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
	self matchesForString: aString distance: aNumber limitNodes: nil do: aBlock
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
matchesForString: aString distance: aNumber limitNodes: maxNodes do: aBlock
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
	| nodeCount |
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   126
	nodeCount _ 0.
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   127
	aString isEmpty ifTrue: [^ self].
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   128
	root isNil ifTrue: [^ self].
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   129
	root matchesForString: aString startingAt: 1 distance: aNumber do: aBlock nodesDo:
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   130
		[:ea |
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
		nodeCount _ nodeCount + 1.
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
		nodeCount = maxNodes ifTrue: [^ self]]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
! !
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   134
3012
bc770831871d added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2762
diff changeset
   135
!TSTree methodsFor:'testing'!
2282
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   136
3012
bc770831871d added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2762
diff changeset
   137
isFixedSize
bc770831871d added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2762
diff changeset
   138
    "return true if the receiver cannot grow"
2468
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   139
3012
bc770831871d added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2762
diff changeset
   140
    ^ false
2282
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   141
! !
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   142
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   143
!TSTree::TSTreeNode class methodsFor:'as yet unclassified'!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   144
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   145
key: aCharacter
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   146
	^ self basicNew initializeWithKey: aCharacter
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   147
! !
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   148
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   149
!TSTree::TSTreeNode class methodsFor:'documentation'!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   150
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   151
documentation
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   152
"
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   153
    [author:]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   154
        Avi Bryant
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   155
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   156
    [license:]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   157
        Dual licensed under both SqueakL and MIT. 
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   158
        This enables both base Squeak inclusion and 100% reuse.
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   159
"
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   160
! !
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   161
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   162
!TSTree::TSTreeNode methodsFor:'private'!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   163
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   164
canBeCulled
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   165
	^ self value isNil
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   166
		and: [low isNil]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   167
		and: [equal isNil]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   168
		and: [high isNil]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   169
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   170
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   171
cullNode: aNode
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   172
	low == aNode ifTrue: [^ low _ nil].
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   173
	equal == aNode ifTrue: [^ equal _ nil].
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   174
	high == aNode ifTrue: [^ high _ nil]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   175
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   176
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   177
do: aBlock
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   178
	self nodesDo: [:ea | ea value ifNotNilDo: aBlock]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   179
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   180
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   181
initializeWithKey: aCharacter
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   182
	key _ aCharacter
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   183
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   184
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   185
lookupString: aString startingAt: i
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   186
"inlined for performance"
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   187
"
2468
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   188
        self
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   189
                lookupString: aString
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   190
                startingAt: i
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   191
                whenFound: [^ value]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   192
                whenNil: [:c | ^ nil]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   193
                recurseWith: [:node :j | ^ node lookupString: aString startingAt: j]"
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   194
        | char |
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   195
        char := aString at: i.
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   196
        char = key
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   197
                ifTrue:
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   198
                        [aString size = i
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   199
                                ifTrue: [^ value]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   200
                                ifFalse: [^ equal notNil ifTrue: [equal lookupString: aString startingAt: i+1] ifFalse:[nil]]]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   201
                ifFalse:
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   202
                        [char < key
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   203
                                ifTrue: [^ low notNil ifTrue: [low lookupString: aString startingAt: i] ifFalse:[nil]]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   204
                                ifFalse: [^ high notNil ifTrue: [high lookupString: aString startingAt: i] ifFalse:[nil]]]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   205
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   206
    "Modified: / 08-08-2010 / 15:18:03 / cg"
2282
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   207
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   208
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   209
lookupString: aString startingAt: i insert: anObject
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   210
	self
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   211
		lookupString: aString
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   212
		startingAt: i
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   213
		whenFound: [self value: anObject]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   214
		whenNil: [:c | self newNodeWithKey: c]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   215
		recurseWith: [:node :j | node lookupString: aString startingAt: j insert: anObject]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   216
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   217
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   218
lookupString: aString startingAt: i whenFound: foundBlock whenNil: nilBlock recurseWith: recurseBlock
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   219
	| char |
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   220
	char := aString at: i.
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   221
	char = key
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   222
		ifTrue:
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   223
			[aString size = i
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   224
				ifTrue: [foundBlock value]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   225
				ifFalse: [equal ifNil: [equal := nilBlock value: (aString at: i+1)].
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   226
						 recurseBlock value: equal value: i+1]]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   227
		ifFalse:
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   228
			[char < key
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   229
				ifTrue: [low ifNil: [low := nilBlock value: char].
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   230
						recurseBlock value: low value: i]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   231
				ifFalse: [high ifNil: [high := nilBlock value: char].
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   232
						recurseBlock value: high value: i]]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   233
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   234
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   235
matchesForPrefix: aString startingAt: i do: aBlock
2468
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   236
        self
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   237
                lookupString: aString
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   238
                startingAt: i
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   239
                whenFound: [value notNil ifTrue: [aBlock value: value].  equal notNil ifTrue: [equal do: aBlock]]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   240
                whenNil: [:c | ^ self]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   241
                recurseWith: [:n :j | n matchesForPrefix: aString startingAt: j do: aBlock]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   242
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   243
    "Modified: / 08-08-2010 / 15:18:44 / cg"
2282
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   244
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   245
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   246
matchesForString: aString startingAt: i distance: d do: aBlock nodesDo: nodeBlock
2468
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   247
        
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   248
        | char d2 |
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   249
        nodeBlock value: self.
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   250
        d < 0 ifTrue: [^ self].
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   251
        
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   252
        char := aString at: i.
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   253
        (d > 0 or: [char < key])
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   254
                ifTrue: [low notNil ifTrue: [low matchesForString: aString startingAt: i distance: d do: aBlock nodesDo: nodeBlock]].
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   255
                
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   256
        d2 := char = key ifTrue: [d] ifFalse: [d-1].
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   257
        (i + d2 = aString size and: [value notNil]) ifTrue: [aBlock value: value].
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   258
        equal ifNotNil: [equal matchesForString: aString startingAt: (i+1 min: aString size) distance: d2 do: aBlock nodesDo: nodeBlock].
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   259
        
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   260
        (d > 0 or: [char > key])
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   261
                ifTrue: [high notNil ifTrue: [high matchesForString: aString startingAt: i distance: d do: aBlock nodesDo: nodeBlock]]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   262
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   263
    "Modified: / 08-08-2010 / 15:18:57 / cg"
2282
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   264
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   265
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   266
newNodeWithKey: aCharacter
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   267
	^ self class key: aCharacter
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   268
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   269
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   270
nodesDo: aBlock
2468
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   271
        aBlock value: self.
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   272
        low notNil ifTrue: [low nodesDo: aBlock].
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   273
        equal notNil ifTrue: [equal nodesDo: aBlock].
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   274
        high notNil ifTrue: [high nodesDo: aBlock]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   275
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   276
    "Modified: / 08-08-2010 / 15:19:05 / cg"
2282
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   277
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   278
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   279
removeString: aString startingAt: i
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   280
	| val |
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   281
	self
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   282
		lookupString: aString
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   283
		startingAt: i
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   284
		whenFound: [val _ self value. self value: nil]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   285
		whenNil: [:c | ^ nil]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   286
		recurseWith:
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   287
			[:node :j |
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   288
			val _ node removeString: aString startingAt: j.
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   289
			node canBeCulled ifTrue:
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   290
				[self cullNode: node]].
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   291
	^ val
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   292
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   293
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   294
value
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   295
	^ value
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   296
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   297
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   298
value: anObject
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   299
	value _ anObject
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   300
! !
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   301
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   302
!TSTree class methodsFor:'documentation'!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   303
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   304
version_CVS
3012
bc770831871d added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2762
diff changeset
   305
    ^ '$Header: /cvs/stx/stx/libbasic2/TSTree.st,v 1.4 2013-06-25 11:22:58 cg Exp $'
2282
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   306
! !
3012
bc770831871d added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2762
diff changeset
   307