BTree.st
author Claus Gittinger <cg@exept.de>
Mon, 26 Aug 2019 23:15:10 +0200
changeset 5169 dbb252dcf51e
parent 4733 35c4cee51cc9
child 5208 afa165584dde
permissions -rw-r--r--
#DOCUMENTATION by exept class: BTree comment/format in: #keys #values class: BTree::BTreeLeafNode comment/format in: #keys class: BTree::BTreeNode comment/format in: #children #values
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5169
dbb252dcf51e #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 4733
diff changeset
     1
"{ Encoding: utf8 }"
dbb252dcf51e #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 4733
diff changeset
     2
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     3
"{ Package: 'stx:libbasic2' }"
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
4098
7da8ffc32c43 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3118
diff changeset
     5
"{ NameSpace: Smalltalk }"
7da8ffc32c43 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3118
diff changeset
     6
4363
8935796156c6 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4314
diff changeset
     7
KeyedCollection subclass:#BTree
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
	instanceVariableNames:'root'
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
	classVariableNames:''
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
	poolDictionaries:''
2768
13d275fb5d4a category changes
Claus Gittinger <cg@exept.de>
parents: 2699
diff changeset
    11
	category:'Collections-Ordered-Trees'
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
Object subclass:#BTreeKeys
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
	instanceVariableNames:''
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
	classVariableNames:''
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
	poolDictionaries:''
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
	privateIn:BTree
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
BTree::BTreeKeys variableSubclass:#BTreeKeysArray
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
	instanceVariableNames:''
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
	classVariableNames:''
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
	poolDictionaries:''
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
	privateIn:BTree
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
Object variableSubclass:#BTreeNode
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
	instanceVariableNames:'parent keys'
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
	classVariableNames:''
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
	poolDictionaries:''
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
	privateIn:BTree
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
BTree::BTreeNode variableSubclass:#BTreeInteriorNode
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
	instanceVariableNames:''
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
	classVariableNames:''
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
	poolDictionaries:''
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
	privateIn:BTree::BTreeNode
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
BTree::BTreeNode variableSubclass:#BTreeLeafNode
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
	instanceVariableNames:''
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
	classVariableNames:''
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
	poolDictionaries:''
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
	privateIn:BTree
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
BTree::BTreeKeys subclass:#BTreeStringKeys
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
	instanceVariableNames:'keys prefix abbreviations'
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
	classVariableNames:''
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
	poolDictionaries:''
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
	privateIn:BTree
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
!BTree class methodsFor:'documentation'!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
documentation
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
"
4123
466be2ddcab2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4112
diff changeset
    60
    BTree and TSTree
466be2ddcab2 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4112
diff changeset
    61
    
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
    A bunch of collection classes that are useful for building large indices of things. 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
    It's especially geared towards people using OODBs like GOODS, but can be used it in the image too: 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
    the BTree class is great for when you need to select numeric keys by range, 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
    and TSTree makes a solid basis for full-text search. 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
    TreeSet has an interesting optimized #intersection: that lets you compare two collections without 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
    looking at every item of either. 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
    I'm also going to be rolling some code in here from Benjamin Pollack specifically aimed at indexing 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
    by date ranges, which lets you do quick queries of all the events that overlap with a specific week, 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
    for instance. 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
    This is an implementation of the BTree data structure as a Smalltalk collection. 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
    It provides log(n) inserts, deletes, and retrieves of values by key. 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
    The keys have to be sortable (ie, Magnitudes).
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
    This is useful in situations where you want to minimize the number and size of individual objects 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
    that need to be accessed when using a large collection - for example, when objects are being swapped 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
    out to an object database such as GOODS. 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
    It is probably not a good choice for a collection that will be kept entirely in memory.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
    What you get: efficient sorted iteration through the keys, possibly limited to 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
    a given range.  For example, if you store a list of people keyed by their 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
    birthdate, and then want to find everyone born in a certain year, in order of 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
    birth, you can do that very fast.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
    Also in the BTree package is a TSTree, which has similar properties for String 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
    keys.  So as well as keeping them sorted, you can do efficient lookups of all 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
    the keys with a given prefix.  One other neat trick TSTree can do is a certain 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
    amount of fuzzy matching (eg find all keys with an edit distance of 3 from 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
    'foo') which makes it especially useful for spell checking and similar 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
    applications.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
    [author:]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
        Avi Bryant
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
    [license:]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
        Dual licensed under both SqueakL and MIT. 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
        This enables both base Squeak inclusion and 100% reuse.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
"
2413
c8a500d7f4ed added: #examples
Claus Gittinger <cg@exept.de>
parents: 2396
diff changeset
   101
!
c8a500d7f4ed added: #examples
Claus Gittinger <cg@exept.de>
parents: 2396
diff changeset
   102
c8a500d7f4ed added: #examples
Claus Gittinger <cg@exept.de>
parents: 2396
diff changeset
   103
examples
c8a500d7f4ed added: #examples
Claus Gittinger <cg@exept.de>
parents: 2396
diff changeset
   104
"
c8a500d7f4ed added: #examples
Claus Gittinger <cg@exept.de>
parents: 2396
diff changeset
   105
    |coll|
c8a500d7f4ed added: #examples
Claus Gittinger <cg@exept.de>
parents: 2396
diff changeset
   106
