Queue.st
changeset 122 c379960395f6
parent 112 3e18f2cfe430
child 131 19e548711b65
equal deleted inserted replaced
121:606a3ccd3682 122:c379960395f6
    29  inclusion of the above copyright notice.   This software may not
    29  inclusion of the above copyright notice.   This software may not
    30  be provided or otherwise made available to, or used by, any
    30  be provided or otherwise made available to, or used by, any
    31  other person.  No title to or ownership of the software is
    31  other person.  No title to or ownership of the software is
    32  hereby transferred.
    32  hereby transferred.
    33 "
    33 "
    34 !
       
    35 
       
    36 version
       
    37     ^ '$Header: /cvs/stx/stx/libbasic2/Queue.st,v 1.13 1995-11-11 15:21:32 cg Exp $'
       
    38 !
    34 !
    39 
    35 
    40 documentation
    36 documentation
    41 "
    37 "
    42     Queues provides a simple implementation of a queue, where
    38     Queues provides a simple implementation of a queue, where
    52     since accesses to the internals are not protected against process-switches.
    48     since accesses to the internals are not protected against process-switches.
    53 
    49 
    54     See SharedQueue for a class which is safe with processes and blocks
    50     See SharedQueue for a class which is safe with processes and blocks
    55     on write when full or on read when empty.
    51     on write when full or on read when empty.
    56 "
    52 "
       
    53 !
       
    54 
       
    55 version
       
    56     ^ '$Header: /cvs/stx/stx/libbasic2/Queue.st,v 1.14 1995-11-23 01:17:16 cg Exp $'
    57 ! !
    57 ! !
    58 
    58 
    59 !Queue class methodsFor:'instance creation'!
    59 !Queue class methodsFor:'instance creation'!
       
    60 
       
    61 new
       
    62     "return a new queue with space for some elements"
       
    63 
       
    64     ^ self new:50
       
    65 !
    60 
    66 
    61 new:size
    67 new:size
    62     "return a new queue with space for size elements"
    68     "return a new queue with space for size elements"
    63 
    69 
    64     ^ super new init:size
    70     ^ super new init:size
    82      Transcript show:(q next); space.
    88      Transcript show:(q next); space.
    83      Transcript show:(q next); space.
    89      Transcript show:(q next); space.
    84      Transcript show:(q next); space.
    90      Transcript show:(q next); space.
    85      Transcript showCr:(q next).
    91      Transcript showCr:(q next).
    86     "
    92     "
    87 !
       
    88 
       
    89 new
       
    90     "return a new queue with space for some elements"
       
    91 
       
    92     ^ self new:50
       
    93 ! !
       
    94 
       
    95 !Queue methodsFor:'initialization'!
       
    96 
       
    97 init:size
       
    98     "initialize the receiver for size entries"
       
    99 
       
   100     contentsArray := Array new:size.
       
   101     readPosition := writePosition := 1.
       
   102     tally := 0.
       
   103 ! !
    93 ! !
   104 
    94 
   105 !Queue methodsFor:'accessing'!
    95 !Queue methodsFor:'accessing'!
   106 
       
   107 peek
       
   108     "return the next value in the queue without removing it.
       
   109      If the queue is empty, return nil."
       
   110 
       
   111     (tally == 0) ifTrue:[^ nil].
       
   112     ^ contentsArray at:readPosition.
       
   113 !
       
   114 
    96 
   115 next
    97 next
   116     "return the next value in the queue; 
    98     "return the next value in the queue; 
   117      Return nil, if the queue is empty"
    99      Return nil, if the queue is empty"
   118 
   100 
   152 
   134 
   153 nextPutAll:aCollection
   135 nextPutAll:aCollection
   154     "enter all elements from aCollection into the queue."
   136     "enter all elements from aCollection into the queue."
   155 
   137 
   156     aCollection do:[:element | self nextPut:element]
   138     aCollection do:[:element | self nextPut:element]
       
   139 !
       
   140 
       
   141 peek
       
   142     "return the next value in the queue without removing it.
       
   143      If the queue is empty, return nil."
       
   144 
       
   145     (tally == 0) ifTrue:[^ nil].
       
   146     ^ contentsArray at:readPosition.
   157 ! !
   147 ! !
   158 
   148 
   159 !Queue methodsFor:'enumerating'!
   149 !Queue methodsFor:'enumerating'!
   160 
   150 
   161 do:aBlock
   151 do:aBlock
   172 	    pos := 1
   162 	    pos := 1
   173 	]
   163 	]
   174     ]
   164     ]
   175 ! !
   165 ! !
   176 
   166 
       
   167 !Queue methodsFor:'initialization'!
       
   168 
       
   169 init:size
       
   170     "initialize the receiver for size entries"
       
   171 
       
   172     contentsArray := Array new:size.
       
   173     readPosition := writePosition := 1.
       
   174     tally := 0.
       
   175 ! !
       
   176 
   177 !Queue methodsFor:'queries'!
   177 !Queue methodsFor:'queries'!
       
   178 
       
   179 capacity 
       
   180     "return the number of elements the queue can hold"
       
   181 
       
   182     ^ contentsArray size
       
   183 !
   178 
   184 
   179 isEmpty
   185 isEmpty
   180     "return true, if there are no elements in the queue"
   186     "return true, if there are no elements in the queue"
   181 
   187 
   182     ^ tally == 0
   188     ^ tally == 0
   191 
   197 
   192 size
   198 size
   193     "return the number of elements in the queue"
   199     "return the number of elements in the queue"
   194 
   200 
   195     ^ tally
   201     ^ tally
   196 !
   202 ! !
   197 
   203 
   198 capacity 
       
   199     "return the number of elements the queue can hold"
       
   200 
       
   201     ^ contentsArray size
       
   202 ! !