"
COPYRIGHT (c) 1993 by Claus Gittinger
All Rights Reserved
This software is furnished under a license and may be used
only in accordance with the terms of that license and with the
inclusion of the above copyright notice. This software may not
be provided or otherwise made available to, or used by, any
other person. No title to or ownership of the software is
hereby transferred.
"
Collection subclass:#Queue
instanceVariableNames:'contentsArray readPosition writePosition tally'
classVariableNames:''
poolDictionaries:''
category:'Collections-Ordered'
!
Queue comment:'
COPYRIGHT (c) 1993 by Claus Gittinger
All Rights Reserved
'!
!Queue class methodsFor:'documentation'!
copyright
"
COPYRIGHT (c) 1993 by Claus Gittinger
All Rights Reserved
This software is furnished under a license and may be used
only in accordance with the terms of that license and with the
inclusion of the above copyright notice. This software may not
be provided or otherwise made available to, or used by, any
other person. No title to or ownership of the software is
hereby transferred.
"
!
version
"
$Header: /cvs/stx/stx/libbasic2/Queue.st,v 1.3 1994-06-02 16:21:28 claus Exp $
"
!
documentation
"
Queues provides a simple implementation of a queue - it is not
safe when two processes access Queues simultanously since accesses
to the internals are not protected against process-switches.
See SharedQueue for a class supporting this.
"
! !
!Queue class methodsFor:'instance creation'!
new:size
"return a new queue with space for size elements"
^ super new init:size
"
|q|
q := Queue new.
q nextPut:1.
q nextPut:2.
q nextPut:3.
q nextPut:4.
q nextPut:5.
Transcript show:(q next) printString; space.
Transcript show:(q next) printString; space.
Transcript show:(q next) printString; space.
Transcript show:(q next) printString; space.
q nextPut:6.
q nextPut:7.
q nextPut:8.
q nextPut:9.
q nextPut:10.
Transcript show:(q next) printString; space.
Transcript show:(q next) printString; space.
Transcript show:(q next) printString; space.
Transcript show:(q next) printString; space.
Transcript show:(q next) printString; space.
Transcript show:(q next) printString; space.
Transcript show:(q next) printString; space.
Transcript show:(q next) printString; space.
Transcript show:(q next) printString; space.
Transcript show:(q next) printString; space.
"
!
new
"return a new queue with space for 10 elements"
^ self new:10
! !
!Queue methodsFor:'initialization'!
init:size
"initialize the receiver for size entries"
contentsArray := Array new:size.
readPosition := writePosition := 1.
tally := 0.
! !
!Queue methodsFor:'accessing'!
next
"return the next value in the queue; if it its empty, return nil"
|value|
(tally == 0) ifTrue:[^ nil].
value := contentsArray at:readPosition.
readPosition := readPosition + 1.
readPosition > contentsArray size ifTrue:[
readPosition := 1
].
tally := tally - 1.
^ value
!
nextPut:anObject
"enter anObject into the queue - if the queue is full, report an error"
(tally == contentsArray size) ifTrue:[
self error:'queue is full'.
^ self
].
contentsArray at:writePosition put:anObject.
writePosition := writePosition + 1.
writePosition > contentsArray size ifTrue:[
writePosition := 1
].
tally := tally + 1
! !
!Queue methodsFor:'queries'!
isEmpty
"return true, if there are no elements in the queue"
^ tally == 0
!
isFull
"return true, if the queue is full i.e. if writing is not
possible"
^ tally == contentsArray size
!
size
"return the number of elements in the queue"
^ tally
! !