473,320 Members | 1,926 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

read, write and seek method in ring buffer class

Could any body help me with creating a ring buffer class using a string. use memory circular buffer not an IO buffer. just read, write and seek method. Read method should take anumber and return the string. write method should take a string. seek should take a number and return nuthing. use three member variables a buffer itself as a string, read_position, write_position. use >>, << methods.

I have tried this code but i need full functionality. May be you can understand the functionality by the below code

<code>
class RingBuffer

# Instanciate a ring buffer of the given size.
# The buffer will contain at most +size+ elements

def initialize(size)

# initialize the max

@max = size

# initialize the buffer array

@buffer = Array.new(size)

# initialize the read position

@read_position = read_position

# initialize the write position

@write_position = write_position

end

#
# Method to clear the buffer
#

def clear

@buffer = []

end

#
# method call to read the contents in a buffer
#

def read(read_position)

# is read position is greater than the maximum

if @read_position > @max

# if so

raise "Illeagal Read Position, the value is greater than the maximum value of buffer"

# is read position is lesser than the minimm value

if @read_position < -1

# if so

raise " Illeagal Read Position, the value is less than minimum"

end

end

# read the buffer value

str = @buffer[read_position].to_s

# return the string

return str

end


#
# method call to write contents in a buffer
#

def write(value)


# shift the buffer

@buffer.shift

# append the value at the end

@buffer << value

end


#
# method call to convert buffer array
#

def to_a

# return the buffer element

return @buffer.dup

end

#
# method call to convert to string
#

def to_s

# initialize the str

str = ""

# each value in the buffer is converted to string

@buffer.each { |entry|
str << entry.to_s << "\n"

}

# return the string elements

return str

end

#
# method call to seek position
#

def seek




end

end
</code>

Thanks in advance
Nov 21 '07 #1
0 3818

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Ben Collingsworth | last post by:
Anyone have some efficient source code for implementing a ring buffer?
6
by: ataanis | last post by:
Hi, Can anybody tell me , why if I change the second value in the read() function, I get an error message, I'm dealing here with a file, that has at least 240 caracters per line Private Sub...
5
by: Eric Cadwell | last post by:
Is there a faster way to write the last 100K of a large file? This code takes almost two minutes to run on my machine. int buffer = 100000; int length = 2000000000; string file =...
7
by: alexandre_irrthum | last post by:
Hi there, I am trying to use pyserial to read data from a temperature logger device (T-logger). T-logger is based on the DS1615 temperature recorder chip (Dallas Semiconductor). According to the...
6
by: ericunfuk | last post by:
Hi ALL, I want to read a binary file(it's pic.tif file, I guess it's binary file?), then write it to a new file), I have several questions about this process: When I use fread() to read a...
7
by: Boki | last post by:
Multi-thread read/write to a single file. I have two processing threads, thread A and thread B; and I called my queue as Q. Thread A will feed data into Q by user input, the timing is random....
5
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a...
0
by: Anthony Jones | last post by:
"DR" <softwareengineer98037@yahoo.comwrote in message news:OZoyDzR8IHA.1200@TK2MSFTNGP04.phx.gbl... of A simple experiment in .NET 2.0 and Windows XP shows that it does appear to flush on the...
1
by: Steven D'Aprano | last post by:
On Wed, 22 Oct 2008 16:59:45 -0400, Terry Reedy wrote: With respect Terry, I think what you have missed is the reason why the OP thinks this is a bug. He's not surprised that buffering is going...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.