Is this range behaviour correct?
Ali Çehreli
acehreli at yahoo.com
Sat Mar 16 21:54:56 PDT 2013
On 03/15/2013 02:57 AM, Andrea Fontana wrote:
> On Friday, 15 March 2013 at 03:44:51 UTC, Ali Çehreli wrote:
>>>> I had toyed with the idea of making a ForwardRange from an InputRange
>>>> by caching the elements. Without any guarantees, :) here is the code:
> It *seems* to work :) Funny. The only problem is that caching db results
> maybe it's not a good idea if i read too much data.
Agreed but that's the only feature of the range. :) The way the data is
"uncached" is by moving the slice forward by this code:
/* Nobody is using the first part of the cache anymore */
cache = cache[(minPtr - cache.ptr) .. $];
I think it is sufficient for the GC to free the previous first part of
the array. The following program keeps running without consuming the memory:
void main()
{
int[] a;
while (true) {
a ~= 42;
a = a[1 .. $];
}
}
> I'm not sure if it's fine or not but:
> - I've changed AsForwardRange from class to struct
Good idea. It looks like I had already made it a struct in my local copy
of it.
> - I've added a method (it's an "hack" to make it works):
> this(this)
> {
> auto dummy = lazyCache.newView(lazyCache.cache.ptr);
> myId = dummy.myId;
> }
I am not sure why that was necessary. Additionally, some of the member
functions like empty() can be made const.
> - I've add @property to save() -> isForwardRange gives false without
this.
Agreed.
> I did this to make it similar to other ranges (original topic!)
> Shouldn't a view unregister pointer on its d-tor?
That's a good idea too. :)
Ali
More information about the Digitalmars-d-learn
mailing list