c8a500d7f4ed added: #examples
Claus Gittinger <cg@exept.de>
parents: 2396
diff changeset
   107
    coll := BTree new.
c8a500d7f4ed added: #examples
Claus Gittinger <cg@exept.de>
parents: 2396
diff changeset
   108
    (1 to:10) do:[:i | coll at:(i printString) put:(i squared) ].
c8a500d7f4ed added: #examples
Claus Gittinger <cg@exept.de>
parents: 2396
diff changeset
   109
    coll inspect.
c8a500d7f4ed added: #examples
Claus Gittinger <cg@exept.de>
parents: 2396
diff changeset
   110
    coll at:'10'       
c8a500d7f4ed added: #examples
Claus Gittinger <cg@exept.de>
parents: 2396
diff changeset
   111
"
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
! !
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
2803
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   114
!BTree class methodsFor:'instance creation'!
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
keys: aBTreeKeys
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
	^ self basicNew initializeWithKeys: aBTreeKeys
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
new
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
	^ self order: 5
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
order: aNumber
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
	^ self keys: (BTreeKeysArray new: aNumber)
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   126
! !
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   127
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   128
!BTree methodsFor:'accessing'!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   129
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   130
at: aMagnitude ifAbsent: errorBlock
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
       | leaf |
2699
0848b8b6a294 changed: #at:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2586
diff changeset
   132
       leaf := root existingLeafForKey: aMagnitude.
0848b8b6a294 changed: #at:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2586
diff changeset
   133
       leaf isNil ifTrue: [^ errorBlock value].
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   134
       ^ leaf valueForKey: aMagnitude ifAbsent: errorBlock
2699
0848b8b6a294 changed: #at:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2586
diff changeset
   135
0848b8b6a294 changed: #at:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2586
diff changeset
   136
    "Modified (format): / 18-11-2011 / 14:10:16 / cg"
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   137
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   138
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   139
depth
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   140
	^ root depth
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   141
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   142
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   143
keys
5169
dbb252dcf51e #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 4733
diff changeset
   144
    ^ Array streamContents:[:s |
dbb252dcf51e #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 4733
diff changeset
   145
            self keysDo: [:k | s nextPut: k]
dbb252dcf51e #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 4733
diff changeset
   146
    ]
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   147
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   148
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   149
order
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   150
	^ root size
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   151
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   152
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   153
values
5169
dbb252dcf51e #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 4733
diff changeset
   154
    ^ Array streamContents:[:s |
dbb252dcf51e #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 4733
diff changeset
   155
        self keysAndValuesDo: [:k :v | s nextPut: v]
dbb252dcf51e #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 4733
diff changeset
   156
    ]
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   157
! !
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   158
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   159
!BTree methodsFor:'adding'!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   160
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   161
at: aMagnitude put: anObject
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   162
	| leaf |
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   163
	leaf _ root leafForKey: aMagnitude.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   164
	leaf insertKey: aMagnitude value: anObject.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   165
	root _ leaf root.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   166
	^ anObject
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   167
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   168
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   169
removeKey: aMagnitude
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   170
	| leaf |
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   171
	leaf _ root leafForKey: aMagnitude.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   172
	leaf removeKey: aMagnitude.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   173
	root _ leaf root
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   174
! !
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   175
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   176
!BTree methodsFor:'enumerating'!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   177
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   178
commonKeysWith: aTree keysAndValuesDo: aBlock
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   179
	^ aTree depth < self depth
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   180
		ifTrue: [aTree root commonKeysWith: root keysAndValuesDo: aBlock flip: true]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   181
		ifFalse: [root commonKeysWith: aTree root keysAndValuesDo: aBlock flip: false]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   182
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   183
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   184
do: aBlock
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   185
	root allLeavesDo: [:ea | ea valuesDo: aBlock]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   186
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   187
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   188
from: start to: end do: aBlock
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   189
	self from: start to: end keysAndValuesDo: [:k :v | aBlock value: v]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   190
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   191
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   192
from: start to: end keysAndValuesDo: aBlock
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   193
	root leavesFrom: start to: end do:
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   194
		[:ea |
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   195
		ea keysAndValuesDo:
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   196
			[:k :v |
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   197
			(k between: start and: end) ifTrue:
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   198
				[aBlock value: k value: v]]]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   199
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   200
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   201
keysAndValuesDo: aBlock
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   202
	root allLeavesDo: [:ea | ea keysAndValuesDo: aBlock]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   204
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   205
keysDo: aBlock
2487
5872274c4abd commented: #keysDo:
Claus Gittinger <cg@exept.de>
parents: 2466
diff changeset
   206
    "evaluate the argument, aBlock for every key in the collection."
5872274c4abd commented: #keysDo:
Claus Gittinger <cg@exept.de>
parents: 2466
diff changeset
   207
5872274c4abd commented: #keysDo:
Claus Gittinger <cg@exept.de>
parents: 2466
diff changeset
   208
    root allLeavesDo: [:ea | ea keysDo: aBlock]
5872274c4abd commented: #keysDo:
Claus Gittinger <cg@exept.de>
parents: 2466
diff changeset
   209
