OrderedCollection.st
author Stefan Vogel <sv@exept.de>
Tue, 28 Apr 2020 16:22:44 +0200
changeset 25376 88a3329875ba
parent 25370 c417f7edaec1
permissions -rw-r--r--
#REFACTORING by stefan class: MethodDictionary class removed: #newWithCapacity: moved to superclass
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25366
779764ba117b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 25351
diff changeset
     1
"{ Encoding: utf8 }"
779764ba117b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 25351
diff changeset
     2
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     3
"
5
67342904af11 *** empty log message ***
claus
parents: 3
diff changeset
     4
 COPYRIGHT (c) 1989 by Claus Gittinger
159
514c749165c3 *** empty log message ***
claus
parents: 114
diff changeset
     5
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    12
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    13
"
5401
81ea19e6843c test for class
ps
parents: 4862
diff changeset
    14
"{ Package: 'stx:libbasic' }"
81ea19e6843c test for class
ps
parents: 4862
diff changeset
    15
17705
76b6ce4a59cd class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 17167
diff changeset
    16
"{ NameSpace: Smalltalk }"
76b6ce4a59cd class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 17167
diff changeset
    17
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    18
SequenceableCollection subclass:#OrderedCollection
1071
5875a6eedd28 added #includesIdentical:
Claus Gittinger <cg@exept.de>
parents: 679
diff changeset
    19
	instanceVariableNames:'contentsArray firstIndex lastIndex'
18879
5d1bbfc1761d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18811
diff changeset
    20
	classVariableNames:'MinContentsArraySize'
1071
5875a6eedd28 added #includesIdentical:
Claus Gittinger <cg@exept.de>
parents: 679
diff changeset
    21
	poolDictionaries:''
5875a6eedd28 added #includesIdentical:
Claus Gittinger <cg@exept.de>
parents: 679
diff changeset
    22
	category:'Collections-Sequenceable'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    23
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    24
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
    25
!OrderedCollection class methodsFor:'documentation'!
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
    26
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    27
copyright
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    28
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    29
 COPYRIGHT (c) 1989 by Claus Gittinger
159
514c749165c3 *** empty log message ***
claus
parents: 114
diff changeset
    30
	      All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    31
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    32
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    33
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    34
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    35
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    36
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    37
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    38
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    39
!
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    40
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
    41
documentation
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
    42
"
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    43
    OrderedCollections (OCs) have their elements ordered as they were added.
18182
8504bb241a13 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18177
diff changeset
    44
    In addition, they provide all indexing access protocol
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    45
    and bulk copying (much like Arrays).
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    46
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    47
    Insertion and removal at both ends is possible and also usually fast
18182
8504bb241a13 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18177
diff changeset
    48
    - therefore they can be used for queues and stacks.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    49
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    50
    [Instance variables:]
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    51
	contentsArray   <Array>         the actual contents
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    52
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    53
	firstIndex      <SmallInteger>  index of first valid element
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    54
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    55
	lastIndex       <SmallInteger>  index of last valid element
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    56
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    57
    [performance hint:]
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    58
	They overallocate a few slots to make insertion at either end often O(1),
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    59
	but sometimes O(n), where n is the current size of the collection
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    60
	(i.e. they have reallocate the underlying element buffer and therefore copy the
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    61
	 elements into a new one. However, this reallocation is not done on every insert,
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    62
	 and if elements are deleted and others reinserted, the buffer is usually already able
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    63
	 to hold the new element)
19096
dc826aced958 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18879
diff changeset
    64
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    65
	Insertion in the middle is O(n), and therefore slower, because elements have to be
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    66
	shuffled towards either end, in order to make space for the new element.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    67
	Therefore, it is often cheaper, to instantiate a new object, and copy over the
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    68
	elements.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    69
	see SegmentedOrderedCollection for a collection, which is specialized for this
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    70
	kind of usage with many elements.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    71
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    72
    [beginners bug hint:]
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    73
	notice that:
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    74
	    Array new:n
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    75
	is quite different from:
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    76
	    OrderedCollection new:n
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
    77
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    78
	The later creates an OC which is prepared to hold <n> elements,
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    79
	but has a logical size of 0 (zero). To get an OC containing <n> nils,
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    80
	use:
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    81
	     (OrderedCollection new) grow:n
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    82
	or the syntactic sugar for that:
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    83
	     OrderedCollection newWithSize:n
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    84
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    85
	I know, this is confusing for beginners and was a bad semantic decision.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    86
	However, that's the way the standard was defined and how all Smalltalk's behave,
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    87
	so we can't change that here.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    88
4064
fd3d2922fb8b documentation
Claus Gittinger <cg@exept.de>
parents: 3968
diff changeset
    89
    [memory requirements:]
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    90
	OBJ-HEADER + (3 * ptr-size)
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    91
		   + (size-roundedUpToNextPowerOf2 * ptr-size)
4064
fd3d2922fb8b documentation
Claus Gittinger <cg@exept.de>
parents: 3968
diff changeset
    92
19096
dc826aced958 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 18879
diff changeset
    93
    [complexity:]
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    94
	access by index: O(1)
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    95
	insertion at either end: mostly O(1)
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    96
	removal at either end: O(1)
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    97
	insertion in the middle: O(n)
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    98
	searching: O(n)
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
    99
	min/max: O(n)
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   100
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   101
    [see also:]
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   102
	Array
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   103
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
   104
    [author:]
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   105
	Claus Gittinger
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
   106
"
1321
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   107
!
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   108
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   109
examples
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   110
"
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   111
  using OC as a stack:
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   112
									[exBegin]
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   113
    |stack top|
1321
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   114
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   115
    stack := OrderedCollection new.
1321
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   116
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   117
    1 to:10 do:[:i |
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   118
	stack add:i
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   119
    ].
1321
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   120
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   121
    10 timesRepeat:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   122
	top := stack removeLast.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   123
	Transcript showCR:top
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   124
    ]
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   125
									[exEnd]
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   126
  using OC as a queue (you should use Queue right away ..):
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   127
									[exBegin]
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   128
    |queue dequeued|
1321
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   129
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   130
    queue := OrderedCollection new.
1321
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   131
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   132
    1 to:10 do:[:i |
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   133
	queue addLast:i
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   134
    ].
1321
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   135
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   136
    10 timesRepeat:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   137
	dequeued := queue removeFirst.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   138
	Transcript showCR:dequeued
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   139
    ]
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   140
									[exEnd]
1324
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   141
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   142
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   143
  examples to support the performance hint (see documentation)
1324
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   144
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   145
    timing removal of all odd elements in a collection of 10000:
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   146
    (940 ms on P5/133)
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   147
									[exBegin]
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   148
	|coll time|
1324
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   149
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   150
	coll := (1 to:10000) asOrderedCollection.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   151
	time := Time millisecondsToRun:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   152
	    coll removeAllSuchThat:[:el | el even]
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   153
	].
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   154
	Transcript show:'time is '; show:time; showCR:' ms'.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   155
									[exEnd]
1324
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   156
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   157
    tuning the removal by doing it reverse
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   158
    (less copying in #removeAtIndex:) speeds it up by a factor of 2:
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   159
									[exBegin]
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   160
	|coll time|
1324
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   161
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   162
	coll := (1 to:10000) asOrderedCollection.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   163
	time := Time millisecondsToRun:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   164
	    coll size to:1 by:-1 do:[:index |
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   165
		(coll at:index) even ifTrue:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   166
		    coll removeAtIndex:index
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   167
		]
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   168
	    ]
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   169
	].
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   170
	Transcript show:'time is '; show:time; showCR:' ms'.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   171
									[exEnd]
1324
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   172
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   173
    rebuilding a new collection:
1324
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   174
    (64 ms on P5/133)
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   175
									[exBegin]
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   176
	|coll time|
1324
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   177
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   178
	coll := (1 to:10000) asOrderedCollection.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   179
	time := Time millisecondsToRun:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   180
	    coll := coll select:[:el | el odd]
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   181
	].
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   182
	Transcript show:'time is '; show:time; showCR:' ms'.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   183
									[exEnd]
11271
5b75b4a7fb35 tuned makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 11269
diff changeset
   184
5b75b4a7fb35 tuned makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 11269
diff changeset
   185
    adding at the end (fast):
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   186
									[exBegin]
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   187
	|coll time|
11271
5b75b4a7fb35 tuned makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 11269
diff changeset
   188
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   189
	coll := OrderedCollection new.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   190
	time := TimeDuration toRun:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   191
	    (1 to:100000) do:[:el | coll add:el].
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   192
	].
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   193
	Transcript show:'time is '; showCR:time.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   194
	self assert:(coll = (1 to:100000)).
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   195
	self assert:(coll asBag = (1 to:100000) asBag).
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   196
									[exEnd]
11271
5b75b4a7fb35 tuned makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 11269
diff changeset
   197
    adding at front (fast):
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   198
									[exBegin]
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   199
	|coll time|
11271
5b75b4a7fb35 tuned makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 11269
diff changeset
   200
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   201
	coll := OrderedCollection new.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   202
	time := TimeDuration toRun:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   203
	    (1 to:100000) reverseDo:[:el | coll addFirst:el].
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   204
	].
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   205
	Transcript show:'time is '; showCR:time.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   206
	self assert:(coll = (1 to:100000)).
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   207
	self assert:(coll asBag = (1 to:100000) asBag).
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   208
									[exEnd]
11271
5b75b4a7fb35 tuned makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 11269
diff changeset
   209
    inserting in the middle (slow):
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   210
									[exBegin]
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   211
	|coll time|
11271
5b75b4a7fb35 tuned makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 11269
diff changeset
   212
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   213
	coll := OrderedCollection new.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   214
	time := TimeDuration toRun:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   215
	    (1 to:100000) do:[:el | coll add:el beforeIndex:(coll size // 2)+1 ].
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   216
	].
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   217
	Transcript show:'time is '; showCR:time.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   218
	self assert:(coll asBag = (1 to:100000) asBag).
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   219
									[exEnd]
1321
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   220
"
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
   221
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   222
18879
5d1bbfc1761d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18811
diff changeset
   223
!OrderedCollection class methodsFor:'initialization'!
5d1bbfc1761d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18811
diff changeset
   224
5d1bbfc1761d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18811
diff changeset
   225
initialize
19990
c819e0a71d64 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19865
diff changeset
   226
    MinContentsArraySize := 4. "the minimum size of a non-empty contentsArray"
18879
5d1bbfc1761d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18811
diff changeset
   227
! !
5d1bbfc1761d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18811
diff changeset
   228
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   229
!OrderedCollection class methodsFor:'instance creation'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   230
a27a279701f8 Initial revision
claus
parents:
diff changeset
   231
new
1106
a101113cc0a8 commentary
Claus Gittinger <cg@exept.de>
parents: 1090
diff changeset
   232
    "create a new, empty OrderedCollection"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   233
19991
7675defcd5c2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19990
diff changeset
   234
    ^ self basicNew initContents:0
18879
5d1bbfc1761d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18811
diff changeset
   235
5d1bbfc1761d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18811
diff changeset
   236
    "
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   237
	self new
18879
5d1bbfc1761d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18811
diff changeset
   238
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   239
	|nEmpty|
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   240
	nEmpty := 0.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   241
	self allInstancesDo:[:e| e size == 0 ifTrue:[nEmpty := nEmpty + 1]].
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   242
	nEmpty
18879
5d1bbfc1761d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18811
diff changeset
   243
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   244
	|nEmpty|
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   245
	nEmpty := OrderedCollection new.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   246
	self allInstancesDo:[:e| (e size == 0 and:[e contentsArray size ~~ 0]) ifTrue:[nEmpty add:e]].
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   247
	nEmpty
18879
5d1bbfc1761d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18811
diff changeset
   248
    "
1106
a101113cc0a8 commentary
Claus Gittinger <cg@exept.de>
parents: 1090
diff changeset
   249
a101113cc0a8 commentary
Claus Gittinger <cg@exept.de>
parents: 1090
diff changeset
   250
    "Modified: 19.3.1996 / 17:53:12 / cg"
293
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
   251
!
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
   252
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   253
new:size
1106
a101113cc0a8 commentary
Claus Gittinger <cg@exept.de>
parents: 1090
diff changeset
   254
    "create a new, empty OrderedCollection with a preallocated physical
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   255
     size.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   256
     NOTICE:
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   257
	the logical size of the returned collection is 0 (i.e. it is empty).
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   258
	This may be confusing, in that it is different from what Array>>new:
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   259
	returns. However, that's the way OrderedCollections work in every other
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   260
	Smalltalk, so here it should as well.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   261
     See also newWithSize:, which might do what you really want.
1106
a101113cc0a8 commentary
Claus Gittinger <cg@exept.de>
parents: 1090
diff changeset
   262
     "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   263
19991
7675defcd5c2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19990
diff changeset
   264
    ^ self basicNew initContents:size
1106
a101113cc0a8 commentary
Claus Gittinger <cg@exept.de>
parents: 1090
diff changeset
   265
a101113cc0a8 commentary
Claus Gittinger <cg@exept.de>
parents: 1090
diff changeset
   266
    "Modified: 19.3.1996 / 17:53:47 / cg"
4427
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   267
!
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   268
6700
e4254778c239 new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents: 6088
diff changeset
   269
new:size withAll:element
e4254778c239 new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents: 6088
diff changeset
   270
    "return a new collection of size, where all elements are
e4254778c239 new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents: 6088
diff changeset
   271
     initialized to element."
e4254778c239 new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents: 6088
diff changeset
   272
22295
d47bb2912953 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21658
diff changeset
   273
    ^ (self newWithSize:size)
d47bb2912953 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21658
diff changeset
   274
        atAllPut:element;
d47bb2912953 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21658
diff changeset
   275
        yourself.
6700
e4254778c239 new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents: 6088
diff changeset
   276
e4254778c239 new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents: 6088
diff changeset
   277
    "
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   278
     OrderedCollection new:10 withAll:1234
6700
e4254778c239 new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents: 6088
diff changeset
   279
    "
22295
d47bb2912953 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21658
diff changeset
   280
d47bb2912953 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21658
diff changeset
   281
    "Modified: / 09-10-2017 / 17:04:11 / stefan"
6700
e4254778c239 new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents: 6088
diff changeset
   282
!
e4254778c239 new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents: 6088
diff changeset
   283
4427
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   284
newFrom:aCollection
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   285
    "return a new OrderedCollection filled with all elements from the argument,
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   286
     aCollection"
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   287
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   288
    |newColl|
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   289
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   290
    newColl := self new:(aCollection size).
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   291
    newColl addAll:aCollection.
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   292
    ^ newColl
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   293
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   294
    "
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   295
     OrderedCollection newFrom:#(1 2 3 4)
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   296
     OrderedCollection newFrom:(Set with:1 with:2 with:3)
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   297
    "
17167
6b129633ca35 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 16713
diff changeset
   298
!
6b129633ca35 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 16713
diff changeset
   299
6b129633ca35 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 16713
diff changeset
   300
newLikelyToRemainEmpty
6b129633ca35 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 16713
diff changeset
   301
    "create a new, empty OrderedCollection, for which we already know
6b129633ca35 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 16713
diff changeset
   302
     that it is very likely to remain empty.
6b129633ca35 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 16713
diff changeset
   303
     Use this for collections which may sometimes get elements added, but usually not.
19991
7675defcd5c2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19990
diff changeset
   304
     This is now obsolete and the same as new:
7675defcd5c2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19990
diff changeset
   305
     as the algorithm is now clever enough to deal efficiently with this situation"
17167
6b129633ca35 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 16713
diff changeset
   306
19991
7675defcd5c2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19990
diff changeset
   307
    ^ self new:0
17167
6b129633ca35 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 16713
diff changeset
   308
6b129633ca35 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 16713
diff changeset
   309
    "
6b129633ca35 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 16713
diff changeset
   310
     self newLikelyToRemainEmpty size
6b129633ca35 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 16713
diff changeset
   311
     self newLikelyToRemainEmpty add:1; size
6b129633ca35 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 16713
diff changeset
   312
     self newLikelyToRemainEmpty addAll:#(1 2); size
6b129633ca35 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 16713
diff changeset
   313
    "
25366
779764ba117b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 25351
diff changeset
   314
!
779764ba117b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 25351
diff changeset
   315
25369
b3d232a613c9 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 25366
diff changeset
   316
newWithCapacity:size
b3d232a613c9 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 25366
diff changeset
   317
    "create a new, empty OrderedCollection with a preallocated physical size.
b3d232a613c9 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 25366
diff changeset
   318
     Different from #new: for the StringCollection subclass, which allocates size empty lines."
b3d232a613c9 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 25366
diff changeset
   319
b3d232a613c9 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 25366
diff changeset
   320
    ^ self new:size
b3d232a613c9 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 25366
diff changeset
   321
b3d232a613c9 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 25366
diff changeset
   322
    "Created: / 28-04-2020 / 14:32:27 / Stefan Vogel"
293
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
   323
! !
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
   324
20026
c4063e226888 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19991
diff changeset
   325
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   326
!OrderedCollection methodsFor:'accessing'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   327
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   328
at:anInteger
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   329
    "return the element at index, anInteger"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   330
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   331
    |idx "{ Class: SmallInteger }"|
387
claus
parents: 384
diff changeset
   332
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   333
    idx := anInteger + firstIndex - 1.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   334
    ((anInteger < 1) or:[idx > lastIndex]) ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   335
	^ self subscriptBoundsError:anInteger
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   336
    ].
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   337
    ^ contentsArray basicAt:idx
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   338
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   339
    "Modified: / 12.11.1997 / 17:56:05 / cg"
