caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] How could I implement an efficient ring buffer?
@ 2012-04-02 14:31 Hongbo Zhang
  2012-04-02 15:04 ` David Allsopp
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Hongbo Zhang @ 2012-04-02 14:31 UTC (permalink / raw)
  To: Caml List

Hi List,
    I want to implement sliding window algorithm (in place, no memory 
copy), I wonder whether I need to write c code.

    To make it clear and simple,
       In c, you can record the head pointer of the array, and do the 
modulo operations when get and set
       In ocaml, it seems you have an array a of type int array,  you 
can not do things like this *(&a+5).
    Many thanks


^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: [Caml-list] How could I implement an efficient ring buffer?
  2012-04-02 14:31 [Caml-list] How could I implement an efficient ring buffer? Hongbo Zhang
@ 2012-04-02 15:04 ` David Allsopp
  2012-04-02 15:15   ` Gabriel Scherer
  2012-04-02 21:15   ` Jon Harrop
  2012-04-02 15:44 ` Benedikt Grundmann
  2012-04-03  0:06 ` Francois Berenger
  2 siblings, 2 replies; 6+ messages in thread
From: David Allsopp @ 2012-04-02 15:04 UTC (permalink / raw)
  To: Hongbo Zhang, Caml List

Hongbo Zhang wrote:
> Hi List,
>     I want to implement sliding window algorithm (in place, no memory
> copy), I wonder whether I need to write c code.
> 
>     To make it clear and simple,
>        In c, you can record the head pointer of the array, and do the
> modulo operations when get and set
>        In ocaml, it seems you have an array a of type int array,  you can
> not do things like this *(&a+5).

Perhaps I'm missing something, but if you're planning on using arrays, what's wrong with retrieving the item using the array index modulo the length of the array?

i.e.

let a = [| ... or whatever ... |] in
a.(5 mod Array.length a)

If accessing an array by index instead of by pointer worries you, then you're looking at the wrong language ;o)


David


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] How could I implement an efficient ring buffer?
  2012-04-02 15:04 ` David Allsopp
@ 2012-04-02 15:15   ` Gabriel Scherer
  2012-04-02 21:15   ` Jon Harrop
  1 sibling, 0 replies; 6+ messages in thread
From: Gabriel Scherer @ 2012-04-02 15:15 UTC (permalink / raw)
  To: David Allsopp; +Cc: Hongbo Zhang, Caml List

A small implementation of a FIFO queue implemented as a circular
buffer of fixed length:

type 'a circular_buffer = {
  mutable pos : int;
  mutable count : int;
  data : 'a array;
  size : int;
}

let create size dummy = {
  pos = 0;
  count = 0;
  data = Array.make size dummy;
  size;
}

let push buffer elem =
  let k = buffer.pos + buffer.count in
  buffer.data.(if k < buffer.size then k else 0) <- elem;
  if buffer.count < buffer.size then
    buffer.count <- buffer.count + 1
  else
    buffer.pos <- buffer.pos + 1;
  ()

let pop buffer =
  if buffer.count = 0 then raise Not_found;
  let result = buffer.data.(buffer.pos) in
  (* if you want to free the buffer content, buffer.data.(pos) <- dummy *)
  let pos' = buffer.pos + 1 in
  buffer.pos <- (if pos' < buffer.size then pos' else 0);
  buffer.count <- buffer.count - 1;
  result

(* test *)
let () =
  let buf = create 2 '0' in
  (* *)
  push buf '1';
  (* 1 *)
  push buf '2';
  (* 1 2 *)
  push buf '3';
  (*   2 3 *)
  assert (pop buf = '2');
  (*     3 *)
  push buf '4';
  (*     3 4 *)
  assert (pop buf = '3');
  (*       4 *)
  assert (pop buf = '4');
  (*         *)
  assert (try ignore (pop buf); false with Not_found -> true);
  assert (try ignore (pop buf); false with Not_found -> true);


On Mon, Apr 2, 2012 at 5:04 PM, David Allsopp <dra-news@metastack.com> wrote:
> Hongbo Zhang wrote:
>> Hi List,
>>     I want to implement sliding window algorithm (in place, no memory
>> copy), I wonder whether I need to write c code.
>>
>>     To make it clear and simple,
>>        In c, you can record the head pointer of the array, and do the
>> modulo operations when get and set
>>        In ocaml, it seems you have an array a of type int array,  you can
>> not do things like this *(&a+5).
>
> Perhaps I'm missing something, but if you're planning on using arrays, what's wrong with retrieving the item using the array index modulo the length of the array?
>
> i.e.
>
> let a = [| ... or whatever ... |] in
> a.(5 mod Array.length a)
>
> If accessing an array by index instead of by pointer worries you, then you're looking at the wrong language ;o)
>
>
> David
>
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] How could I implement an efficient ring buffer?
  2012-04-02 14:31 [Caml-list] How could I implement an efficient ring buffer? Hongbo Zhang
  2012-04-02 15:04 ` David Allsopp
