Skip to content

Commit

Permalink
fixed buffered istream
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam-Belliveau committed Jan 23, 2020
1 parent e119fb4 commit 3711226
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 22 deletions.
2 changes: 1 addition & 1 deletion docs/allclasses-index.html
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ <h1 title="All&amp;nbsp;Classes" class="title">All&nbsp;Classes</h1>
<th class="colLast" scope="row">
<div class="block">This class allows you to use an input stream while recording the last N
values from the stream

It extends from IStream, so it also workes with the existing IStream classes</div>
</th>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion docs/com/stuypulse/stuylib/streams/BufferedIStream.html
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ <h2 title="Class BufferedIStream" class="title">Class BufferedIStream</h2>
implements <a href="IStream.html" title="interface in com.stuypulse.stuylib.streams">IStream</a></pre>
<div class="block">This class allows you to use an input stream while recording the last N
values from the stream

It extends from IStream, so it also workes with the existing IStream classes</div>
</li>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion docs/com/stuypulse/stuylib/streams/package-summary.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ <h1 title="Package" class="title">Package&nbsp;com.stuypulse.stuylib.streams</h1
<td class="colLast">
<div class="block">This class allows you to use an input stream while recording the last N
values from the stream

It extends from IStream, so it also workes with the existing IStream classes</div>
</td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion docs/index-all.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ <h2 class="title">B</h2>
<dd>
<div class="block">This class allows you to use an input stream while recording the last N
values from the stream

It extends from IStream, so it also workes with the existing IStream classes</div>
</dd>
<dt><span class="memberNameLink"><a href="com/stuypulse/stuylib/streams/BufferedIStream.html#%3Cinit%3E(com.stuypulse.stuylib.streams.IStream)">BufferedIStream(IStream)</a></span> - Constructor for class com.stuypulse.stuylib.streams.<a href="com/stuypulse/stuylib/streams/BufferedIStream.html" title="class in com.stuypulse.stuylib.streams">BufferedIStream</a></dt>
Expand Down
Binary file modified docs/member-search-index.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/overview-summary.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<!-- NewPage -->
<html lang="en">
<head>
<!-- Generated by javadoc (11.0.5) on Thu Jan 23 12:42:53 EST 2020 -->
<!-- Generated by javadoc (11.0.5) on Thu Jan 23 13:12:22 EST 2020 -->
<title>StuyLib API</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">window.location.replace('index.html')</script>
Expand Down
Binary file modified docs/package-search-index.zip
Binary file not shown.
Binary file modified docs/type-search-index.zip
Binary file not shown.
32 changes: 15 additions & 17 deletions src/com/stuypulse/stuylib/streams/BufferedIStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
import com.stuypulse.stuylib.streams.IStream;
import com.stuypulse.stuylib.exception.ConstructionError;

import java.util.ArrayList;

/**
* This class allows you to use an input stream while recording the last N
* values from the stream
*
*
* It extends from IStream, so it also workes with the existing IStream classes
*
*
* @author Kevin (kc16777216@gmail.com)
* @author Sam (sam.belliveau@gmail.com)
*/
Expand All @@ -22,12 +20,12 @@ public class BufferedIStream implements IStream {
*/
public static final int kDefaultSize = 50;

private ArrayList<Double> mBuffer;
private double[] mBuffer;
private IStream mIStream;

/**
* Creates a buffered istream with an istream and a custom buffer size
*
*
* @param istream istream that will be buffered
* @param size size of buffer
*/
Expand All @@ -36,13 +34,13 @@ public BufferedIStream(IStream istream, int size) throws ConstructionError {
throw new ConstructionError("BufferedIStream(IStream istream, int size)", "size must be greater than 0!");
}

mBuffer = new ArrayList<Double>(size);
mBuffer = new double[size];
mIStream = istream;
}

/**
* Creates a buffered istream with default buffer size (kDefaultSize)
*
*
* @param istream istren array listam that will be buffered
*/
public BufferedIStream(IStream istream) {
Expand All @@ -51,7 +49,7 @@ public BufferedIStream(IStream istream) {

/**
* Get value from istream and add value to buffer
*
*
* @return value from the istream
*/
public double get() {
Expand All @@ -60,22 +58,22 @@ public double get() {

/**
* Get value from istream and add value to buffer
*
*
* @param delta how far back in the buffer to go
* @return value from the istream
*/
public double get(int delta) {
for (int i = mBuffer.size() - 1; i > 0; --i) {
mBuffer.set(i, mBuffer.get(i - 1));
for (int i = mBuffer.length - 1; i > 0; --i) {
mBuffer[i] = mBuffer[i - 1];
}
mBuffer.set(0, mIStream.get());

mBuffer[0] = mIStream.get();
return last(delta);
}

/**
* Get the most recent value from the istream
*
*
* @return most recent value from the istream
*/
public double last() {
Expand All @@ -84,11 +82,11 @@ public double last() {

/**
* Go back [delta] amount in the buffer, ie. 2 elements back
*
*
* @param delta how far back in the buffer to go
* @return the value of that spot in the buffer
*/
public double last(int delta) {
return mBuffer.get(delta);
return mBuffer[Math.min(Math.max(delta, 0), mBuffer.length - 1)];
}
}

0 comments on commit 3711226

Please sign in to comment.