360
claus
parents: 349
diff changeset
   340
!
claus
parents: 349
diff changeset
   341
18811
54e839ea9d44 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18379
diff changeset
   342
at:anInteger ifAbsent:exceptionValue
54e839ea9d44 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18379
diff changeset
   343
    "return the element at index, anInteger.
54e839ea9d44 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18379
diff changeset
   344
     If the index is invalid, return the value from exceptionValue"
54e839ea9d44 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18379
diff changeset
   345
54e839ea9d44 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18379
diff changeset
   346
    |idx "{ Class: SmallInteger }"|
54e839ea9d44 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18379
diff changeset
   347
54e839ea9d44 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18379
diff changeset
   348
    idx := anInteger + firstIndex - 1.
54e839ea9d44 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18379
diff changeset
   349
    ((anInteger < 1) or:[idx > lastIndex]) ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   350
	^ exceptionValue value
18811
54e839ea9d44 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18379
diff changeset
   351
    ].
54e839ea9d44 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18379
diff changeset
   352
    ^ contentsArray basicAt:idx
54e839ea9d44 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18379
diff changeset
   353
!
54e839ea9d44 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18379
diff changeset
   354
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   355
at:anInteger put:anObject
1221
46d72af387e9 commentary
Claus Gittinger <cg@exept.de>
parents: 1170
diff changeset
   356
    "set the element at index, to be anInteger.
46d72af387e9 commentary
Claus Gittinger <cg@exept.de>
parents: 1170
diff changeset
   357
     Return anObject (sigh)."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   358
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   359
    |idx "{ Class: SmallInteger }"|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   360
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   361
    idx := anInteger + firstIndex - 1.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   362
    ((anInteger < 1) or:[idx > lastIndex]) ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   363
	^ self subscriptBoundsError:anInteger
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   364
    ].
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   365
    ^ contentsArray basicAt:idx put:anObject
1221
46d72af387e9 commentary
Claus Gittinger <cg@exept.de>
parents: 1170
diff changeset
   366
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   367
    "Modified: / 12.11.1997 / 17:56:32 / cg"
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 159
diff changeset
   368
!
40ca7cc6fb9c *** empty log message ***
claus
parents: 159
diff changeset
   369
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   370
first
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   371
    "return the first element"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   372
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   373
    firstIndex <= lastIndex ifTrue:[
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   374
	^ contentsArray at:firstIndex
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   375
    ].
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   376
    "error if collection is empty"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   377
    ^ self emptyCollectionError
2
claus
parents: 1
diff changeset
   378
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   379
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   380
     (OrderedCollection withAll:#(1 2 3 4 5)) first
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   381
     (SortedCollection withAll:#(5 4 3 2 1)) first
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   382
    "
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   383
!
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   384
22392
3a8fce0e8d11 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22319
diff changeset
   385
firstOrNil
3a8fce0e8d11 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22319
diff changeset
   386
    "return the first element or nil, if empty."
3a8fce0e8d11 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22319
diff changeset
   387
3a8fce0e8d11 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22319
diff changeset
   388
    firstIndex <= lastIndex ifTrue:[
3a8fce0e8d11 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22319
diff changeset
   389
        ^ contentsArray at:firstIndex
3a8fce0e8d11 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22319
diff changeset
   390
    ].
3a8fce0e8d11 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22319
diff changeset
   391
    ^ nil
3a8fce0e8d11 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22319
diff changeset
   392
3a8fce0e8d11 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22319
diff changeset
   393
    "
3a8fce0e8d11 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22319
diff changeset
   394
     (OrderedCollection withAll:#(1 2 3 4 5)) firstOrNil
3a8fce0e8d11 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22319
diff changeset
   395
     (SortedCollection withAll:#()) firstOrNil
3a8fce0e8d11 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22319
diff changeset
   396
    "
3a8fce0e8d11 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22319
diff changeset
   397
3a8fce0e8d11 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22319
diff changeset
   398
    "Created: / 13-12-2017 / 23:16:02 / stefan"
3a8fce0e8d11 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22319
diff changeset
   399
!
3a8fce0e8d11 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 22319
diff changeset
   400
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   401
last
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   402
    "return the last element"
68
59faa75185ba *** empty log message ***
claus
parents: 56
diff changeset
   403
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   404
    firstIndex <= lastIndex ifTrue:[
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   405
	^ contentsArray at:lastIndex
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   406
    ].
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   407
    "error if collection is empty"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   408
    ^ self emptyCollectionError
68
59faa75185ba *** empty log message ***
claus
parents: 56
diff changeset
   409
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   410
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   411
     (OrderedCollection withAll:#(1 2 3 4 5)) last
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   412
     (SortedCollection withAll:#(5 4 3 2 1)) last
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   413
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   414
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   415
a27a279701f8 Initial revision
claus
parents:
diff changeset
   416
!OrderedCollection methodsFor:'adding & removing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   417
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   418
add:anObject
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   419
    "add the argument, anObject to the end of the collection
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   420
     Return the argument, anObject."
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   421
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   422
    |idx "{ Class:SmallInteger }"|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   423
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   424
    idx := lastIndex.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   425
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   426
    (idx == contentsArray size) ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   427
	self makeRoomAtLast.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   428
	idx := lastIndex.
42
e33491f6f260 *** empty log message ***
claus
parents: 40
diff changeset
   429
    ].
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   430
    lastIndex := idx := idx + 1.
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   431
    contentsArray basicAt:idx put:anObject.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   432
    ^ anObject
42
e33491f6f260 *** empty log message ***
claus
parents: 40
diff changeset
   433
348
claus
parents: 345
diff changeset
   434
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   435
     |c|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   436
     c := #(4 3 2 1) asOrderedCollection.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   437
     c add:'here'
348
claus
parents: 345
diff changeset
   438
    "
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   439
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   440
    "Modified: / 12.11.1997 / 17:49:47 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   441
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   442
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   443
add:newObject after:oldObject
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   444
    "insert the argument, newObject after oldObject.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   445
     If oldObject is not in the receiver, report an error,
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   446
     otherwise return the argument, anObject."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   447
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   448
    |idx|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   449
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   450
    idx := self indexOf:oldObject.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   451
    idx ~~ 0 ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   452
	self add:newObject beforeIndex:(idx + 1).
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   453
	^ newObject
42
e33491f6f260 *** empty log message ***
claus
parents: 40
diff changeset
   454
    ].
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   455
    ^ self errorValueNotFound:oldObject
42
e33491f6f260 *** empty log message ***
claus
parents: 40
diff changeset
   456
348
claus
parents: 345
diff changeset
   457
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   458
     |c|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   459
     c := #(4 3 2 1) asOrderedCollection.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   460
     c add:'here' after:3; yourself.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   461
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   462
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   463
     |c|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   464
     c := #(4 3 2 1) asOrderedCollection.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   465
     c add:'here' after:1; yourself.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   466
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   467
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   468
     |c|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   469
     c := #(4 3 2 1) asOrderedCollection.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   470
     c add:'here' after:5; yourself
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   471
    "
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   472
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   473
    "Modified: 12.4.1996 / 13:51:56 / cg"
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   474
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   475
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   476
add:anObject afterIndex:index
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   477
    "insert the argument, anObject to become located at index.
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   478
     Return the receiver (sigh - ST-80 compatibility)."
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   479
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   480
    ^ self add:anObject beforeIndex:(index + 1)
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   481
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   482
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   483
     |c|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   484
     c := #(4 3 2 1) asOrderedCollection.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   485
     c add:'here' afterIndex:2
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   486
    "
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   487
    "
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   488
     |c|
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   489
     c := #(4 3 2 1) asOrderedCollection.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   490
     c add:'here' afterIndex:4
348
claus
parents: 345
diff changeset
   491
    "
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   492
    "
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   493
     |c|
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   494
     c := #(4 3 2 1) asOrderedCollection.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   495
     c add:'here' afterIndex:0
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   496
    "
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   497
    "
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   498
     |c|
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   499
     c := #(4 3 2 1) asOrderedCollection.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   500
     c add:'here' afterIndex:5
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   501
    "
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   502
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   503
    "Modified: 12.4.1996 / 13:52:41 / cg"
348
claus
parents: 345
diff changeset
   504
!
claus
parents: 345
diff changeset
   505
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   506
add:newObject before:oldObject
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   507
    "insert the argument, newObject before oldObject.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   508
     If oldObject is not in the receiver, report an error,
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   509
     otherwise return the argument, anObject."
348
claus
parents: 345
diff changeset
   510
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   511
    |idx|
348
claus
parents: 345
diff changeset
   512
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   513
    idx := self indexOf:oldObject.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   514
    idx ~~ 0 ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   515
	self add:newObject beforeIndex:idx.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   516
	^ newObject
348
claus
parents: 345
diff changeset
   517
    ].
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   518
    ^ self errorValueNotFound:oldObject
348
claus
parents: 345
diff changeset
   519
claus
parents: 345
diff changeset
   520
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   521
     |c|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   522
     c := #(4 3 2 1) asOrderedCollection.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   523
     c add:'here' before:3.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   524
     c add:'here' before:5
348
claus
parents: 345
diff changeset
   525
    "
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   526
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   527
    "Modified: 12.4.1996 / 13:25:47 / cg"
348
claus
parents: 345
diff changeset
   528
!
claus
parents: 345
diff changeset
   529
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   530
add:anObject beforeIndex:index
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   531
    "insert the argument, anObject to become located at index.
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   532
     Return the receiver (sigh - ST-80 compatibility)."
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   533
3236
b5f80b416cff check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 3109
diff changeset
   534
    |idx physicalIndex|
348
claus
parents: 345
diff changeset
   535
3236
b5f80b416cff check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 3109
diff changeset
   536
    physicalIndex := index + firstIndex - 1.
b5f80b416cff check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 3109
diff changeset
   537
    physicalIndex > lastIndex ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   538
	"can only add to the end of the collection"
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   539
	physicalIndex == (lastIndex+1) ifTrue:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   540
	    idx := lastIndex.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   541
	    (idx == contentsArray size) ifTrue:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   542
		self makeRoomAtLast.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   543
		idx := lastIndex.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   544
	    ].
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   545
	    lastIndex := idx := idx + 1.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   546
	] ifFalse:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   547
	    ^ self subscriptBoundsError:index.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   548
	]
10839
285fa5534f61 #add:beforeIndex: take care for redefinitions of #add: in subclasses sending #add:beforeIndex:
Stefan Vogel <sv@exept.de>
parents: 10799
diff changeset
   549
    ] ifFalse:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   550
	"notice: this may change firstIndex"
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   551
	idx := self makeRoomAtIndex:physicalIndex.
3236
b5f80b416cff check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 3109
diff changeset
   552
    ].
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   553
    contentsArray basicAt:idx put:anObject.
348
claus
parents: 345
diff changeset
   554
claus
parents: 345
diff changeset
   555
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   556
     |c|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   557
     c := #(4 3 2 1) asOrderedCollection.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   558
     c add:'here' beforeIndex:3
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   559
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   560
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   561
     |c|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   562
     c := #(4 3 2 1) asOrderedCollection.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   563
     c add:'here' beforeIndex:1
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   564
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   565
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   566
     |c|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   567
     c := #(4 3 2 1) asOrderedCollection.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   568
     c add:'here' beforeIndex:5
348
claus
parents: 345
diff changeset
   569
    "
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   570
3236
b5f80b416cff check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 3109
diff changeset
   571
    "Modified: / 28.1.1998 / 17:17:48 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   572
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   573
3109
2ac640e610aa reimplement #addAll:aCollection
ca
parents: 3107
diff changeset
   574
addAll:aCollection
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   575
    "add all elements of the argument, aCollection to the receiver.
7752
134213bdebd8 comment
Claus Gittinger <cg@exept.de>
parents: 7260
diff changeset
   576
     Returns the argument, aCollection (sigh)."
3109
2ac640e610aa reimplement #addAll:aCollection
ca
parents: 3107
diff changeset
   577
2ac640e610aa reimplement #addAll:aCollection
ca
parents: 3107
diff changeset
   578
    self addAll:aCollection beforeIndex:(1 + self size).
2ac640e610aa reimplement #addAll:aCollection
ca
parents: 3107
diff changeset
   579
    ^ aCollection
2ac640e610aa reimplement #addAll:aCollection
ca
parents: 3107
diff changeset
   580
!
2ac640e610aa reimplement #addAll:aCollection
ca
parents: 3107
diff changeset
   581
15442
5ae3f8265977 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15401
diff changeset
   582
addAll:aCollection afterIndex:index
5ae3f8265977 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15401
diff changeset
   583
    "insert the argument, anObject to become located after index.
5ae3f8265977 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15401
diff changeset
   584
     Return the receiver (sigh - ST-80 compatibility)."
5ae3f8265977 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15401
diff changeset
   585
5ae3f8265977 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15401
diff changeset
   586
    ^ self addAll:aCollection beforeIndex:(index + 1)
5ae3f8265977 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15401
diff changeset
   587
5ae3f8265977 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15401
diff changeset
   588
    "
5ae3f8265977 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15401
diff changeset
   589
     |c|
5ae3f8265977 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15401
diff changeset
   590
     c := #(4 3 2 1) asOrderedCollection.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   591
     c addAll:#(10 20 30) afterIndex:2
15442
5ae3f8265977 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15401
diff changeset
   592
    "
5ae3f8265977 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15401
diff changeset
   593
    "
5ae3f8265977 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15401
diff changeset
   594
     |c|
5ae3f8265977 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15401
diff changeset
   595
     c := #(4 3 2 1) asOrderedCollection.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   596
     c addAll:#(10 20 30) afterIndex:4
15442
5ae3f8265977 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15401
diff changeset
   597
    "
5ae3f8265977 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15401
diff changeset
   598
    "
5ae3f8265977 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15401
diff changeset
   599
     |c|
5ae3f8265977 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15401
diff changeset
   600
     c := #(4 3 2 1) asOrderedCollection.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   601
     c addAll:#(10 20 30) afterIndex:0
15442
5ae3f8265977 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15401
diff changeset
   602
    "
5ae3f8265977 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15401
diff changeset
   603
5ae3f8265977 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15401
diff changeset
   604
    "Modified: 12.4.1996 / 13:52:41 / cg"
