LinkedList.st
author Claus Gittinger <cg@exept.de>
Tue, 09 Jul 2019 20:55:17 +0200
changeset 24417 03b083548da2
parent 23711 2f0fb1e83275
child 24614 e64574a14076
permissions -rw-r--r--
#REFACTORING by exept class: Smalltalk class changed: #recursiveInstallAutoloadedClassesFrom:rememberIn:maxLevels:noAutoload:packageTop:showSplashInLevels: Transcript showCR:(... bindWith:...) -> Transcript showCR:... with:...
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     1
"
5
67342904af11 *** empty log message ***
claus
parents: 3
diff changeset
     2
 COPYRIGHT (c) 1989 by Claus Gittinger
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
     3
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     4
a27a279701f8 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
"
5519
925d9a1eda2c fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents: 5244
diff changeset
    12
"{ Package: 'stx:libbasic' }"
925d9a1eda2c fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents: 5244
diff changeset
    13
17262
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
    14
"{ NameSpace: Smalltalk }"
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
    15
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    16
SequenceableCollection subclass:#LinkedList
1110
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 629
diff changeset
    17
	instanceVariableNames:'firstLink lastLink numberOfNodes'
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 629
diff changeset
    18
	classVariableNames:''
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 629
diff changeset
    19
	poolDictionaries:''
1364
94e30904604e category change
Claus Gittinger <cg@exept.de>
parents: 1313
diff changeset
    20
	category:'Collections-Linked'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    21
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    22
88
81dacba7a63a *** empty log message ***
claus
parents: 13
diff changeset
    23
!LinkedList class methodsFor:'documentation'!
81dacba7a63a *** empty log message ***
claus
parents: 13
diff changeset
    24
81dacba7a63a *** empty log message ***
claus
parents: 13
diff changeset
    25
copyright
81dacba7a63a *** empty log message ***
claus
parents: 13
diff changeset
    26
"
81dacba7a63a *** empty log message ***
claus
parents: 13
diff changeset
    27
 COPYRIGHT (c) 1989 by Claus Gittinger
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
    28
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    29
88
81dacba7a63a *** empty log message ***
claus
parents: 13
diff changeset
    30
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 13
diff changeset
    31
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 13
diff changeset
    32
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 13
diff changeset
    33
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 13
diff changeset
    34
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 13
diff changeset
    35
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 13
diff changeset
    36
"
81dacba7a63a *** empty log message ***
claus
parents: 13
diff changeset
    37
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    38
88
81dacba7a63a *** empty log message ***
claus
parents: 13
diff changeset
    39
documentation
81dacba7a63a *** empty log message ***
claus
parents: 13
diff changeset
    40
"
81dacba7a63a *** empty log message ***
claus
parents: 13
diff changeset
    41
    this class implements an anchor to a list of Links.
10647
9345efb77a76 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9118
diff changeset
    42
    The data itself is held in the link elements.
243
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
    43
    See (the abstract) Link, ValueLink and (possibly other) classes,
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
    44
    which can be used as elements of a linkedList.
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
    45
360
claus
parents: 243
diff changeset
    46
    LinkedList does not care for storage; all it does is handling
claus
parents: 243
diff changeset
    47
    chained link elements, which must respond to #nextLink/#nextLink:.
17123
784f5f9ce77f class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 15428
diff changeset
    48
    (i.e. any object which can do this, can be used as elements of a linked list).
2146
Claus Gittinger <cg@exept.de>
parents: 1364
diff changeset
    49
    An abstract superclass for linkElements is Link; a concrete class is
Claus Gittinger <cg@exept.de>
parents: 1364
diff changeset
    50
    ValueLink, which holds a reference to some object.
Claus Gittinger <cg@exept.de>
parents: 1364
diff changeset
    51
Claus Gittinger <cg@exept.de>
parents: 1364
diff changeset
    52
    [warning:]
13727
05c6543b983b changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 13163
diff changeset
    53
        Be careful when subclassing Link, since there is a big drawback,
05c6543b983b changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 13163
diff changeset
    54
        which may be overlooked by beginners:
17123
784f5f9ce77f class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 15428
diff changeset
    55
            a Link element can ONLY be in one LinkedList at a time
13727
05c6543b983b changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 13163
diff changeset
    56
            - adding the same element to another LinkedList
17123
784f5f9ce77f class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 15428
diff changeset
    57
              will remove it from the first as a side effect.
13727
05c6543b983b changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 13163
diff changeset
    58
        Therefore, NEVER simply add something to a linkedList (except for
05c6543b983b changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 13163
diff changeset
    59
        valueLinks) unless you know what you do.
05c6543b983b changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 13163
diff changeset
    60
        The ST-80 implementors probably wanted this behavior, to move
