Skip to content

Commit 40a61f2

Browse files
authored
Merge pull request #8 from Alokzh/fix/api-naming-conventions
Fix API to match OrderedCollection conventions for easy drop-in replacement
2 parents 525d40a + 8c23095 commit 40a61f2

8 files changed

Lines changed: 85 additions & 66 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ This example demonstrates how to use a FIFO buffer to maintain a chat history, a
5050

5151
```smalltalk
5252
"Chat room - keep last 50 messages automatically"
53-
chat := CTFIFOBuffer withCapacity: 50.
53+
chat := CTFIFOBuffer new: 50.
5454
5555
chat push: 'User1: Hello everyone!'.
5656
chat push: 'User2: How are you doing?'.
@@ -68,7 +68,7 @@ This example shows how to implement undo/redo functionality in an editor using a
6868

6969
```smalltalk
7070
"Implementing undo/redo in an editor"
71-
undoBuffer := CTLIFOBuffer withCapacity: 20. "Keep last 20 actions"
71+
undoBuffer := CTLIFOBuffer new: 20. "Keep last 20 actions"
7272
undoBuffer push: 'Hello World'.
7373
undoBuffer push: 'Add bold formatting'.
7474
undoBuffer push: 'Add italic formatting'.
@@ -86,7 +86,7 @@ This example demonstrates how to implement a simple browser history using a LIFO
8686

8787
```smalltalk
8888
"Browser back button - show most recent pages first"
89-
browserHistory := CTLIFOBuffer withCapacity: 20.
89+
browserHistory := CTLIFOBuffer new: 20.
9090
9191
browserHistory push: 'https://pharo.org'.
9292
browserHistory push: 'https://github.com/pharo-containers'.
@@ -111,7 +111,7 @@ log := OrderedCollection new.
111111
].
112112
113113
"Buffer - Fast and automatic"
114-
log := CTFIFOBuffer withCapacity: 1000.
114+
log := CTFIFOBuffer new: 1000.
115115
1 to: 100000 do: [ :i |
116116
log push: 'entry ', i asString. "O(1) operation - Automatic Management !"
117117
].

docs/Circular_Buffer.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ A Circular buffer is a fixed-size data structure that automatically overwrites t
3131

3232
```smalltalk
3333
"Circular buffer approach - elegant and efficient"
34-
recentMessages := CTFIFOBuffer withCapacity: 100.
34+
recentMessages := CTFIFOBuffer new: 100.
3535
recentMessages push: newMessage. "That's it! Always O(1) & No Manual Cleanup"
3636
```
3737

@@ -46,7 +46,7 @@ FIFO buffers work like a queue - the first element added is the first one retrie
4646

4747
### Dry Run Example
4848
```smalltalk
49-
buffer := CTFIFOBuffer withCapacity: 2.
49+
buffer := CTFIFOBuffer new: 2.
5050
buffer push: 'A'. "Buffer state: [A, _] readIndex=1, writeIndex=2"
5151
buffer push: 'B'. "Buffer state: [A, B] readIndex=1, writeIndex=1"
5252
buffer push: 'C'. "Buffer state: [C, B] readIndex=2, writeIndex=2 (A overwritten)"
@@ -60,7 +60,7 @@ buffer pop. "Returns 'C', Buffer state: [_, _] (empty)"
6060

6161
```smalltalk
6262
"Chat room that keeps last 4 messages for display"
63-
chatHistory := CTFIFOBuffer withCapacity: 4.
63+
chatHistory := CTFIFOBuffer new: 4.
6464
6565
"Users send messages throughout the day"
6666
chatHistory push: 'Alok: Hello everyone!'.
@@ -100,7 +100,7 @@ LIFO buffers work like a stack - the last element added is the first one retriev
100100

101101
### Dry Run Example
102102
```smalltalk
103-
buffer := CTLIFOBuffer withCapacity: 2.
103+
buffer := CTLIFOBuffer new: 2.
104104
buffer push: 'A'. "Buffer state: [A, _] readIndex=1, writeIndex=2"
105105
buffer push: 'B'. "Buffer state: [A, B] readIndex=2, writeIndex=1"
106106
buffer push: 'C'. "Buffer state: [C, B] readIndex=1, writeIndex=2 (A overwritten)"
@@ -114,7 +114,7 @@ buffer pop. "Returns 'B', Buffer state: [_, _] (empty)"
114114

