5
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
Object subclass:#Queue
|
|
14 |
instanceVariableNames:'contentsArray readPosition writePosition tally'
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Ordered'
|
|
18 |
!
|
|
19 |
|
|
20 |
Queue comment:'
|
|
21 |
Queues provides a simple implementation of a queue - it is not
|
|
22 |
safe when two processes access Queues simultanously - see SharedQueue
|
|
23 |
for this.
|
|
24 |
|
|
25 |
$Header: /cvs/stx/stx/libbasic2/Queue.st,v 1.1 1993-11-08 02:31:40 claus Exp $
|
|
26 |
'!
|
|
27 |
|
|
28 |
!Queue class methodsFor:'instance creation'!
|
|
29 |
|
|
30 |
new:size
|
|
31 |
"return a new queue with space for size elements"
|
|
32 |
|
|
33 |
^ super new init:size
|
|
34 |
|
|
35 |
"
|
|
36 |
|q|
|
|
37 |
|
|
38 |
q := Queue new.
|
|
39 |
q nextPut:1.
|
|
40 |
q nextPut:2.
|
|
41 |
q nextPut:3.
|
|
42 |
q nextPut:4.
|
|
43 |
q nextPut:5.
|
|
44 |
Transcript show:(q next) printString; space.
|
|
45 |
Transcript show:(q next) printString; space.
|
|
46 |
Transcript show:(q next) printString; space.
|
|
47 |
Transcript show:(q next) printString; space.
|
|
48 |
q nextPut:6.
|
|
49 |
q nextPut:7.
|
|
50 |
q nextPut:8.
|
|
51 |
q nextPut:9.
|
|
52 |
q nextPut:10.
|
|
53 |
Transcript show:(q next) printString; space.
|
|
54 |
Transcript show:(q next) printString; space.
|
|
55 |
Transcript show:(q next) printString; space.
|
|
56 |
Transcript show:(q next) printString; space.
|
|
57 |
Transcript show:(q next) printString; space.
|
|
58 |
Transcript show:(q next) printString; space.
|
|
59 |
Transcript show:(q next) printString; space.
|
|
60 |
Transcript show:(q next) printString; space.
|
|
61 |
Transcript show:(q next) printString; space.
|
|
62 |
Transcript show:(q next) printString; space.
|
|
63 |
"
|
|
64 |
!
|
|
65 |
|
|
66 |
new
|
|
67 |
"return a new queue with space for 10 elements"
|
|
68 |
|
|
69 |
^ self new:10
|
|
70 |
! !
|
|
71 |
|
|
72 |
!Queue methodsFor:'initialization'!
|
|
73 |
|
|
74 |
init:size
|
|
75 |
contentsArray := Array new:size.
|
|
76 |
readPosition := writePosition := 1.
|
|
77 |
tally := 0.
|
|
78 |
! !
|
|
79 |
|
|
80 |
!Queue methodsFor:'accessing'!
|
|
81 |
|
|
82 |
next
|
|
83 |
"return the next value in the queue; if it its empty, return nil"
|
|
84 |
|
|
85 |
|value|
|
|
86 |
|
|
87 |
(tally == 0) ifTrue:[^ nil].
|
|
88 |
value := contentsArray at:readPosition.
|
|
89 |
readPosition := readPosition + 1.
|
|
90 |
readPosition > contentsArray size ifTrue:[
|
|
91 |
readPosition := 1
|
|
92 |
].
|
|
93 |
tally := tally - 1.
|
|
94 |
^ value
|
|
95 |
!
|
|
96 |
|
|
97 |
nextPut:anObject
|
|
98 |
"enter anObject into the queue - if the queue is full, report an error"
|
|
99 |
|
|
100 |
(tally == contentsArray size) ifTrue:[
|
|
101 |
self error:'queue is full'.
|
|
102 |
^ self
|
|
103 |
].
|
|
104 |
contentsArray at:writePosition put:anObject.
|
|
105 |
writePosition := writePosition + 1.
|
|
106 |
writePosition > contentsArray size ifTrue:[
|
|
107 |
writePosition := 1
|
|
108 |
].
|
|
109 |
tally := tally + 1
|
|
110 |
! !
|
|
111 |
|
|
112 |
!Queue methodsFor:'queries'!
|
|
113 |
|
|
114 |
size
|
|
115 |
"return the number of elements in the queue"
|
|
116 |
|
|
117 |
^ tally
|
|
118 |
! !
|