@ 2012-04-02 15:44 ` Benedikt Grundmann
  2012-04-03  0:06 ` Francois Berenger
  2 siblings, 0 replies; 6+ messages in thread
From: Benedikt Grundmann @ 2012-04-02 15:44 UTC (permalink / raw)
  To: Hongbo Zhang; +Cc: Caml List

Use core's Dequeue module.

Cheers,

Bene

On 2 April 2012 15:31, Hongbo Zhang <bobzhang1988@gmail.com> wrote:
> Hi List,
>   I want to implement sliding window algorithm (in place, no memory copy), I
> wonder whether I need to write c code.
>
>   To make it clear and simple,
>      In c, you can record the head pointer of the array, and do the modulo
> operations when get and set
>      In ocaml, it seems you have an array a of type int array,  you can not
> do things like this *(&a+5).
>   Many thanks
>
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>



-- 
Calvin: I try to make everyone's day a little more
surreal.

(From Calvin & Hobbes)


^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: [Caml-list] How could I implement an efficient ring buffer?
  2012-04-02 15:04 ` David Allsopp
  2012-04-02 15:15   ` Gabriel Scherer
@ 2012-04-02 21:15   ` Jon Harrop
  1 sibling, 0 replies; 6+ messages in thread
From: Jon Harrop @ 2012-04-02 21:15 UTC (permalink / raw)
  To: 'David Allsopp', 'Hongbo Zhang', 'Caml List'


The "mod" and the write barrier will significantly degrade performance vs C.
Probably faster to replace "mod" with if-based wrap around but there's
nothing you can do about the write barrier.

Cheers,
Jon.

> -----Original Message-----
> From: David Allsopp [mailto:dra-news@metastack.com]
> Sent: 02 April 2012 16:05
> To: Hongbo Zhang; Caml List
> Subject: RE: [Caml-list] How could I implement an efficient ring buffer?
> 
> Hongbo Zhang wrote:
> > Hi List,
> >     I want to implement sliding window algorithm (in place, no memory
> > copy), I wonder whether I need to write c code.
> >
> >     To make it clear and simple,
> >        In c, you can record the head pointer of the array, and do the
> > modulo operations when get and set
> >        In ocaml, it seems you have an array a of type int array,  you
> > can not do things like this *(&a+5).
> 
> Perhaps I'm missing something, but if you're planning on using arrays,
what's
> wrong with retrieving the item using the array index modulo the length of
the
> array?
> 
> i.e.
> 
> let a = [| ... or whatever ... |] in
> a.(5 mod Array.length a)
> 
> If accessing an array by index instead of by pointer worries you, then
you're
> looking at the wrong language ;o)
> 
> 
> David
> 
> 
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] How could I implement an efficient ring buffer?
  2012-04-02 14:31 [Caml-list] How could I implement an efficient ring buffer? Hongbo Zhang
  2012-04-02 15:04 ` David Allsopp
  2012-04-02 15:44 ` Benedikt Grundmann
@ 2012-04-03  0:06 ` Francois Berenger
  2 siblings, 0 replies; 6+ messages in thread
From: Francois Berenger @ 2012-04-03  0:06 UTC (permalink / raw)
  To: caml-list

On 04/02/2012 11:31 PM, Hongbo Zhang wrote:
> Hi List,
> I want to implement sliding window algorithm (in place, no memory copy),
> I wonder whether I need to write c code.

Queues are described in Chris Okasaki's book, with a functional 
implementation in all senses of the term.

If you don't want to roll your own, someone suggested Janestreet's 
core's Dequeue module and Gabriel sent you some implementation.

Regards,
F.

> To make it clear and simple,
> In c, you can record the head pointer of the array, and do the modulo
> operations when get and set
> In ocaml, it seems you have an array a of type int array, you can not do
> things like this *(&a+5).
> Many thanks
>
>


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2012-04-03  0:06 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-02 14:31 [Caml-list] How could I implement an efficient ring buffer? Hongbo Zhang
2012-04-02 15:04 ` David Allsopp
2012-04-02 15:15   ` Gabriel Scherer
2012-04-02 21:15   ` Jon Harrop
2012-04-02 15:44 ` Benedikt Grundmann
2012-04-03  0:06 ` Francois Berenger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).