Queue.st
author claus
Fri, 12 May 1995 14:35:26 +0200
changeset 74 79b7f6a5bbce
parent 50 983d862738c1
child 75 a7bf47d849af
permissions -rw-r--r--
.

"
 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

$Header: /cvs/stx/stx/libbasic2/Queue.st,v 1.8 1995-05-12 12:35:19 claus Exp $
'!

!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.8 1995-05-12 12:35:19 claus Exp $
"
!

documentation
"
    Queues provides a simple implementation of a queue, where
    elements are enterred at one end and removed at the other.
    Access protocol is somewhat like a streams protocol, i.e. access
    is by #nextPut: and #next.
    The queue is created with a size argument, defining how many elements
    are to be stored. It will report an error if the queue ever becomes full
    and another element is to be added. Likewise, it will report an error
    if its empty and an element is to be removed.

    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 which is safe with processes and blocks
    on write when full or on read when empty.
"
! !

!Queue class methodsFor:'instance creation'!

new:size
    "return a new queue with space for size elements"

    ^ super new init:size

    "
     |q|

     q := Queue new.
     (1 to:5) do:[:i | q nextPut:i].
     Transcript show:(q next); space.
     Transcript show:(q next); space.
     Transcript show:(q next); space.
     Transcript show:(q next); space.
     q nextPutAll:(6 to:10).
     Transcript show:(q next); space.
     Transcript show:(q next); space.
     Transcript show:(q next); space.
     Transcript show:(q next); space.
     Transcript show:(q next); space.
     Transcript show:(q next); space.
     Transcript show:(q next); space.
     Transcript show:(q next); space.
     Transcript show:(q next); space.
     Transcript showCr:(q next).
    "
!

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; 
     Return nil, if the queue is empty"

    |value pos "{ Class: SmallInteger }"|

    (tally == 0) ifTrue:[^ nil].

    pos := readPosition.

    value := contentsArray at:pos.
    pos := pos + 1.
    pos > contentsArray size ifTrue:[pos := 1].
    readPosition := pos.
    tally := tally - 1.
    ^ value
!

nextPut:anObject
    "enter anObject into the queue - if the queue is full, report an error"

    |sz pos "{ Class: SmallInteger }" |

    sz := contentsArray size.
    pos := writePosition.

    (tally == sz) ifTrue:[
	self error:'queue is full'.
	^ self
    ].

    contentsArray at:pos put:anObject.
    pos := pos + 1.
    pos > sz ifTrue:[pos := 1].
    writePosition := pos.
    tally := tally + 1
!

nextPutAll:aCollection
    "enter all elements from aCollection into the queue."

    aCollection do:[:element | self nextPut:element]
! !

!Queue methodsFor:'enumerating'!

do:aBlock
    "evaluate the argument, aBlock for each element in the queue"

    |pos endPos|

    pos := readPosition.
    endPos := writePosition.
    1 to:tally do:[:i |
	aBlock value:(contentsArray at:pos).
	pos := pos + 1.
	pos > contentsArray size ifTrue:[
	    pos := 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
! !