-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added circular buffer * Updated config.json * Added tests and solution for Circular Buffer exercise * Updated tests, metadata, docs for circular buffer exercise * Fixed missing name attribute for circular buffer in config.json * Moved solution file to .meta directory for circular buffer * simplified overwrite method on circular buffer * updated example solution file * updated UUID to version 4 of circular buffer exercise * updated UUD in config and exercise dir
- Loading branch information
Showing
13 changed files
with
962 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
" | ||
I represent circular buffer - a data structure that uses a single, fixed-size buffer as if it were connected end-to-end. I support operations like read, write, overwrite, clear. | ||
" | ||
Class { | ||
#name : #CircularBuffer, | ||
#superclass : #Object, | ||
#instVars : [ | ||
'buffer', | ||
'capacity' | ||
], | ||
#category : #'Exercise@CircularBuffer' | ||
} | ||
|
||
{ #category : #private } | ||
CircularBuffer >> buffer [ | ||
|
||
^ buffer | ||
] | ||
|
||
{ #category : #private } | ||
CircularBuffer >> buffer: anObject [ | ||
|
||
buffer := anObject | ||
] | ||
|
||
{ #category : #private } | ||
CircularBuffer >> bufferFull [ | ||
|
||
^ self buffer size >= self capacity | ||
] | ||
|
||
{ #category : #accessing } | ||
CircularBuffer >> capacity [ | ||
|
||
^ capacity | ||
] | ||
|
||
{ #category : #accessing } | ||
CircularBuffer >> capacity: anObject [ | ||
|
||
capacity := anObject | ||
] | ||
|
||
{ #category : #operations } | ||
CircularBuffer >> clear [ | ||
|
||
self buffer removeAll | ||
] | ||
|
||
{ #category : #initialization } | ||
CircularBuffer >> initialize [ | ||
|
||
super initialize. | ||
self buffer: OrderedCollection new | ||
] | ||
|
||
{ #category : #operations } | ||
CircularBuffer >> overwrite: aValue [ | ||
|
||
self bufferFull ifTrue: [ self buffer removeFirst ]. | ||
^ self write: aValue | ||
] | ||
|
||
{ #category : #'error signalling' } | ||
CircularBuffer >> raiseEmptyBufferOnRead [ | ||
|
||
^ Error signal: 'Cannot read from empty buffer!' | ||
] | ||
|
||
{ #category : #'error signalling' } | ||
CircularBuffer >> raiseFullBufferOnWrite [ | ||
|
||
^ Error signal: 'Cannot write to full buffer!' | ||
] | ||
|
||
{ #category : #operations } | ||
CircularBuffer >> read [ | ||
|
||
self buffer ifEmpty: [ ^ self raiseEmptyBufferOnRead ]. | ||
^ self buffer removeFirst | ||
|
||
] | ||
|
||
{ #category : #operations } | ||
CircularBuffer >> write: aValue [ | ||
|
||
self bufferFull ifTrue: [ ^ self raiseFullBufferOnWrite ]. | ||
^ self buffer addLast: aValue. | ||
] |
Oops, something went wrong.