5ae3f8265977 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15401
diff changeset
   605
!
5ae3f8265977 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15401
diff changeset
   606
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   607
addAll:aCollection beforeIndex:index
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   608
    "insert all elements of the argument, anObject to become located at index.
24894
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   609
     The collection may be unordered, but then the order of the sliced-in elements
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   610
     is undefined.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   611
     Return the receiver."
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   612
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   613
    |idx count|
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   614
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   615
    aCollection isSequenceable ifTrue:[
24894
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   616
        "/ we are lucky - that thing can count & do bulk copies
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   617
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   618
        count := aCollection size.
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   619
        idx := self makeRoomAtIndex:(index + firstIndex - 1) for:count.
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   620
        "/ notice: the above may change firstIndex
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   621
        contentsArray replaceFrom:idx to:(idx + count - 1) with:aCollection startingAt:1.
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   622
        ^ self
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   623
    ].
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   624
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   625
    idx := index.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   626
    aCollection do:[:element |
24894
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   627
        self add:element beforeIndex:idx.
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   628
        idx := idx + 1.
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   629
    ].
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   630
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   631
    "
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   632
     |c|
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   633
     c := #(1 2 3 4) asOrderedCollection.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   634
     c addAll:'here' beforeIndex:3
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   635
    "
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   636
    "
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   637
     |c|
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   638
     c := #(1 2 3 4) asOrderedCollection.
2549
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
   639
     c removeFirst.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   640
     c addAll:'here' beforeIndex:3
2549
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
   641
    "
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
   642
    "
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
   643
     |c|
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
   644
     c := #(1 2 3 4) asOrderedCollection.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   645
     c addAll:'here' beforeIndex:1
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   646
    "
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   647
    "
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   648
     |c|
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   649
     c := #(1 2 3 4) asOrderedCollection.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   650
     c addAll:'here' beforeIndex:5
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   651
    "
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   652
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   653
    "
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   654
     |c|
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   655
     c := #(1 2 3 4) asOrderedCollection.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   656
     c addAll:('hello' asSet) beforeIndex:3
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   657
    "
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   658
2549
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
   659
    "Modified: 15.4.1997 / 12:43:59 / cg"
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   660
!
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   661
23245
cccc6c4abcfc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22671
diff changeset
   662
addAll:aCollection from:startIndex to:endIndex beforeIndex:index
cccc6c4abcfc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22671
diff changeset
   663
    "insert elements start to stop from the argument
cccc6c4abcfc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22671
diff changeset
   664
     Return the receiver."
cccc6c4abcfc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22671
diff changeset
   665
cccc6c4abcfc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22671
diff changeset
   666
    |count idx|
cccc6c4abcfc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22671
diff changeset
   667
cccc6c4abcfc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22671
diff changeset
   668
    aCollection isSequenceable ifFalse:[
cccc6c4abcfc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22671
diff changeset
   669
        self error:'collection must be sequenceable'
cccc6c4abcfc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22671
diff changeset
   670
    ].
cccc6c4abcfc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22671
diff changeset
   671
    count := endIndex - startIndex + 1.
cccc6c4abcfc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22671
diff changeset
   672
    idx := self makeRoomAtIndex:(index + firstIndex - 1) for:count.
cccc6c4abcfc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22671
diff changeset
   673
cccc6c4abcfc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22671
diff changeset
   674
    "/ we are lucky - that thing can count & do bulk copies
cccc6c4abcfc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22671
diff changeset
   675
    "/ notice: the above may change firstIndex
cccc6c4abcfc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22671
diff changeset
   676
    contentsArray replaceFrom:idx to:(idx + count - 1) with:aCollection startingAt:startIndex.
cccc6c4abcfc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22671
diff changeset
   677
cccc6c4abcfc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22671
diff changeset
   678
    "Created: / 30-07-2018 / 11:15:05 / Stefan Vogel"
cccc6c4abcfc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22671
diff changeset
   679
!
cccc6c4abcfc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22671
diff changeset
   680
24894
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   681
addAllFirst:aCollection
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   682
    "insert all elements of the argument, aCollection at the beginning
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   683
     of the receiver. Returns the argument, aCollection."
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   684
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   685
    self addAll:aCollection beforeIndex:1.
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   686
    ^ aCollection
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   687
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   688
    "
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   689
     |c|
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   690
     c := #(1 2 3 4) asOrderedCollection.
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   691
     c addAllFirst:#(10 20 30).
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   692
     c                        
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   693
    "
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   694
    "
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   695
     |c|
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   696
     c := #(1 2 3 4) asOrderedCollection.
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   697
     c addAllFirst:#().
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   698
     c   
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   699
    "
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   700
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   701
    "
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   702
     |c|
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   703
     c := #() asOrderedCollection.
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   704
     c addAllFirst:#(10 20 30).
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   705
     c   
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   706
    "
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   707
    "
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   708
     |c|
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   709
     c := #() asOrderedCollection.
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   710
     c addAllFirst:#().
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   711
     c   
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   712
    "
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   713
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   714
    "Modified: / 12.11.1997 / 17:58:05 / cg"
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   715
!
09ca874a6160 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24892
diff changeset
   716
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   717
addFirst:anObject
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   718
    "add the argument, anObject to the beginning of the collection.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   719
     Return the argument, anObject."
56
be0ed17e6f85 *** empty log message ***
claus
parents: 42
diff changeset
   720
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   721
    |idx "{ Class:SmallInteger }"|
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   722
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   723
    (idx := firstIndex) == 1 ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   724
	self makeRoomAtFront.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   725
	idx := firstIndex.
348
claus
parents: 345
diff changeset
   726
    ].
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   727
    firstIndex := idx := idx - 1.
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   728
    contentsArray basicAt:idx put:anObject.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   729
    ^ anObject
56
be0ed17e6f85 *** empty log message ***
claus
parents: 42
diff changeset
   730
be0ed17e6f85 *** empty log message ***
claus
parents: 42
diff changeset
   731
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   732
     |c|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   733
     c := #(1 2 3 4) asOrderedCollection.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   734
     c addFirst:'here'.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   735
     c
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   736
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   737
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   738
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   739
     |c|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   740
     c := #() asOrderedCollection.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   741
     c addFirst:'here'.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   742
     c
