-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
557 additions
and
16 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 |
---|---|---|
@@ -1,7 +1,60 @@ | ||
=begin | ||
Write your code for the 'Circular Buffer' exercise in this file. Make the tests in | ||
`circular_buffer_test.rb` pass. | ||
# frozen_string_literal: false | ||
|
||
To get started with TDD, see the `README.md` file in your | ||
`ruby/circular-buffer` directory. | ||
=end | ||
# https://exercism.org/tracks/ruby/exercises/circular-buffer | ||
# Circular Buffer exercise | ||
class CircularBuffer | ||
def initialize(capacity = 1) | ||
@capacity = capacity | ||
@buffer = Array.new(@capacity, nil) | ||
@read_ptr = 0 | ||
@write_ptr = 0 | ||
end | ||
|
||
def clear | ||
@read_ptr = 0 | ||
@write_ptr = 0 | ||
end | ||
|
||
def read | ||
raise BufferEmptyException if @read_ptr == @write_ptr | ||
|
||
data = @buffer[@read_ptr] | ||
@read_ptr = (@read_ptr + 1) % (@capacity + 1) | ||
|
||
data | ||
end | ||
|
||
def write(data) | ||
new_write_ptr = (@write_ptr + 1) % (@capacity + 1) | ||
raise BufferFullException if new_write_ptr == @read_ptr | ||
|
||
@buffer[@write_ptr] = data | ||
@write_ptr = new_write_ptr | ||
end | ||
|
||
def write!(data) | ||
new_write_ptr = (@write_ptr + 1) % (@capacity + 1) | ||
@read_ptr = (@read_ptr + 1) % (@capacity + 1) if new_write_ptr == @read_ptr | ||
|
||
@buffer[@write_ptr] = data | ||
@write_ptr = new_write_ptr | ||
end | ||
|
||
def to_s | ||
output = [] | ||
output.push(" buffer: #{@buffer}") | ||
output.push("write_ptr: #{@write_ptr}") | ||
output.push(" read_ptr: #{@read_ptr}") | ||
output.push(" capacity: #{@capacity}") | ||
|
||
output.join("\n") | ||
end | ||
|
||
# Raised when the buffer size is zero | ||
class BufferEmptyException < StandardError | ||
end | ||
|
||
# Raised when the buffer is full | ||
class BufferFullException < StandardError | ||
end | ||
end |
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,5 @@ | ||
{ | ||
"result": { | ||
"line": 81.81 | ||
} | ||
} |
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,71 @@ | ||
{ | ||
"test:exercism": { | ||
"coverage": { | ||
"/home/vpayno/git_vpayno/exercism-workspace/ruby/circular-buffer/circular_buffer.rb": { | ||
"lines": [ | ||
null, | ||
null, | ||
null, | ||
null, | ||
1, | ||
1, | ||
10, | ||
10, | ||
10, | ||
10, | ||
null, | ||
null, | ||
1, | ||
1, | ||
1, | ||
null, | ||
null, | ||
1, | ||
29, | ||
null, | ||
22, | ||
22, | ||
null, | ||
22, | ||
null, | ||
null, | ||
1, | ||
28, | ||
28, | ||
null, | ||
27, | ||
27, | ||
null, | ||
null, | ||
1, | ||
4, | ||
4, | ||
null, | ||
4, | ||
4, | ||
null, | ||
null, | ||
1, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
null, | ||
0, | ||
null, | ||
null, | ||
null, | ||
1, | ||
null, | ||
null, | ||
null, | ||
1, | ||
null, | ||
null | ||
] | ||
} | ||
}, | ||
"timestamp": 1698644242 | ||
} | ||
} |
Empty file.
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,52 @@ | ||
<?xml version='1.0'?> | ||
<!DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-04.dtd"> | ||
<!-- Generated by simplecov-cobertura version 2.1.0 (https://github.com/dashingrocket/simplecov-cobertura) --> | ||
<coverage line-rate="0.82" lines-covered="27" lines-valid="33" complexity="0" version="0" timestamp="1698644242"> | ||
<sources> | ||
<source>/home/vpayno/git_vpayno/exercism-workspace/ruby/circular-buffer</source> | ||
</sources> | ||
<packages> | ||
<package name="circular-buffer" line-rate="0.82" complexity="0"> | ||
<classes> | ||
<class name="circular_buffer" filename="circular_buffer.rb" line-rate="0.82" complexity="0"> | ||
<methods/> | ||
<lines> | ||
<line number="5" hits="1"/> | ||
<line number="6" hits="1"/> | ||
<line number="7" hits="10"/> | ||
<line number="8" hits="10"/> | ||
<line number="9" hits="10"/> | ||
<line number="10" hits="10"/> | ||
<line number="13" hits="1"/> | ||
<line number="14" hits="1"/> | ||
<line number="15" hits="1"/> | ||
<line number="18" hits="1"/> | ||
<line number="19" hits="29"/> | ||
<line number="21" hits="22"/> | ||
<line number="22" hits="22"/> | ||
<line number="24" hits="22"/> | ||
<line number="27" hits="1"/> | ||
<line number="28" hits="28"/> | ||
<line number="29" hits="28"/> | ||
<line number="31" hits="27"/> | ||
<line number="32" hits="27"/> | ||
<line number="35" hits="1"/> | ||
<line number="36" hits="4"/> | ||
<line number="37" hits="4"/> | ||
<line number="39" hits="4"/> | ||
<line number="40" hits="4"/> | ||
<line number="43" hits="1"/> | ||
<line number="44" hits="0"/> | ||
<line number="45" hits="0"/> | ||
<line number="46" hits="0"/> | ||
<line number="47" hits="0"/> | ||
<line number="48" hits="0"/> | ||
<line number="50" hits="0"/> | ||
<line number="54" hits="1"/> | ||
<line number="58" hits="1"/> | ||
</lines> | ||
</class> | ||
</classes> | ||
</package> | ||
</packages> | ||
</coverage> |
Oops, something went wrong.