5872274c4abd commented: #keysDo:
Claus Gittinger <cg@exept.de>
parents: 2466
diff changeset
   210
    "Modified: / 24-08-2010 / 10:13:24 / cg"
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   211
! !
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   212
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   213
!BTree methodsFor:'initialize-release'!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   214
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   215
initializeWithKeys: aBTreeKeys
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   216
	aBTreeKeys size > 3 ifFalse: [self error: 'The BTree order must be at least 4'].
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   217
	root _ BTreeLeafNode keys: aBTreeKeys
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   218
! !
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   219
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   220
!BTree methodsFor:'private'!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   221
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   222
root
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   223
	^ root
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   224
! !
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   225
3016
f877b3acc3f5 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2803
diff changeset
   226
!BTree methodsFor:'testing'!
f877b3acc3f5 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2803
diff changeset
   227
f877b3acc3f5 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2803
diff changeset
   228
isFixedSize
f877b3acc3f5 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2803
diff changeset
   229
    "return true if the receiver cannot grow"
f877b3acc3f5 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2803
diff changeset
   230
f877b3acc3f5 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2803
diff changeset
   231
    ^ false
f877b3acc3f5 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2803
diff changeset
   232
! !
f877b3acc3f5 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2803
diff changeset
   233
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   234
!BTree::BTreeKeys class methodsFor:'documentation'!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   235
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   236
documentation
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   237
"
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   238
    [author:]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   239
        Avi Bryant
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   240
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   241
    [license:]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   242
        Dual licensed under both SqueakL and MIT. 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   243
        This enables both base Squeak inclusion and 100% reuse.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   244