56
be0ed17e6f85 *** empty log message ***
claus
parents: 42
diff changeset
   743
    "
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   744
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   745
    "Modified: / 12.11.1997 / 17:58:05 / cg"
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   746
!
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   747
3368
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   748
clearContents
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   749
    "remove all elements from the collection but keep the contentsArray.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   750
     Useful for huge lists, if the contents will be rebuild soon (using #add:)
3368
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   751
     to a size which is similar to the lists current size.
16304
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
   752
     Destructive: modifies the receiver.
3368
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   753
     Returns the receiver."
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   754
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   755
    "/ clear those references, to give the garbage collector
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   756
    "/ a chance ...
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   757
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   758
    firstIndex >= lastIndex ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   759
	contentsArray from:firstIndex to:lastIndex put:nil.
3368
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   760
    ].
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   761
    firstIndex := 1.
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   762
    lastIndex := 0.
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   763
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   764
    "/ this is the same as:
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   765
    "/ self initContents:(self size)
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   766
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   767
    "Modified: 12.4.1996 / 13:34:19 / cg"
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   768
!
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   769
24042
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   770
dropLast:n
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   771
    "remove the last n elements from the receiver collection.
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   772
     Destructive: modifies the receiver.
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   773
     Return the receiver."
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   774
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   775
    |mySize ret|
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   776
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   777
    mySize := self size.
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   778
    mySize < n ifTrue:[
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   779
        "error if collection has not enough elements"
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   780
        ^ self notEnoughElementsError.
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   781
    ].
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   782
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   783
    "/
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   784
    "/ nil-out contents array, to not keep elements from being GC'd
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   785
    "/
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   786
    contentsArray from:lastIndex - n + 1 to:lastIndex put:nil.
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   787
    lastIndex := lastIndex - n.
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   788
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   789
    firstIndex > lastIndex ifTrue:[
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   790
        "reset to avoid ever growing"
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   791
        firstIndex := 1.
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   792
        lastIndex := 0
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   793
    ].
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   794
    ^ ret
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   795
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   796
    "
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   797
     (OrderedCollection withAll:#(1 2 3 4 5)) dropLast:2; yourself
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   798
     (OrderedCollection withAll:#(1 2 3 4 5)) dropLast:0; yourself
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   799
     (OrderedCollection withAll:#(1 2 3 4 5)) dropLast:6; yourself
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   800
    "
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   801
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   802
    "Created: / 03-04-2019 / 12:37:30 / Claus Gittinger"
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   803
!
2c4768bb2e89 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24003
diff changeset
   804
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   805
remove:anObject ifAbsent:exceptionBlock
2348
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
   806
    "remove the first element which is equal to anObject;
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   807
     if found, remove and return it;
2348
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
   808
     if not, return the value from evaluating exceptionBlock.
16304
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
   809
     Destructive: modifies the receiver.
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   810
     Uses equality compare (=) to search for the element."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   811
2354
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
   812
    |index retVal|
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
   813
12035
b927a2b293f8 changed:
Claus Gittinger <cg@exept.de>
parents: 11330
diff changeset
   814
    index := self indexOf:anObject.
2354
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
   815
    index ~~ 0 ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   816
	retVal := contentsArray at:index+firstIndex-1.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   817
	self removeFromIndex:index toIndex:index.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   818
	^ retVal
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   819
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   820
    ^ exceptionBlock value
348
claus
parents: 345
diff changeset
   821
claus
parents: 345
diff changeset
   822
    "
12035
b927a2b293f8 changed:
Claus Gittinger <cg@exept.de>
parents: 11330
diff changeset
   823
     #(1 2 3 4 5) asOrderedCollection remove:9 ifAbsent:[self halt]; yourself
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   824
     #(1 2 3 4 5) asOrderedCollection remove:3 ifAbsent:[self halt]; yourself
1496
bea70dc24829 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   825
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   826
     #(1 2 3 4 5 6 7 8 9) asOrderedCollection remove:3 ifAbsent:'oops'
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   827
     #(1 2 3 4 5) asOrderedCollection remove:9 ifAbsent:'oops'
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   828
     #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection remove:4 ifAbsent:'oops'
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   829
     #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection removeIdentical:4 ifAbsent:'oops'
348
claus
parents: 345
diff changeset
   830
    "
2348
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
   831
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   832
    "Modified: 8.2.1997 / 19:17:19 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   833
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   834
114
0353ec6749c8 added faster removeAll
claus
parents: 93
diff changeset
   835
removeAll
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   836
    "remove all elements from the collection.
16304
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
   837
     Returns the receiver.
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
   838
     Destructive: modifies the receiver."
114
0353ec6749c8 added faster removeAll
claus
parents: 93
diff changeset
   839
18879
5d1bbfc1761d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18811
diff changeset
   840
    self initContents:0
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   841
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   842
    "Modified: 12.4.1996 / 13:34:19 / cg"
114
0353ec6749c8 added faster removeAll
claus
parents: 93
diff changeset
   843
!
0353ec6749c8 added faster removeAll
claus
parents: 93
diff changeset
   844
348
claus
parents: 345
diff changeset
   845
removeAllSuchThat:aBlock
claus
parents: 345
diff changeset
   846
    "remove all elements that meet a test criteria as specified in aBlock.
claus
parents: 345
diff changeset
   847
     The argument, aBlock is evaluated for successive elements and all those,
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   848
     for which it returns true, are removed.
16304
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
   849
     Destructive: modifies the receiver.
1496
bea70dc24829 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   850
     Return a collection containing the removed elements.
bea70dc24829 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   851
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   852
     Performance:
22671
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   853
        this is an O(N) algorithm (the receiver's elements are scanned once)."
348
claus
parents: 345
diff changeset
   854
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   855
    "/ this is a q&d implementation (possibly slow).
348
claus
parents: 345
diff changeset
   856
22671
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   857
    |removedElements element
19831
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   858
     srcIndex "{ Class: SmallInteger }"
19865
6b114e3b40b9 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19836
diff changeset
   859
     dstIndex "{ Class: SmallInteger }"
6b114e3b40b9 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19836
diff changeset
   860
     lastIdx "{ Class: SmallInteger }"|
348
claus
parents: 345
diff changeset
   861
19831
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   862
    "/ first search forward to the first element which has
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   863
    "/ to be removed (meets the criteria)
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   864
    srcIndex := firstIndex.
19865
6b114e3b40b9 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19836
diff changeset
   865
    lastIdx := lastIndex.
6b114e3b40b9 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19836
diff changeset
   866
25351
30b332af1f33 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 24894
diff changeset
   867
    removedElements := OrderedCollection new.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   868
    [
22671
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   869
        srcIndex > lastIdx
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   870
        or:[ aBlock value:(contentsArray at:srcIndex) ]
19831
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   871
    ] whileFalse:[
22671
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   872
        srcIndex := srcIndex + 1.
19831
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   873
    ].
19865
6b114e3b40b9 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19836
diff changeset
   874
    srcIndex > lastIdx ifTrue:[
25351
30b332af1f33 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 24894
diff changeset
   875
        "/ nothing removed. Answer an empty OrderedCollection (not an Array)
30b332af1f33 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 24894
diff changeset
   876
        "/ maybe sender wants to add elements.
30b332af1f33 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 24894
diff changeset
   877
        ^ removedElements
19831
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   878
    ].
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   879
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   880
    "/ now srcIndex is the index of element, which is the first to be removed
22671
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   881
    removedElements add:(contentsArray at:srcIndex).
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   882
19831
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   883
    dstIndex := srcIndex.
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   884
    srcIndex := srcIndex + 1.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   885
19865
6b114e3b40b9 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19836
diff changeset
   886
    srcIndex to:lastIdx do:[:idx|
22671
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   887
        element := contentsArray at:idx.
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   888
        (aBlock value:element) ifTrue:[
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   889
            removedElements add:element
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   890
        ] ifFalse:[
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   891
            contentsArray at:dstIndex put:element.
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   892
            dstIndex := dstIndex + 1.
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   893
        ].
348
claus
parents: 345
diff changeset
   894
    ].
19865
6b114e3b40b9 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19836
diff changeset
   895
    contentsArray from:dstIndex to:lastIdx put:nil.
16304
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
   896
    lastIndex := dstIndex - 1.
22671
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   897
    ^ removedElements
348
claus
parents: 345
diff changeset
   898
claus
parents: 345
diff changeset
   899
    "
claus
parents: 345
diff changeset
   900
     |coll|
claus
parents: 345
diff changeset
   901
claus
parents: 345
diff changeset
   902
     coll := OrderedCollection withAll:(1 to:10).
19831
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   903
     Transcript show:'removed: '; showCR:(coll removeAllSuchThat:[:el | el even]).
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   904
     Transcript show:'coll: '; showCR:coll
348
claus
parents: 345
diff changeset
   905
    "
19831
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   906
16304
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
   907
    "
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
   908
     |coll1 coll2|
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
   909
19831
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   910
     Transcript showCR:'no element removed:'.
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   911
16304
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
   912
     coll1 := OrderedCollection withAll:(1 to:1000).
19831
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   913
     Transcript show:'  (1000) - '; showCR:(
22671
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   914
        Time millisecondsToRun:[
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   915
            100 timesRepeat:[ coll1 copy removeAllSuchThat:[:el | el == 500] ]
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   916
        ]).
19831
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   917
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   918
     coll2 := OrderedCollection withAll:(1 to:10000).
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   919
     Transcript show:'  (10000) - '; showCR:(
22671
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   920
        Time millisecondsToRun:[
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   921
            100 timesRepeat:[ coll2 copy removeAllSuchThat:[:el | el == 5000] ]
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   922
        ]).
19831
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   923
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   924
     coll2 := OrderedCollection withAll:(1 to:50000).
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   925
     Transcript show:'  (50000) - '; showCR:(
22671
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   926
        Time millisecondsToRun:[
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   927
            100 timesRepeat:[ coll2 copy removeAllSuchThat:[:el | el == 25000] ]
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   928
        ]).
19831
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   929
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   930
     Transcript showCR:'small number of elements removed:'.
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   931
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   932
     coll1 := OrderedCollection withAll:(1 to:1000).
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   933
     Transcript show:'  (1000) - '; showCR:(
22671
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   934
        Time millisecondsToRun:[
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   935
            100 timesRepeat:[ coll1 copy removeAllSuchThat:[:el | el between:500 and:550] ]
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   936
        ]).
19831
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   937
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   938
     coll2 := OrderedCollection withAll:(1 to:10000).
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   939
     Transcript show:'  (10000) - '; showCR:(
22671
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   940
        Time millisecondsToRun:[
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   941
            100 timesRepeat:[ coll2 copy removeAllSuchThat:[:el | el between:5000 and:5050] ]
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   942
        ]).
19831
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   943
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   944
     coll2 := OrderedCollection withAll:(1 to:50000).
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   945
     Transcript show:'  (50000) - '; showCR:(
22671
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   946
        Time millisecondsToRun:[
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   947
            100 timesRepeat:[ coll2 copy removeAllSuchThat:[:el | el between:25000 and:25050] ]
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   948
        ]).
19831
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   949
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   950
     Transcript showCR:'many elements removed:'.
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   951
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   952
     coll1 := OrderedCollection withAll:(1 to:1000).
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   953
     Transcript show:'  (1000) - '; showCR:(
22671
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   954
        Time millisecondsToRun:[
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   955
            100 timesRepeat:[ coll1 copy removeAllSuchThat:[:el | el even] ]
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   956
        ]).
16304
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
   957
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
   958
     coll2 := OrderedCollection withAll:(1 to:10000).
19831
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   959
     Transcript show:'  (10000) - '; showCR:(
22671
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   960
        Time millisecondsToRun:[
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   961
            100 timesRepeat:[ coll2 copy removeAllSuchThat:[:el | el even] ]
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   962
        ]).
16304
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
   963
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
   964
     coll2 := OrderedCollection withAll:(1 to:50000).
19831
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   965
     Transcript show:'  (50000) - '; showCR:(
22671
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   966
        Time millisecondsToRun:[
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   967
            100 timesRepeat:[ coll2 copy removeAllSuchThat:[:el | el even] ]
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   968
        ]).
16304
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
   969
    "
19831
5e0f261a1daa #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 19792
diff changeset
   970
    "
22671
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   971
                    compiled bytecode bytecode/new (no jitter)
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   972
        no element removed:
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   973
          (1000)  -  10          20         20
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   974
          (10000) -  70         240        200
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   975
          (50000) - 390        1190        940
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   976
        small number of elements removed:
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   977
          (1000)  -  10          30         20
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   978
          (10000) - 160         300        260
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   979
          (50000) - 700        1540       1180
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   980
        many elements removed:
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   981
          (1000)  -  10          20         30
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   982
          (10000) - 130         290        260
03d29bf8c5bb #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 22392
diff changeset
   983
          (50000) - 720        1470       1300
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   984
    "
25351
30b332af1f33 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 24894
diff changeset
   985
30b332af1f33 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 24894
diff changeset
   986
    "Modified: / 08-02-1997 / 19:19:00 / cg"
30b332af1f33 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 24894
diff changeset
   987
    "Modified: / 31-03-2020 / 11:36:20 / Stefan Vogel"
348
claus
parents: 345
diff changeset
   988
!
claus
parents: 345
diff changeset
   989
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   990
removeFirst
3695
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   991
    "remove the first element from the collection; return the element.
16304
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
   992
     If there is no element in the receiver collection, raise an error.
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
   993
     Destructive: modifies the receiver"
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 159
diff changeset
   994
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
   995
    |anObject
3968
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
   996
     fI "{ Class: SmallInteger }" |
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   997
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   998
    fI := firstIndex.
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   999
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
  1000
    fI > lastIndex ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1001
	"error if collection is empty"
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1002
	^ self emptyCollectionError.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1003
    ].
2354
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
  1004
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
  1005
    anObject := contentsArray basicAt:fI.
1496
bea70dc24829 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
  1006
bea70dc24829 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
  1007
    "/ nil it out, to allow GC to reclaim it.
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
  1008
    contentsArray basicAt:fI put:nil.
1496
bea70dc24829 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
  1009
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
  1010
    fI := fI + 1.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1011
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
  1012
    fI > lastIndex ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1013
	"reset to avoid ever growing"
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1014
	fI := 1.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1015
	lastIndex := 0
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1016
    ].
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
  1017
    firstIndex := fI.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1018
    ^ anObject
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1019
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1020
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1021
     (OrderedCollection withAll:#(1 2 3 4 5)) removeFirst; yourself
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1022
     OrderedCollection new removeFirst
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1023
     (SortedCollection withAll:#(5 4 3 2 1)) removeFirst; yourself
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
  1024
    "
2354
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
  1025
3968
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
  1026
    "Modified: / 5.2.1999 / 23:22:58 / cg"
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1027
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1028
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1029
removeFirst:n
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1030
    "remove the first n elements from the collection;
16304
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1031
     Return a collection containing the removed elements.
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1032
     Destructive: modifies the receiver"
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1033
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
  1034
    |mySize ret newFirstIndex|
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1035
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1036
    mySize := self size.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1037
    mySize < n ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1038
	"error if collection has not enough elements"
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1039
	^ self notEnoughElementsError.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1040
    ].
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1041
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1042
    ret := Array new:n.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1043
    ret replaceFrom:1 to:n with:contentsArray startingAt:firstIndex.
1496
bea70dc24829 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
  1044
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1045
    "/
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1046
    "/ nil-out contents array, to not keep elements from being GC'd
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1047
    "/
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
  1048
    newFirstIndex := firstIndex + n.
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
  1049
    contentsArray from:firstIndex to:newFirstIndex - 1 put:nil.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1050
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
  1051
    newFirstIndex > lastIndex ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1052
	"reset to avoid ever growing"
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1053
	newFirstIndex := 1.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1054
	lastIndex := 0
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1055
    ].
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
  1056
    firstIndex := newFirstIndex.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1057
    ^ ret
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
  1058
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
  1059
    "
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1060
     (OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:2; yourself
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1061
     (OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:0; yourself
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1062
     OrderedCollection new removeFirst:2
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1063
     (OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:6
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1064
     (SortedCollection withAll:#(5 4 3 2 1)) removeFirst:2; yourself
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1065
    "
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1066
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
  1067
    "Modified: 8.2.1997 / 19:20:18 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1068
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1069
3695
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1070
removeFirstIfAbsent:exceptionBlock
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1071
    "remove the first element from the collection; return the element.
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1072
     If there is no element in the receiver collection, return the value from
16304
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1073
     exceptionBlock.
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1074
     Destructive: modifies the receiver"
3695
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1075
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1076
    |anObject fI "{ Class: SmallInteger }" |
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1077
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1078
    fI := firstIndex.
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1079
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1080
    fI > lastIndex ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1081
	"error if collection is empty"
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1082
	^ exceptionBlock value.
3695
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1083
    ].
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1084
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1085
    anObject := contentsArray basicAt:fI.
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1086
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1087
    "/ nil it out, to allow GC to reclaim it.
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1088
    contentsArray basicAt:fI put:nil.
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1089
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1090
    fI := fI + 1.
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1091
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1092
    fI > lastIndex ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1093
	"reset to avoid ever growing"
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1094
	fI := 1.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1095
	lastIndex := 0
3695
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1096
    ].
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1097
    firstIndex := fI.
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1098
    ^ anObject
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1099
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1100
    "
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1101
     (OrderedCollection withAll:#(1 2 3 4 5)) removeFirst; yourself
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1102
     OrderedCollection new removeFirst
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1103
     (SortedCollection withAll:#(5 4 3 2 1)) removeFirst; yourself
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1104
    "
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1105
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1106
    "Modified: / 12.11.1997 / 17:58:43 / cg"
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1107
    "Created: / 30.7.1998 / 13:19:42 / cg"
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1108
!
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
  1109
2549
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
  1110
removeFrom:startIndex to:stopIndex
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
  1111
    "added for ST-80 compatibility.
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
  1112
     Same as removeFromIndex:toIndex:."
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
  1113
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
  1114
    ^ self removeFromIndex:startIndex toIndex:stopIndex
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
  1115
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
  1116
    "Created: 15.4.1997 / 12:39:00 / cg"
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
  1117
!
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
  1118
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1119
removeFromIndex:startIndex toIndex:stopIndex
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1120
    "remove the elements stored under startIndex up to and including
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1121
     the elements under stopIndex.
16304
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1122
     Destructive: modifies the receiver
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1123
     Return the receiver.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1124
     Returning the receiver here is a historic leftover - it may change.
14917
e61f35bf3521 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 14616
diff changeset
  1125
     Please use yourself in a cascade, if you need the receiver's value
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1126
     when using this method."
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1127
3968
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
  1128
    |nDeleted "{ Class: SmallInteger }"
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1129
     fI "{ Class: SmallInteger }"
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1130
     lI "{ Class: SmallInteger }"
2364
788423a8d67f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2362
diff changeset
  1131
     newLastIndex sz|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1132
2364
788423a8d67f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2362
diff changeset
  1133
    sz := self size.
788423a8d67f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2362
diff changeset
  1134
788423a8d67f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2362
diff changeset
  1135
    (startIndex < 1 or:[stopIndex > sz]) ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1136
	^ self notEnoughElementsError
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1137
    ].
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
  1138
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
  1139
    nDeleted := stopIndex - startIndex + 1.
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
  1140
    nDeleted < 0 ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1141
	"/ mhmh - what should be done here ?
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1142
	^ self error:'bad index range'
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
  1143
    ].
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
  1144
    nDeleted == 0 ifTrue:[^ self].
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
  1145
3968
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
  1146
    fI := firstIndex.
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
  1147
    lI := lastIndex.
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
  1148
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
  1149
    "/
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
  1150
    "/ can be done faster, when removing the first elements
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
  1151
    "/
2364
788423a8d67f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2362
diff changeset
  1152
    startIndex == 1 ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1153
	"/ nil out (helps GC)
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1154
	contentsArray
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1155
	    from:fI
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1156
	    to:fI + nDeleted - 1
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1157
	    put:nil.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1158
	firstIndex := fI := fI + nDeleted
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
  1159
    ] ifFalse:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1160
	"/
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1161
	"/ can be done faster, when removing the last elements
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1162
	"/
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1163
	stopIndex == sz ifTrue:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1164
	    "/ nil out (helps GC)
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1165
	    contentsArray
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1166
		from:lI - nDeleted + 1
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1167
		to:lI
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1168
		put:nil.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1169
	    lastIndex := lI := lI - nDeleted
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1170
	] ifFalse:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1171
	    "/
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1172
	    "/ must shuffle
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1173
	    "/ TODO:
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1174
	    "/    for big collections, try to copy the smallest
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1175
	    "/    possible number of elements
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1176
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1177
	    newLastIndex := lI - nDeleted.
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
  1178
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1179
	    contentsArray
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1180
		replaceFrom:(fI + startIndex - 1)
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1181
		to:newLastIndex
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1182
		with:contentsArray
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1183
		startingAt:(fI + stopIndex).
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
  1184
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1185
	    "/ nil out rest (helps GC)
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1186
	    contentsArray
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1187
		from:(newLastIndex + 1)
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1188
		to:lI
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1189
		put:nil.
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
  1190
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1191
	    lastIndex := lI := newLastIndex.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1192
	]
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
  1193
    ].
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
  1194
3968
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
  1195
    fI > lI ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1196
	"reset to avoid ever growing"
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1197
	firstIndex := 1.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1198
	lastIndex := 0
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1199
    ]
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1200
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1201
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1202
     #(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:3 toIndex:6
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
  1203
     #(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:6 toIndex:8
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
  1204
     #(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:1 toIndex:3
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
  1205
     #(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:6 toIndex:9
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1206
     #(1 2 3 4 5) asOrderedCollection removeFromIndex:3 toIndex:6
217
a0400fdbc933 *** empty log message ***
claus
parents: 202
diff changeset
  1207
    "
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
  1208
3968
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
  1209
    "Modified: / 5.2.1999 / 23:22:07 / cg"
217
a0400fdbc933 *** empty log message ***
claus
parents: 202
diff changeset
  1210
!
a0400fdbc933 *** empty log message ***
claus
parents: 202
diff changeset
  1211
2348
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
  1212
removeIdentical:anObject ifAbsent:exceptionBlock
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
  1213
    "remove the first element which is identical to anObject;
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1214
     if found, remove and return it;
2348
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
  1215
     if not, return the value from evaluating exceptionBlock.
16304
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1216
     Destructive: modifies the receiver.
2361
dd95b53674e9 oops - removeIdentical was wrong
Claus Gittinger <cg@exept.de>
parents: 2355
diff changeset
  1217
     Uses identity compare (==) to search for the element."
2354
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
  1218
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
  1219
    |index|
2348
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
  1220
2354
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
  1221
    index := contentsArray identityIndexOf:anObject startingAt:firstIndex endingAt:lastIndex.
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
  1222
    index ~~ 0 ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1223
	index == firstIndex ifTrue:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1224
	    contentsArray basicAt:firstIndex put:nil.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1225
	    firstIndex := firstIndex + 1.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1226
	    firstIndex > lastIndex ifTrue:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1227
		"reset to avoid ever growing"
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1228
		firstIndex := 1.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1229
		lastIndex := 0
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1230
	    ].
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1231
	] ifFalse:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1232
	    index == lastIndex ifTrue:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1233
		contentsArray basicAt:lastIndex put:nil.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1234
		lastIndex := lastIndex - 1.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1235
		firstIndex > lastIndex ifTrue:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1236
		    "reset to avoid ever growing"
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1237
		    firstIndex := 1.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1238
		    lastIndex := 0
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1239
		].
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1240
	    ] ifFalse:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1241
		index := index - firstIndex + 1.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1242
		self removeFromIndex:index toIndex:index.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1243
	    ]
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1244
	].
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1245
	^ anObject
2348
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
  1246
    ].
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
  1247
    ^ exceptionBlock value
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
  1248
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
  1249
    "
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1250
     #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection remove:4 ifAbsent:'oops'
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1251
     #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection remove:4 ifAbsent:'oops'; yourself
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1252
     #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection removeIdentical:4 ifAbsent:'oops'
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1253
     #(fee foo bar baz) asOrderedCollection removeIdentical:#fee; yourself
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1254
     #(fee foo bar baz) asOrderedCollection removeIdentical:#foo; yourself
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1255
     #(fee foo bar baz) asOrderedCollection removeIdentical:#baz; yourself
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1256
     #(fee) asOrderedCollection removeIdentical:#fee; yourself
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1257
     #(fee) asOrderedCollection removeIdentical:#foo; yourself
2348
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
  1258
    "
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
  1259
2361
dd95b53674e9 oops - removeIdentical was wrong
Claus Gittinger <cg@exept.de>
parents: 2355
diff changeset
  1260
    "Modified: 8.2.1997 / 18:57:43 / cg"
2348
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
  1261
!
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
  1262
16304
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1263
removeIndices:aSortedCollectionOfIndices
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1264
    "remove all elements stored in any of aSortedCollectionOfIndices,
16304
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1265
     which must be sorted and sequenceable.
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1266
     Destructive: modifies the receiver.
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1267
     Returns a collection of removed elements.
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1268
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1269
     Performance:
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1270
	this is an O(N) algorithm (N being the size of the receiver).
18184
b72a856332b8 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18183
diff changeset
  1271
b72a856332b8 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18183
diff changeset
  1272
     This could be done much better, especially if the removed indices are
b72a856332b8 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18183
diff changeset
  1273
     at either end of the receiver. However, as it is currently not heavily used,
b72a856332b8 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18183
diff changeset
  1274
     I leave that as an exercise to the brave reader..."
