Queue.st
author Claus Gittinger <cg@exept.de>
Thu, 25 Apr 1996 18:18:33 +0200
changeset 254 cccfa2590e6e
parent 131 19e548711b65
child 350 93d5932c76e6
permissions -rw-r--r--
documentation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5
78a5b7c73feb Initial revision
claus
parents:
diff changeset
     1
"
78a5b7c73feb Initial revision
claus
parents:
diff changeset
     2
 COPYRIGHT (c) 1993 by Claus Gittinger
47
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
     3
	      All Rights Reserved
5
78a5b7c73feb Initial revision
claus
parents:
diff changeset
     4
78a5b7c73feb Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
78a5b7c73feb Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
78a5b7c73feb Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
78a5b7c73feb Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
78a5b7c73feb Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
78a5b7c73feb Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
78a5b7c73feb Initial revision
claus
parents:
diff changeset
    11
"
78a5b7c73feb Initial revision
claus
parents:
diff changeset
    12
30
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    13
Collection subclass:#Queue
254
cccfa2590e6e documentation
Claus Gittinger <cg@exept.de>
parents: 131
diff changeset
    14
	instanceVariableNames:'contentsArray readPosition writePosition tally'
cccfa2590e6e documentation
Claus Gittinger <cg@exept.de>
parents: 131
diff changeset
    15
	classVariableNames:''
cccfa2590e6e documentation
Claus Gittinger <cg@exept.de>
parents: 131
diff changeset
    16
	poolDictionaries:''
cccfa2590e6e documentation
Claus Gittinger <cg@exept.de>
parents: 131
diff changeset
    17
	category:'Collections-Ordered'
5
78a5b7c73feb Initial revision
claus
parents:
diff changeset
    18
!
78a5b7c73feb Initial revision
claus
parents:
diff changeset
    19
30
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    20
!Queue class methodsFor:'documentation'!
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    21
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    22
copyright
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    23
"
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    24
 COPYRIGHT (c) 1993 by Claus Gittinger
47
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
    25
	      All Rights Reserved
5
78a5b7c73feb Initial revision
claus
parents:
diff changeset
    26
30
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    27
 This software is furnished under a license and may be used
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    28
 only in accordance with the terms of that license and with the
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    29
 inclusion of the above copyright notice.   This software may not
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    30
 be provided or otherwise made available to, or used by, any
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    31
 other person.  No title to or ownership of the software is
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    32
 hereby transferred.
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    33
"
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    34
!
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    35
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    36
documentation
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    37
"
74
claus
parents: 50
diff changeset
    38
    Queues provides a simple implementation of a queue, where
claus
parents: 50
diff changeset
    39
    elements are enterred at one end and removed at the other.
claus
parents: 50
diff changeset
    40
    Access protocol is somewhat like a streams protocol, i.e. access
claus
parents: 50
diff changeset
    41
    is by #nextPut: and #next.
claus
parents: 50
diff changeset
    42
    The queue is created with a size argument, defining how many elements
claus
parents: 50
diff changeset
    43
    are to be stored. It will report an error if the queue ever becomes full
claus
parents: 50
diff changeset
    44
    and another element is to be added. Likewise, it will report an error
claus
parents: 50
diff changeset
    45
    if its empty and an element is to be removed.
claus
parents: 50
diff changeset
    46
claus
parents: 50
diff changeset
    47
    It is not safe when two processes access Queues simultanously,
claus
parents: 50
diff changeset
    48
    since accesses to the internals are not protected against process-switches.
claus
parents: 50
diff changeset
    49
claus
parents: 50
diff changeset
    50
    See SharedQueue for a class which is safe with processes and blocks
claus
parents: 50
diff changeset
    51
    on write when full or on read when empty.
254
cccfa2590e6e documentation
Claus Gittinger <cg@exept.de>
parents: 131
diff changeset
    52
cccfa2590e6e documentation
Claus Gittinger <cg@exept.de>
parents: 131
diff changeset
    53
    [author:]
cccfa2590e6e documentation
Claus Gittinger <cg@exept.de>
parents: 131
diff changeset
    54
        Claus Gittinger
30
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    55
"
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
    56
! !
5
78a5b7c73feb Initial revision
claus
parents:
diff changeset
    57
