diff --git a/src/main/scala/pubsub/collection/BoundedBuffer.scala b/src/main/scala/pubsub/collection/BoundedBuffer.scala index 26b69be..e7ea418 100644 --- a/src/main/scala/pubsub/collection/BoundedBuffer.scala +++ b/src/main/scala/pubsub/collection/BoundedBuffer.scala @@ -15,9 +15,32 @@ class BoundedBuffer[T](size: Int) extends AbstractBoundedBuffer[T](size) { // You do not need to create those variables yourself! // They are inherited from the AbstractBoundedBuffer class. - override def put(e: T): Unit = ??? + override def put(e: T): Unit = synchronized{ + while(isFull) wait() + buffer(head) = e + head = nextHeadIndex; + count = count + 1; + notifyAll() + } - override def take(): T = ??? + override def take(): T = synchronized{ + while(isEmpty) wait() + + val e = buffer(tailIndex); + buffer.delete(tailIndex); + count = count - 1; + notifyAll() + e + } + + def isEmpty : Boolean = count == 0; + def isFull : Boolean = count == size + def tailIndex : Int = { + val diff = head - count + if(diff >= 0) diff + else diff + size + } + def nextHeadIndex : Int = (head + 1) % size // You may want to add methods to: // - check whether the buffer is empty