16304
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1275
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1276
    |removed element dstIndex indexIndex nextIndex numIndices|
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1277
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1278
    removed := OrderedCollection new.
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1279
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1280
    dstIndex := firstIndex.
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1281
    numIndices := aSortedCollectionOfIndices size.
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1282
    indexIndex := 1.
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1283
    nextIndex := aSortedCollectionOfIndices at:1.
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1284
    firstIndex to: lastIndex do:[:srcIndex |
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1285
	element := contentsArray at:srcIndex.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1286
	srcIndex == nextIndex ifTrue:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1287
	    removed add:element.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1288
	    indexIndex := indexIndex + 1.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1289
	    indexIndex > numIndices ifTrue:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1290
		nextIndex := nil.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1291
	    ] ifFalse:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1292
		nextIndex := aSortedCollectionOfIndices at:indexIndex.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1293
	    ].
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1294
	] ifFalse:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1295
	    contentsArray at:dstIndex put:element.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1296
	    dstIndex := dstIndex + 1.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1297
	].
16304
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1298
    ].
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1299
    contentsArray from:dstIndex to:lastIndex put:nil.
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1300
    lastIndex := dstIndex - 1.
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1301
    ^ removed
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1302
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1303
    "
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1304
     |coll|
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1305
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1306
     coll := OrderedCollection withAll:(1 to:10).
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1307
     Transcript showCR:(coll removeIndices:#(1 5 7)).
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1308
     Transcript showCR:coll
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1309
    "
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1310
!
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1311
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1312
removeLast
16304
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1313
    "remove the last element from the collection.
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1314
     Return the removed element.
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1315
     Destructive: modifies the receiver"
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1316
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
  1317
    |anObject
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
  1318
     idx "{ Class: SmallInteger }" |
217
a0400fdbc933 *** empty log message ***
claus
parents: 202
diff changeset
  1319
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
  1320
    idx := lastIndex.
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
  1321
    firstIndex > idx ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1322
	"error if collection is empty"
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1323
	^ self emptyCollectionError.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1324
    ].
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
  1325
    anObject := contentsArray basicAt:idx.
1496
bea70dc24829 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
  1326
bea70dc24829 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
  1327
    "/ nil it (helps GC)
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
  1328
    contentsArray basicAt:idx put:nil.
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
  1329
    lastIndex := idx := idx - 1.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1330
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
  1331
    firstIndex > idx ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1332
	"reset to avoid ever growing"
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1333
	firstIndex := 1.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1334
	lastIndex := 0
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1335
    ].
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1336
    ^ anObject
217
a0400fdbc933 *** empty log message ***
claus
parents: 202
diff changeset
  1337
a0400fdbc933 *** empty log message ***
claus
parents: 202
diff changeset
  1338
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1339
     (OrderedCollection withAll:#(1 2 3 4 5)) removeLast; yourself
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1340
     OrderedCollection new removeLast
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1341
     (SortedCollection withAll:#(5 4 3 2 1)) removeLast; yourself
217
a0400fdbc933 *** empty log message ***
claus
parents: 202
diff changeset
  1342
    "
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
  1343
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
  1344
    "Modified: / 12.11.1997 / 17:58:57 / cg"
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1345
!
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1346
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1347
removeLast:n
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1348
    "remove the last n elements from the receiver collection.
16304
2d8de035fd7a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15757
diff changeset
  1349
     Destructive: modifies the receiver.
11330
4a1580833cb5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11275
diff changeset
  1350
     Return a collection of removed elements."
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1351
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1352
    |mySize ret|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1353
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1354
    mySize := self size.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1355
    mySize < n ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1356
	"error if collection has not enough elements"
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1357
	^ self notEnoughElementsError.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1358
    ].
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1359
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1360
    ret := Array new:n.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1361
    ret replaceFrom:1 to:n with:contentsArray startingAt:lastIndex - n + 1.
1496
bea70dc24829 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
  1362
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1363
    "/
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1364
    "/ nil-out contents array, to not keep elements from being GC'd
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1365
    "/
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1366
    contentsArray from:lastIndex - n + 1 to:lastIndex put:nil.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1367
    lastIndex := lastIndex - n.
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1368
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1369
    firstIndex > lastIndex ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1370
	"reset to avoid ever growing"
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1371
	firstIndex := 1.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1372
	lastIndex := 0
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1373
    ].
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1374
    ^ ret
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1375
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1376
    "
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1377
     (OrderedCollection withAll:#(1 2 3 4 5)) removeLast:2; yourself
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1378
     (OrderedCollection withAll:#(1 2 3 4 5)) removeLast:0; yourself
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1379
     (OrderedCollection withAll:#(1 2 3 4 5)) removeLast:6; yourself
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1380
     (SortedCollection withAll:#(5 4 3 2 1)) removeLast:2; yourself
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1381
     (SortedCollection withAll:#(5 4 3 2 1)) removeLast:0; yourself
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1382
     (SortedCollection withAll:#(5 4 3 2 1)) removeLast:0
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1383
    "
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1384
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1385
    "Modified: 12.4.1996 / 13:39:12 / cg"
4862
5df99a53c364 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4427
diff changeset
  1386
!
5df99a53c364 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4427
diff changeset
  1387
24128
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1388
removeLastIfAbsent:exceptionValue
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1389
    "remove the last element from the collection.
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1390
     Return the removed element or the value from exceptionValue if empty.
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1391
     Destructive: modifies the receiver"
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1392
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1393
    |anObject
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1394
     idx "{ Class: SmallInteger }" |
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1395
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1396
    idx := lastIndex.
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1397
    firstIndex > idx ifTrue:[
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1398
        "collection is empty"
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1399
        ^ exceptionValue value.
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1400
    ].
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1401
    anObject := contentsArray basicAt:idx.
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1402
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1403
    "/ nil it (helps GC)
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1404
    contentsArray basicAt:idx put:nil.
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1405
    lastIndex := idx := idx - 1.
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1406
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1407
    firstIndex > idx ifTrue:[
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1408
        "reset to avoid ever growing"
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1409
        firstIndex := 1.
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1410
        lastIndex := 0
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1411
    ].
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1412
    ^ anObject
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1413
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1414
    "
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1415
     OrderedCollection new removeLastIfAbsent:nil
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1416
    "
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1417
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1418
    "Created: / 21-05-2019 / 08:47:55 / Claus Gittinger"
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1419
!
4029e964d0f1 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 24042
diff changeset
  1420
4862
5df99a53c364 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4427
diff changeset
  1421
reset
5df99a53c364 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4427
diff changeset
  1422
    "logically remove all elements from the collection.
16305
964f7ffb315d class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 16304
diff changeset
  1423
     That's almost the same as #removeAll, but keeps the contentsArray.
4862
5df99a53c364 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4427
diff changeset
  1424
     Returns the receiver."
5df99a53c364 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4427
diff changeset
  1425
5df99a53c364 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4427
diff changeset
  1426
    firstIndex := contentsArray size // 3 max: 1.
5df99a53c364 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4427
diff changeset
  1427
    lastIndex := firstIndex - 1
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1428
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1429
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1430
!OrderedCollection methodsFor:'converting'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1431
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1432
asArray
1170
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1433
    "return the receiver as an array."
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1434
4153
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1435
    ^ contentsArray copyFrom:firstIndex to:lastIndex
1170
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1436
4153
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1437
"/    |newArray sz|
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1438
"/
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1439
"/    sz := self size.
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1440
"/    newArray := Array new:sz.
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1441
"/    newArray replaceFrom:1 to:sz with:contentsArray startingAt:firstIndex.
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1442
"/    ^ newArray
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1443
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1444
    "
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1445
     OrderedCollection new asArray
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1446
     OrderedCollection new add:1;add:2;asArray
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1447
     OrderedCollection new add:1;removeFirst;asArray
4153
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1448
    "
1170
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1449
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1450
    "
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1451
     |o rnd|
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1452
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1453
     o := OrderedCollection new.
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1454
     rnd := Random new.
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1455
     10000 timesRepeat:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1456
	 o add:rnd next.
1170
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1457
     ].
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1458
     Time millisecondsToRun:[o asArray]
1170
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1459
    "
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1460
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1461
    "Modified: 13.4.1996 / 12:10:56 / cg"
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1462
!
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1463
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1464
asNewOrderedCollection
14999
e6ad9751587b class: OrderedCollection
Stefan Vogel <sv@exept.de>
parents: 14919
diff changeset
  1465
    "return the receiver as an ordered collection.
e6ad9751587b class: OrderedCollection
Stefan Vogel <sv@exept.de>
parents: 14919
diff changeset
  1466
     Make sure to return a unique new OrderedCollection"
e6ad9751587b class: OrderedCollection
Stefan Vogel <sv@exept.de>
parents: 14919
diff changeset
  1467
e6ad9751587b class: OrderedCollection
Stefan Vogel <sv@exept.de>
parents: 14919
diff changeset
  1468
    "could be an instance of a subclass..."
e6ad9751587b class: OrderedCollection
Stefan Vogel <sv@exept.de>
parents: 14919
diff changeset
  1469
    self class == OrderedCollection ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1470
	^ self copy
14999
e6ad9751587b class: OrderedCollection
Stefan Vogel <sv@exept.de>
parents: 14919
diff changeset
  1471
    ].
e6ad9751587b class: OrderedCollection
Stefan Vogel <sv@exept.de>
parents: 14919
diff changeset
  1472
    ^ super asOrderedCollection
e6ad9751587b class: OrderedCollection
Stefan Vogel <sv@exept.de>
parents: 14919
diff changeset
  1473
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1474
    "
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1475
	|s|
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1476
	s := #(1 2 3 4) asOrderedCollection.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1477
	self assert:(s ~~ s asNewOrderedCollection).
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1478
	self assert:(s = s asNewOrderedCollection).
14999
e6ad9751587b class: OrderedCollection
Stefan Vogel <sv@exept.de>
parents: 14919
diff changeset
  1479
     "
e6ad9751587b class: OrderedCollection
Stefan Vogel <sv@exept.de>
parents: 14919
diff changeset
  1480
!
e6ad9751587b class: OrderedCollection
Stefan Vogel <sv@exept.de>
parents: 14919
diff changeset
  1481
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1482
asOrderedCollection
15062
755e0e15b7fa class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 14999
diff changeset
  1483
    "return the receiver as an ordered collection.
755e0e15b7fa class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 14999
diff changeset
  1484
     Notice: this returns the receiver. Use asNewOrderedCollection, if you intent to
755e0e15b7fa class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 14999
diff changeset
  1485
     modify the returned collection."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1486
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1487
    "could be an instance of a subclass..."
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1488
    self class == OrderedCollection ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1489
	^ self
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1490
    ].
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1491
    ^ super asOrderedCollection
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1492
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1493
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1494
!OrderedCollection methodsFor:'copying'!
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1495
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1496
, aCollection
14071
8e39d6128e1e comment/format in: #,
Claus Gittinger <cg@exept.de>
parents: 12036
diff changeset
  1497
    "return a new collection formed from concatenating the receiver with the argument"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1498
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1499
    |newCollection|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1500
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1501
    newCollection := self copyEmpty:(self size + aCollection size).
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1502
    ^ newCollection
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1503
	addAll:self;
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1504
	addAll:aCollection;
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1505
	yourself.
42
e33491f6f260 *** empty log message ***
claus
parents: 40
diff changeset
  1506
253
30daee717a53 *** empty log message ***
claus
parents: 229
diff changeset
  1507
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1508
     #(1 2 3) asOrderedCollection , #(4 5 6) asOrderedCollection
20026
c4063e226888 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19991
diff changeset
  1509
     #(1 3 5) asSortedCollection , #(6 4 2) asSortedCollection
c4063e226888 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19991
diff changeset
  1510
     #(1 3 5) asSortedCollection , #(6 4 2) asOrderedCollection
c4063e226888 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19991
diff changeset
  1511
     #(1 3 5) asSortedCollection , (#(6 4 2) asSortedCollection:[:a :b| a > b])
253
30daee717a53 *** empty log message ***
claus
parents: 229
diff changeset
  1512
    "
14071
8e39d6128e1e comment/format in: #,
Claus Gittinger <cg@exept.de>
parents: 12036
diff changeset
  1513
8e39d6128e1e comment/format in: #,
Claus Gittinger <cg@exept.de>
parents: 12036
diff changeset
  1514
    "Modified (comment): / 01-04-2012 / 13:17:30 / cg"
42
e33491f6f260 *** empty log message ***
claus
parents: 40
diff changeset
  1515
!
e33491f6f260 *** empty log message ***
claus
parents: 40
diff changeset
  1516
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1517
copy
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1518
    "return a new OrderedCollection containing the elements of the receiver."
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1519
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1520
    "redefinition is a consequence of the implementation with a
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1521
     separate array - otherwise we get a shallow copy of the
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1522
     contents array, which is not what we want here"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1523
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1524
    ^ self copyFrom:1 to:self size
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1525
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1526
22319
abb4316c6bff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22295
diff changeset
  1527
copyWithoutDuplicates
abb4316c6bff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22295
diff changeset
  1528
    "return a new orderedCollection containing my entries,
abb4316c6bff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22295
diff changeset
  1529
     but not any duplicates"
abb4316c6bff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22295
diff changeset
  1530
abb4316c6bff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22295
diff changeset
  1531
    |newColl already|
abb4316c6bff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22295
diff changeset
  1532
abb4316c6bff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22295
diff changeset
  1533
    already := Set new.
abb4316c6bff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22295
diff changeset
  1534
    
abb4316c6bff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22295
diff changeset
  1535
    newColl := self speciesForCollecting new.
abb4316c6bff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22295
diff changeset
  1536
    self do:[:eachElement |
abb4316c6bff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22295
diff changeset
  1537
        (already includes:eachElement) ifFalse:[
abb4316c6bff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22295
diff changeset
  1538
            already add:eachElement.
abb4316c6bff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22295
diff changeset
  1539
            newColl add:eachElement.
abb4316c6bff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22295
diff changeset
  1540
        ]
abb4316c6bff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22295
diff changeset
  1541
    ].
abb4316c6bff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22295
diff changeset
  1542
    ^ newColl.
abb4316c6bff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22295
diff changeset
  1543
abb4316c6bff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22295
diff changeset
  1544
    "
abb4316c6bff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22295
diff changeset
  1545
     #(7 1 2 3 2 3 4 5 2 3 6 7 3 5 1) asOrderedCollection copyWithoutDuplicates
abb4316c6bff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22295
diff changeset
  1546
    "
abb4316c6bff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22295
diff changeset
  1547
abb4316c6bff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22295
diff changeset
  1548
    "Created: / 22-10-2017 / 01:39:25 / cg"
24178
ddcab3a9c2df #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 24128
diff changeset
  1549
! !
ddcab3a9c2df #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 24128
diff changeset
  1550
ddcab3a9c2df #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 24128
diff changeset
  1551
!OrderedCollection methodsFor:'copying-private'!
22319
abb4316c6bff #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22295
diff changeset
  1552
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1553
postCopy
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1554
    "have to copy the contentsArray too"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1555
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1556
    contentsArray := contentsArray shallowCopy
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1557
! !
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1558
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1559
!OrderedCollection methodsFor:'enumerating'!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1560
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1561
collect:aBlock
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1562
    "evaluate the argument, aBlock for every element in the collection
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1563
     and return a collection of the results"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1564
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1565
    |newCollection
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1566
     start  "{ Class:SmallInteger }"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1567
     stop   "{ Class:SmallInteger }" |
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1568
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1569
    newCollection := self copyEmpty:(self size).
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1570
    stop := lastIndex.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1571
    start := firstIndex.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1572
    start to:stop do:[:index |
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1573
	newCollection add:(aBlock value:(contentsArray at:index)).
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1574
    ].
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1575
    ^ newCollection
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1576
253
30daee717a53 *** empty log message ***
claus
parents: 229
diff changeset
  1577
    "
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1578
     #(1 2 3 4) asOrderedCollection collect:[:i | i * i]
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1579
     #(1 2 3 4) asOrderedCollection collect:[:i | i even]
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1580
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1581
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1582
5508
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1583
collect:collectBlock thenSelect:selectBlock
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1584
    "combination of collect followed by select;
7164
5cdefa506c0e comment
Claus Gittinger <cg@exept.de>
parents: 6756
diff changeset
  1585
     redefined to avoid the creation of an intermediate (garbage) collection."
5508
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1586
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1587
    |newCollection newElement
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1588
     start  "{ Class:SmallInteger }"
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1589
     stop   "{ Class:SmallInteger }" |
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1590
7166
Claus Gittinger <cg@exept.de>
parents: 7165
diff changeset
  1591
    newCollection := self copyEmpty.
5508
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1592
    stop := lastIndex.
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1593
    start := firstIndex.
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1594
    start to:stop do:[:index |
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1595
	newElement := collectBlock value:(contentsArray at:index).
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1596
	(selectBlock value:newElement) ifTrue:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1597
	    newCollection add:newElement.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1598
	]
5508
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1599
    ].
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1600
    ^ newCollection
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1601
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1602
    "
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1603
     #(1 2 3 4) asOrderedCollection collect:[:i | i * i] thenSelect:[:each | each > 5]
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1604
     ( #(1 2 3 4) asOrderedCollection collect:[:i | i * i]) select:[:each | each > 5]
5508
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1605
7165
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1606
     |coll|
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1607
     coll := #(1 2 3 4) asOrderedCollection.
5508
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1608
     Time millisecondsToRun:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1609
	100000 timesRepeat:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1610
	    coll collect:[:i | i * i] thenSelect:[:each | each > 5]
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1611
	]
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1612
     ]
5508
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1613
7165
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1614
     |coll|
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1615
     coll := #(1 2 3 4) asOrderedCollection.
5508
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1616
     Time millisecondsToRun:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1617
	100000 timesRepeat:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1618
	    ( coll collect:[:i | i * i]) select:[:each | each > 5]
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1619
	]
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1620
     ]
