Jump to page: 1 2
Thread overview
Is this range behaviour correct?
Mar 14, 2013
Andrea Fontana
Mar 14, 2013
Andrea Fontana
Mar 14, 2013
Andrea Fontana
Mar 14, 2013
monarch_dodra
Mar 14, 2013
Andrea Fontana
Mar 14, 2013
monarch_dodra
Mar 15, 2013
Andrea Fontana
Mar 14, 2013
Ali Çehreli
Mar 14, 2013
Jesse Phillips
Mar 15, 2013
Ali Çehreli
Mar 15, 2013
Andrea Fontana
Mar 17, 2013
Ali Çehreli
Mar 17, 2013
Andrea Fontana
Mar 14, 2013
Jonathan M Davis
March 14, 2013
I'm trying to implement a db cursor as simple InputRange. I can't implement as forward range because using c-api I can't clone/save cursor.

I wrote popFront() front() and empty() method and my range works fine.

Using on a foreach() it browses all elements inside range until range is exausthed.

If i do another foreach() it doesn't restart (empty() is called and it returns true, of course).

Is it the correct behaviour of a inputrange or have i missed something?







March 14, 2013
On Thursday, 14 March 2013 at 10:08:53 UTC, Andrea Fontana wrote:
> I'm trying to implement a db cursor as simple InputRange. I can't implement as forward range because using c-api I can't clone/save cursor.
>
> I wrote popFront() front() and empty() method and my range works fine.
>
> Using on a foreach() it browses all elements inside range until range is exausthed.
>
> If i do another foreach() it doesn't restart (empty() is called and it returns true, of course).
>
> Is it the correct behaviour of a inputrange or have i missed something?

Maybe I got it. I implemented this range as class, so it is passed as reference to foreach, usually ranges are struct and passed by value. I guess this is the problem.

Is there a way to make a class based range like a struct one?
March 14, 2013
On Thursday, March 14, 2013 11:08:52 Andrea Fontana wrote:
> I'm trying to implement a db cursor as simple InputRange. I can't implement as forward range because using c-api I can't clone/save cursor.
> 
> I wrote popFront() front() and empty() method and my range works
> fine.
> 
> Using on a foreach() it browses all elements inside range until
> range is exausthed.
> 
> If i do another foreach() it doesn't restart (empty() is called
> and it returns true, of course).
> 
> Is it the correct behaviour of a inputrange or have i missed something?

foreach(e; range)
{
}

translates to something like

for(auto _range = range; !_range.empty; _range.popFront())
{
    auto e = _range.front;
}

That means that foreach will alter the original range unless it's a struct which implicitly saves via =. Pure input ranges can't be saved, and therefore if you iterate over one until it's empty, it's empty, and it won't magically have elements again if you use foreach on it again.

- Jonathan M Davis
March 14, 2013
On 3/14/13 6:45 AM, Andrea Fontana wrote:
> On Thursday, 14 March 2013 at 10:08:53 UTC, Andrea Fontana wrote:
>> I'm trying to implement a db cursor as simple InputRange. I can't
>> implement as forward range because using c-api I can't clone/save cursor.
>>
>> I wrote popFront() front() and empty() method and my range works fine.
>>
>> Using on a foreach() it browses all elements inside range until range
>> is exausthed.
>>
>> If i do another foreach() it doesn't restart (empty() is called and it
>> returns true, of course).
>>
>> Is it the correct behaviour of a inputrange or have i missed something?
>
> Maybe I got it. I implemented this range as class, so it is passed as
> reference to foreach, usually ranges are struct and passed by value. I
> guess this is the problem.
>
> Is there a way to make a class based range like a struct one?

Add .save() to it.

Andrei
March 14, 2013
On Thursday, 14 March 2013 at 12:29:26 UTC, Andrei Alexandrescu wrote:
> On 3/14/13 6:45 AM, Andrea Fontana wrote:
>> On Thursday, 14 March 2013 at 10:08:53 UTC, Andrea Fontana wrote:
>>> I'm trying to implement a db cursor as simple InputRange. I can't
>>> implement as forward range because using c-api I can't clone/save cursor.
> Add .save() to it.
>
> Andrei

I can't add save and create a forward range: c-api doesn't allow me to clone db cursor.

March 14, 2013
On Thursday, 14 March 2013 at 13:20:51 UTC, Andrea Fontana wrote:
> On Thursday, 14 March 2013 at 12:29:26 UTC, Andrei Alexandrescu wrote:
>> On 3/14/13 6:45 AM, Andrea Fontana wrote:
>>> On Thursday, 14 March 2013 at 10:08:53 UTC, Andrea Fontana wrote:
>>>> I'm trying to implement a db cursor as simple InputRange. I can't
>>>> implement as forward range because using c-api I can't clone/save cursor.
>> Add .save() to it.
>>
>> Andrei
>
> I can't add save and create a forward range: c-api doesn't allow me to clone db cursor.