78a5b7c73feb Initial revision
claus
parents:
diff changeset
    58
!Queue class methodsFor:'instance creation'!
78a5b7c73feb Initial revision
claus
parents:
diff changeset
    59
122
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
    60
new
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
    61
    "return a new queue with space for some elements"
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
    62
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
    63
    ^ self new:50
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
    64
!
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
    65
5
78a5b7c73feb Initial revision
claus
parents:
diff changeset
    66
new:size
78a5b7c73feb Initial revision
claus
parents:
diff changeset
    67
    "return a new queue with space for size elements"
78a5b7c73feb Initial revision
claus
parents:
diff changeset
    68
78a5b7c73feb Initial revision
claus
parents:
diff changeset
    69
    ^ super new init:size
78a5b7c73feb Initial revision
claus
parents:
diff changeset
    70
78a5b7c73feb Initial revision
claus
parents:
diff changeset
    71
    "
78a5b7c73feb Initial revision
claus
parents:
diff changeset
    72
     |q|
78a5b7c73feb Initial revision
claus
parents:
diff changeset
    73
78a5b7c73feb Initial revision
claus
parents:
diff changeset
    74
     q := Queue new.
39
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
    75
     (1 to:5) do:[:i | q nextPut:i].
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
    76
     Transcript show:(q next); space.
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
    77
     Transcript show:(q next); space.
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
    78
     Transcript show:(q next); space.
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
    79
     Transcript show:(q next); space.
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
    80
     q nextPutAll:(6 to:10).
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
    81
     Transcript show:(q next); space.
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
    82
     Transcript show:(q next); space.
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
    83
     Transcript show:(q next); space.
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
    84
     Transcript show:(q next); space.
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
    85
     Transcript show:(q next); space.
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
    86
     Transcript show:(q next); space.
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
    87
     Transcript show:(q next); space.
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
    88
     Transcript show:(q next); space.
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
    89
     Transcript show:(q next); space.
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
    90
     Transcript showCr:(q next).
5
78a5b7c73feb Initial revision
claus
parents:
diff changeset
    91
    "
78a5b7c73feb Initial revision
claus
parents:
diff changeset
    92
! !
78a5b7c73feb Initial revision
claus
parents:
diff changeset
    93
78a5b7c73feb Initial revision
claus
parents:
diff changeset
    94
!Queue methodsFor:'accessing'!
78a5b7c73feb Initial revision
claus
parents:
diff changeset
    95
78a5b7c73feb Initial revision
claus
parents:
diff changeset
    96
next
39
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
    97
    "return the next value in the queue; 
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
    98
     Return nil, if the queue is empty"
5
78a5b7c73feb Initial revision
claus
parents:
diff changeset
    99
47
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   100
    |value pos "{ Class: SmallInteger }"|
5
78a5b7c73feb Initial revision
claus
parents:
diff changeset
   101
78a5b7c73feb Initial revision
claus
parents:
diff changeset
   102
    (tally == 0) ifTrue:[^ nil].
47
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   103
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   104
    pos := readPosition.
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   105
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   106
    value := contentsArray at:pos.
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   107
    pos := pos + 1.
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   108
    pos > contentsArray size ifTrue:[pos := 1].
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   109
    readPosition := pos.
5
78a5b7c73feb Initial revision
claus
parents:
diff changeset
   110
    tally := tally - 1.
78a5b7c73feb Initial revision
claus
parents:
diff changeset
   111
    ^ value
78a5b7c73feb Initial revision
claus
parents:
diff changeset
   112
!
78a5b7c73feb Initial revision
claus
parents:
diff changeset
   113
78a5b7c73feb Initial revision
claus
parents:
diff changeset
   114
nextPut:anObject
78a5b7c73feb Initial revision
claus
parents:
diff changeset
   115
    "enter anObject into the queue - if the queue is full, report an error"
78a5b7c73feb Initial revision
claus
parents:
diff changeset
   116
47
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   117
    |sz pos "{ Class: SmallInteger }" |
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   118
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   119
    sz := contentsArray size.
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   120
    pos := writePosition.
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   121
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   122
    (tally == sz) ifTrue:[
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   123
	self error:'queue is full'.
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   124
	^ self
5
78a5b7c73feb Initial revision
claus
parents:
diff changeset
   125
    ].
