TSTree.st
author Claus Gittinger <cg@exept.de>
Sun, 08 Aug 2010 15:21:30 +0200
changeset 2468 36c571b96063
parent 2282 a723f74eaae4
child 2762 0532634862eb
permissions -rw-r--r--
changed: #do: #matchesForPrefix:do: code cleanup
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:''
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
	category:'Collections-Ordered'
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
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
!TSTree methodsFor:'as yet unclassified'!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
matchesForPrefix: aString
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
	^ Array streamContents:
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
		[:s |
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
		self matchesForPrefix: aString do:
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
			[:match |
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
			s nextPut: match]]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
!
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 do: aBlock
2468
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
    97
        aString isEmpty
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
    98
                ifTrue: [self do: aBlock]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
    99
                ifFalse: [root notNil ifTrue: [root matchesForPrefix: aString startingAt: 1 do: aBlock]]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   100
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   101
    "Modified: / 08-08-2010 / 15:17:01 / cg"
2282
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
matchesForString: aString distance: aNumber
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
	^ Array streamContents:
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
		[:s |
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
		self matchesForString: aString distance: aNumber do:
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
			[:match |
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
			s nextPut: match]]
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 do: aBlock
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
	self matchesForString: aString distance: aNumber limitNodes: nil do: aBlock
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
matchesForString: aString distance: aNumber limitNodes: maxNodes do: aBlock
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
	| nodeCount |
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
	nodeCount _ 0.
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
	aString isEmpty ifTrue: [^ self].
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
	root isNil ifTrue: [^ self].
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
	root matchesForString: aString startingAt: 1 distance: aNumber do: aBlock nodesDo:
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
		[:ea |
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
		nodeCount _ nodeCount + 1.
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
		nodeCount = maxNodes ifTrue: [^ self]]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
! !
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   126
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   127
!TSTree methodsFor:'enumerating'!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   128
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   129
do: aBlock
2468
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   130
        root notNil ifTrue: [root do: aBlock]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   131
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   132
    "Modified: / 08-08-2010 / 15:16:53 / cg"
2282
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
! !
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   134
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   135
!TSTree::TSTreeNode class methodsFor:'as yet unclassified'!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   136
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   137
key: aCharacter
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   138
	^ self basicNew initializeWithKey: aCharacter
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   139
! !
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   140
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   141
!TSTree::TSTreeNode class methodsFor:'documentation'!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   142
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   143
documentation
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   144
"
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   145
    [author:]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   146
        Avi Bryant
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   147
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   148
    [license:]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   149
        Dual licensed under both SqueakL and MIT. 
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   150
        This enables both base Squeak inclusion and 100% reuse.
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   151
"
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   152
! !
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   153
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   154
!TSTree::TSTreeNode methodsFor:'private'!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   155
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   156
canBeCulled
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   157
	^ self value isNil
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   158
		and: [low isNil]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   159
		and: [equal isNil]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   160
		and: [high isNil]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   161
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   162
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   163
cullNode: aNode
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   164
	low == aNode ifTrue: [^ low _ nil].
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   165
	equal == aNode ifTrue: [^ equal _ nil].
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   166
	high == aNode ifTrue: [^ high _ nil]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   167
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   168
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   169
do: aBlock
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   170
	self nodesDo: [:ea | ea value ifNotNilDo: aBlock]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   171
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   172
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   173
initializeWithKey: aCharacter
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   174
	key _ aCharacter
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
lookupString: aString startingAt: i
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   178
"inlined for performance"
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   179
"
2468
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   180
        self
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   181
                lookupString: aString
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   182
                startingAt: i
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   183
                whenFound: [^ value]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   184
                whenNil: [:c | ^ nil]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   185
                recurseWith: [:node :j | ^ node lookupString: aString startingAt: j]"
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   186
        | char |
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   187
        char := aString at: i.
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   188
        char = key
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   189
                ifTrue:
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   190
                        [aString size = i
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   191
                                ifTrue: [^ value]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   192
                                ifFalse: [^ equal notNil ifTrue: [equal lookupString: aString startingAt: i+1] ifFalse:[nil]]]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   193
                ifFalse:
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   194
                        [char < key
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   195
                                ifTrue: [^ low notNil ifTrue: [low lookupString: aString startingAt: i] ifFalse:[nil]]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   196
                                ifFalse: [^ high notNil ifTrue: [high lookupString: aString startingAt: i] ifFalse:[nil]]]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   197
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   198
    "Modified: / 08-08-2010 / 15:18:03 / cg"