05c6543b983b changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 13163
diff changeset
    61
        processes from the waitingList to runLists and vice versa;
05c6543b983b changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 13163
diff changeset
    62
        however, literature seems to not point this out enough.
360
claus
parents: 243
diff changeset
    63
243
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
    64
    Although LinkedList is a subclass of SequenceableCollection (and therefore
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
    65
    supports indexed access via at:), you should be careful in using it or
10647
9345efb77a76 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9118
diff changeset
    66
    other methods based upon at:.
360
claus
parents: 243
diff changeset
    67
    The reason is that #at: walks the linkedlist to find the indexed element
10647
9345efb77a76 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9118
diff changeset
    68
    and is therefore slow.
9345efb77a76 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9118
diff changeset
    69
    This means that some linear-in-time algorithms inherited from
360
claus
parents: 243
diff changeset
    70
    SequenceableCollection become square in runtime.
243
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
    71
    In general, if you need access via a numeric index, you better use Array,
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
    72
    OrderedCollection or similar.
360
claus
parents: 243
diff changeset
    73
claus
parents: 243
diff changeset
    74
    For the above reasons, the system does not make heavily use of LinkedLists;
claus
parents: 243
diff changeset
    75
    the only good application is where elements must be repeatedly be removed
claus
parents: 243
diff changeset
    76
    at the front and added at the end.