47
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   126
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   127
    contentsArray at:pos put:anObject.
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   128
    pos := pos + 1.
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   129
    pos > sz ifTrue:[pos := 1].
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   130
    writePosition := pos.
5
78a5b7c73feb Initial revision
claus
parents:
diff changeset
   131
    tally := tally + 1
39
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
   132
!
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
   133
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
   134
nextPutAll:aCollection
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
   135
    "enter all elements from aCollection into the queue."
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
   136
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
   137
    aCollection do:[:element | self nextPut:element]
122
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   138
!
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   139
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   140
peek
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   141
    "return the next value in the queue without removing it.
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   142
     If the queue is empty, return nil."
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   143
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   144
    (tally == 0) ifTrue:[^ nil].
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   145
    ^ contentsArray at:readPosition.
39
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
   146
! !
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
   147
50
983d862738c1 *** empty log message ***
claus
parents: 47
diff changeset
   148
!Queue methodsFor:'enumerating'!
39
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
   149
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
   150
do:aBlock
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
   151
    "evaluate the argument, aBlock for each element in the queue"
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
   152
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
   153
    |pos endPos|
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
   154
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
   155
    pos := readPosition.
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
   156
    endPos := writePosition.
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
   157
    1 to:tally do:[:i |
47
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   158
	aBlock value:(contentsArray at:pos).
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   159
	pos := pos + 1.
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   160
	pos > contentsArray size ifTrue:[
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   161
	    pos := 1
2fc2796fbec8 *** empty log message ***
claus
parents: 39
diff changeset
   162
	]
39
47fc4acc24db added do:, nextPutAll:
claus
parents: 33
diff changeset
   163
    ]
5
78a5b7c73feb Initial revision
claus
parents:
diff changeset
   164
! !
78a5b7c73feb Initial revision
claus
parents:
diff changeset
   165
122
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   166
!Queue methodsFor:'initialization'!
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   167
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   168
init:size
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   169
    "initialize the receiver for size entries"
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   170
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   171
    contentsArray := Array new:size.
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   172
    readPosition := writePosition := 1.
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   173
    tally := 0.
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   174
! !
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   175
5
78a5b7c73feb Initial revision
claus
parents:
diff changeset
   176
!Queue methodsFor:'queries'!
78a5b7c73feb Initial revision
claus
parents:
diff changeset
   177
122
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   178
capacity 
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   179
    "return the number of elements the queue can hold"
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   180
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   181
    ^ contentsArray size
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   182
!
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   183
30
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
   184
isEmpty
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
   185
    "return true, if there are no elements in the queue"
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
   186
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
   187
    ^ tally == 0
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
   188
!
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
   189
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
   190
isFull
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
   191
    "return true, if the queue is full i.e. if writing is not
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
   192
     possible"
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
   193
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
   194
    ^ tally == contentsArray size
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
   195
!
f34b335ac2d7 *** empty log message ***
claus
parents: 14
diff changeset
   196
5
78a5b7c73feb Initial revision
claus
parents:
diff changeset
   197
size
78a5b7c73feb Initial revision
claus
parents:
diff changeset
   198
    "return the number of elements in the queue"
78a5b7c73feb Initial revision
claus
parents:
diff changeset
   199
78a5b7c73feb Initial revision
claus
parents:
diff changeset
   200
    ^ tally
122
c379960395f6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 112
diff changeset
   201
! !
81
claus
parents: 75
diff changeset
   202
131
19e548711b65 version at the end
Claus Gittinger <cg@exept.de>
parents: 122
diff changeset
   203
!Queue class methodsFor:'documentation'!
19e548711b65 version at the end
Claus Gittinger <cg@exept.de>
parents: 122
diff changeset
   204
19e548711b65 version at the end
Claus Gittinger <cg@exept.de>
parents: 122
diff changeset
   205
version
254
cccfa2590e6e documentation
Claus Gittinger <cg@exept.de>
parents: 131
diff changeset
   206
    ^ '$Header: /cvs/stx/stx/libbasic2/Queue.st,v 1.16 1996-04-25 16:15:14 cg Exp $'
131
19e548711b65 version at the end
Claus Gittinger <cg@exept.de>
parents: 122
diff changeset
   207
! !