www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - list21.backwards

reply simon.hodson hssnet.com writes:
I'm playing with lists as an alternative to arrays and have come across a
problem:

module people;
private import std.stdio;
private import list;

class Person
{
mixin List; // Turn the Person class into a linked list.

char[] name,tname;
ubyte age;

this(char[] name, ubyte age){this.name = name;this.age = age;}
}

int main()
{
Person.ListHead people,others;

foreach(Person p; people.each)
{
writefln("Person %s is %d years old", p.name, p.age);
}

writefln("---");

foreach(Person p; others.each.backwards)
{
writefln("Person %s is %d years old", p.name, p.age);
}

return 0;
}


produces:

C:\list21>testing6
---
Error: Access Violation

C:\list21>

So iterating over a list is OK provided you don't try to reverse it. I'm
guessing this is an error in the library but not sure how to fix it.  Anyone got
an idea?

Simon
Jun 20 2006
parent reply "Chris Miller" <chris dprogramming.com> writes:
On Tue, 20 Jun 2006 12:36:20 -0400, <simon.hodson hssnet.com> wrote:
 So iterating over a list is OK provided you don't try to reverse it. I'm
 guessing this is an error in the library but not sure how to fix it.   
 Anyone got
 an idea?

 Simon
A fixed version is uploaded to http://www.dprogramming.com/list.php - thanks for letting me know about this. It was only an issue in a contract (debug only).
Jun 21 2006
parent reply simon.hodson hssnet.com writes:
Thanks Chris,

I've found another similar assert issue where a filter is applied against an
empty list and then the list be sorted backwards: -

module people;
private import std.stdio;
private import list;

class Person
{
mixin List; // Turn the Person class into a linked list.

char[] name,tname;
ubyte age;

this(char[] name, ubyte age){this.name = name;this.age = age;}
}

int main()
{
Person.ListHead people,others;

writefln("-start filter-");
others.filter(
delegate bool(Person f)
{
if(f.name is null)
{
others.remove(f);
}
return true;
}
);
writefln("-end filter-");

foreach(Person p; others.each.backwards)
{
writefln("Person %s is %d years old", p.name, p.age);
}
return 0;
}

produces: -

C:\list21>testing6
-start filter-
Error: AssertError Failure list.d(648)


Regards

Simon


In article <op.tbhrcyuppo9bzi moe>, Chris Miller says...
On Tue, 20 Jun 2006 12:36:20 -0400, <simon.hodson hssnet.com> wrote:
 So iterating over a list is OK provided you don't try to reverse it. I'm
 guessing this is an error in the library but not sure how to fix it.   
 Anyone got
 an idea?

 Simon
A fixed version is uploaded to http://www.dprogramming.com/list.php - thanks for letting me know about this. It was only an issue in a contract (debug only).
Jun 21 2006
parent "Chris Miller" <chris dprogramming.com> writes:
On Wed, 21 Jun 2006 10:49:29 -0400, <simon.hodson hssnet.com> wrote:
 I've found another similar assert issue where a filter is applied  
 against an
 empty list and then the list be sorted backwards: -
Thanks, updated.
Jun 21 2006