--- a/OrderedCollection.st Thu Oct 29 06:54:02 2015 +0100
+++ b/OrderedCollection.st Fri Oct 30 06:59:59 2015 +0100
@@ -15,7 +15,7 @@
SequenceableCollection subclass:#OrderedCollection
instanceVariableNames:'contentsArray firstIndex lastIndex'
- classVariableNames:''
+ classVariableNames:'MinContentsArraySize'
poolDictionaries:''
category:'Collections-Sequenceable'
!
@@ -202,12 +202,35 @@
"
! !
+!OrderedCollection class methodsFor:'initialization'!
+
+initialize
+ MinContentsArraySize := 3. "the minimum size of a non-empty contentsArray"
+! !
+
!OrderedCollection class methodsFor:'instance creation'!
new
"create a new, empty OrderedCollection"
- ^ (self basicNew) initContents:10
+ MinContentsArraySize isNil ifTrue:[
+ self initialize.
+ ].
+ ^ self basicNew initContents:0
+
+ "
+ self new
+
+ |nEmpty|
+ nEmpty := 0.
+ self allInstancesDo:[:e| e size == 0 ifTrue:[nEmpty := nEmpty + 1]].
+ nEmpty
+
+ |nEmpty|
+ nEmpty := OrderedCollection new.
+ self allInstancesDo:[:e| (e size == 0 and:[e contentsArray size ~~ 0]) ifTrue:[nEmpty add:e]].
+ nEmpty
+ "
"Modified: 19.3.1996 / 17:53:12 / cg"
!
@@ -223,7 +246,13 @@
See also newWithSize:, which might do what you really want.
"
- ^ (self basicNew) initContents:(size max:3)
+ |sz|
+
+ MinContentsArraySize isNil ifTrue:[
+ self initialize.
+ ].
+ sz := size.
+ ^ self basicNew initContents:sz
"Modified: 19.3.1996 / 17:53:47 / cg"
!
@@ -266,7 +295,10 @@
This creates an initial contents array of size 0 in contrast to the default new:0,
which preserves space for 3 elements."
- ^ (self basicNew) initContents:0
+ MinContentsArraySize isNil ifTrue:[
+ self initialize.
+ ].
+ ^ self basicNew initContents:0
"
self newLikelyToRemainEmpty size
@@ -275,6 +307,7 @@
"
! !
+
!OrderedCollection methodsFor:'accessing'!
at:anInteger
@@ -683,14 +716,7 @@
Returns the receiver.
Destructive: modifies the receiver."
- contentsArray size <= 20 ifTrue:[
- "/ reuse the contents array
- contentsArray atAllPut:nil.
- firstIndex := 1.
- lastIndex := 0.
- ] ifFalse:[
- self initContents:10
- ].
+ self initContents:0
"Modified: 12.4.1996 / 13:34:19 / cg"
!
@@ -1270,19 +1296,11 @@
copyEmpty
"return a copy of the receiver without any elements."
- ^ self copyEmpty:10
-!
-
-copyFrom:start to:stop
- "return a new OrderedCollection containing the elements
- from start to stop."
+ ^ self copyEmpty:0
- |newCollection sz|
-
- sz := stop - start + 1.
- newCollection := self copyEmptyAndGrow:sz. "must grow, otherwise replace fails"
- newCollection replaceFrom:1 to:sz with:self startingAt:start.
- ^ newCollection
+ "
+ self new copyEmpty
+ "
!
postCopy
@@ -1579,7 +1597,7 @@
newLast := firstIndex + newSize - 1.
newSize < oldSize ifTrue:[
newSize == 0 ifTrue:[
- self initContents:10.
+ self initContents:0.
^ self.
].
oldLast := lastIndex.
@@ -1604,12 +1622,17 @@
! !
+
!OrderedCollection methodsFor:'private'!
initContents:size
"setup the receiver-collection to hold size entries"
- contentsArray := Array basicNew:size.
+ size == 0 ifTrue:[
+ contentsArray := #(). "save garbage"
+ ] ifFalse:[
+ contentsArray := Array basicNew:size.
+ ].
firstIndex := 1.
lastIndex := 0
!
@@ -1631,7 +1654,7 @@
sz := lastIndex - firstIndex + 1.
((oldSize == 0) or:[sz == 0]) ifTrue:[
- contentsArray := Array basicNew:3.
+ contentsArray := Array basicNew:MinContentsArraySize.
firstIndex := 2. lastIndex := 1.
^ self
].
@@ -1654,7 +1677,6 @@
]
].
newSize := oldSize * 2.
- newSize == 0 ifTrue:[ newSize := 3].
newContents := Array basicNew:newSize.
newContents
replaceFrom:(oldSize + 1) to:newSize
@@ -1745,36 +1767,42 @@
^ index
].
- newSize := oldSize * 2.
- newSize == 0 ifTrue:[ newSize := 3].
+ oldSize == 0 ifTrue:[
+ newSize := MinContentsArraySize
+ ] ifFalse:[
+ newSize := oldSize * 2.
+ ].
newContents := Array basicNew:newSize.
index == first ifTrue:[
"/ if there is a lot at the end (> 50), make all new space at the beginning.
"/ otherwise make 3/4 of the new space to the beginning, 1/4 to the end
- (last < (oldSize - 50)) ifTrue:[
- lastIndex := newSize - (oldSize-last).
- firstIndex := lastIndex - (last - first).
- ] ifFalse:[
- firstIndex := oldSize * 3 // 4.
- lastIndex := firstIndex + (last - first).
+ oldSize ~~ 0 ifTrue:[
+ (last < (oldSize - 50)) ifTrue:[
+ lastIndex := newSize - (oldSize-last).
+ firstIndex := lastIndex - (last - first).
+ ] ifFalse:[
+ firstIndex := oldSize * 3 // 4.
+ lastIndex := firstIndex + (last - first).
+ ].
+ newContents
+ replaceFrom:firstIndex to:lastIndex
+ with:contentsArray startingAt:first.
].
- newContents
- replaceFrom:firstIndex to:lastIndex
- with:contentsArray startingAt:first.
-
contentsArray := newContents.
firstIndex := firstIndex - 1.
^ firstIndex.
] ifFalse:[
- newContents
- replaceFrom:1 to:(index - first)
- with:contentsArray startingAt:first.
+ oldSize ~~ 0 ifTrue:[
+ newContents
+ replaceFrom:1 to:(index - first)
+ with:contentsArray startingAt:first.
- index <= last ifTrue:[
- newContents
- replaceFrom:(index - first + 2) to:(last - first + 2)
- with:contentsArray startingAt:index.
+ index <= last ifTrue:[
+ newContents
+ replaceFrom:(index - first + 2) to:(last - first + 2)
+ with:contentsArray startingAt:index.
+ ].
].
contentsArray := newContents.
firstIndex := 1.
@@ -1816,7 +1844,6 @@
shiftLeft := shiftRight := false.
((first > howMany) and:[first > oneFourthOfSize]) ifTrue:[
"there is room (>25%) at the beginning"
-
shiftLeft := true.
] ifFalse:[
((last + howMany) <= oldSize
@@ -1861,19 +1888,21 @@
].
newContents := Array basicNew:newSize.
- index > first ifTrue:[
- newContents
- replaceFrom:1
- to:index - first
- with:contentsArray
- startingAt:first.
- ].
- index <= last ifTrue:[
- newContents
- replaceFrom:index - first + howMany + 1
- to:(last - first + howMany + 1)
- with:contentsArray
- startingAt:(index).
+ oldSize ~~ 0 ifTrue:[
+ index > first ifTrue:[
+ newContents
+ replaceFrom:1
+ to:index - first
+ with:contentsArray
+ startingAt:first.
+ ].
+ index <= last ifTrue:[
+ newContents
+ replaceFrom:index - first + howMany + 1
+ to:(last - first + howMany + 1)
+ with:contentsArray
+ startingAt:(index).
+ ].
].
contentsArray := newContents.
firstIndex := 1.
@@ -1918,12 +1947,17 @@
lastIndex := startIndex + sz - 1.
^ self
].
- newSize := oldSize * 2.
- newSize == 0 ifTrue:[newSize := 3].
+ oldSize == 0 ifTrue:[
+ newSize := MinContentsArraySize
+ ] ifFalse:[
+ newSize := oldSize * 2.
+ ].
newContents := Array basicNew:newSize.
- newContents
- replaceFrom:1 to:oldSize
- with:contentsArray startingAt:1.
+ oldSize ~~ 0 ifTrue:[
+ newContents
+ replaceFrom:1 to:oldSize
+ with:contentsArray startingAt:1.
+ ].
contentsArray := newContents
"Modified: / 22-10-2008 / 11:50:28 / cg"
@@ -2193,3 +2227,5 @@
^ '$Header$'
! !
+
+OrderedCollection initialize!