From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id q32F4lFf011385 for ; Mon, 2 Apr 2012 17:04:47 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ArMCABG/eU9RZ90vlGdsb2JhbABDuR0iAQEBAQkLCQkUAySCCQEBAQQ6TwIBCA4KChQQMiUCBAEaiAIDtweKeoU/YwSVYZMWgVw X-IronPort-AV: E=Sophos;i="4.75,357,1330902000"; d="scan'208";a="138729634" Received: from mtaout01-winn.ispmail.ntl.com ([81.103.221.47]) by mail4-smtp-sop.national.inria.fr with ESMTP; 02 Apr 2012 17:04:42 +0200 Received: from aamtaout04-winn.ispmail.ntl.com ([81.103.221.35]) by mtaout01-winn.ispmail.ntl.com (InterMail vM.7.08.04.00 201-2186-134-20080326) with ESMTP id <20120402150441.ULYE3740.mtaout01-winn.ispmail.ntl.com@aamtaout04-winn.ispmail.ntl.com>; Mon, 2 Apr 2012 16:04:41 +0100 Received: from romulus.metastack.com ([81.102.132.77]) by aamtaout04-winn.ispmail.ntl.com (InterMail vG.3.00.04.00 201-2196-133-20080908) with ESMTP id <20120402150441.HNXA23925.aamtaout04-winn.ispmail.ntl.com@romulus.metastack.com>; Mon, 2 Apr 2012 16:04:41 +0100 Received: from remus.metastack.local (remus.metastack.com [172.16.0.1]) by romulus.metastack.com (8.14.2/8.14.2) with ESMTP id q32F4baw020374 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=FAIL); Mon, 2 Apr 2012 16:04:37 +0100 Received: from Remus.metastack.local ([fe80::547c:3c42:e1da:eda2]) by Remus.metastack.local ([fe80::547c:3c42:e1da:eda2%10]) with mapi id 14.01.0355.002; Mon, 2 Apr 2012 16:04:37 +0100 From: David Allsopp To: Hongbo Zhang , Caml List Thread-Topic: [Caml-list] How could I implement an efficient ring buffer? Thread-Index: AQHNEN1p8f3Jssp8w0SfCGHxDk69epaHoJyg Date: Mon, 2 Apr 2012 15:04:36 +0000 Message-ID: References: <4F79B858.2090502@gmail.com> In-Reply-To: <4F79B858.2090502@gmail.com> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [172.16.0.103] Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Organization: MetaStack Solutions Ltd. X-Scanned-By: MIMEDefang 2.65 on 172.16.0.20 X-Cloudmark-Analysis: v=1.1 cv=JvdXmxIgLJv2/GthKqHpGJEEHukvLcvELVXUanXFreg= c=1 sm=0 a=zQHuVQ-mkvYA:10 a=-tnAv0XnCDgA:10 a=cTs9vV391PwA:10 a=kj9zAlcOel0A:10 a=xqWC_Br6kY4A:10 a=81CB9Rd3EorVxJbzURsA:9 a=CjuIK1q_8ugA:10 a=UQd5GoTPhMMsk9NJ:21 a=rq0p_OHtSV9v9pzh:21 a=HpAAvcLHHh0Zw7uRqdWCyQ==:117 Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by walapai.inria.fr id q32F4lFf011385 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