www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Avoiding default generic types, and allocator awareness

reply helxi <brucewayneshit gmail.com> writes:
As an exercise in http://ddili.org/ders/d.en/pointers.html, I was 
implementing a very stripped down version of singly linked list 
like below:
struct Node(T)
{
     T item;
     Node!T* next_item;
}

string to_string(T)(in Node!T node)
{
     import std.format;
     return node.nextItem ? "%s -> %s".format(node.item, 
*(node.nextItem))
                          : "%s".format(node.item);

}

struct Forward_List(T, N = size_t)
{
     Node!T* head;
     N size;
}

string to_string(T, N = size_t)(ref Forward_List!(T, N) list)
{
     //todo
     import std.format;
     return "head*: %s, size: %s)".format(&list, list.size);
}

void push_front(T, N = size_t)(ref Forward_List!(T, N) list, T 
item)
{
     list.head = new Node!T(item, list.head);
     ++list.size;
}

1. If you have noticed, I am needlessly repeating `N = size_t` in 
to_string and push_front. If I leave the signature like these, is 
it actually going to affect the structure in anyway?:
void push_front(T, N = size_t)(ref Forward_List!(T, N) list, T 
item);
string to_string(T, N = size_t)(ref Forward_List!(T, N) list);

2. How does one make it "allocator aware"? What other allocators 
can I use?
Dec 30 2017
parent helxi <brucewayneshit gmail.com> writes:
On Saturday, 30 December 2017 at 15:00:32 UTC, helxi wrote:
 As an exercise in http://ddili.org/ders/d.en/pointers.html, I 
 was implementing a very stripped down version of singly linked 
 list like below:
 struct Node(T)
 {
     T item;
     Node!T* next_item;
 }

 [...]
Correction, I meant: If I leave the signature like
 these, is it actually going to affect the structure in anyway?:
 void push_front(T, N)(ref Forward_List!(T, N) list, T item);
 string to_string(T, N)(ref Forward_List!(T, N) list);
Dec 30 2017