But think about it. You say you can't save, but you want pass by value to preserve state. If you can preserve state, doesn't that mean you just saved?

Once you've passed your "can't preserve state" to a foreach, then your range is consumed. So yes, it is correct behavior.

Most input ranges should model reference semantics when passed/copied by value. If somebody modifies the range, all ranges are affected. The tough part is usually preserving correct state.
March 14, 2013
On Thursday, 14 March 2013 at 13:58:56 UTC, monarch_dodra wrote:
> On Thursday, 14 March 2013 at 13:20:51 UTC, Andrea Fontana wrote:
>> On Thursday, 14 March 2013 at 12:29:26 UTC, Andrei Alexandrescu wrote:
>>> On 3/14/13 6:45 AM, Andrea Fontana wrote:
>>>> On Thursday, 14 March 2013 at 10:08:53 UTC, Andrea Fontana wrote:
>>>>> I'm trying to implement a db cursor as simple InputRange. I can't
>>>>> implement as forward range because using c-api I can't clone/save cursor.
>>> Add .save() to it.
>>>
>>> Andrei
>>
>> I can't add save and create a forward range: c-api doesn't allow me to clone db cursor.
>
> But think about it. You say you can't save, but you want pass by value to preserve state. If you can preserve state, doesn't that mean you just saved?
>
> Once you've passed your "can't preserve state" to a foreach, then your range is consumed. So yes, it is correct behavior.
>
> Most input ranges should model reference semantics when passed/copied by value. If somebody modifies the range, all ranges are affected. The tough part is usually preserving correct state.

I was hoping there's something I didn't know like rewind() to implement :)

Ok, I'll pray db developer to add a clone method or I'll use something like

auto res = cursor.array(); if needed



March 14, 2013
On Thursday, 14 March 2013 at 14:29:59 UTC, Andrea Fontana wrote:
> On Thursday, 14 March 2013 at 13:58:56 UTC, monarch_dodra wrote:
>> On Thursday, 14 March 2013 at 13:20:51 UTC, Andrea Fontana wrote:
>>> On Thursday, 14 March 2013 at 12:29:26 UTC, Andrei Alexandrescu wrote:
>>>> On 3/14/13 6:45 AM, Andrea Fontana wrote:
>>>>> On Thursday, 14 March 2013 at 10:08:53 UTC, Andrea Fontana wrote:
>>>>>> I'm trying to implement a db cursor as simple InputRange. I can't
>>>>>> implement as forward range because using c-api I can't clone/save cursor.
>>>> Add .save() to it.
>>>>
>>>> Andrei
>>>
>>> I can't add save and create a forward range: c-api doesn't allow me to clone db cursor.
>>
>> But think about it. You say you can't save, but you want pass by value to preserve state. If you can preserve state, doesn't that mean you just saved?
>>
>> Once you've passed your "can't preserve state" to a foreach, then your range is consumed. So yes, it is correct behavior.
>>
>> Most input ranges should model reference semantics when passed/copied by value. If somebody modifies the range, all ranges are affected. The tough part is usually preserving correct state.
>
> I was hoping there's something I didn't know like rewind() to implement :)
>
> Ok, I'll pray db developer to add a clone method or I'll use something like
>
> auto res = cursor.array(); if needed

You *could* add rewind to your range, but it isn't quite the same as save: Rewind means you can move back and forth in your view of the file (or cursor/db), but it does not mean you can actually *duplicate* (save) your range, to have two distinct ranges with a distinct view of the your db.

For example, while the interface is different, a C "FILE*" is what you'd call a rewindable input range with reference semantics.
March 14, 2013
On 03/14/2013 07:29 AM, Andrea Fontana wrote:
> On Thursday, 14 March 2013 at 13:58:56 UTC, monarch_dodra wrote:

>> Most input ranges should model reference semantics when passed/copied
>> by value. If somebody modifies the range, all ranges are affected. The
>> tough part is usually preserving correct state.
>
> I was hoping there's something I didn't know like rewind() to implement :)
>
> Ok, I'll pray db developer to add a clone method or I'll use something like
>
> auto res = cursor.array(); if needed

I had toyed with the idea of making a ForwardRange from an InputRange by caching the elements. Without any guarantees, :) here is the code:

  http://forum.dlang.org/thread/ifg5ei$2qc7$1@digitalmars.com

Ali

March 14, 2013
On Thursday, 14 March 2013 at 21:40:34 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:
>
>   http://forum.dlang.org/thread/ifg5ei$2qc7$1@digitalmars.com
>
> Ali

I attempted to create a sliceable forward range from an input range. I think I ran into a problem of keeping the buffer in sync when doing lookahead.

list2 = list1.save();

while(true) {
    list1.popFront();
    list2.popFront();

    assert(list1.front == list2.front);
}

What happens when your buffer needs to fill, how do you get the data in list 1 & 2? I'm not posing this question as impossible only something to make work.
« First   ‹ Prev
1 2