"
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   245
! !
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   246
2586
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   247
!BTree::BTreeKeys methodsFor:'accessing'!
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   248
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   249
first
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   250
    ^ self at:1
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   251
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   252
    "Modified (format): / 02-08-2011 / 09:19:05 / cg"
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   253
! !
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   254
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   255
!BTree::BTreeKeys methodsFor:'as yet unclassified'!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   256
2586
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   257
emptyCopy
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   258
    ^ self class new:self size
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   259
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   260
    "Modified (format): / 02-08-2011 / 09:19:22 / cg"
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   261
!
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   262
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   263
findIndexForKey:aMagnitude 
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   264
    self 
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   265
        withIndexDo:[:key :i | 
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   266
            (key isNil or:[ key > aMagnitude ]) ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   267
                ^ i - 1
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   268
            ]
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   269
        ].
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   270
    ^ self size
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   271
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   272
    "Modified (format): / 02-08-2011 / 09:19:10 / cg"
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   273
!
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   274
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   275
shiftLeftTo:index 
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   276
    index to:self size - 1 by:1 do:[:i | 
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   277
        self at:i put:(self at:i + 1)
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   278
    ].
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   279
    self at:self size put:nil.
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   280
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   281
    "Modified (format): / 02-08-2011 / 09:18:52 / cg"
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   282
!
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   283
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   284
shiftRightFrom:index 
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   285
    self size to:index + 1 by:-1 do:[:i | 
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   286
        self at:i put:(self at:i - 1)
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   287
    ]
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   288
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   289
    "Modified (format): / 02-08-2011 / 09:18:57 / cg"
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   290
! !
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   291
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   292
!BTree::BTreeKeys methodsFor:'enumeration'!
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   293
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   294
withIndexDo:aBlock 
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   295
    1 to:self size do:[:i | 
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   296
        aBlock value:(self at:i) value:i
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   297
    ]
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   298
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   299
    "Modified (format): / 02-08-2011 / 09:19:01 / cg"
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   300
! !
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   301
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   302
!BTree::BTreeKeys methodsFor:'queries'!
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   303
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   304
canGrow
2586
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   305
    ^ (self at:self size) isNil
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   306
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   307
    "Modified (format): / 02-08-2011 / 09:19:27 / cg"
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   308
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   309
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   310
canShrink
2586
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   311
    ^ (self at:self size // 2 + 1) notNil
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   312
2586
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   313
    "Modified (format): / 02-08-2011 / 09:19:23 / cg"
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   314
! !
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   315
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   316
!BTree::BTreeKeysArray class methodsFor:'documentation'!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   317
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   318
documentation
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   319
"
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   320
    [author:]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   321
        Avi Bryant
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   322
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   323
    [license:]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   324
        Dual licensed under both SqueakL and MIT. 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   325
        This enables both base Squeak inclusion and 100% reuse.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   326
"
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   327
! !
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   328
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   329
!BTree::BTreeNode class methodsFor:'as yet unclassified'!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   330
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   331
keys: anArray
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   332
	^ (self new: (anArray size)) keys: anArray
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   333
! !
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   334
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   335
!BTree::BTreeNode class methodsFor:'documentation'!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   336
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   337
documentation
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   338
"
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   339
    [author:]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   340
        Avi Bryant
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   341
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   342
    [license:]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   343
        Dual licensed under both SqueakL and MIT. 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   344
        This enables both base Squeak inclusion and 100% reuse.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   345
"
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   346
! !
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   347
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   348
!BTree::BTreeNode methodsFor:'accessing'!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   349
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   350
children
5169
dbb252dcf51e #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 4733
diff changeset
   351
    ^ Array streamContents: [:s | 
dbb252dcf51e #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 4733
diff changeset
   352
        self childrenDo: [:ea | s nextPut: ea]
dbb252dcf51e #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 4733
diff changeset
   353
    ]
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   354
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   355
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   356
depth
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   357
	^ parent ifNil: [1] ifNotNil: [1 + parent depth]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   358
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   359
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   360
firstKey
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   361
	^ keys first
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   362
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   363
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   364
parent
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   365
	^ parent
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   366
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   367
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   368
parent: aBTreeNode
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   369
	parent _ aBTreeNode
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   370
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   371
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   372
root
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   373
	^ parent
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   374
		ifNil: [self]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   375
		ifNotNil: [parent root]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   376
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   377
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   378
values
5169
dbb252dcf51e #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 4733
diff changeset
   379
    ^ Array streamContents: [:s | 
dbb252dcf51e #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 4733
diff changeset
   380
        self valuesDo: [:ea | s nextPut: ea]
dbb252dcf51e #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 4733
diff changeset
   381
    ]
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   382
! !
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   383
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   384
!BTree::BTreeNode methodsFor:'enumerating'!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   385
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   386
allChildrenDo: aBlock
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   387
	self childrenDo:
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   388
		[:ea |
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   389
		aBlock value: ea.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   390
		ea allChildrenDo: aBlock]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   391
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   392
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   393
allLeavesDo: aBlock
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   394
	self withAllChildrenDo: [:ea | ea isLeaf ifTrue: [aBlock value: ea]]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   395
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   396
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   397
childrenDo: aBlock
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   398
	self subclassResponsibility
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   399
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   400
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   401
keysAndValuesDo: aBlock
2466
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   402
        keys withIndexDo:
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   403
                [:key :i |
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   404
                key notNil ifTrue: [aBlock value: key value: (self at: i)]]
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   405
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   406
    "Modified: / 08-08-2010 / 14:39:17 / cg"
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   407
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   408
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   409
keysDo: aBlock
2586
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   410
    keys withIndexDo:[:key :i |
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   411
        key isNil ifTrue:[^ self].
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   412
        aBlock value: key
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   413
    ]
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   414
Claus Gittinger <cg@exept.de>
parents: 2487
diff changeset
   415
    "Modified: / 02-08-2011 / 09:17:49 / cg"
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   416
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   417
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   418
leavesFrom: start to: end do: aBlock
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   419
	self subclassResponsibility
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   420
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   421
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   422
valuesDo: aBlock
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   423
	self keysAndValuesDo: [:k :v | aBlock value: v]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   424
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   425
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   426
withAllChildrenDo: aBlock
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   427
	aBlock value: self.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   428
	self allChildrenDo: aBlock.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   429
! !
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   430
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   431
!BTree::BTreeNode methodsFor:'inserting'!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   432
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   433
insertKey: aMagnitude value: anObject
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   434
	| index key |
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   435
	index _ keys findIndexForKey: aMagnitude.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   436
	index = 0 ifTrue:
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   437
		[self canGrow
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   438
			ifTrue:
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   439
				[self shiftRightFrom: 1.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   440
				^ self insertKey: aMagnitude value: anObject at: 1]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   441
			ifFalse:
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   442
				[self split.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   443
				^ (parent childForKey: aMagnitude) insertKey: aMagnitude value: anObject]].
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   444
	
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   445
	key _ keys at: index.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   446
	key = aMagnitude ifTrue:
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   447
		[^ self insertKey: aMagnitude value: anObject at: index].
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   448
	index < self size ifTrue:
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   449
		[key _ keys at: index + 1.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   450
		key
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   451
			ifNil: [^ self insertKey: aMagnitude value: anObject at: index+1]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   452
			ifNotNil:
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   453
				[self canGrow ifTrue:
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   454
					[self shiftRightFrom: index+1.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   455
					^ self insertKey: aMagnitude value: anObject at: index+1]]].
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   456
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   457
	"otherwise"
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   458
	self split.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   459
	^ (parent childForKey: aMagnitude) insertKey: aMagnitude value: anObject
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   460
! !
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   461
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   462
!BTree::BTreeNode methodsFor:'private'!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   463
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   464
ensureParent
2699
0848b8b6a294 changed: #at:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2586
diff changeset
   465
        parent isNil ifTrue:[
0848b8b6a294 changed: #at:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2586
diff changeset
   466
                parent := BTreeInteriorNode keys: keys emptyCopy.
0848b8b6a294 changed: #at:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2586
diff changeset
   467
                parent insertKey: self firstKey value: self
0848b8b6a294 changed: #at:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2586
diff changeset
   468
        ].
0848b8b6a294 changed: #at:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2586
diff changeset
   469
        ^ parent
0848b8b6a294 changed: #at:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2586
diff changeset
   470
0848b8b6a294 changed: #at:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2586
diff changeset
   471
    "Modified: / 18-11-2011 / 14:11:11 / cg"
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   472
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   473
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   474
grow
4314
46aca31f42e4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4210
diff changeset
   475
    | sibling |
46aca31f42e4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4210
diff changeset
   476
46aca31f42e4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4210
diff changeset
   477
    parent notNil ifTrue:[
46aca31f42e4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4210
diff changeset
   478
        sibling := parent nextSiblingForChild: self.
46aca31f42e4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4210
diff changeset
   479
        sibling isNil ifTrue: ["we're the new root" parent := nil. ^ self].
46aca31f42e4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4210
diff changeset
   480
        sibling canShrink ifTrue: [
46aca31f42e4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4210
diff changeset
   481
            self stealFrom: sibling
46aca31f42e4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4210
diff changeset
   482
        ] ifFalse: [
46aca31f42e4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4210
diff changeset
   483
            self mergeWith: sibling
46aca31f42e4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4210
diff changeset
   484
        ]
46aca31f42e4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4210
diff changeset
   485
    ]
2466
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   486
2699
0848b8b6a294 changed: #at:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2586
diff changeset
   487
    "Modified: / 18-11-2011 / 14:29:49 / cg"
4314
46aca31f42e4 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4210
diff changeset
   488
    "Modified (format): / 10-02-2017 / 15:16:10 / cg"
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   489
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   490
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   491
insertKey: aMagnitude value: anObject at: index
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   492
	keys at: index put: aMagnitude.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   493
	self at: index put: anObject
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   494
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   495
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   496
keys: anArray
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   497
	keys _ anArray
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   498
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   499
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   500
mergeWith: aNode		
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   501
	| oldKey |
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   502
	oldKey _ self firstKey.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   503
	aNode keysAndValuesDo:
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   504
		[:k :v |
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   505
		self insertKey: k value: v].
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   506
	parent removeKey: aNode firstKey.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   507
	parent updateKey: oldKey to: self firstKey.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   508
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   509
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   510
shiftLeftTo: index
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   511
	keys shiftLeftTo: index.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   512
	index to: self size - 1 by: 1 do:
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   513
		[:i |
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   514
		self at: i put: (self at: i+1)].
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   515
	self at: self size put: nil.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   516
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   517
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   518
shiftRightFrom: index
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   519
	keys shiftRightFrom: index.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   520
	self size to: index+1 by: -1 do:
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   521
		[:i |
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   522
		self at: i put: (self at: i-1)]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   523
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   524
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   525
split
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   526
	| other midpoint |
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   527
	other _ self class keys: keys emptyCopy.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   528
	midpoint _ self size // 2 + 1.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   529
	midpoint to: self size do:
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   530
		[:i |
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   531
		other insertKey: (keys at: i) value: (self at: i) at: (i - midpoint + 1).
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   532
		keys at: i put: nil.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   533
		self at: i put: nil].
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   534
	
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   535
	self ensureParent insertKey: other firstKey value: other
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   536
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   537
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   538
stealFrom: aNode
2466
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   539
        | key value |
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   540
        aNode firstKey > self firstKey
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   541
                ifTrue: [value := aNode at: 1. key := aNode firstKey]
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   542
                ifFalse:
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   543
                        [aNode keysAndValuesDo: [:k :v | key := k. value := v].
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   544
                        parent notNil ifTrue: [parent updateKey: self firstKey to: key]].
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   545
        self insertKey: key value: value.
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   546
        aNode removeKey: key
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   547
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   548
    "Modified: / 08-08-2010 / 14:39:50 / cg"
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   549
! !
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   550
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   551
!BTree::BTreeNode methodsFor:'removing'!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   552
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   553
removeKey: aMagnitude
2466
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   554
        | index key |
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   555
        self canShrink ifFalse: [self grow].
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   556
        
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   557
        index := keys findIndexForKey: aMagnitude.
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   558
        key := keys at: index.
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   559
        key = aMagnitude ifFalse: [^ self error: 'No such key'].
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   560
        
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   561
        self shiftLeftTo: index.
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   562
        
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   563
        index = 1 ifTrue: [parent notNil ifTrue: [parent updateKey: key to: self firstKey]]
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   564
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   565
    "Modified: / 08-08-2010 / 14:39:29 / cg"
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   566
! !
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   567
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   568
!BTree::BTreeNode methodsFor:'testing'!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   569
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   570
canGrow
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   571
	^ keys canGrow
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   572
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   573
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   574
canShrink
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   575
	^ keys canShrink
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   576
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   577
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   578
isLeaf
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   579
	self subclassResponsibility
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   580
! !
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   581
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   582
!BTree::BTreeNode::BTreeInteriorNode class methodsFor:'documentation'!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   583
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   584
documentation
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   585
"
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   586
    [author:]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   587
        Avi Bryant
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   588
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   589
    [license:]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   590
        Dual licensed under both SqueakL and MIT. 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   591
        This enables both base Squeak inclusion and 100% reuse.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   592
"
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   593
! !
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   594
2803
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   595
!BTree::BTreeNode::BTreeInteriorNode methodsFor:'accessing'!
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   596
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   597
childForKey: aMagnitude
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   598
	| index |
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   599
	index _ keys findIndexForKey: aMagnitude.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   600
	index = 0 ifTrue:
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   601
		[keys at: 1 put: aMagnitude.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   602
		^ self at: 1].
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   603
	^ self at: index
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   604
		
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   605
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   606
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   607
existingChildForKey: aMagnitude
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   608
       "Unlike #childForKey:, this method looks for a child, but doesn't mess with the tree if it doesn't exist."
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   609
       | index |
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   610
       index _ keys findIndexForKey: aMagnitude.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   611
       index = 0
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   612
               ifTrue: [^ nil]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   613
               ifFalse: [^ self at: index].
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   614
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   615
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   616
existingLeafForKey: aMagnitude
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   617
       "Unlike #leafForKey:, this method looks for a leaf but doesn't mess with the tree if it doesn't exist."
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   618
       | child |
2466
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   619
       child := self existingChildForKey: aMagnitude.
4210
45924614ad32 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4123
diff changeset
   620
       ^ child notNil 
45924614ad32 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4123
diff changeset
   621
            ifTrue: [child existingLeafForKey: aMagnitude]
45924614ad32 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4123
diff changeset
   622
            ifFalse:[nil]
2466
33511e8d269c code cleanup
Claus Gittinger <cg@exept.de>
parents: 2413
diff changeset
   623
4210
45924614ad32 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4123
diff changeset
   624
    "Modified: / 19-11-2016 / 12:20:31 / cg"
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   625
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   626
2803
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   627
insertKey: aMagnitude value: anObject at: index
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   628
	super insertKey: aMagnitude value: anObject at: index.
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   629
	anObject parent: self
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   630
!
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   631
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   632
updateKey: oldMagnitude to: newMagnitude
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   633
       keys withIndexDo:
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   634
               [:key :i |
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   635
               key = oldMagnitude ifTrue:
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   636
                       [(i = 1 and: [parent notNil]) ifTrue:
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   637
                               [parent updateKey: oldMagnitude to: newMagnitude].
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   638
                       ^ keys at: i put: newMagnitude]].
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   639
       self error: 'No such key'
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   640
! !
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   641
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   642
!BTree::BTreeNode::BTreeInteriorNode methodsFor:'enumerating'!
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   643
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   644
childrenDo: aBlock
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   645
	self valuesDo: aBlock
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   646
!
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   647
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   648
leavesFrom: start to: end do: aBlock
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   649
	| startIndex endIndex |
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   650
	startIndex _ (keys findIndexForKey: start) max: 1.
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   651
	endIndex _ (keys findIndexForKey: end).
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   652
	startIndex to: endIndex do: [:i | (self at: i) leavesFrom: start to: end do: aBlock]
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   653
! !
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   654
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   655
!BTree::BTreeNode::BTreeInteriorNode methodsFor:'misc'!
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   656
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   657
commonKeysWith: aNode keysAndValuesDo: aBlock flip: aBoolean
3041
563f38b0db89 class: BTree
Claus Gittinger <cg@exept.de>
parents: 3016
diff changeset
   658
    | index |
563f38b0db89 class: BTree
Claus Gittinger <cg@exept.de>
parents: 3016
diff changeset
   659
    aNode firstKey < self firstKey ifTrue: [^ aNode commonKeysWith: self keysAndValuesDo: aBlock flip: aBoolean not].
563f38b0db89 class: BTree
Claus Gittinger <cg@exept.de>
parents: 3016
diff changeset
   660
    index := (keys findIndexForKey: aNode firstKey) max: 1.
563f38b0db89 class: BTree
Claus Gittinger <cg@exept.de>
parents: 3016
diff changeset
   661
    index to: self size do: [:i |
563f38b0db89 class: BTree
Claus Gittinger <cg@exept.de>
parents: 3016
diff changeset
   662
        |c|
563f38b0db89 class: BTree
Claus Gittinger <cg@exept.de>
parents: 3016
diff changeset
   663
563f38b0db89 class: BTree
Claus Gittinger <cg@exept.de>
parents: 3016
diff changeset
   664
        (c := self at: i) notNil ifTrue:[
563f38b0db89 class: BTree
Claus Gittinger <cg@exept.de>
parents: 3016
diff changeset
   665
             c commonKeysWith: aNode keysAndValuesDo: aBlock flip: aBoolean
563f38b0db89 class: BTree
Claus Gittinger <cg@exept.de>
parents: 3016
diff changeset
   666
        ]
563f38b0db89 class: BTree
Claus Gittinger <cg@exept.de>
parents: 3016
diff changeset
   667
    ]
2803
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   668
! !
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   669
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   670
!BTree::BTreeNode::BTreeInteriorNode methodsFor:'queries'!
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   671
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   672
depth
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   673
	^ 1 + self firstChild depth
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   674
!
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   675
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   676
firstChild
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   677
	self childrenDo: [:ea | ^ ea].
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   678
	self error: 'No children'.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   679
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   680
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   681
isLeaf
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   682
	^ false
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   683
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   684
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   685
leafForKey: aMagnitude
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   686
	^ (self childForKey: aMagnitude) leafForKey: aMagnitude
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   687
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   688
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   689
nextSiblingForChild: aNode
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   690
	| index |
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   691
	index _ keys findIndexForKey: aNode firstKey.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   692
	^ (index = self size or: [(keys at: index+1) isNil]) 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   693
		ifTrue: [index = 1 ifFalse: [self at: index - 1] ifTrue: [nil]]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   694
		ifFalse: [self at: index + 1]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   695
! !
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   696
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   697
!BTree::BTreeLeafNode class methodsFor:'documentation'!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   698
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   699
documentation
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   700
"
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   701
    [author:]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   702
        Avi Bryant
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   703
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   704
    [license:]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   705
        Dual licensed under both SqueakL and MIT. 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   706
        This enables both base Squeak inclusion and 100% reuse.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   707
"
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   708
! !
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   709
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   710
!BTree::BTreeLeafNode methodsFor:'as yet unclassified'!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   711
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   712
childrenDo: aBlock
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   713
	"no children"
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   714
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   715
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   716
commonKeysWith: aNode keysAndValuesDo: aBlock flip: aBoolean
2395
63737e9ab514 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2394
diff changeset
   717
        | index key block leaf advanceKey last |
4112
6567e3778a4c #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4111
diff changeset
   718
4111
d2e51602faf4 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4110
diff changeset
   719
        aBoolean 
d2e51602faf4 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4110
diff changeset
   720
             ifTrue: [ block := [:k :v1 :v2 | aBlock value: k value: v2 value: v1] ] 
d2e51602faf4 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4110
diff changeset
   721
             ifFalse: [ block := aBlock ].
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   722
2699
0848b8b6a294 changed: #at:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2586
diff changeset
   723
        index := 0.
0848b8b6a294 changed: #at:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2586
diff changeset
   724
        advanceKey :=
0848b8b6a294 changed: #at:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2586
diff changeset
   725
                [index := index + 1.
2395
63737e9ab514 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2394
diff changeset
   726
                index > self size ifTrue: [^ self].
2699
0848b8b6a294 changed: #at:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2586
diff changeset
   727
                key := keys at: index.
0848b8b6a294 changed: #at:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2586
diff changeset
   728
                key isNil ifTrue: [^ self]].
0848b8b6a294 changed: #at:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2586
diff changeset
   729
        last := self lastKey.
2395
63737e9ab514 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2394
diff changeset
   730
        
63737e9ab514 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2394
diff changeset
   731
        advanceKey value.
2396
3633fba9dfb9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2395
diff changeset
   732
        [key < aNode firstKey] whileTrue: [advanceKey value ].
2395
63737e9ab514 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2394
diff changeset
   733
                
63737e9ab514 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2394
diff changeset
   734
        [
2699
0848b8b6a294 changed: #at:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2586
diff changeset
   735
            leaf := aNode existingLeafForKey: key.
2396
3633fba9dfb9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2395
diff changeset
   736
            leaf lastKey < key 
3633fba9dfb9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2395
diff changeset
   737
                ifTrue: [advanceKey value ] 
3633fba9dfb9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2395
diff changeset
   738
                ifFalse:[
3633fba9dfb9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2395
diff changeset
   739
                    leaf keysAndValuesDo: [:otherKey :otherValue |
3633fba9dfb9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2395
diff changeset
   740
                        otherKey > last ifTrue: [^ self].
3633fba9dfb9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2395
diff changeset
   741
                        [key < otherKey] whileTrue: [advanceKey value ].
3633fba9dfb9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2395
diff changeset
   742
                        key = otherKey ifTrue: [block value: key value: (self at: index) value: otherValue]
3633fba9dfb9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2395
diff changeset
   743
                    ].
3633fba9dfb9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2395
diff changeset
   744
                    key > leaf lastKey ifFalse: [advanceKey value ]
3633fba9dfb9 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2395
diff changeset
   745
                ]
2395
63737e9ab514 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2394
diff changeset
   746
        ] loop
2699
0848b8b6a294 changed: #at:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2586
diff changeset
   747
0848b8b6a294 changed: #at:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2586
diff changeset
   748
    "Modified (format): / 18-11-2011 / 14:10:45 / cg"
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   749
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   750
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   751
existingLeafForKey: aMagnitude
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   752
       ^ self
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   753
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   754
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   755
keys
5169
dbb252dcf51e #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 4733
diff changeset
   756
    ^ Array streamContents: [:s | 
dbb252dcf51e #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 4733
diff changeset
   757
        self keysDo: [:ea | s nextPut: ea]
dbb252dcf51e #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 4733
diff changeset
   758
    ]
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   759
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   760
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   761
lastKey
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   762
	| last |
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   763
	last _ nil.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   764
	self keysDo: [:k | last _ k].
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   765
	^ last
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   766
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   767
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   768
leafForKey: aMagnitude
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   769
	^ self
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   770
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   771
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   772
leavesFrom: start to: end do: aBlock
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   773
	aBlock value: self
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   774
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   775
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   776
valueForKey: aMagnitude ifAbsent: errorBlock
2393
b8abac04c44e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2281
diff changeset
   777
        | i |
b8abac04c44e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2281
diff changeset
   778
        i _ keys findIndexForKey: aMagnitude.
b8abac04c44e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2281
diff changeset
   779
        (i > 0 and: [(keys at: i) = aMagnitude])
b8abac04c44e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2281
diff changeset
   780
                ifTrue: [^ self at: i].
b8abac04c44e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2281
diff changeset
   781
        ^ errorBlock value
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   782
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   783
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   784
valueForKey: aMagnitude ifPresent: aBlock
2394
00020d8b5514 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2393
diff changeset
   785
        ^ aBlock value: (self valueForKey: aMagnitude ifAbsent: [nil])
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   786
! !
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   787
2803
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   788
!BTree::BTreeLeafNode methodsFor:'queries'!
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   789
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   790
depth
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   791
	^ 1
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   792
!
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   793
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   794
isLeaf
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   795
	^ true
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   796
! !
bc70d2d7ab9f category of:
Claus Gittinger <cg@exept.de>
parents: 2768
diff changeset
   797
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   798
!BTree::BTreeStringKeys class methodsFor:'as yet unclassified'!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   799
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   800
new
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   801
	^ self new: 8
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   802
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   803
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   804
new: aNumber
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   805
	^ self basicNew initializeWithSize: aNumber
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   806
! !
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   807
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   808
!BTree::BTreeStringKeys class methodsFor:'documentation'!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   809
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   810
documentation
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   811
"
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   812
    [author:]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   813
        Avi Bryant
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   814
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   815
    [license:]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   816
        Dual licensed under both SqueakL and MIT. 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   817
        This enables both base Squeak inclusion and 100% reuse.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   818
"
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   819
! !
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   820
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   821
!BTree::BTreeStringKeys methodsFor:'as yet unclassified'!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   822
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   823
abbreviationSize
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   824
	^ 3
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   825
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   826
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   827
abbreviationsAndIndicesDo: aBlock
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   828
	| stream |
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   829
	stream _ abbreviations readStream.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   830
	1 to: self size do:
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   831
		[:i |
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   832
		stream atEnd
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   833
			ifFalse: [aBlock value: prefix, (stream next: self abbreviationSize) value: i]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   834
			ifTrue: [aBlock value: nil value: i]]
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   835
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   836
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   837
at: aNumber
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   838
	^ keys at: aNumber
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   839
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   840
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   841
at: aNumber put: aString
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   842
	keys at: aNumber put: aString.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   843
	prefix _ self nilPrefix.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   844
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   845
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   846
buildAbbreviationsFrom: readStreams
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   847
	| nextChars |
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   848
	1 to: self abbreviationSize do:
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   849
		[:i |
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   850
		nextChars _ readStreams collect: [:ea | ea next ifNil: [Character value: 0]].
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   851
		nextChars withIndexDo:
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   852
			[:c :j |
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   853
			abbreviations at: (j-1 * self abbreviationSize) + i put: c]].
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   854
	^ abbreviations
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   855
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   856
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   857
extractPrefixFrom: readStreams
3118
2079e721da7a class: BTree
Claus Gittinger <cg@exept.de>
parents: 3041
diff changeset
   858
        | prefixStream nextChars |
4098
7da8ffc32c43 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3118
diff changeset
   859
        prefixStream := '' writeStream.
3118
2079e721da7a class: BTree
Claus Gittinger <cg@exept.de>
parents: 3041
diff changeset
   860
        
2079e721da7a class: BTree
Claus Gittinger <cg@exept.de>
parents: 3041
diff changeset
   861
        [readStreams anySatisfy: [:ea | ea atEnd]] whileFalse:
2079e721da7a class: BTree
Claus Gittinger <cg@exept.de>
parents: 3041
diff changeset
   862
                [nextChars _ readStreams collect: [:ea | ea next].
2079e721da7a class: BTree
Claus Gittinger <cg@exept.de>
parents: 3041
diff changeset
   863
                (nextChars conform: [:ea | ea = nextChars first])
2079e721da7a class: BTree
Claus Gittinger <cg@exept.de>
parents: 3041
diff changeset
   864
                                ifTrue: [prefixStream nextPut: nextChars first]
2079e721da7a class: BTree
Claus Gittinger <cg@exept.de>
parents: 3041
diff changeset
   865
                                ifFalse: [readStreams do: [:ea | ea skip: -1]. ^ prefixStream contents]].
2079e721da7a class: BTree
Claus Gittinger <cg@exept.de>
parents: 3041
diff changeset
   866
        ^ prefixStream contents
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   867
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   868
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   869
findIndexForKey: aString
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   870
        | stream str diff |
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   871
        prefix = self nilPrefix ifTrue: [self rebuildAbbreviations].
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   872
        stream _ aString readStream.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   873
        str _ stream nextAvailable: prefix size + self abbreviationSize. 
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   874
        diff _ prefix size + self abbreviationSize - str size.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   875
        str _ str, (String new: diff).
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   876
        self abbreviationsAndIndicesDo:
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   877
                [:abbr :i |
2699
0848b8b6a294 changed: #at:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2586
diff changeset
   878
                abbr isNil ifTrue: [^ i - 1].
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   879
                str < abbr ifTrue: [^ i - 1].
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   880
                str = abbr ifTrue: [^ super findIndexForKey: aString]].
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   881
        ^ self size
2699
0848b8b6a294 changed: #at:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2586
diff changeset
   882
0848b8b6a294 changed: #at:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2586
diff changeset
   883
    "Modified: / 18-11-2011 / 14:30:07 / cg"
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   884
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   885
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   886
initializeWithSize: aNumber
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   887
	keys _ Array new: aNumber.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   888
	prefix _ self nilPrefix.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   889
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   890
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   891
nilPrefix
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   892
	^ '^^^'
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   893
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   894
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   895
rebuildAbbreviations
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   896
	| keyStreams filled |
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   897
	filled _ keys count: [:ea | ea notNil].
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   898
	abbreviations _ String new: (filled * self abbreviationSize).
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   899
	filled = 0 ifTrue: [prefix _ ''. ^ self ].
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   900
	keyStreams _ (1 to: filled) collect: [:i | (keys at: i) readStream].
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   901
	
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   902
	prefix _ self extractPrefixFrom: keyStreams.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   903
	abbreviations _ self buildAbbreviationsFrom: keyStreams.
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   904
!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   905
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   906
size
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   907
	^ keys size
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   908
! !
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   909
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   910
!BTree class methodsFor:'documentation'!
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   911
4111
d2e51602faf4 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4110
diff changeset
   912
version
d2e51602faf4 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4110
diff changeset
   913
    ^ '$Header$'
d2e51602faf4 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4110
diff changeset
   914
!
d2e51602faf4 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4110
diff changeset
   915
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   916
version_CVS
4098
7da8ffc32c43 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3118
diff changeset
   917
    ^ '$Header$'
2281
9e4bcbba3b73 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   918
! !
3016
f877b3acc3f5 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2803
diff changeset
   919