www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Templated Lists

reply Jolly James <j.j jmail.com> writes:
I know that there are arrays, some strange container classes and 
so on.

But how does one create and use a templated list in D that 
supports adding, removing and sorting items? One that can be used 
for structs and for classes?
Jun 16
next sibling parent Jonathan M Davis via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Friday, June 16, 2017 19:07:49 Jolly James via Digitalmars-d wrote:
 I know that there are arrays, some strange container classes and
 so on.

 But how does one create and use a templated list in D that
 supports adding, removing and sorting items? One that can be used
 for structs and for classes?
AliasSeq is used to create a list of items - e.g. AliasSeq!(int, float). It can hold types, expressions, or both. It's basically what you get when you have a template argument list or parameter list or a function argument list or parameter list - hence why it can hold a bit of a hodepodge of things and why giving it a good name is rather difficult (its current name being short for alias sequence). It used to be called TypeTuple but was renamed because it doesn't only hold types, and it isn't really a tuple (it auto expands, so you can't really nest them - e.g. AliasSeq!(int, float) and AliasSeq!(int, AliasSeq!float) are going to end up being the same thing). Confusingly, the language documentation tends to call it Tuple (not to be confused with std.typecons.Tuple), even though it isn't really a tuple. But that largely comes down to the fact that no one has a good name for the thing. std.meta provides a variety of templates for operating on AliasSeqs with predicates and whatnot, so it would be the main place to look if you want to operate on an AliasSeq: http://dlang.org/phobos/std_meta.html And of course, std.traits helps with some of the building blocks - particularly for creating compile-time predicates: http://dlang.org/phobos/std_traits.html - Jonathan M Davis
Jun 16
prev sibling parent reply Cym13 <cpicard openmailbox.org> writes:
On Friday, 16 June 2017 at 19:07:49 UTC, Jolly James wrote:
 I know that there are arrays, some strange container classes 
 and so on.

 But how does one create and use a templated list in D that 
 supports adding, removing and sorting items? One that can be 
 used for structs and for classes?
I'm unclear about your intent: do you want a compile-time list or a parameterized list (but that can be used at runtime)? In the first case the answer above is what you want, in the second I recommend reading about templates, maybe http://nomad.so/2013/07/templates-in-d-explained/ Whatever your use case such questions find better answers in the "Learn" section of this forum :)
Jun 16
parent reply Jolly James <j.j jmail.com> writes:
On Friday, 16 June 2017 at 19:42:44 UTC, Cym13 wrote:
 I'm unclear about your intent: do you want a compile-time list
I am looking for something similar to C#'s generic list.
 interface IList<T>
 {
     int Count { get; }

     void Add(T item);
     void Remove(T item);

     T this[int index];

     // [...]
 }
Jun 16
next sibling parent Stefan Koch <uplink.coder googlemail.com> writes:
On Friday, 16 June 2017 at 23:08:58 UTC, Jolly James wrote:
 On Friday, 16 June 2017 at 19:42:44 UTC, Cym13 wrote:
 I'm unclear about your intent: do you want a compile-time list
I am looking for something similar to C#'s generic list.
 interface IList<T>
 {
     int Count { get; }

     void Add(T item);
     void Remove(T item);

     T this[int index];

     // [...]
 }
just use d's dynamic arrays :)
Jun 16
prev sibling next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 06/16/2017 04:08 PM, Jolly James wrote:
 On Friday, 16 June 2017 at 19:42:44 UTC, Cym13 wrote:
 I'm unclear about your intent: do you want a compile-time list
I am looking for something similar to C#'s generic list.
std.variant.Variant perhaps? https://dlang.org/phobos/std_variant.html import std.variant; void main() { Variant[] arr; arr ~= Variant(42); arr ~= Variant("hello"); } Also consider Algebraic in that module. Ali
Jun 16
parent reply Jolly James <j.j jmail.com> writes:
On Friday, 16 June 2017 at 23:38:46 UTC, Ali Çehreli wrote:
 On 06/16/2017 04:08 PM, Jolly James wrote:
 On Friday, 16 June 2017 at 19:42:44 UTC, Cym13 wrote:
 I'm unclear about your intent: do you want a compile-time list
I am looking for something similar to C#'s generic list.
std.variant.Variant perhaps? https://dlang.org/phobos/std_variant.html import std.variant; void main() { Variant[] arr; arr ~= Variant(42); arr ~= Variant("hello"); } Also consider Algebraic in that module. Ali
Thx, but I do not need to mix different types (using variant). Assuming I use simply an dynamic array, how does one remove an specific item?
Jun 16
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 06/16/2017 05:02 PM, Jolly James wrote:

 I am looking for something similar to C#'s generic list.
 Thx, but I do not need to mix different types (using variant).
If I did well on my quick research, C#'s generic lists are exactly that. :)
 Assuming I use simply an dynamic array, how does one remove an
 specific item?
With std.algorithm.remove: import std.algorithm; void main() { auto arr = [ 1, 2, 3, 4, 5, 6 ]; arr = arr.remove(5); // Removes element at index 3 assert(arr == [ 1, 2, 3, 4, 5 ]); arr = arr.remove!(e => e % 2); // Removes elements with odd values assert(arr == [ 2, 4 ]); } Ali
Jun 16
parent Jolly James <j.j jmail.com> writes:
On Saturday, 17 June 2017 at 00:28:50 UTC, Ali Çehreli wrote:
 On 06/16/2017 05:02 PM, Jolly James wrote:
 If I did well on my quick research, C#'s generic lists are 
 exactly that. :)
C#'s generics allow to specify one datatype T that is used, some kind of similar to a template. So, a generic list can contain only elements of type T. That what makes it generic is that M$ had to implement this class only once and now it can be used with any datatype by simply specifying it ( → List<int> ).
 With std.algorithm.remove:

 import std.algorithm;

 void main() {
     auto arr = [ 1, 2, 3, 4, 5, 6 ];

     arr = arr.remove(5);            // Removes element at index 
 3
     assert(arr == [ 1, 2, 3, 4, 5 ]);

     arr = arr.remove!(e => e % 2);  // Removes elements with 
 odd values
     assert(arr == [ 2, 4 ]);
 }
Thank you very much :)
Jun 16
prev sibling parent Jonathan M Davis via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Friday, June 16, 2017 23:08:58 Jolly James via Digitalmars-d wrote:
 On Friday, 16 June 2017 at 19:42:44 UTC, Cym13 wrote:
 I'm unclear about your intent: do you want a compile-time list
I am looking for something similar to C#'s generic list.
 interface IList<T>
 {

     int Count { get; }

     void Add(T item);
     void Remove(T item);

     T this[int index];

     // [...]

 }
I see that I completely misunderstood the question then. I thought that you were trying to do stuff with lists at compile time. For that, I'd just dynamic arrays, but there's also DList in std.container, and http://code.dlang.org/packages/emsi_containers has several containers. - Jonathan M Davis
Jun 16