17123
784f5f9ce77f class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 15428
diff changeset
    77
    (the scheduler's process handling code does this to manage process lists.)
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    78
4063
998e2be3d244 documentation
Claus Gittinger <cg@exept.de>
parents: 2350
diff changeset
    79
    [memory requirements:]
13727
05c6543b983b changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 13163
diff changeset
    80
        (OBJ-HEADER + (3 * ptr-size)) * size
05c6543b983b changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 13163
diff changeset
    81
                    + any additional instvars due to subclassing
4063
998e2be3d244 documentation
Claus Gittinger <cg@exept.de>
parents: 2350
diff changeset
    82
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    83
    [author:]
13727
05c6543b983b changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 13163
diff changeset
    84
        Claus Gittinger (July 1993)
2146
Claus Gittinger <cg@exept.de>
parents: 1364
diff changeset
    85
Claus Gittinger <cg@exept.de>
parents: 1364
diff changeset
    86
    [see also:]
13727
05c6543b983b changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 13163
diff changeset
    87
        Link ValueLink Process
360
claus
parents: 243
diff changeset
    88
"
claus
parents: 243
diff changeset
    89
!
claus
parents: 243
diff changeset
    90
10647
9345efb77a76 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9118
diff changeset
    91
examples
360
claus
parents: 243
diff changeset
    92
"
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
    93
                                                                        [exBegin]
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
    94
    |l|
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
    95
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
    96
    l := LinkedList new.
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
    97
    l addLast:'one'.
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
    98
    l addLast:'two'.
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
    99
    l addLast:'three'.
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   100
    l addLast:'four'.
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   101
    l inspect
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   102
                                                                        [exEnd]
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   103
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   104
                                                                        [exBegin]
360
claus
parents: 243
diff changeset
   105
    |l|
claus
parents: 243
diff changeset
   106
claus
parents: 243
diff changeset
   107
    l := LinkedList new.
claus
parents: 243
diff changeset
   108
    l addLast:(ValueLink new value:'one').
claus
parents: 243
diff changeset
   109
    l addLast:(ValueLink new value:'two').
claus
parents: 243
diff changeset
   110
    l addLast:(ValueLink new value:'three').
claus
parents: 243
diff changeset
   111
    l addLast:(ValueLink new value:'four').
claus
parents: 243
diff changeset
   112
    l inspect
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   113
                                                                        [exEnd]
360
claus
parents: 243
diff changeset
   114
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   115
                                                                        [exBegin]
360
claus
parents: 243
diff changeset
   116
    |l|
claus
parents: 243
diff changeset
   117
claus
parents: 243
diff changeset
   118
    l := LinkedList new.
claus
parents: 243
diff changeset
   119
    l addLast:(ValueLink new value:'one').
claus
parents: 243
diff changeset
   120
    l addLast:(ValueLink new value:'two').
claus
parents: 243
diff changeset
   121
    l addLast:(ValueLink new value:'three').
claus
parents: 243
diff changeset
   122
    l addLast:(ValueLink new value:'four').
claus
parents: 243
diff changeset
   123
    (l at:3) value inspect.        'slow operation for large lists'.
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   124
                                                                        [exEnd]
360
claus
parents: 243
diff changeset
   125
claus
parents: 243
diff changeset
   126
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   127
                                                                        [exBegin]
360
claus
parents: 243
diff changeset
   128
    |l link|
claus
parents: 243
diff changeset
   129
claus
parents: 243
diff changeset
   130
    l := LinkedList new.
claus
parents: 243
diff changeset
   131
    l addLast:(ValueLink new value:'one').
claus
parents: 243
diff changeset
   132
    l addLast:(ValueLink new value:'two').
claus
parents: 243
diff changeset
   133
    l addLast:(ValueLink new value:'three').
claus
parents: 243
diff changeset
   134
    l addLast:(ValueLink new value:'four').
claus
parents: 243
diff changeset
   135
    link := l removeFirst.
claus
parents: 243
diff changeset
   136
    l addLast:link.
10647
9345efb77a76 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9118
diff changeset
   137
    l inspect.
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   138
                                                                        [exEnd]
88
81dacba7a63a *** empty log message ***
claus
parents: 13
diff changeset
   139
"
81dacba7a63a *** empty log message ***
claus
parents: 13
diff changeset
   140
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   141
a27a279701f8 Initial revision
claus
parents:
diff changeset
   142
!LinkedList class methodsFor:'instance creation'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   143
a27a279701f8 Initial revision
claus
parents:
diff changeset
   144
new
a27a279701f8 Initial revision
claus
parents:
diff changeset
   145
    "create and return a new LinkedList"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   146
4300
7378b8d2ba04 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4063
diff changeset
   147
    ^ self basicNew initialize
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   148
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   149
a27a279701f8 Initial revision
claus
parents:
diff changeset
   150
!LinkedList methodsFor:'accessing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   151
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   152
at:index
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   153
    "return the n'th value - use of this method should be avoided,
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   154
     since it is slow to walk through the list - think about using
9118
192331be0626 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9117
diff changeset
   155
     another collection if you need indexed access.
17575
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   156
     Notice:
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   157
        that many methods in SeqColl are based on at:-access,
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   158
        so other inherited methods may be very slow (showing O^2 runtime).
17575
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   159
        It is a very bad idea to access LinkedList elements by index.
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   160
        many algorithms degenerate to poor performance if you do.
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   161
        This method is provided for protocol completeness,
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   162
        but please consider using another type of collection if you use it"
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   163
9117
62e21d3b40e2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5556
diff changeset
   164
    ^ self at:index ifAbsent:[ self subscriptBoundsError:index]
62e21d3b40e2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5556
diff changeset
   165
!
62e21d3b40e2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5556
diff changeset
   166
19975
a2fa3e857c74 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19970
diff changeset
   167
at:index ifAbsent:exceptionValue
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   168
    "return the n'th value - use of this method should be avoided,
9117
62e21d3b40e2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5556
diff changeset
   169
     since it is slow to walk through the list - think about using
9118
192331be0626 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9117
diff changeset
   170
     another collection if you need indexed access.
17575
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   171
     Notice:
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   172
        that many methods in SeqColl are based on at:-access,
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   173
        so other inherited methods may be very slow (showing O^2 runtime).
17575
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   174
        It is a very bad idea to access LinkedList elements by index.
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   175
        many algorithms degenerate to poor performance if you do.
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   176
        This method is provided for protocol completeness,
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   177
        but please consider using another type of collection if you use it"
9117
62e21d3b40e2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5556
diff changeset
   178
19975
a2fa3e857c74 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19970
diff changeset
   179
    |theLink|
159
514c749165c3 *** empty log message ***
claus
parents: 93
diff changeset
   180
19975
a2fa3e857c74 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19970
diff changeset
   181
    theLink := self linkAt:index ifAbsent:[^ exceptionValue value].
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   182
    ^ theLink value
20231
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   183
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   184
    "
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   185
     |l|
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   186
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   187
     l := LinkedList new.
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   188
     l add:'one'.
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   189
     l add:'two'.
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   190
     l add:'hello'.
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   191
     l at:3 ifAbsent:'missing'.
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   192
     l at:4 ifAbsent:'missing'.
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   193
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   194
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   195
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   196
first
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   197
    "return the first value in the list"
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   198
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   199
    ^ self firstLink value
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   200
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   201
10647
9345efb77a76 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9118
diff changeset
   202
firstIfEmpty:exceptionalValue
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   203
    "return the first value in the list or exceptionlValue, if empty"
17262
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   204
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   205
    firstLink isNil ifTrue:[^ exceptionalValue value].
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   206
    ^ firstLink value
17262
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   207
!
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   208
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   209
firstLink
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   210
    "return the first node in the list"
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   211
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   212
    firstLink isNil ifTrue:[^ self emptyCollectionError].
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   213
    ^ firstLink
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   214
!
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   215
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   216
firstLinkIfEmpty:exceptionalValue
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   217
    "return the first node in the list or exceptionlValue, if empty"
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   218
10647
9345efb77a76 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9118
diff changeset
   219
    firstLink isNil ifTrue:[^ exceptionalValue value].
9345efb77a76 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9118
diff changeset
   220
    ^ firstLink
9345efb77a76 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9118
diff changeset
   221
!
9345efb77a76 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9118
diff changeset
   222
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   223
last
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   224
    "return last value in the list"
17262
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   225
17274
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   226
    lastLink isNil ifTrue:[^ self emptyCollectionError].
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   227
    ^ lastLink value
17262
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   228
!
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   229
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   230
lastLink
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   231
    "return last node in the list"
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   232
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   233
    lastLink isNil ifTrue:[self emptyCollectionError].
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   234
    ^ lastLink
17272
46ac2aac776b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17262
diff changeset
   235
!
46ac2aac776b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17262
diff changeset
   236
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   237
linkAt:index ifAbsent:exceptionBlock
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   238
    "return the n'th link - use of this method should be avoided,
17272
46ac2aac776b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17262
diff changeset
   239
     since it is slow to walk through the list - think about using
46ac2aac776b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17262
diff changeset
   240
     another collection if you need indexed access.
17575
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   241
     Notice:
20231
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   242
        that many methods in the superclass, SequenceableCollection are based on at:-access,
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   243
        so other inherited methods may be very slow (showing O^2 runtime).
17575
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   244
        It is a very bad idea to access LinkedList elements by index.
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   245
        many algorithms degenerate to poor performance if you do.
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   246
        This method is provided for protocol completeness,
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   247
        but please consider using another type of collection if you use it"
17272
46ac2aac776b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17262
diff changeset
   248
46ac2aac776b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17262
diff changeset
   249
    |theLink
20231
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   250
     count "{Class: SmallInteger}"|
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   251
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   252
    count := index.
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   253
    (count < 1 or:[count > numberOfNodes]) ifTrue:[
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   254
        ^ exceptionBlock value.
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   255
    ].
17272
46ac2aac776b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17262
diff changeset
   256
46ac2aac776b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17262
diff changeset
   257
    theLink := firstLink.
20231
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   258
    count-1 timesRepeat:[
17272
46ac2aac776b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17262
diff changeset
   259
        theLink isNil ifTrue:[^ exceptionBlock value].
46ac2aac776b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17262
diff changeset
   260
        theLink := theLink nextLink.
46ac2aac776b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17262
diff changeset
   261
    ].
46ac2aac776b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17262
diff changeset
   262
    ^ theLink
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   263
! !
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   264
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   265
!LinkedList methodsFor:'adding & removing'!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   266
17262
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   267
add:aLinkOrAnyOtherObject
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   268
    "adds aLink to the end of the sequence. Returns aLink"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   269
17262
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   270
    |newLink|
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   271
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   272
    newLink := aLinkOrAnyOtherObject asLink.
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   273
    
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   274
    newLink nextLink:nil.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   275
    lastLink isNil ifTrue:[
17262
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   276
        firstLink := newLink
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   277
    ] ifFalse: [
17262
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   278
        lastLink nextLink:newLink
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   279
    ].
17262
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   280
    lastLink := newLink.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   281
    numberOfNodes := numberOfNodes + 1.
17262
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   282
    ^ newLink
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   283
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   284
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   285
add:aLinkOrAnyOtherObject after:aLinkOrValue
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   286
    "adds aLinkOrAnyOtherObject after another aLinkOrValue. 
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   287
     If aLinkOrValue is nil, linkToAdd is inserted at the beginning.
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   288
     If aLinkOrValue is not in the list, linkToAdd is added at the end.
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   289
     Returns aLinkOrAnyOtherObject."
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   290
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   291
    |this linkToAdd linkOrValue|
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   292
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   293
    aLinkOrValue isNil ifTrue:[^ self addFirst:aLinkOrAnyOtherObject ].
17262
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   294
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   295
    linkToAdd := aLinkOrAnyOtherObject asLink.
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   296
    linkOrValue := aLinkOrValue value.
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   297
    
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   298
    this := firstLink.
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   299
    [this notNil and:[this ~~ linkOrValue]] whileTrue:[
17262
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   300
        this := this nextLink
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   301
    ].
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   302
    this isNil ifTrue:[^ self add:linkToAdd ].
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   303
    linkToAdd nextLink:(this nextLink).
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   304
    this nextLink:linkToAdd.
10647
9345efb77a76 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9118
diff changeset
   305
    numberOfNodes := numberOfNodes + 1.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   306
    ^ linkToAdd
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   307
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   308
17262
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   309
addFirst:aLinkOrAnyOtherObject
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   310
    "adds aLink to the beginning of the sequence. Returns aLink"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   311
17262
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   312
    |linkToAdd|
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   313
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   314
    linkToAdd := aLinkOrAnyOtherObject asLink.
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   315
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   316
    firstLink isNil ifTrue:[
17262
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   317
        lastLink := linkToAdd.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   318
    ].
17262
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   319
    linkToAdd nextLink:firstLink.
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   320
    firstLink := linkToAdd.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   321
    numberOfNodes := numberOfNodes + 1.
17262
e7181a123eac class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17210
diff changeset
   322
    ^ linkToAdd
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   323
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   324
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   325
remove:aLinkOrValue ifAbsent:exceptionBlock
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   326
    "remove the argument, aLinkOrValue from the sequence and return it;
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   327
     if absent, evaluate the exceptionBlock."
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   328
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   329
    |prevNode nextNode thisNode linkOrValue|
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   330
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   331
    linkOrValue := aLinkOrValue value.
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   332
    thisNode := firstLink.
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   333
    [thisNode notNil] whileTrue:[
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   334
        nextNode := thisNode nextLink.
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   335
        (thisNode value = linkOrValue) ifTrue:[
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   336
            prevNode isNil ifTrue:[
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   337
                firstLink := nextNode
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   338
            ] ifFalse:[
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   339
                prevNode nextLink:nextNode
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   340
            ].
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   341
            nextNode isNil ifTrue:[
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   342
                lastLink := prevNode
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   343
            ].
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   344
            numberOfNodes := numberOfNodes - 1.
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   345
            thisNode nextLink:nil.
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   346
            ^ thisNode value
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   347
        ].
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   348
        prevNode := thisNode.
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   349
        thisNode := nextNode
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   350
    ].
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   351
    ^ exceptionBlock value
2349
b68ab8aaabe1 commentary
Claus Gittinger <cg@exept.de>
parents: 2146
diff changeset
   352
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   353
    "Created: / 30-11-2010 / 13:38:25 / cg"
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   354
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   355
1110
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 629
diff changeset
   356
removeAll
1164
38c54a4f1273 commentary
Claus Gittinger <cg@exept.de>
parents: 1110
diff changeset
   357
    "remove all elements from the sequence. Returns the receiver."
1110
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 629
diff changeset
   358
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 629
diff changeset
   359
    firstLink := lastLink := nil.
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 629
diff changeset
   360
    numberOfNodes := 0
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 629
diff changeset
   361
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 629
diff changeset
   362
    "Created: 21.3.1996 / 15:24:38 / cg"
1164
38c54a4f1273 commentary
Claus Gittinger <cg@exept.de>
parents: 1110
diff changeset
   363
    "Modified: 12.4.1996 / 13:34:53 / cg"
1110
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 629
diff changeset
   364
!
c425b5d28a89 redefined #removeAll
Claus Gittinger <cg@exept.de>
parents: 629
diff changeset
   365
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   366
removeFirst
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   367
    "remove and return the first node from the sequence"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   368
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   369
    |link|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   370
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   371
    firstLink isNil ifTrue:[
10647
9345efb77a76 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9118
diff changeset
   372
	^ self emptyCollectionError
5519
925d9a1eda2c fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents: 5244
diff changeset
   373
    ].
925d9a1eda2c fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents: 5244
diff changeset
   374
    link := firstLink.
10647
9345efb77a76 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9118
diff changeset
   375
    firstLink := firstLink nextLink.
9345efb77a76 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9118
diff changeset
   376
    firstLink isNil ifTrue:[
9345efb77a76 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9118
diff changeset
   377
	lastLink := nil
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   378
    ].
5519
925d9a1eda2c fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents: 5244
diff changeset
   379
    link nextLink:nil.
925d9a1eda2c fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents: 5244
diff changeset
   380
    numberOfNodes := numberOfNodes - 1.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   381
    ^ link
13153
572dc11e2e96 added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 13150
diff changeset
   382
!
572dc11e2e96 added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 13150
diff changeset
   383
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   384
removeIdentical:aLinkOrValue ifAbsent:exceptionBlock
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   385
    "remove the argument, aLinkOrValue from the sequence and return it;
13163
e4845fce0ada changed comment: #removeIdentical:ifAbsent:
Stefan Vogel <sv@exept.de>
parents: 13153
diff changeset
   386
     if absent, evaluate the exceptionBlock."
13153
572dc11e2e96 added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 13150
diff changeset
   387
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   388
    |prevNode nextNode thisNode linkOrValue|
13153
572dc11e2e96 added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 13150
diff changeset
   389
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   390
    linkOrValue := aLinkOrValue value.
13153
572dc11e2e96 added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 13150
diff changeset
   391
    thisNode := firstLink.
572dc11e2e96 added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 13150
diff changeset
   392
    [thisNode notNil] whileTrue:[
572dc11e2e96 added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 13150
diff changeset
   393
        nextNode := thisNode nextLink.
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   394
        (thisNode value == linkOrValue) ifTrue:[
13153
572dc11e2e96 added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 13150
diff changeset
   395
            prevNode isNil ifTrue:[
572dc11e2e96 added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 13150
diff changeset
   396
                firstLink := nextNode
572dc11e2e96 added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 13150
diff changeset
   397
            ] ifFalse:[
572dc11e2e96 added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 13150
diff changeset
   398
                prevNode nextLink:nextNode
572dc11e2e96 added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 13150
diff changeset
   399
            ].
572dc11e2e96 added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 13150
diff changeset
   400
            nextNode isNil ifTrue:[
572dc11e2e96 added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 13150
diff changeset
   401
                lastLink := prevNode
572dc11e2e96 added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 13150
diff changeset
   402
            ].
572dc11e2e96 added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 13150
diff changeset
   403
            numberOfNodes := numberOfNodes - 1.
572dc11e2e96 added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 13150
diff changeset
   404
            thisNode nextLink:nil.
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   405
            ^ thisNode value
13153
572dc11e2e96 added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 13150
diff changeset
   406
        ].
572dc11e2e96 added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 13150
diff changeset
   407
        prevNode := thisNode.
572dc11e2e96 added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 13150
diff changeset
   408
        thisNode := nextNode
572dc11e2e96 added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 13150
diff changeset
   409
    ].
572dc11e2e96 added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 13150
diff changeset
   410
    ^ exceptionBlock value
572dc11e2e96 added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 13150
diff changeset
   411
572dc11e2e96 added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 13150
diff changeset
   412
    "Created: / 30-11-2010 / 13:38:25 / cg"
17274
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   413
!
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   414
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   415
removeLast
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   416
    "remove the last link element and return it;
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   417
     if empty, raise an exception."
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   418
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   419
    |nextNode thisNode|
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   420
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   421
    thisNode := firstLink.
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   422
    thisNode isNil ifTrue:[
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   423
        ^ self emptyCollectionError
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   424
    ].
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   425
    thisNode == lastLink ifTrue:[
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   426
        firstLink := lastLink := nil.
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   427
        numberOfNodes := 0.
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   428
        ^ thisNode
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   429
    ].
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   430
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   431
    [
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   432
        nextNode := thisNode nextLink.
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   433
        nextNode == lastLink ifTrue:[
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   434
            firstLink == lastLink ifTrue:[
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   435
                firstLink := thisNode
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   436
            ].
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   437
            lastLink := thisNode.
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   438
            numberOfNodes := numberOfNodes - 1.
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   439
            thisNode nextLink:nil.
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   440
            ^ nextNode.
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   441
        ].
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   442
        thisNode := nextNode
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   443
    ] loop.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   444
! !
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   445
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   446
!LinkedList methodsFor:'enumerating'!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   447
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   448
do:aBlock
19976
64c8185fd0de #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19975
diff changeset
   449
    "evaluate the argument, aBlock with 1 arg for every value element in the list"
17274
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   450
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   451
    |thisNode|
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   452
20231
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   453
    "aBlock may add elements, so do not use 'numberOfNodes-1 timesRepeat:[]'"
17274
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   454
    thisNode := firstLink.
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   455
    [thisNode notNil] whileTrue:[
19976
64c8185fd0de #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 19975
diff changeset
   456
        aBlock value:thisNode value.
17274
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   457
        thisNode := thisNode nextLink
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   458
    ]
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   459
!
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   460
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   461
linksDo:aBlock
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   462
    "evaluate the argument, aBlock with 1 arg for every link element in the list"
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   463
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   464
    |thisNode|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   465
20231
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   466
    "aBlock may add elements, so do not use 'numberOfNodes-1 timesRepeat:[]'"
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   467
    thisNode := firstLink.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   468
    [thisNode notNil] whileTrue:[
17274
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   469
        aBlock value:thisNode.
e28efb6e95e8 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17273
diff changeset
   470
        thisNode := thisNode nextLink
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   471
    ]
17406
87bc620b6ee0 class: LinkedList
Stefan Vogel <sv@exept.de>
parents: 17274
diff changeset
   472
!
87bc620b6ee0 class: LinkedList
Stefan Vogel <sv@exept.de>
parents: 17274
diff changeset
   473
87bc620b6ee0 class: LinkedList
Stefan Vogel <sv@exept.de>
parents: 17274
diff changeset
   474
printElementsDo:aBlock
87bc620b6ee0 class: LinkedList
Stefan Vogel <sv@exept.de>
parents: 17274
diff changeset
   475
    "perform aBlock (1 arg) for all elements.
87bc620b6ee0 class: LinkedList
Stefan Vogel <sv@exept.de>
parents: 17274
diff changeset
   476
     Used in #printOn:."
87bc620b6ee0 class: LinkedList
Stefan Vogel <sv@exept.de>
parents: 17274
diff changeset
   477
87bc620b6ee0 class: LinkedList
Stefan Vogel <sv@exept.de>
parents: 17274
diff changeset
   478
    ^ self linksDo:aBlock
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
5244
7ddbb129a552 category change
Claus Gittinger <cg@exept.de>
parents: 4300
diff changeset
   481
!LinkedList methodsFor:'initialization'!
606
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
initialize
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   484
    numberOfNodes := 0
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   485
! !
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   486
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   487
!LinkedList methodsFor:'queries'!
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   488
23711
2f0fb1e83275 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20636
diff changeset
   489
isEmpty
2f0fb1e83275 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20636
diff changeset
   490
    "return true, if the collection is empty"
2f0fb1e83275 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20636
diff changeset
   491
2f0fb1e83275 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20636
diff changeset
   492
    ^ firstLink isNil
2f0fb1e83275 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20636
diff changeset
   493
!
2f0fb1e83275 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20636
diff changeset
   494
2f0fb1e83275 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20636
diff changeset
   495
notEmpty
2f0fb1e83275 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20636
diff changeset
   496
    "return true, if the collection is not empty"
2f0fb1e83275 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20636
diff changeset
   497
2f0fb1e83275 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20636
diff changeset
   498
    ^ firstLink notNil
2f0fb1e83275 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20636
diff changeset
   499
!
2f0fb1e83275 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 20636
diff changeset
   500
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   501
size
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   502
    "return the size of the LinkedList i.e. the number of nodes"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   503
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   504
    ^ numberOfNodes
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   505
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   506
17209
7447f1b1e6c5 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17123
diff changeset
   507
!LinkedList methodsFor:'searching-equality'!
7447f1b1e6c5 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17123
diff changeset
   508
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   509
indexOf:aLinkOrValue startingAt:start
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   510
    "search the collection for aLinkOrValue, starting the search at index start;
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   511
     if found, return the index otherwise return 0. 
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   512
     Here, index is defined as the link-node's position in the list.
17575
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   513
     The comparison is done using = (i.e. equality test - not identity test).
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   514
     Warning: 
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   515
        it is a very bad idea to access LinkedList elements by index.
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   516
        many algorithms degenerate to poor performance if you do.
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   517
        This method is provided for protocol completeness,
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   518
        but please consider using another type of collection if you use it.
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   519
     "
17209
7447f1b1e6c5 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17123
diff changeset
   520
20231
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   521
    |theNode count "{ Class: SmallInteger }"
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   522
     linkOrValue|
17209
7447f1b1e6c5 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17123
diff changeset
   523
20231
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   524
    count := start.
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   525
    (count < 1 or:[count > numberOfNodes]) ifTrue:[
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   526
        ^ 0.
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   527
    ].
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   528
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   529
    theNode := firstLink.
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   530
    count-1 timesRepeat:[
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   531
        theNode isNil ifTrue:[^ 0].     "reached the end"
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   532
        theNode := theNode nextLink.
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   533
    ].
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   534
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   535
    linkOrValue := aLinkOrValue value.
20231
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   536
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   537
    [theNode notNil] whileTrue:[
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   538
        (linkOrValue = theNode value) ifTrue:[^ count].
17575
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   539
        theNode := theNode nextLink.
20231
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   540
        count := count + 1.
17209
7447f1b1e6c5 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17123
diff changeset
   541
    ].                                  "reached the end"
7447f1b1e6c5 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17123
diff changeset
   542
    ^ 0
7447f1b1e6c5 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17123
diff changeset
   543
7447f1b1e6c5 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17123
diff changeset
   544
    "
7447f1b1e6c5 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17123
diff changeset
   545
     |l|
7447f1b1e6c5 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17123
diff changeset
   546
7447f1b1e6c5 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17123
diff changeset
   547
     l := LinkedList new.
20231
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   548
     l indexOf:'hello' startingAt:1
17209
7447f1b1e6c5 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17123
diff changeset
   549
    "
7447f1b1e6c5 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17123
diff changeset
   550
7447f1b1e6c5 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17123
diff changeset
   551
    "
7447f1b1e6c5 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17123
diff changeset
   552
     |l v|
7447f1b1e6c5 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17123
diff changeset
   553
7447f1b1e6c5 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17123
diff changeset
   554
     l := LinkedList new.
7447f1b1e6c5 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17123
diff changeset
   555
     l add:(ValueLink new value:'one').
7447f1b1e6c5 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17123
diff changeset
   556
     l add:(ValueLink new value:'two').
7447f1b1e6c5 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17123
diff changeset
   557
     l add:(v := ValueLink new value:'hello').
20231
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   558
     l indexOf:v startingAt:2.
17209
7447f1b1e6c5 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17123
diff changeset
   559
    "
7447f1b1e6c5 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17123
diff changeset
   560
! !
7447f1b1e6c5 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17123
diff changeset
   561
7447f1b1e6c5 class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17123
diff changeset
   562
!LinkedList methodsFor:'searching-identity'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   563
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   564
identityIndexOf:aLinkOrValue startingAt:start
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   565
    "search the collection for aLinkOrValue, starting the search at index start;
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   566
     if found, return the index otherwise return 0. 
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   567
     Here, index is defined as the link-node's position in the list.
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   568
     The comparison is done using == (i.e. identity test - not equality test).
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   569
     Warning: 
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   570
        it is a very bad idea to access LinkedList elements by index.
17575
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   571
        many algorithms degenerate to poor performance if you do.
11ce80e0ee6b class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17406
diff changeset
   572
        This method is provided for protocol completeness,
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   573
        but please consider using another type of collection if you use it.
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   574
     "
243
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
   575
20231
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   576
    |theNode count "{ Class: SmallInteger }"
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   577
     linkOrValue|
243
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
   578
20231
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   579
    count := start.
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   580
    (count < 1 or:[count > numberOfNodes]) ifTrue:[
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   581
        ^ 0.
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   582
    ].
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   583
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   584
    theNode := firstLink.
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   585
    count-1 timesRepeat:[
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   586
        theNode isNil ifTrue:[^ 0].     "reached the end"
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   587
        theNode := theNode nextLink.
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   588
    ].
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   589
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   590
    linkOrValue := aLinkOrValue value.
20231
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   591
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   592
    [theNode notNil] whileTrue:[
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   593
        (linkOrValue == theNode value) ifTrue:[^ count].
13150
e20625aa25a7 comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents: 10647
diff changeset
   594
        theNode := theNode nextLink.
20231
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   595
        count := count + 1.
243
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
   596
    ].                                  "reached the end"
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
   597
    ^ 0
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
   598
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
   599
    "
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
   600
     |l|
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
   601
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
   602
     l := LinkedList new.
20231
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   603
     l identityIndexOf:'hello' startingAt:1
243
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
   604
    "
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
   605
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
   606
    "
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
   607
     |l v|
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
   608
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
   609
     l := LinkedList new.
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
   610
     l add:(ValueLink new value:'one').
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
   611
     l add:(ValueLink new value:'two').
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
   612
     l add:(v := ValueLink new value:'hello').
20231
27fdecf3cd79 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 19976
diff changeset
   613
     l identityIndexOf:v startingAt:2.
243
1b7ab889a45f tuning a little bit
claus
parents: 241
diff changeset
   614
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   615
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   616
17210
a43dc2b020bc class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17209
diff changeset
   617
!LinkedList methodsFor:'testing'!
a43dc2b020bc class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17209
diff changeset
   618
a43dc2b020bc class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17209
diff changeset
   619
isFixedSize
a43dc2b020bc class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17209
diff changeset
   620
    "return true if the receiver cannot grow"
a43dc2b020bc class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17209
diff changeset
   621
a43dc2b020bc class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17209
diff changeset
   622
    ^ false
a43dc2b020bc class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17209
diff changeset
   623
! !
a43dc2b020bc class: LinkedList
Claus Gittinger <cg@exept.de>
parents: 17209
diff changeset
   624
629
2ceefe9b5a19 version at the end
Claus Gittinger <cg@exept.de>
parents: 606
diff changeset
   625
!LinkedList class methodsFor:'documentation'!
2ceefe9b5a19 version at the end
Claus Gittinger <cg@exept.de>
parents: 606
diff changeset
   626
2ceefe9b5a19 version at the end
Claus Gittinger <cg@exept.de>
parents: 606
diff changeset
   627
version
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   628
    ^ '$Header$'
13150
e20625aa25a7 comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents: 10647
diff changeset
   629
!
e20625aa25a7 comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents: 10647
diff changeset
   630
e20625aa25a7 comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents: 10647
diff changeset
   631
version_CVS
19970
b37f85226106 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 17575
diff changeset
   632
    ^ '$Header$'
629
2ceefe9b5a19 version at the end
Claus Gittinger <cg@exept.de>
parents: 606
diff changeset
   633
! !
15428
8a9d687ee50a added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 13727
diff changeset
   634