5508
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1621
    "
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1622
!
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1623
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1624
do:aBlock
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1625
    "evaluate the argument, aBlock for every element in the collection."
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1626
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1627
    contentsArray from:firstIndex to:lastIndex do:aBlock
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1628
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1629
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1630
keysAndValuesDo:aTwoArgBlock
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1631
    "evaluate the argument, aBlock for every element in the collection,
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1632
     passing both index and element as arguments."
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1633
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1634
    |start  "{ Class:SmallInteger }"
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1635
     stop   "{ Class:SmallInteger }"
1090
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1636
     idx    "{ Class:SmallInteger }" |
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1637
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1638
    stop := lastIndex.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1639
    start := firstIndex.
1090
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1640
    idx := 1.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1641
    start to:stop do:[:index |
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1642
	aTwoArgBlock value:idx value:(contentsArray at:index).
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1643
	idx := idx + 1.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1644
    ]
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1645
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1646
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1647
     #(10 20 30 40) asOrderedCollection keysAndValuesDo:[:index :value |
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1648
	Transcript show:index; show:' '; showCR:value
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1649
     ]
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1650
    "
1090
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1651
    "
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1652
     |oc|
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1653
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1654
     oc := #(10 20 30 40 50 60 70 80) asOrderedCollection.
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1655
     oc removeFirst; removeFirst.
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1656
     oc keysAndValuesDo:[:index :value |
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1657
	Transcript show:index; show:' '; showCR:value
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1658
     ]
1090
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1659
    "
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1660
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1661
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1662
679
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1663
keysAndValuesReverseDo:aTwoArgBlock
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1664
    "evaluate the argument, aBlock for every element in the collection,
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1665
     passing both index and element as arguments."
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1666
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1667
    |start  "{ Class:SmallInteger }"
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1668
     stop   "{ Class:SmallInteger }"
1090
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1669
     idx    "{ Class:SmallInteger }"|
679
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1670
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1671
    stop := lastIndex.
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1672
    start := firstIndex.
1090
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1673
    idx := (stop - start + 1).
679
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1674
    stop to:start by: -1 do:[:index |
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1675
	aTwoArgBlock value:idx value:(contentsArray at:index).
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1676
	idx := idx - 1.
679
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1677
    ]
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1678
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1679
    "
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1680
     #(10 20 30 40) asOrderedCollection keysAndValuesReverseDo:[:index :value |
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1681
	Transcript show:index; show:' '; showCR:value
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1682
     ]
1090
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1683
    "
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1684
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1685
    "
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1686
     |oc|
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1687
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1688
     oc := #(10 20 30 40 50 60 70 80) asOrderedCollection.
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1689
     oc removeFirst; removeFirst.
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1690
     oc keysAndValuesReverseDo:[:index :value |
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1691
	Transcript show:index; show:' '; showCR:value
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1692
     ]
679
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1693
    "
1071
5875a6eedd28 added #includesIdentical:
Claus Gittinger <cg@exept.de>
parents: 679
diff changeset
  1694
!
5875a6eedd28 added #includesIdentical:
Claus Gittinger <cg@exept.de>
parents: 679
diff changeset
  1695
5875a6eedd28 added #includesIdentical:
Claus Gittinger <cg@exept.de>
parents: 679
diff changeset
  1696
reverseDo:aBlock
5875a6eedd28 added #includesIdentical:
Claus Gittinger <cg@exept.de>
parents: 679
diff changeset
  1697
    "evaluate the argument, aBlock for every element in the collection
5875a6eedd28 added #includesIdentical:
Claus Gittinger <cg@exept.de>
parents: 679
diff changeset
  1698
     procesing elements in reverse direction (i.e. starting with the last)"
5875a6eedd28 added #includesIdentical:
Claus Gittinger <cg@exept.de>
parents: 679
diff changeset
  1699
5875a6eedd28 added #includesIdentical:
Claus Gittinger <cg@exept.de>
parents: 679
diff changeset
  1700
    contentsArray from:firstIndex to:lastIndex reverseDo:aBlock
7165
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1701
!
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1702
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1703
select:selectBlock thenCollect:collectBlock
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1704
    "combination of select followed by collect;
7165
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1705
     redefined to avoid the creation of an intermediate (garbage) collection."
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1706
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1707
    |newCollection element
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1708
     start  "{ Class:SmallInteger }"
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1709
     stop   "{ Class:SmallInteger }" |
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1710
7166
Claus Gittinger <cg@exept.de>
parents: 7165
diff changeset
  1711
    newCollection := self copyEmpty.
7165
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1712
    stop := lastIndex.
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1713
    start := firstIndex.
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1714
    start to:stop do:[:index |
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1715
	element := contentsArray at:index.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1716
	(selectBlock value:element) ifTrue:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1717
	    newCollection add:(collectBlock value:element).
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1718
	]
7165
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1719
    ].
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1720
    ^ newCollection
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1721
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1722
    "
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1723
     #(1 2 3 4 5 6 7 8) asOrderedCollection
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1724
	select:[:each | each > 5] thenCollect:[:i | i * i]
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1725
     ( #(1 2 3 4 5 6 7 8) asOrderedCollection
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1726
	select:[:each | each > 5]) collect:[:i | i * i]
7165
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1727
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1728
     |coll|
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1729
     coll := #(1 2 3 4 5 6 7 8) asOrderedCollection.
7165
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1730
     Time millisecondsToRun:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1731
	100000 timesRepeat:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1732
	    coll select:[:each | each > 5] thenCollect:[:i | i * i]
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1733
	]
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1734
     ]
7165
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1735
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1736
     |coll|
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1737
     coll := #(1 2 3 4 5 6 7 8) asOrderedCollection.
7165
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1738
     Time millisecondsToRun:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1739
	100000 timesRepeat:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1740
	    ( coll select:[:each | each > 5]) collect:[:i | i * i]
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1741
	]
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1742
     ]
7165
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1743
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1744
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1745
56
be0ed17e6f85 *** empty log message ***
claus
parents: 42
diff changeset
  1746
!OrderedCollection methodsFor:'filling & replacing'!
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1747
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1748
replaceFrom:start to:stop with:aCollection startingAt:repStart
1170
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1749
    "replace elements in the receiver between index start and stop,
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1750
     with elements  taken from replacementCollection starting at repStart.
3236
b5f80b416cff check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 3109
diff changeset
  1751
     Return the receiver.
b5f80b416cff check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 3109
diff changeset
  1752
     Redefined here; can be done faster as the inherited operation."
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1753
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1754
    |end|
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1755
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1756
    end := stop + firstIndex - 1.
24892
2213eb56e0c7 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24178
diff changeset
  1757
    ((start > 0) and:[end <= lastIndex]) ifTrue:[
2213eb56e0c7 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24178
diff changeset
  1758
        aCollection class == self class ifTrue:[
2213eb56e0c7 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24178
diff changeset
  1759
            contentsArray
2213eb56e0c7 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24178
diff changeset
  1760
                replaceFrom:(start + firstIndex - 1)
2213eb56e0c7 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24178
diff changeset
  1761
                to:end
2213eb56e0c7 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24178
diff changeset
  1762
                with:aCollection contentsArray
2213eb56e0c7 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24178
diff changeset
  1763
                startingAt:(repStart + aCollection firstIndex - 1).
2213eb56e0c7 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24178
diff changeset
  1764
        ] ifFalse:[
2213eb56e0c7 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24178
diff changeset
  1765
            contentsArray
2213eb56e0c7 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24178
diff changeset
  1766
                replaceFrom:(start + firstIndex - 1)
2213eb56e0c7 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24178
diff changeset
  1767
                to:end
2213eb56e0c7 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24178
diff changeset
  1768
                with:aCollection
2213eb56e0c7 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24178
diff changeset
  1769
                startingAt:repStart.
2213eb56e0c7 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24178
diff changeset
  1770
        ].
2213eb56e0c7 #REFACTORING by exept
Claus Gittinger <cg@exept.de>
parents: 24178
diff changeset
  1771
        ^ self
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1772
    ].
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1773
    ^ super replaceFrom:start to:stop with:aCollection startingAt:repStart
293
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1774
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1775
    "
15757
f7e84eeeaaa9 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15749
diff changeset
  1776
     |c1 c2|
f7e84eeeaaa9 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15749
diff changeset
  1777
f7e84eeeaaa9 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15749
diff changeset
  1778
     c1 := #(1 2 3 4 5 6) asOrderedCollection.
f7e84eeeaaa9 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15749
diff changeset
  1779
     c2 := #(a b c d e f) asOrderedCollection.
f7e84eeeaaa9 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15749
diff changeset
  1780
     c2 replaceFrom:3 to:6 with:c1.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1781
     c2
15757
f7e84eeeaaa9 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15749
diff changeset
  1782
    "
f7e84eeeaaa9 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15749
diff changeset
  1783
    "
f7e84eeeaaa9 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15749
diff changeset
  1784
     |c1 c2|
f7e84eeeaaa9 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15749
diff changeset
  1785
f7e84eeeaaa9 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15749
diff changeset
  1786
     c1 := #(1 2 3 4 5 6) asOrderedCollection.
f7e84eeeaaa9 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15749
diff changeset
  1787
     c2 := #(a b c d e f) asOrderedCollection.
f7e84eeeaaa9 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15749
diff changeset
  1788
     c2 replaceFrom:3 to:6 with:c1 startingAt:2.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1789
     c2
15757
f7e84eeeaaa9 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15749
diff changeset
  1790
    "
f7e84eeeaaa9 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15749
diff changeset
  1791
    "
293
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1792
     |c|
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1793
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1794
     c := #(1 2 3 4 5 6) asOrderedCollection.
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1795
     c replaceFrom:3 to:6 with:c startingAt:2.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1796
     c
293
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1797
    "
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1798
    "
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1799
     |c|
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1800
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1801
     c := #(1 2 3 4 5 6) asOrderedCollection.
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1802
     c replaceFrom:3 to:5 with:c startingAt:4.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1803
     c
293
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1804
    "
1170
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1805
3236
b5f80b416cff check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 3109
diff changeset
  1806
    "Modified: / 28.1.1998 / 16:49:31 / cg"
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1807
! !
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1808
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1809
!OrderedCollection methodsFor:'grow & shrink'!
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1810
18379
e36a3b641b80 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18184
diff changeset
  1811
ensureSizeAtLeast:minSize
e36a3b641b80 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18184
diff changeset
  1812
    "ensure that the size is at least minSize.
e36a3b641b80 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18184
diff changeset
  1813
     If the receiver's size is smaller, grow the receiver to minSize,
e36a3b641b80 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18184
diff changeset
  1814
     filling new slots with nil.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1815
     Otherwise, if the size is already >= minSize, leave the receiver unchanged."
18379
e36a3b641b80 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18184
diff changeset
  1816
e36a3b641b80 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18184
diff changeset
  1817
    (self size < minSize) ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1818
	self grow:minSize
18379
e36a3b641b80 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18184
diff changeset
  1819
    ].
e36a3b641b80 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18184
diff changeset
  1820
e36a3b641b80 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18184
diff changeset
  1821
    "
e36a3b641b80 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18184
diff changeset
  1822
     |oc|
e36a3b641b80 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18184
diff changeset
  1823
e36a3b641b80 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18184
diff changeset
  1824
     oc := OrderedCollection new.
e36a3b641b80 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18184
diff changeset
  1825
     oc ensureSizeAtLeast:10.
e36a3b641b80 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18184
diff changeset
  1826
     oc at:10 put:10.
e36a3b641b80 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18184
diff changeset
  1827
     oc add:11.
e36a3b641b80 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18184
diff changeset
  1828
     oc at:11.
e36a3b641b80 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18184
diff changeset
  1829
     oc ensureSizeAtLeast:20.
e36a3b641b80 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18184
diff changeset
  1830
     oc at:20 put:20.
e36a3b641b80 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18184
diff changeset
  1831
     oc.
e36a3b641b80 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18184
diff changeset
  1832
    "
e36a3b641b80 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18184
diff changeset
  1833
!
e36a3b641b80 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18184
diff changeset
  1834
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1835
grow:newSize
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1836
    "grow the receiver to newSize.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1837
     This only logically changes the receiver's size;
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1838
     the underlying contentsArray is kept
19836
f6f451ada167 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 19831
diff changeset
  1839
     (except if growing to a zero size, or too small for newSize)"
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1840
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1841
    |oldSize newContents oldLast newLast|
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1842
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1843
    oldSize := lastIndex - firstIndex + 1.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1844
    newSize ~~ oldSize ifTrue:[
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1845
        newLast := firstIndex + newSize - 1.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1846
        newSize < oldSize ifTrue:[
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1847
            newSize == 0 ifTrue:[
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1848
                self initContents:0.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1849
                ^ self.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1850
            ].
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1851
            oldLast := lastIndex.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1852
            lastIndex := newLast.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1853
            "
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1854
             nil out rest, to give GC a chance to reclaim things
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1855
            "
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1856
            contentsArray from:lastIndex + 1 to:oldLast put:nil.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1857
        ] ifFalse:[
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1858
            newLast <= contentsArray size ifTrue:[
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1859
                lastIndex := newLast.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1860
                ^ self
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1861
            ].
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1862
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1863
            newContents := self containerClass basicNew:newSize.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1864
            newContents replaceFrom:1 to:oldSize with:contentsArray startingAt:firstIndex.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1865
            contentsArray := newContents.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1866
            firstIndex := 1.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1867
            lastIndex := newSize
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1868
        ]
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1869
    ]
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1870
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1871
    "Modified: / 30-07-2018 / 12:06:33 / Claus Gittinger"
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1872
! !
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1873
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1874
25351
30b332af1f33 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 24894
diff changeset
  1875
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1876
!OrderedCollection methodsFor:'private'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1877
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1878
containerClass
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1879
    "the class of the underlying container.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1880
     Here Array; redefined in WeakOrderedCollection to use a WeakArray"
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1881
     
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1882
    ^ Array
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1883
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1884
    "Created: / 30-07-2018 / 12:06:28 / Claus Gittinger"
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1885
!
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1886
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1887
initContents:size
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1888
    "setup the receiver-collection to hold size entries"
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1889
18879
5d1bbfc1761d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18811
diff changeset
  1890
    size == 0 ifTrue:[
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1891
        contentsArray := #().   "save memory by using a shared instance"
18879
5d1bbfc1761d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18811
diff changeset
  1892
    ] ifFalse:[
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1893
        contentsArray := self containerClass basicNew:size.
18879
5d1bbfc1761d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18811
diff changeset
  1894
    ].
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1895
    firstIndex := 1.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1896
    lastIndex := 0
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1897
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1898
    "Modified: / 30-07-2018 / 12:06:49 / Claus Gittinger"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1899
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1900
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1901
makeRoomAtFront
282
94f5c3a6230d *** empty log message ***
claus
parents: 253
diff changeset
  1902
    "grow/shift the contents for more room at the beginning.