115115
```smalltalk
116116
"Browser that remembers last 3 visited pages"
117-
browserHistory := CTLIFOBuffer withCapacity: 3.
117+
browserHistory := CTLIFOBuffer new: 3.
118118
119119
"User browses during the day"
120120
browserHistory push: 'https://pharo.org'.
@@ -180,7 +180,7 @@ Transcript show: 'Average result after running each benchmark for 100 times...';
180180
181181
bufferResults add: ([
182182
| buffer |
183-
buffer := CTFIFOBuffer withCapacity: capacity.
183+
buffer := CTFIFOBuffer new: capacity.
184184
1 to: totalOperations do: [ :i |
185185
buffer push: i.
186186
].

src/Containers-Buffer-Tests/CTAbstractBufferTest.class.st

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ CTAbstractBufferTest >> testAvailableSpace [
3434
CTAbstractBufferTest >> testBufferCreationWithCapacity [
3535

3636
| testBuffer |
37-
testBuffer := buffer class withCapacity: 5.
38-
37+
testBuffer := buffer class new: 5.
38+
3939
self assert: testBuffer capacity equals: 5.
4040
self assert: testBuffer size equals: 0.
4141
self assert: testBuffer isEmpty.
@@ -53,9 +53,12 @@ CTAbstractBufferTest >> testBufferCreationWithInvalidCapacity [
5353
{ #category : 'tests' }
5454
CTAbstractBufferTest >> testClearBuffer [
5555

56-
buffer push: 'a'; push: 'b'; push: 'c'.
57-
buffer clear.
58-
56+
buffer
57+
push: 'a';
58+
push: 'b';
59+
push: 'c'.
60+
buffer removeAll.
61+
5962
self assert: buffer isEmpty.
6063
self assert: buffer size equals: 0.
6164
self assert: buffer readIndex equals: 1.
@@ -100,10 +103,13 @@ CTAbstractBufferTest >> testEmptyBufferErrors [
100103
{ #category : 'tests' }
101104
CTAbstractBufferTest >> testIndicesAfterClear [
102105

103-
buffer push: 'a'; push: 'b'; push: 'c'.
106+
buffer
107+
push: 'a';
108+
push: 'b';
109+
push: 'c'.
104110
buffer pop.
105-
buffer clear.
106-
111+
buffer removeAll.
112+
107113
self assert: buffer readIndex equals: 1.
108114
self assert: buffer writeIndex equals: 1
109115
]

src/Containers-Buffer-Tests/CTFIFOBufferTest.class.st

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,30 @@ Class {
1111

1212
{ #category : 'running' }
1313
CTFIFOBufferTest >> setUp [
14+
1415
super setUp.
15-
buffer := CTFIFOBuffer withCapacity: 3
16+
buffer := CTFIFOBuffer new: 3
1617
]
1718

1819
{ #category : 'tests' }
1920
CTFIFOBufferTest >> testChatMessageQueue [
2021

2122
| chatQueue displayedMessages |
22-
chatQueue := CTFIFOBuffer withCapacity: 4.
23+
chatQueue := CTFIFOBuffer new: 4.
2324
displayedMessages := OrderedCollection new.
24-
25+
2526
chatQueue push: 'Alok: Hello everyone!'.
2627
chatQueue push: 'Sebastian: Hey Alok!'.
2728
chatQueue push: 'Gordana: How is everyone doing?'.
2829
chatQueue push: 'Sebastian: Great to see you all!'.
29-
30+
3031
displayedMessages add: chatQueue pop.
3132
self assert: displayedMessages last equals: 'Alok: Hello everyone!'.
32-
33+
3334
chatQueue push: 'Alok: Sorry I was late!'.
3435
[ chatQueue isEmpty ] whileFalse: [
3536
displayedMessages add: chatQueue pop ].
36-
37+
3738
self assert: displayedMessages size equals: 5.
3839
self assert: (displayedMessages at: 2) equals: 'Sebastian: Hey Alok!'.
3940
self assert: displayedMessages last equals: 'Alok: Sorry I was late!'

src/Containers-Buffer-Tests/CTLIFOBufferTest.class.st

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,37 @@ Class {
1111

1212
{ #category : 'running' }
1313
CTLIFOBufferTest >> setUp [
14-
super setUp.
15-
buffer := CTLIFOBuffer withCapacity: 3
14+
15+
super setUp.
16+
buffer := CTLIFOBuffer new: 3
1617
]
1718

1819
{ #category : 'tests' }
1920
CTLIFOBufferTest >> testBrowserHistory [
2021

21-
| browserHistory visitedPages |
22-
browserHistory := CTLIFOBuffer withCapacity: 3.
22+
| browserHistory visitedPages |
23+
browserHistory := CTLIFOBuffer new: 3.
2324
visitedPages := OrderedCollection new.
24-
25+
2526
browserHistory push: 'https://google.com'.
2627
browserHistory push: 'https://github.com/pharo-containers'.
2728
browserHistory push: 'https://stackoverflow.com'.
2829
browserHistory push: 'https://reddit.com'.
29-
30+
3031
visitedPages add: browserHistory pop.
3132
self assert: visitedPages last equals: 'https://reddit.com'.
32-
33+
3334
browserHistory push: 'https://pharo.org'.
3435
[ browserHistory isEmpty ] whileFalse: [
3536
visitedPages add: browserHistory pop ].
36-
37+
3738
self assert: visitedPages size equals: 4.
3839
self assert: visitedPages first equals: 'https://reddit.com'.
3940
self assert: (visitedPages at: 2) equals: 'https://pharo.org'.
4041
self assert: (visitedPages at: 3) equals: 'https://stackoverflow.com'.
41-
self assert: visitedPages last equals: 'https://github.com/pharo-containers'
42+
self
43+
assert: visitedPages last
44+
equals: 'https://github.com/pharo-containers'
4245
]
4346

4447
{ #category : 'tests' }

src/Containers-Buffer/CTAbstractBuffer.class.st

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,21 @@ Class {
1616
}
1717

1818
{ #category : 'instance creation' }
19-
CTAbstractBuffer class >> withCapacity: anInteger [
19+
CTAbstractBuffer class >> new [
20+
21+
"Create a new buffer with default capacity"
22+
23+
^ self new: 10
24+
]
25+
26+
{ #category : 'instance creation' }
27+
CTAbstractBuffer class >> new: anInteger [
28+
29+
"Create a new buffer with the specified capacity"
2030

2131
anInteger < 1 ifTrue: [ self error: 'Capacity must be positive' ].
22-
^ self new
23-
capacity: anInteger;
32+
^ self basicNew
33+
initializeWithCapacity: anInteger;
2434
yourself
2535
]
2636

@@ -40,24 +50,6 @@ CTAbstractBuffer >> capacity [
4050
^ capacity
4151
]
4252

43-
{ #category : 'accessing' }
44-
CTAbstractBuffer >> capacity: anInteger [
45-
46-
capacity := anInteger.
47-
elements := Array new: capacity
48-
]
49-
50-
{ #category : 'actions' }
51-
CTAbstractBuffer >> clear [
52-
53-
"Remove all elements from the buffer"
54-
55-
1 to: capacity do: [ :i | elements at: i put: nil ].
56-
readIndex := 1.
57-
writeIndex := 1.
58-
currentSize := 0
59-
]
60-
6153
{ #category : 'copying' }
6254
CTAbstractBuffer >> copy [
6355

@@ -80,7 +72,13 @@ CTAbstractBuffer >> elements [
8072
CTAbstractBuffer >> initialize [
8173

8274
super initialize.
83-
capacity := 10.
75+
self initializeWithCapacity: 10
76+
]
77+
78+
{ #category : 'private' }
79+
CTAbstractBuffer >> initializeWithCapacity: anInteger [
80+
81+
capacity := anInteger.
8482
elements := Array new: capacity.
8583
readIndex := 1.
8684
writeIndex := 1.
@@ -130,7 +128,7 @@ CTAbstractBuffer >> pop [
130128
^ element
131129
]
132130

133-
{ #category : 'testing' }
131+
{ #category : 'adding' }
134132
CTAbstractBuffer >> push: anObject [
135133
"Add an element to the buffer"
136134

@@ -159,7 +157,7 @@ CTAbstractBuffer >> push: anObject [
159157
^ anObject
160158
]
161159

162-
{ #category : 'testing' }
160+
{ #category : 'adding' }
163161
CTAbstractBuffer >> pushAll: aCollection [
164162

165163
aCollection do: [ :e | self push: e ].
@@ -173,6 +171,17 @@ CTAbstractBuffer >> readIndex [
173171
^ readIndex
174172
]
175173

174+
{ #category : 'removing' }
175+
CTAbstractBuffer >> removeAll [
176+
177+
"Remove all elements from the buffer"
178+
179+
1 to: capacity do: [ :i | elements at: i put: nil ].
180+
readIndex := 1.
181+
writeIndex := 1.
182+
currentSize := 0
183+
]
184+
176185
{ #category : 'accessing' }
177186
CTAbstractBuffer >> size [
178187

src/Containers-Buffer/CTFIFOBuffer.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Class {
1212
CTFIFOBuffer >> copy [
1313

1414
| copy |
15-
copy := self class withCapacity: capacity.
15+
copy := self class new: capacity.
1616
self do: [ :each | copy push: each ].
1717
^ copy
1818
]

src/Containers-Buffer/CTLIFOBuffer.class.st

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ CTLIFOBuffer >> copy [
1414
"Return a copy of the LIFO buffer with same contents and order"
1515

1616
| copy index |
17-
copy := self class withCapacity: capacity.
17+
copy := self class new: capacity.
1818
self isEmpty ifTrue: [ ^ copy ].
1919

2020
index := readIndex.
21-
(currentSize - 1) timesRepeat: [
22-
index := index = 1 ifTrue: [ capacity ] ifFalse: [ index - 1 ]
23-
].
24-
21+
currentSize - 1 timesRepeat: [
22+
index := index = 1
23+
ifTrue: [ capacity ]
24+
ifFalse: [ index - 1 ] ].
25+
2526
currentSize timesRepeat: [
26-
copy push: (elements at: index).
27-
index := index \\ capacity + 1
28-
].
27+
copy push: (elements at: index).
28+
index := index \\ capacity + 1 ].
2929
^ copy
3030
]
3131

0 commit comments

Comments
 (0)