[Gambas-user] file question

Christopher Brian Jack brian at ...1334...
Sat Oct 14 20:47:43 CEST 2006


...reposted for those who are superstitious...

On Thu, 12 Oct 2006, James Hatridge wrote:

> Hi Christopher et al...
>
> Ok, count me a bit slow, I think I understand. But can you show me a bit of
> code? (code I understand better) :)

Okay snippets are inserted look through...
This is NOT a thread-safe implementation!

Also I may have botched the error reporting syntax royally.  I haven't
used the exception handling in Gambas as of yet.

> Thanks,
>
> JIM
> On Wednesday 11 October 2006 18:31, Christopher Brian Jack wrote:
> > On Wed, 11 Oct 2006, James Hatridge wrote:
> > > Hi all
> > >
> > > Is there anyway I can set up a buffer or file so that when its full the
> > > oldest part is dropped and newst is kept?
> > >
> > > The idea I have is a music buffer that holds X minutes of audio when the
> > > buffer is full it will push out the old and add the new. Is this do-able?
> > >
> > > Thanks,
> > >
> > > JIM
> >
> > The solution to your problem is a ring buffer.

<Create a new class file called RingBuffer>

> > To use a ring buffer split your main buffer into sequential chunks,

Private chunkSize As Integer
Private chunkList As Variant

> > maintaining a head and tail position in the buffer.  If the head and tail
> > indices are zero-based (0 indicates the first chunk) then you can use x
> > MOD NumberOfChunks to get the wraparound effect needed for the index
> > arithmetic.  The explanation below uses MOD and hence assumes zero-based
> > indices.

Private head As Integer
Private tail As Integer
Private status As String

> > When the buffer is initialized  head=0, tail=0, status="empty"

'put into class "init" or "constructor" sub
head=0
tail=0
status="empty"

'NOTE: The init sub/ctor must also require a block size parameter and a
'block count parameter:

Dim chunks(chunkCount) As Variant
chunkList=chunks

'
'It may be useful to be able to query the full/empty state of the
'
Public Property isFull() As Boolean
Public Property isEmpty() As Boolean

Public Sub isFull()
  If Not (head=tail) Then Return FALSE
  If status="full" Then Return TRUE
  Return FALSE
End

Public Sub isEmpty()
  If Not (head=tail) Then Return FALSE
  If status="empty" Then Return TRUE
  Return FALSE
End

> > 1.  To add to the buffer first make sure the status is not "full" (throw a
> > football if it is) then increase the head with head=(head+1) MOD
> > BufferChunkCount if this makes head=tail then set status to "full" and
> > throw a football.  If head!=tail then you can go ahead and fill the chunk
> > now indexed by head.

Public Sub Push(sourceData As String)
  If (status="full") Then Throw("Buffer Full")
  head=(head+1) MOD chunkCount
  If (head=tail) Then
    status="full"
    Throw("Buffer Full")
  End
  chunkList(head)=Mid(sourceData,1,Min(chunkSize,Len(sourceData)))
End

> > 2.  To pull from the buffer first check that the buffer state is not
> > "empty" and throw a football if it is.  Transfer the chunk to whatever
> > needs it then increase the tail by one with tail=(tail+1) MOD
> > BufferChunkCount.  Appropriately setting the status to "empty" if the
> > condition tail=head is true after increasing the tail.

Public Sub Pull() As String
  Dim data As String
  If (status="empty") Then Throw("Buffer Empty")
  data=chunkList(tail)
  chunkList(tail)=NULL
  tail=(tail+1) Mod chunkCount
  If (head=tail) Then status="empty"
  Return data;
End

> > 3. If the accesses to the buffer may be threaded then the head and tail
> > check-and-change need to be done within semaphores or with a locking
> > mechanism to make them atomic/serialized (only one thing accesses the
> > head/tail indices at a time).  Head needs to bumped before writing the
> > data and tail must only be bumped after the chunk is transferred to
> > whatever is using the data.

This is not a thread-safe example.

.=================================================.
|  Christopher BRIAN Jack aka "Gau of the Veldt"  |
+================================================='
| oevna at ...1544...
`=================================================-
Hi Spambots, my email address is sputnik at ...1334...
Hi Humans, my email address uses rot13 cipher





More information about the User mailing list