94f5c3a6230d *** empty log message ***
claus
parents: 253
diff changeset
  1903
     Does not change the logical size.
18183
3178ced76cfa class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18182
diff changeset
  1904
     i.e. the contents array is changed from:
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1905
        #(1 2 3 4 5 6) -> #(nil 1 2 3 4 5 6)
18183
3178ced76cfa class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 18182
diff changeset
  1906
     and the start/stopIndices are adjusted as required"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1907
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1908
    |newContents
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1909
     oldSize    "{ Class:SmallInteger }"
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1910
     newSize    "{ Class:SmallInteger }"
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1911
     startIndex "{ Class:SmallInteger }"
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1912
     sz         "{ Class:SmallInteger }"|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1913
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1914
    oldSize := contentsArray size.
506
984b32fe635a fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents: 407
diff changeset
  1915
    sz := lastIndex - firstIndex + 1.
984b32fe635a fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents: 407
diff changeset
  1916
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1917
    ((oldSize == 0) or:[sz == 0]) ifTrue:[
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1918
        contentsArray := self containerClass basicNew:MinContentsArraySize.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1919
        firstIndex := 2. lastIndex := 1.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1920
        ^ self
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
  1921
    ].
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
  1922
329
claus
parents: 293
diff changeset
  1923
    "
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1924
     if there is lots of room at the end (> 50%),
329
claus
parents: 293
diff changeset
  1925
     shift instead of growing. This helps collections
claus
parents: 293
diff changeset
  1926
     which get elements removed at the end and added at front.
claus
parents: 293
diff changeset
  1927
    "
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1928
    oldSize > (sz * 2) ifTrue:[
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1929
        startIndex := oldSize // 4.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1930
        startIndex > 1 ifTrue:[
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1931
            contentsArray
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1932
                replaceFrom:startIndex to:(startIndex + sz - 1)
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1933
                with:contentsArray startingAt:1.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1934
            contentsArray from:1 to:(startIndex - 1) put:nil.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1935
            firstIndex := startIndex.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1936
            lastIndex := startIndex + sz - 1.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1937
            ^ self
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1938
        ]
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1939
    ].
2
claus
parents: 1
diff changeset
  1940
    newSize := oldSize * 2.
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1941
    newContents := self containerClass basicNew:newSize.
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1942
    newContents
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1943
        replaceFrom:(oldSize + 1) to:newSize
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1944
        with:contentsArray startingAt:1.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1945
    contentsArray := newContents.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1946
    firstIndex := firstIndex + oldSize.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1947
    lastIndex := lastIndex + oldSize
506
984b32fe635a fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents: 407
diff changeset
  1948
11275
2883195d540e makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents: 11271
diff changeset
  1949
    "Created: / 08-11-1995 / 12:47:49 / cg"
2883195d540e makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents: 11271
diff changeset
  1950
    "Modified: / 22-10-2008 / 17:10:13 / cg"
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1951
    "Modified: / 30-07-2018 / 12:08:34 / Claus Gittinger"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1952
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1953
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1954
makeRoomAtIndex:whereToMakeEmptySlot
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1955
    "grow the contents for inserting at whereToMakeEmptySlot.
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1956
     The whereToMakeEmptySlot argument must be a physical index within the contentsArray.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1957
     If there is (plenty of) room at either end, elements are shifted inplace to create
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1958
     an empty slot; otherwise, a new contentsArray is allocated.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1959
     Since this changes the logical size, the modified index is returned.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1960
     i.e.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1961
     #(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:3 -> #(1 2 nil 3 4 5 6)
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1962
     #(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:1 -> #(nil 1 2 3 4 5 6)
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1963
     #(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:7 -> #(1 2 3 4 5 6 nil)"
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1964
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1965
    |newContents
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1966
     newSize         "{ Class:SmallInteger }"
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  1967
     oldSize         "{ Class:SmallInteger }"
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1968
     first           "{ Class:SmallInteger }"
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1969
     last            "{ Class:SmallInteger }"
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1970
     index           "{ Class:SmallInteger }"
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1971
     shiftLeft shiftRight|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1972
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1973
    oldSize := contentsArray size.
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1974
    index := whereToMakeEmptySlot.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1975
    first := firstIndex.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1976
    last := lastIndex.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1977
19791
610d2bf9af00 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 19098
diff changeset
  1978
    (first > 1) ifTrue:[
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1979
        "there is room at the beginning"
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1980
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1981
        shiftLeft := true.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1982
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1983
        index == first ifFalse:[
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1984
            "/ so, we'd have to copy all elements before that index
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1985
            "/ one slot towards the containers beginning ...
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1986
            "/ if there is also space at the end, AND the number of
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1987
            "/ elements after the index is smaller than the number before,
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1988
            "/ copy the remaining elements. To copy the least possible number.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1989
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1990
            (last - index) < (index - first) ifTrue:[
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1991
                last < oldSize ifTrue:[
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1992
                    shiftLeft := false.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1993
                    shiftRight := true.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1994
                ]
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1995
            ]
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1996
        ]
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1997
    ] ifFalse:[
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1998
        last < oldSize ifTrue:[
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  1999
            shiftRight := true
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2000
        ]
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  2001
    ].
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  2002
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  2003
    shiftLeft == true ifTrue:[
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2004
        "there is room at the beginning"
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2005
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2006
        index == first ifFalse:[
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2007
            contentsArray
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2008
                replaceFrom:(first - 1) to:(index - 2)
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2009
                with:contentsArray startingAt:first.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2010
            contentsArray at:index-1 put:nil.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2011
        ].
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2012
        firstIndex := first - 1.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2013
        ^ index - 1
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  2014
    ].
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  2015
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  2016
    shiftRight == true ifTrue:[
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2017
        "there is room at the end"
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2018
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2019
        last := last + 1.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2020
        index == last ifFalse:[
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2021
            contentsArray
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2022
                replaceFrom:(index + 1) to:last
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2023
                with:contentsArray startingAt:index.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2024
            contentsArray at:index put:nil
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2025
        ].
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2026
        lastIndex := last.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2027
        ^ index
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  2028
    ].
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  2029
19791
610d2bf9af00 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 19098
diff changeset
  2030
    "/ no space at either end
610d2bf9af00 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 19098
diff changeset
  2031
    oldSize < MinContentsArraySize ifTrue:[
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2032
        newSize := MinContentsArraySize
18879
5d1bbfc1761d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18811
diff changeset
  2033
    ] ifFalse:[
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2034
        newSize := oldSize * 2.
18879
5d1bbfc1761d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18811
diff changeset
  2035
    ].
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2036
    newContents := self containerClass basicNew:newSize.
11271
5b75b4a7fb35 tuned makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 11269
diff changeset
  2037
    index == first ifTrue:[
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2038
        "/ if there is a lot at the end (> 50), make all new space at the beginning.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2039
        "/ otherwise make 3/4 of the new space to the beginning, 1/4 to the end
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2040
        oldSize ~~ 0 ifTrue:[
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2041
            (last < (oldSize - 50)) ifTrue:[
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2042
                lastIndex := newSize - (oldSize-last).
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2043
                firstIndex := lastIndex - (last - first).
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2044
            ] ifFalse:[
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2045
                firstIndex := oldSize * 3 // 4.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2046
                firstIndex < 2 ifTrue:[firstIndex := 2]. "/ pathological case (was explicitly allocated with size<MinSize
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2047
                lastIndex := firstIndex + (last - first).
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2048
            ].
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2049
            newContents
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2050
                replaceFrom:firstIndex to:lastIndex
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2051
                with:contentsArray startingAt:first.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2052
        ].
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2053
        contentsArray := newContents.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2054
        firstIndex := firstIndex - 1.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2055
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2056
        ^ firstIndex.
11271
5b75b4a7fb35 tuned makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 11269
diff changeset
  2057
    ] ifFalse:[
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2058
        oldSize ~~ 0 ifTrue:[
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2059
            newContents
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2060
                replaceFrom:1 to:(index - first)
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2061
                with:contentsArray startingAt:first.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2062
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2063
            index <= last ifTrue:[
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2064
                newContents
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2065
                    replaceFrom:(index - first + 2) to:(last - first + 2)
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2066
                    with:contentsArray startingAt:index.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2067
            ].
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2068
        ].
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2069
        contentsArray := newContents.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2070
        firstIndex := 1.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2071
        lastIndex := last - first + 2.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2072
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2073
        "/ return the modified index
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2074
        ^ index - (first - firstIndex).
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2075
    ].
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  2076
11275
2883195d540e makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents: 11271
diff changeset
  2077
    "Modified: / 22-10-2008 / 17:11:06 / cg"
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2078
    "Modified: / 30-07-2018 / 12:07:10 / Claus Gittinger"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2079
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2080
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2081
makeRoomAtIndex:whereToMakeEmptySlots for:howMany
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2082
    "grow the contents for inserting at whereToMakeEmptySlot.
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2083
     The whereToMakeEmptySlot argument must be a physical index within the contentsArray.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2084
     If there is (plenty of) room at either end, elements are shifted inplace to create
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2085
     an empty slot; otherwise, a new contentsArray is allocated.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2086
     Since this changes the logical size, the modified index is returned.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2087
     i.e.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2088
     #(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:3 for:2 -> #(1 2 nil nil 3 4 5 6)
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2089
     #(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:1 for:2 -> #(nil nil 1 2 3 4 5 6)
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2090
     #(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:7 for:2 -> #(1 2 3 4 5 6 nil nil)"
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2091
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2092
    |newContents
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2093
     newSize         "{ Class:SmallInteger }"
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2094
     oldSize         "{ Class:SmallInteger }"
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2095
     oneFourthOfSize "{ Class:SmallInteger }"
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2096
     first           "{ Class:SmallInteger }"
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2097
     last            "{ Class:SmallInteger }"
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2098
     index           "{ Class:SmallInteger }"
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2099
     shiftLeft shiftRight|
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2100
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2101
    oldSize := contentsArray size.
