www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.dtl - mixin linked lists?

reply "Ben Hinkle" <bhinkle mathworks.com> writes:
Does anyone see a need for a mixin for a doubly or singly linked list? I'm
basically thinking of

// mixin for linked list support
template Node(Value) {
  Node next, prev;
  ... various linked-list member functions ...
}

// user data structure that wants to be stored in a list
class MyClass {
  mixin Node!(MyClass);
  char[] a_string;
  int an_int;
  ... etc etc
}

The advantages are a smaller memory footprint and simplicity when the object
lives in exactly one list. Sometimes you just want the list semantics to be
built into the class or struct instead of having a separate List!(MyClass)
type with its definition of nodes. Also an SNode template for singly-linked
lists would be nice.

I haven't completely decided what should go into the Node template - so far
I just have about 5 functions to link, add, remove and opApply. Does this
seem useful?

-Ben
Nov 11 2004
next sibling parent "Ben Hinkle" <bhinkle mathworks.com> writes:
 // mixin for linked list support
 template Node(Value) {
   Node next, prev;
   ... various linked-list member functions ...
 }
ack, that should be Value next, prev;
Nov 11 2004
prev sibling next sibling parent Regan Heath <regan netwin.co.nz> writes:
On Thu, 11 Nov 2004 14:21:18 -0500, Ben Hinkle <bhinkle mathworks.com> 
wrote:
 Does anyone see a need for a mixin for a doubly or singly linked list? 
 I'm
 basically thinking of

 // mixin for linked list support
 template Node(Value) {
   Node next, prev;
   ... various linked-list member functions ...
 }

 // user data structure that wants to be stored in a list
 class MyClass {
   mixin Node!(MyClass);
   char[] a_string;
   int an_int;
   .. etc etc
 }

 The advantages are a smaller memory footprint and simplicity when the 
 object
 lives in exactly one list. Sometimes you just want the list semantics to 
 be
 built into the class or struct instead of having a separate 
 List!(MyClass)
 type with its definition of nodes. Also an SNode template for 
 singly-linked
 lists would be nice.

 I haven't completely decided what should go into the Node template - so 
 far
 I just have about 5 functions to link, add, remove and opApply. Does this
 seem useful?
It seems like a cool idea. I was just thinking 'you could do that with an abstract base class' when I realised 'what if you want to add it to a class which is already deriving from something else'. Regan. -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 11 2004
prev sibling parent reply Ant <Ant_member pathlink.com> writes:
In article <cn0e3e$km4$1 digitaldaemon.com>, Ben Hinkle says...
Does anyone see a need for a mixin for a doubly or singly linked list? I'm
basically thinking of
the cost is so low that would don't even need to ask. just add it. but it will require an interface, right? I see (and use) mixins paired with interfaces. Ant
Nov 12 2004
parent "Ben Hinkle" <bhinkle mathworks.com> writes:
 but it will require an interface, right?
 I see (and use) mixins paired with interfaces.
good point. The mixin can also be used for structs but an interface couldn't hurt, I suppose. I'll put one in.
Nov 12 2004