www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Checking all elements are unique.

reply Dorian Haglund <dorian.haglund gmail.com> writes:
Hello,

I have an array of objects of class C which contain a id member.
I want to figure out if all the id members are unique using 
functional primitives.

For example, if I have:

class C
{
   int id;
}

and an array of C 'Cs';

My idea was to do:

auto ids = Cs.map!(c => c.id);
assert(equal(ids.sort().uniq(), ids.sort()));

But it doesn't compile because I can't can call sort on ids.

Any idea why ? and how to solve my initial problem, which is to 
check all ids are unique.

Regards,

Dorian
Aug 31 2016
next sibling parent Edwin van Leeuwen <edder tkwsping.nl> writes:
On Wednesday, 31 August 2016 at 07:40:39 UTC, Dorian Haglund 
wrote:
 Hello,

 I have an array of objects of class C which contain a id member.
 I want to figure out if all the id members are unique using 
 functional primitives.

 For example, if I have:

 class C
 {
   int id;
 }

 and an array of C 'Cs';

 My idea was to do:

 auto ids = Cs.map!(c => c.id);
 assert(equal(ids.sort().uniq(), ids.sort()));

 But it doesn't compile because I can't can call sort on ids.

 Any idea why ? and how to solve my initial problem, which is to 
 check all ids are unique.

 Regards,

 Dorian
Sort require an indexable array. You can convert an insertRange to an indexable array with .array: ids.array.sort() You can also directly sort on id Cs.array.sort!((a,b) => a.id < b.id);
Aug 31 2016
prev sibling next sibling parent reply Andrea Fontana <nospam example.com> writes:
On Wednesday, 31 August 2016 at 07:40:39 UTC, Dorian Haglund 
wrote:
 Hello,

 I have an array of objects of class C which contain a id member.
 I want to figure out if all the id members are unique using 
 functional primitives.

 For example, if I have:

 class C
 {
   int id;
 }

 and an array of C 'Cs';

 My idea was to do:

 auto ids = Cs.map!(c => c.id);
 assert(equal(ids.sort().uniq(), ids.sort()));

 But it doesn't compile because I can't can call sort on ids.

 Any idea why ? and how to solve my initial problem, which is to 
 check all ids are unique.

 Regards,

 Dorian
Something like this: https://dpaste.dzfl.pl/9fa55b2a7927 ? Andrea
Aug 31 2016
next sibling parent Dorian Haglund <dorian.haglund gmail.com> writes:
 Edwin: Thank you for the insight about indexed range.

 Adrea: Thanks, this looks good. Even if I found it a little 
obscure at first sight, it's better than my previous solution.
Aug 31 2016
prev sibling parent Rene Zwanenburg <renezwanenburg gmail.com> writes:
On Wednesday, 31 August 2016 at 08:38:11 UTC, Andrea Fontana 
wrote:
 Something like this: https://dpaste.dzfl.pl/9fa55b2a7927 ?

 Andrea
Or use findAdjacent: auto idsAreUnique = ids.array.sort.findAdjacent.empty; http://dlang.org/phobos/std_algorithm_searching.html#.findAdjacent
Aug 31 2016
prev sibling parent pineapple <meapineapple gmail.com> writes:
On Wednesday, 31 August 2016 at 07:40:39 UTC, Dorian Haglund 
wrote:
 Hello,

 I have an array of objects of class C which contain a id member.
 I want to figure out if all the id members are unique using 
 functional primitives.

 For example, if I have:

 class C
 {
   int id;
 }

 and an array of C 'Cs';

 My idea was to do:

 auto ids = Cs.map!(c => c.id);
 assert(equal(ids.sort().uniq(), ids.sort()));

 But it doesn't compile because I can't can call sort on ids.

 Any idea why ? and how to solve my initial problem, which is to 
 check all ids are unique.

 Regards,

 Dorian
Your post inspired me to write this addition to my D library, I'll commit it later today but you can use it straightaway by just adding this file. It will be far more efficient than any of the other solutions posted here. The file - http://pastebin.com/RN2nagEn The library - https://github.com/pineapplemachine/mach.d Example usage: import std.stdio; import mach.range.unique; class C{ this(int id){this.id = id;} int id; } auto c0 = [new C(0), new C(1), new C(2), new C(3)]; auto c1 = [new C(0), new C(1), new C(2), new C(3), new C(0)]; c0.unique!(c => c.id).writeln; // true c1.unique!(c => c.id).writeln; // false
Aug 31 2016