19792
4625cef49989 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 19791
diff changeset
  2102
    oneFourthOfSize := (oldSize // 4).
4625cef49989 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 19791
diff changeset
  2103
    oneFourthOfSize == 0 ifTrue:[oneFourthOfSize := 1].
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2104
    index := whereToMakeEmptySlots.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2105
    first := firstIndex.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2106
    last := lastIndex.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2107
2549
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
  2108
    shiftLeft := shiftRight := false.
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2109
    ((first > howMany) and:[first > oneFourthOfSize]) ifTrue:[
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2110
        "there is room (>25%) at the beginning"
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2111
        shiftLeft := true.
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2112
    ] ifFalse:[
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2113
        ((last + howMany) <= oldSize
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2114
        and:[last < (oneFourthOfSize * 3)]) ifTrue:[
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2115
            shiftRight := true
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2116
        ]
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2117
    ].
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2118
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2119
    shiftLeft == true ifTrue:[
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2120
        "there is room at the beginning"
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2121
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2122
        index == first ifFalse:[
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2123
            contentsArray
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2124
                replaceFrom:(first - howMany)
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2125
                to:(index - howMany - 1)
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2126
                with:contentsArray
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2127
                startingAt:first.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2128
            contentsArray from:index-howMany to:index-1 put:nil.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2129
        ].
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2130
        firstIndex := first - howMany.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2131
        ^ index - howMany
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2132
    ].
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2133
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2134
    shiftRight == true ifTrue:[
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2135
        "there is room at the end"
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2136
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2137
        last := last + howMany.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2138
        index == last ifFalse:[
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2139
            contentsArray
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2140
                replaceFrom:(index + howMany)
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2141
                to:last
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2142
                with:contentsArray
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2143
                startingAt:index.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2144
            contentsArray from:index to:index+howMany-1 put:nil
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2145
        ].
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2146
        lastIndex := last.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2147
        ^ index
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2148
    ].
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2149
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2150
    newSize := (oldSize+howMany) nextPowerOf2.
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2151
    "/ newSize := (oldSize * 2).
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2152
    "/ [newSize < (oldSize+howMany)] whileTrue:[
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2153
    "/    newSize := (newSize * 2) max:howMany
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2154
    "/ ].
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2155
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2156
    newContents := self containerClass basicNew:newSize.
18879
5d1bbfc1761d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18811
diff changeset
  2157
    oldSize ~~ 0 ifTrue:[
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2158
        index > first ifTrue:[
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2159
            newContents
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2160
                replaceFrom:1
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2161
                to:(index - first)
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2162
                with:contentsArray
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2163
                startingAt:first.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2164
        ].
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2165
        index <= last ifTrue:[
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2166
            newContents
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2167
                replaceFrom:(index - first + howMany + 1)
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2168
                to:(last - first + howMany + 1)
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2169
                with:contentsArray
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2170
                startingAt:(index).
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2171
        ].
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2172
    ].
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2173
    contentsArray := newContents.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2174
    firstIndex := 1.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2175
    lastIndex := last - first + howMany + 1.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2176
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2177
    "/ return the modified index
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2178
    ^ index - (first - firstIndex).
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2179
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2180
    "Modified: / 15-04-1997 / 12:34:16 / cg"
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2181
    "Modified: / 30-07-2018 / 12:07:16 / Claus Gittinger"
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2182
!
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  2183
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2184
makeRoomAtLast
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2185
    "grow/shift the contents for more room at the end.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2186
     Does not change the logical size.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2187
     i.e.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2188
     #(1 2 3 4 5 6) -> #(1 2 3 4 5 6 nil)"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2189
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2190
    |newContents
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2191
     oldSize    "{ Class:SmallInteger }"
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2192
     newSize    "{ Class:SmallInteger }"
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2193
     startIndex "{ Class:SmallInteger }"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2194
     sz         "{ Class:SmallInteger }"|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2195
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2196
    oldSize := contentsArray size.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2197
    sz := lastIndex - firstIndex + 1.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2198
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2199
    "
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2200
     if there is lots of room at the beginning (> 50%),
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2201
     shift instead of growing. This helps collections which get
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2202
     elements removed at front and added at the end.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2203
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2204
    oldSize > (sz * 2) ifTrue:[
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2205
        startIndex := firstIndex // 4.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2206
        startIndex == 0 ifTrue:[
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2207
            startIndex := 1
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2208
        ].
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2209
        contentsArray
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2210
            replaceFrom:startIndex to:startIndex + sz - 1
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2211
            with:contentsArray startingAt:firstIndex.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2212
        contentsArray from:startIndex + sz to:lastIndex put:nil.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2213
        firstIndex := startIndex.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2214
        lastIndex := startIndex + sz - 1.
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2215
        ^ self
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2216
    ].
18879
5d1bbfc1761d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18811
diff changeset
  2217
    oldSize == 0 ifTrue:[
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2218
        newSize := MinContentsArraySize
18879
5d1bbfc1761d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18811
diff changeset
  2219
    ] ifFalse:[
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2220
        newSize := oldSize * 2.
18879
5d1bbfc1761d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18811
diff changeset
  2221
    ].
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2222
    newContents := self containerClass basicNew:newSize.
18879
5d1bbfc1761d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18811
diff changeset
  2223
    oldSize ~~ 0 ifTrue:[
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2224
        newContents
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2225
            replaceFrom:1 to:oldSize
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2226
            with:contentsArray startingAt:1.
18879
5d1bbfc1761d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18811
diff changeset
  2227
    ].
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2228
    contentsArray := newContents
11269
f45a4a2cd7ab *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10839
diff changeset
  2229
f45a4a2cd7ab *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10839
diff changeset
  2230
    "Modified: / 22-10-2008 / 11:50:28 / cg"
23246
35d957c7a840 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23245
diff changeset
  2231
    "Modified: / 30-07-2018 / 12:07:22 / Claus Gittinger"
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2232
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2233
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2234
setFirstIndex:newFirstIndex lastIndex:newLastIndex
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2235
    "set first and last index"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2236
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2237
    firstIndex := newFirstIndex.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2238
    lastIndex := newLastIndex.
6088
db3fc1ec9c1a added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents: 5874
diff changeset
  2239
!
db3fc1ec9c1a added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents: 5874
diff changeset
  2240
db3fc1ec9c1a added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents: 5874
diff changeset
  2241
setIndices
db3fc1ec9c1a added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents: 5874
diff changeset
  2242
    "added for VW compatibility: set the indices for an empty collection"
db3fc1ec9c1a added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents: 5874
diff changeset
  2243
db3fc1ec9c1a added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents: 5874
diff changeset
  2244
    firstIndex := 1.
db3fc1ec9c1a added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents: 5874
diff changeset
  2245
    lastIndex := 0.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2246
! !
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2247
7260
edfa8d6a6046 method category rename
Claus Gittinger <cg@exept.de>
parents: 7166
diff changeset
  2248
!OrderedCollection methodsFor:'private-accessing'!
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2249
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2250
contentsArray
4153
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  2251
    "return the orderedCollections underlying contentsArray.
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  2252
     The actual elements are found here starting at firstIndex,
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  2253
     and ending at lastIndex."
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  2254
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2255
    ^ contentsArray
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2256
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2257
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2258
firstIndex
4153
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  2259
    "return the index of my first element in my underlying contentsArray.
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  2260
     The actual elements are found starting this index,
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  2261
     and ending at lastIndex."
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  2262
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2263
    ^ firstIndex
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2264
! !
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2265
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2266
!OrderedCollection methodsFor:'queries'!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2267
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2268
capacity
20354
a671aac9c70d #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20341
diff changeset
  2269
    "return the number of elements, that the receiver is prepared to take
a671aac9c70d #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20341
diff changeset
  2270
     without growing. 
a671aac9c70d #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 20341
diff changeset
  2271
     Notice, that OCs do automatically resize as required, 
21658
692b6497a669 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21524
diff changeset
  2272
     so knowing the capacity is of no real use."
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2273
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2274
    ^ contentsArray size
21658
692b6497a669 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21524
diff changeset
  2275
692b6497a669 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21524
diff changeset
  2276
    "Modified (comment): / 17-03-2017 / 11:49:58 / stefan"
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2277
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2278
23702
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2279
includes:anObject
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2280
    "return true if anObject is in the collection. Compare using ="
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2281
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2282
    ^ (contentsArray
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2283
		indexOf:anObject
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2284
		startingAt:firstIndex
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2285
		endingAt:lastIndex) ~~ 0
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2286
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2287
    "Modified: 12.4.1996 / 17:57:27 / cg"
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2288
!
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2289
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2290
includesIdentical:anObject
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2291
    "return true if anObject is in the collection. Compare using =="
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2292
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2293
    ^ (contentsArray
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2294
		identityIndexOf:anObject
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2295
		startingAt:firstIndex
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2296
		endingAt:lastIndex) ~~ 0
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2297
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2298
    "Modified: 12.4.1996 / 17:57:09 / cg"
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2299
!
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2300
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2301
isEmpty
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2302
    "return true, if the receiver has no elements"
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2303
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2304
    ^ lastIndex < firstIndex
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2305
!
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2306
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2307
isEmptyOrNil
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2308
    "return true, if the receiver has no elements"
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2309
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2310
    ^ lastIndex < firstIndex
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2311
!
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2312
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2313
notEmpty
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2314
    "return true, if the receiver has any elements"
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2315
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2316
    ^ lastIndex >= firstIndex
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2317
!
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2318
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2319
notEmptyOrNil
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2320
    "return true, if the receiver has any elements"
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2321
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2322
    ^ lastIndex >= firstIndex
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2323
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2324
    "Created: / 22-02-2017 / 14:08:43 / stefan"
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2325
!
6b11890f5f2c #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 23246
diff changeset
  2326
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2327
size
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2328
    "return the number of elements in the collection"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2329
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2330
    ^ lastIndex - firstIndex + 1
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2331
! !
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  2332
3095
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  2333
!OrderedCollection methodsFor:'searching'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2334
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2335
identityIndexOf:anObject
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  2336
    "return the index of anObject or 0 if not found. Compare using =="
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2337
1157
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2338
    |index|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2339
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2340
    index := contentsArray
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2341
		identityIndexOf:anObject
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2342
		startingAt:firstIndex
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2343
		endingAt:lastIndex.
1157
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2344
    index == 0 ifTrue:[^ 0].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2345
    ^ index - firstIndex + 1
1157
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2346
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2347
    "Modified: 12.4.1996 / 17:58:25 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2348
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2349
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2350
identityIndexOf:anObject startingAt:startIndex
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  2351
    "return the index of anObject, starting search at startIndex.
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  2352
     Compare using ==; return 0 if not found in the collection"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2353
1157
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2354
    |index|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2355
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2356
    index := contentsArray
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2357
		identityIndexOf:anObject
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2358
		startingAt:(startIndex + firstIndex - 1)
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2359
		endingAt:lastIndex.
1157
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2360
    index == 0 ifTrue:[^ 0].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2361
    ^ index - firstIndex + 1
1157
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2362
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2363
    "Modified: 12.4.1996 / 17:58:19 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2364
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2365
24003
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2366
identityIndexOf:anObject startingAt:startIndex endingAt:stopIndex
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2367
    "return the index of anObject, starting search at startIndex,
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2368
     ending at stop.
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2369
     If found (within the range), return the index, otherwise return 0.
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2370
     Compare using ==; return 0 if not found in the collection"
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2371
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2372
    |index|
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2373
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2374
    index := contentsArray
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2375
                identityIndexOf:anObject
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2376
                startingAt:(startIndex + firstIndex - 1)
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2377
                endingAt:((stopIndex + firstIndex - 1) min:lastIndex).
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2378
    index == 0 ifTrue:[^ 0].
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2379
    ^ index - firstIndex + 1
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2380
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2381
    "
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2382
     |coll|
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2383
     coll := #(1 2 3 4 5 6 7 8 9 10) asOrderedCollection.
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2384
     coll identityIndexOf:7 startingAt:2 endingAt:6.  
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2385
     coll identityIndexOf:7 startingAt:2 endingAt:7.  
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2386
     coll identityIndexOf:7 startingAt:2 endingAt:8.  
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2387
     coll identityIndexOf:7 startingAt:2 endingAt:9. 
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2388
     coll removeFirst.
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2389
     coll removeFirst.
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2390
     coll.     'now: OrderedCollection(3 4 5 6 7 8 9 10)'.   
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2391
     coll identityIndexOf:7 startingAt:2 endingAt:4.    
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2392
     coll identityIndexOf:7 startingAt:2 endingAt:5.     
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2393
     coll identityIndexOf:7 startingAt:2 endingAt:6.  
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2394
     coll identityIndexOf:7 startingAt:2 endingAt:7. 
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2395
    "
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2396
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2397
    "Modified: 12.4.1996 / 17:58:19 / cg"
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2398
!
abb6108fb06b #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 23702
diff changeset
  2399
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2400
indexOf:anObject
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  2401
    "return the index of anObject or 0 if not found in the collection.
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  2402
     Compare using ="
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2403
1157
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2404
    |index|
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2405
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2406
    index := contentsArray
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2407
		indexOf:anObject
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2408
		startingAt:firstIndex
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2409
		endingAt:lastIndex.
1157
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2410
    index == 0 ifTrue:[^ 0].
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2411
    ^ index - firstIndex + 1
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2412
1157
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2413
    "
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2414
     |c|
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2415
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2416
     c := OrderedCollection new:10000.
12035
b927a2b293f8 changed:
Claus Gittinger <cg@exept.de>
parents: 11330
diff changeset
  2417
     c add:10; add:20; add:30.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2418
     c indexOf:99
1157
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2419
    "
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2420
12035
b927a2b293f8 changed:
Claus Gittinger <cg@exept.de>
parents: 11330
diff changeset
  2421
    "
b927a2b293f8 changed:
Claus Gittinger <cg@exept.de>
parents: 11330
diff changeset
  2422
     |c|
b927a2b293f8 changed:
Claus Gittinger <cg@exept.de>
parents: 11330
diff changeset
  2423
b927a2b293f8 changed:
Claus Gittinger <cg@exept.de>
parents: 11330
diff changeset
  2424
     c := OrderedCollection new:10000.
b927a2b293f8 changed:
Claus Gittinger <cg@exept.de>
parents: 11330
diff changeset
  2425
     c add:10; add:20; add:30.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2426
     c indexOf:30
12035
b927a2b293f8 changed:
Claus Gittinger <cg@exept.de>
parents: 11330
diff changeset
  2427
    "
b927a2b293f8 changed:
Claus Gittinger <cg@exept.de>
parents: 11330
diff changeset
  2428
1157
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2429
    "Modified: 12.4.1996 / 17:57:54 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2430
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2431
12036
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2432
indexOf:anObject ifAbsent:exceptionValue
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2433
    "return the index of anObject or 0 if not found in the collection.
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2434
     Compare using =
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2435
     If the receiver does not contain anElement,
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2436
     return the result of evaluating the argument, exceptionBlock."
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2437
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2438
    |index|
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2439
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2440
    index := contentsArray
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2441
		indexOf:anObject
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2442
		startingAt:firstIndex
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2443
		endingAt:lastIndex.
12036
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2444
    index == 0 ifTrue:[^ exceptionValue value].
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2445
    ^ index - firstIndex + 1
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2446
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2447
    "
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2448
     |c|
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2449
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2450
     c := OrderedCollection new:10000.
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2451
     c add:10; add:20; add:30.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2452
     c indexOf:99 ifAbsent:'nope'
12036
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2453
    "
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2454
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2455
    "
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2456
     |c|
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2457
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2458
     c := OrderedCollection new:10000.
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2459
     c add:10; add:20; add:30.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2460
     c indexOf:30 ifAbsent:'nope'
12036
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2461
    "
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2462
!
4ce4157ce6a9 added: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 12035
diff changeset
  2463
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2464
indexOf:anObject startingAt:startIndex
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  2465
    "return the index of anObject, starting search at startIndex.
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  2466
     Compare using =; return 0 if not found in the collection"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2467
1157
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2468
    |index|
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2469
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2470
    index := contentsArray
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2471
		indexOf:anObject
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2472
		startingAt:(startIndex + firstIndex - 1)
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2473
		endingAt:lastIndex.
1157
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2474
    index == 0 ifTrue:[^ 0].
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2475
    ^ index - firstIndex + 1
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2476
1157
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2477
    "
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2478
     |c|
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2479
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2480
     c := OrderedCollection new:10000.
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2481
     c add:1; add:2; add:3.
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2482
     c indexOf:4 startingAt:5
1157
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2483
    "
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2484
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  2485
    "Modified: 12.4.1996 / 17:58:53 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2486
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  2487
3095
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  2488
!OrderedCollection methodsFor:'testing'!
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  2489
5556
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  2490
isFixedSize
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  2491
    "return true if the receiver cannot grow - this will vanish once
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  2492
     Arrays and Strings learn how to grow ..."
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  2493
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  2494
    ^ false
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  2495
!
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  2496
5401
81ea19e6843c test for class
ps
parents: 4862
diff changeset
  2497
isOrderedCollection
81ea19e6843c test for class
ps
parents: 4862
diff changeset
  2498
    "return true, if the receiver is some kind of ordered collection (or list etc);
81ea19e6843c test for class
ps
parents: 4862
diff changeset
  2499
     true is returned here - the method is only redefined in Object."
81ea19e6843c test for class
ps
parents: 4862
diff changeset
  2500
81ea19e6843c test for class
ps
parents: 4862
diff changeset
  2501
    ^ true
81ea19e6843c test for class
ps
parents: 4862
diff changeset
  2502
81ea19e6843c test for class
ps
parents: 4862
diff changeset
  2503
3095
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  2504
! !
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  2505
15660
3143ca7c443a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15442
diff changeset
  2506
!OrderedCollection methodsFor:'tuning'!
3143ca7c443a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15442
diff changeset
  2507
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2508
quickSortFrom:inBegin to:inEnd
15660
3143ca7c443a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15442
diff changeset
  2509
    self class == OrderedCollection ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2510
	"/ because array-at/at:put: is much faster, this speeds up sorting by
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2511
	"/ up to 30%
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2512
	contentsArray
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2513
	    quickSortFrom:(firstIndex + inBegin - 1)
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2514
	    to:(firstIndex + inEnd - 1)
15660
3143ca7c443a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15442
diff changeset
  2515
    ] ifFalse:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2516
	super quickSortFrom:inBegin to:inEnd
15660
3143ca7c443a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15442
diff changeset
  2517
    ].
3143ca7c443a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15442
diff changeset
  2518
!
3143ca7c443a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15442
diff changeset
  2519
3143ca7c443a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15442
diff changeset
  2520
quickSortFrom:inBegin to:inEnd sortBlock:sortBlock
3143ca7c443a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15442
diff changeset
  2521
    self class == OrderedCollection ifTrue:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2522
	"/ because array-at/at:put: is much faster, this speeds up sorting by
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2523
	"/ up to 30%
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2524
	contentsArray
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2525
	    quickSortFrom:(firstIndex + inBegin - 1)
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2526
	    to:(firstIndex + inEnd - 1)
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2527
	    sortBlock:sortBlock
15660
3143ca7c443a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15442
diff changeset
  2528
    ] ifFalse:[
20341
72ed68b0d0ec little code cleanup
Claus Gittinger <cg@exept.de>
parents: 20026
diff changeset
  2529
	super quickSortFrom:inBegin to:inEnd sortBlock:sortBlock
15660
3143ca7c443a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15442
diff changeset
  2530
    ]
3143ca7c443a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15442
diff changeset
  2531
! !
3143ca7c443a class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 15442
diff changeset
  2532
629
2ceefe9b5a19 version at the end
Claus Gittinger <cg@exept.de>
parents: 606
diff changeset
  2533
!OrderedCollection class methodsFor:'documentation'!
2ceefe9b5a19 version at the end
Claus Gittinger <cg@exept.de>
parents: 606
diff changeset
  2534
2ceefe9b5a19 version at the end
Claus Gittinger <cg@exept.de>
parents: 606
diff changeset
  2535
version
18811
54e839ea9d44 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18379
diff changeset
  2536
    ^ '$Header$'
16305
964f7ffb315d class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 16304
diff changeset
  2537
!
964f7ffb315d class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 16304
diff changeset
  2538
964f7ffb315d class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 16304
diff changeset
  2539
version_CVS
18811
54e839ea9d44 #BUGFIX
Claus Gittinger <cg@exept.de>
parents: 18379
diff changeset
  2540
    ^ '$Header$'
629
2ceefe9b5a19 version at the end
Claus Gittinger <cg@exept.de>
parents: 606
diff changeset
  2541
! !
14616
e763997b09d1 class: OrderedCollection
Claus Gittinger <cg@exept.de>
parents: 14071
diff changeset
  2542
18879
5d1bbfc1761d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18811
diff changeset
  2543
5d1bbfc1761d #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18811
diff changeset
  2544
OrderedCollection initialize!