2282
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   199
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   200
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   201
lookupString: aString startingAt: i insert: anObject
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   202
	self
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
		lookupString: aString
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   204
		startingAt: i
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   205
		whenFound: [self value: anObject]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   206
		whenNil: [:c | self newNodeWithKey: c]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   207
		recurseWith: [:node :j | node lookupString: aString startingAt: j insert: anObject]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   208
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   209
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   210
lookupString: aString startingAt: i whenFound: foundBlock whenNil: nilBlock recurseWith: recurseBlock
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   211
	| char |
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   212
	char := aString at: i.
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   213
	char = key
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   214
		ifTrue:
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   215
			[aString size = i
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   216
				ifTrue: [foundBlock value]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   217
				ifFalse: [equal ifNil: [equal := nilBlock value: (aString at: i+1)].
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   218
						 recurseBlock value: equal value: i+1]]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   219
		ifFalse:
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   220
			[char < key
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   221
				ifTrue: [low ifNil: [low := nilBlock value: char].
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   222
						recurseBlock value: low value: i]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   223
				ifFalse: [high ifNil: [high := nilBlock value: char].
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   224
						recurseBlock value: high value: i]]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   225
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   226
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   227
matchesForPrefix: aString startingAt: i do: aBlock
2468
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   228
        self
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   229
                lookupString: aString
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   230
                startingAt: i
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   231
                whenFound: [value notNil ifTrue: [aBlock value: value].  equal notNil ifTrue: [equal do: aBlock]]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   232
                whenNil: [:c | ^ self]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   233
                recurseWith: [:n :j | n matchesForPrefix: aString startingAt: j do: aBlock]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   234
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   235
    "Modified: / 08-08-2010 / 15:18:44 / cg"
2282
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   236
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   237
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   238
matchesForString: aString startingAt: i distance: d do: aBlock nodesDo: nodeBlock
2468
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   239
        
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   240
        | char d2 |
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   241
        nodeBlock value: self.
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   242
        d < 0 ifTrue: [^ self].
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   243
        
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   244
        char := aString at: i.
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   245
        (d > 0 or: [char < key])
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   246
                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
   247
                
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   248
        d2 := char = key ifTrue: [d] ifFalse: [d-1].
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   249
        (i + d2 = aString size and: [value notNil]) ifTrue: [aBlock value: value].
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   250
        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
   251
        
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   252
        (d > 0 or: [char > key])
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   253
                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
   254
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   255
    "Modified: / 08-08-2010 / 15:18:57 / cg"
2282
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   256
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   257
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   258
newNodeWithKey: aCharacter
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   259
	^ self class key: aCharacter
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   260
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   261
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   262
nodesDo: aBlock
2468
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   263
        aBlock value: self.
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   264
        low notNil ifTrue: [low nodesDo: aBlock].
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   265
        equal notNil ifTrue: [equal nodesDo: aBlock].
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   266
        high notNil ifTrue: [high nodesDo: aBlock]
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   267
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   268
    "Modified: / 08-08-2010 / 15:19:05 / cg"
2282
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   269
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   270
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   271
removeString: aString startingAt: i
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   272
	| val |
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   273
	self
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   274
		lookupString: aString
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   275
		startingAt: i
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   276
		whenFound: [val _ self value. self value: nil]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   277
		whenNil: [:c | ^ nil]
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   278
		recurseWith:
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   279
			[:node :j |
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   280
			val _ node removeString: aString startingAt: j.
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   281
			node canBeCulled ifTrue:
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   282
				[self cullNode: node]].
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   283
	^ val
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   284
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   285
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   286
value
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   287
	^ value
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   288
!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   289
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   290
value: anObject
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   291
	value _ anObject
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
!TSTree class methodsFor:'documentation'!
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   295
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   296
version_CVS
2468
36c571b96063 changed:
Claus Gittinger <cg@exept.de>
parents: 2282
diff changeset
   297
    ^ '$Header: /cvs/stx/stx/libbasic2/TSTree.st,v 1.2 2010-08-08 13:21:30 cg Exp $'
2282
a723f74eaae4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   298
! !