www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Creating an array of unique elements

reply Guilherme Vieira <n2.nitrogen gmail.com> writes:
--001636416495f9dbf804985f311b
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: quoted-printable

Hi, guys. =97 said the shy newcomer.

I've started reading The D Programming Language just yesterday and I'm
making my first attempts to dig into D now. I must say I'm loving the
language beyond recognition. I never thought there was a language out there
that had everything I ever wanted in C++ (I even considered developing my
own language before knowing D!).

Right now I'm wondering how's the best way to create a dynamic array object
which will only accept "unique" elements (i.e., elements !=3D from the
existing elements in the array).

I wanted a class that kept all the functionality of an array (e.g. being th=
e
right range types so that they can be passed to std.format.formatValue and
trigger the right specialization) for maximum integration with the standard
library. I thought about writing a class template privately containing an
array and redirecting everything but the assignment/insertion operations to
it. All ways of placing an object that was already there should throw an
exception, but everything else should work the same.

Doing it this way is a lot of work for a simple thing, so some sort of
internal alert in me tell me I might just be "doing-it-wrong". I want to
know what your ideas are.

I want some way to achieve this sort of thing:

import myproject.helpers.UniqueArray;

void main()
{
    auto a0 =3D [1, 2, 3];

    // I'm not yet sure how to go about the constructor, since:

    auto a1 =3D UniqueArray!(int)(a0[1 .. $]); // error: should not be able=
 to
internally hold reference to
                                             // a raw array since this coul=
d
be used to break the "unique
                                             // elements" contract promise
of UniqueArray
                                             // copy of elements can be
considered, but I'd rather
                                             // have clients copy the array
themselves so that they
                                             // know it is happening

    auto a2 =3D UniqueArray!(int)(a0[1 .. $].dup); // should be fine if D h=
ad
some sort of non-const
                                                 // rvalue reference
support, but I think it does not;
                                                 // am I wrong?

    auto a3 =3D UniqueArray!(int)(a0[1 .. $].idup); // semantically pleasin=
g
at first sight, but
                                                  // suboptimal: the
constructor would have to copy
                                                  // the passed array again
to get rid of immutability

    auto a4 =3D bestOptionOutOf(a1, a2, a3); // (:

    a4[1 .. $] =3D [3, 4, 5]; // ok: would first construct a UniqueArray ou=
t
of the rvalue (thus ensuring
                            // "uniqueness" of elements) and then would wor=
k
like a usual slice
                            // assignment

    a4 ~=3D 5; // throws exception: 5 is already in the array!
    a4 ~=3D 6; // ok: 6 is not there

    writeln(a4); // ok, output: [2, 3, 4, 5, 6]
                 // could just implement UniqueArray.toString() for this to
work, but making UniqueArray
                 // properly model the ranges an array models solves this
problem and others at the same
                 // time

    auto a5 =3D a4.dup; // all properties of an array, such as dup here,
should hold and overall
                      // the object should behave as one would expect from
an array

    int[] a6 =3D a5; // error: obviously shouldn't work since a6 could then=
 be
used to break the
                   // UniqueArray contract

}


What do you think?

--=20
Atenciosamente / Sincerely,
Guilherme ("n2liquid") Vieira

--001636416495f9dbf804985f311b
Content-Type: text/html; charset=windows-1252
Content-Transfer-Encoding: quoted-printable

Hi, guys.=A0<span class=3D"Apple-style-span" style=3D"font-family: sans-ser=
if; font-size: 12px; line-height: 18px; -webkit-border-horizontal-spacing: =
2px; -webkit-border-vertical-spacing: 2px; ">=97 said the shy newcomer.</sp=
an><div>
<font class=3D"Apple-style-span" face=3D"sans-serif"><span class=3D"Apple-s=
tyle-span" style=3D"font-size: 12px; line-height: 18px; -webkit-border-hori=
zontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;"><br></span></fo=
nt></div>
<div><font class=3D"Apple-style-span" face=3D"sans-serif"><span class=3D"Ap=
ple-style-span" style=3D"font-size: 12px; line-height: 18px; -webkit-border=
-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">I&#39;ve s=
tarted reading The D Programming Language just yesterday and I&#39;m making=
 my first attempts to dig into D now. I must say I&#39;m loving the languag=
e beyond recognition. I never thought there was a language out there that h=
ad everything I ever wanted in C++ (I even considered developing my own lan=
guage before knowing D!).</span></font></div>
<div><font class=3D"Apple-style-span" face=3D"sans-serif"><span class=3D"Ap=
ple-style-span" style=3D"font-size: 12px; line-height: 18px; -webkit-border=
-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;"><br></span=
</font></div>

ple-style-span" style=3D"font-size: 12px; line-height: 18px; -webkit-border= -horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">Right now = I&#39;m wondering how&#39;s the best way to create a dynamic array object w= hich will only accept &quot;unique&quot; elements (i.e., elements !=3D from= the existing elements in the array).</span></font></div> <div><font class=3D"Apple-style-span" face=3D"sans-serif"><span class=3D"Ap= ple-style-span" style=3D"font-size: 12px; line-height: 18px; -webkit-border= -horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;"><br></span=
</font></div>

ple-style-span" style=3D"font-size: 12px; line-height: 18px; -webkit-border= -horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">I wanted a= class that kept all the functionality of an array (e.g. being the right ra= nge types so that they can be passed to std.format.formatValue and trigger = the right specialization) for maximum integration with the standard library= . I thought about writing a class template privately containing an array an= d redirecting everything but the assignment/insertion operations to it. All= ways of placing an object that was already there should throw an exception= , but everything else should work the same.</span></font></div> <div><font class=3D"Apple-style-span" face=3D"sans-serif"><span class=3D"Ap= ple-style-span" style=3D"font-size: 12px; line-height: 18px; -webkit-border= -horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;"><br></span=
</font></div>

ple-style-span" style=3D"font-size: 12px; line-height: 18px; -webkit-border= -horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">Doing it t= his way is a lot of work for a simple thing, so some sort of internal alert= in me tell me I might just be &quot;doing-it-wrong&quot;. I want to know w= hat your ideas are.</span></font></div> <div><font class=3D"Apple-style-span" face=3D"sans-serif"><span class=3D"Ap= ple-style-span" style=3D"font-size: 12px; line-height: 18px; -webkit-border= -horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;"><br></span=
</font></div>

ple-style-span" style=3D"font-size: 12px; line-height: 18px; -webkit-border= -horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">I want som= e way to achieve this sort of thing:</span></font></div> <div><font class=3D"Apple-style-span" face=3D"sans-serif"><span class=3D"Ap= ple-style-span" style=3D"font-size: 12px; line-height: 18px; -webkit-border= -horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;"><br></span=
</font></div>

border: none; padding: 0px;"><div><span class=3D"Apple-style-span" style= =3D"font-size: 12px; line-height: 18px; -webkit-border-horizontal-spacing: = 2px; -webkit-border-vertical-spacing: 2px; "><font class=3D"Apple-style-spa= n" face=3D"&#39;courier new&#39;, monospace">import myproject.helpers.Uniqu= eArray;</font></span></div> <div><span class=3D"Apple-style-span" style=3D"font-size: 12px; line-height= : 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spa= cing: 2px; "><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;= , monospace"><br> </font></span></div><div><span class=3D"Apple-style-span" style=3D"font-siz= e: 12px; line-height: 18px; -webkit-border-horizontal-spacing: 2px; -webkit= -border-vertical-spacing: 2px; "><font class=3D"Apple-style-span" face=3D"&= #39;courier new&#39;, monospace">void main()</font></span></div> <div><span class=3D"Apple-style-span" style=3D"font-size: 12px; line-height= : 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spa= cing: 2px; "><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;= , monospace">{</font></span></div> <div><span class=3D"Apple-style-span" style=3D"font-size: 12px; line-height= : 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spa= cing: 2px; "><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;= , monospace">=A0=A0 =A0auto a0 =3D [1, 2, 3];</font></span></div> <div><span class=3D"Apple-style-span" style=3D"font-size: 12px; line-height= : 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spa= cing: 2px; "><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;= , monospace"><br> </font></span></div><div><span class=3D"Apple-style-span" style=3D"font-siz= e: 12px; line-height: 18px; -webkit-border-horizontal-spacing: 2px; -webkit= -border-vertical-spacing: 2px; "><font class=3D"Apple-style-span" face=3D"&= #39;courier new&#39;, monospace">=A0=A0 =A0// I&#39;m not yet sure how to g= o about the constructor, since:</font></span></div> <div><span class=3D"Apple-style-span" style=3D"font-size: 12px; line-height= : 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spa= cing: 2px; "><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;= , monospace"><br> </font></span></div><div><span class=3D"Apple-style-span" style=3D"font-siz= e: 12px; line-height: 18px; -webkit-border-horizontal-spacing: 2px; -webkit= -border-vertical-spacing: 2px; "><font class=3D"Apple-style-span" face=3D"&= #39;courier new&#39;, monospace">=A0=A0 =A0auto a1 =3D UniqueArray!(int)(a0= [1 .. $]); // error: should not be able to internally hold reference to</fo= nt></span></div> <div><span class=3D"Apple-style-span" style=3D"font-size: 12px; line-height= : 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spa= cing: 2px; "><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;= , monospace">=A0=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0 =A0 =A0 =A0 =A0 //=A0</font></span><span class=3D"Apple-style-= span" style=3D"font-family: &#39;courier new&#39;, monospace; font-size: 12= px; line-height: 18px; -webkit-border-horizontal-spacing: 2px; -webkit-bord= er-vertical-spacing: 2px; ">a raw=A0</span><span class=3D"Apple-style-span"= style=3D"font-family: &#39;courier new&#39;, monospace; font-size: 12px; l= ine-height: 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-ve= rtical-spacing: 2px; ">array since this could be used to break the &quot;un= ique</span></div> <div><span class=3D"Apple-style-span" style=3D"font-size: 12px; line-height= : 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spa= cing: 2px; "><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;= , monospace">=A0=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0 =A0 =A0 =A0 =A0 // elements&quot; contract promise of UniqueAr= ray</font></span></div> <div><span class=3D"Apple-style-span" style=3D"font-size: 12px; line-height= : 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spa= cing: 2px; "><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;= , monospace">=A0=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0 =A0 =A0 =A0 =A0 // copy of elements can be considered, but I&#= 39;d rather</font></span></div> <div><span class=3D"Apple-style-span" style=3D"font-size: 12px; line-height= : 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spa= cing: 2px; "><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;= , monospace">=A0=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0 =A0 =A0 =A0 =A0 // have clients copy the array themselves so t= hat they</font></span></div> <div><span class=3D"Apple-style-span" style=3D"font-size: 12px; line-height= : 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spa= cing: 2px; "><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;= , monospace">=A0=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0 =A0 =A0 =A0 =A0 // know it is happening</font></span></div> <div><span class=3D"Apple-style-span" style=3D"font-size: 12px; line-height= : 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spa= cing: 2px; "><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;= , monospace"><br> </font></span></div><div><span class=3D"Apple-style-span" style=3D"font-siz= e: 12px; line-height: 18px; -webkit-border-horizontal-spacing: 2px; -webkit= -border-vertical-spacing: 2px; "><font class=3D"Apple-style-span" face=3D"&= #39;courier new&#39;, monospace">=A0=A0 =A0auto a2 =3D UniqueArray!(int)(</= font></span><span class=3D"Apple-style-span" style=3D"font-family: &#39;cou= rier new&#39;, monospace; font-size: 12px; line-height: 18px; -webkit-borde= r-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">a0[1 .. = $].dup); // should be fine if D had some sort of</span><span class=3D"Apple= -style-span" style=3D"font-family: &#39;courier new&#39;, monospace; font-s= ize: 12px; line-height: 18px; -webkit-border-horizontal-spacing: 2px; -webk= it-border-vertical-spacing: 2px; ">=A0non-const</span></div> <div><span class=3D"Apple-style-span" style=3D"font-family: &#39;courier ne= w&#39;, monospace; font-size: 12px; line-height: 18px; -webkit-border-horiz= ontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">=A0=A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 // rvalue reference support, but I think it does not;</span></div> <div><span class=3D"Apple-style-span" style=3D"font-family: &#39;courier ne= w&#39;, monospace; font-size: 12px; line-height: 18px; -webkit-border-horiz= ontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">=A0=A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 // am I wrong?</span></div> <div><span class=3D"Apple-style-span" style=3D"font-family: &#39;courier ne= w&#39;, monospace; font-size: 12px; line-height: 18px; -webkit-border-horiz= ontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "><br></span></di= v> <div><span class=3D"Apple-style-span" style=3D"font-family: &#39;courier ne= w&#39;, monospace; font-size: 12px; line-height: 18px; -webkit-border-horiz= ontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">=A0=A0 =A0auto = a3 =3D UniqueArray!(int)(</span><span class=3D"Apple-style-span" style=3D"f= ont-family: &#39;courier new&#39;, monospace; font-size: 12px; line-height:= 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spac= ing: 2px; ">a0[1 .. $].idup</span><span class=3D"Apple-style-span" style=3D= "font-family: &#39;courier new&#39;, monospace; font-size: 12px; line-heigh= t: 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-sp= acing: 2px; ">); // semantically pleasing at first sight, but</span></div> <div><span class=3D"Apple-style-span" style=3D"font-family: &#39;courier ne= w&#39;, monospace; font-size: 12px; line-height: 18px; -webkit-border-horiz= ontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">=A0=A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0// suboptimal: the constructor would have to copy</span></div> <div><span class=3D"Apple-style-span" style=3D"font-family: &#39;courier ne= w&#39;, monospace; font-size: 12px; line-height: 18px; -webkit-border-horiz= ontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">=A0=A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0// the passed array again to get rid of immutability</span></di= v> <div><span class=3D"Apple-style-span" style=3D"font-family: &#39;courier ne= w&#39;, monospace; font-size: 12px; line-height: 18px; -webkit-border-horiz= ontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "><br></span></di= v> <div><span class=3D"Apple-style-span" style=3D"font-family: &#39;courier ne= w&#39;, monospace; font-size: 12px; line-height: 18px; -webkit-border-horiz= ontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">=A0=A0 =A0auto = a4 =3D bestOptionOutOf(a1, a2, a3); // (:</span></div> <div><span class=3D"Apple-style-span" style=3D"font-family: &#39;courier ne= w&#39;, monospace; font-size: 12px; line-height: 18px; -webkit-border-horiz= ontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "><br></span></di= v> <div><span class=3D"Apple-style-span" style=3D"font-size: 12px; line-height= : 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spa= cing: 2px; "><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;= , monospace">=A0=A0 =A0a4[1 .. $] =3D [3, 4, 5]; // ok: would first constru= ct a UniqueArray out of the rvalue (thus ensuring</font></span></div> <div><span class=3D"Apple-style-span" style=3D"font-size: 12px; line-height= : 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spa= cing: 2px; "><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;= , monospace">=A0=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0// &= quot;uniqueness&quot; of elements) and then would work like a usual slice</= font></span></div> <div><span class=3D"Apple-style-span" style=3D"font-size: 12px; line-height= : 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spa= cing: 2px; "><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;= , monospace">=A0=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0//= =A0</font></span><span class=3D"Apple-style-span" style=3D"font-family: &#3= 9;courier new&#39;, monospace; font-size: 12px; line-height: 18px; -webkit-= border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">ass= ignment</span></div> <div><span class=3D"Apple-style-span" style=3D"font-family: &#39;courier ne= w&#39;, monospace; font-size: 12px; line-height: 18px; -webkit-border-horiz= ontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "><br></span></di= v> <div><span class=3D"Apple-style-span" style=3D"font-family: &#39;courier ne= w&#39;, monospace; font-size: 12px; line-height: 18px; -webkit-border-horiz= ontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">=A0=A0 =A0a4 ~= =3D 5; // throws exception: 5 is already in the array!</span></div> <div><span class=3D"Apple-style-span" style=3D"font-family: &#39;courier ne= w&#39;, monospace; font-size: 12px; line-height: 18px; -webkit-border-horiz= ontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">=A0=A0 =A0a4 ~= =3D 6; // ok: 6 is not there</span></div> <div><span class=3D"Apple-style-span" style=3D"font-family: &#39;courier ne= w&#39;, monospace; font-size: 12px; line-height: 18px; -webkit-border-horiz= ontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "><br></span></di= v> <div><span class=3D"Apple-style-span" style=3D"font-family: &#39;courier ne= w&#39;, monospace; font-size: 12px; line-height: 18px; -webkit-border-horiz= ontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">=A0=A0 =A0</spa= n><span class=3D"Apple-style-span" style=3D"font-family: &#39;courier new&#= 39;, monospace; font-size: 12px; line-height: 18px; -webkit-border-horizont= al-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">writeln(a4); // ok= , output: [2, 3, 4, 5, 6]</span></div> <div><span class=3D"Apple-style-span" style=3D"font-family: &#39;courier ne= w&#39;, monospace; font-size: 12px; line-height: 18px; -webkit-border-horiz= ontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">=A0=A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 // could just implement UniqueArray.toString() for this= to work, but making UniqueArray</span></div> <div><span class=3D"Apple-style-span" style=3D"font-family: &#39;courier ne= w&#39;, monospace; font-size: 12px; line-height: 18px; -webkit-border-horiz= ontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">=A0=A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 //=A0</span><span class=3D"Apple-style-span" style=3D"f= ont-family: &#39;courier new&#39;, monospace; font-size: 12px; line-height:= 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spac= ing: 2px; ">properly=A0</span><span class=3D"Apple-style-span" style=3D"fon= t-family: &#39;courier new&#39;, monospace; font-size: 12px; line-height: 1= 8px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacin= g: 2px; ">model the ranges an array models solves this problem and others a= t the same</span></div> <div><span class=3D"Apple-style-span" style=3D"font-family: &#39;courier ne= w&#39;, monospace; font-size: 12px; line-height: 18px; -webkit-border-horiz= ontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">=A0=A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 // time</span></div> <div><span class=3D"Apple-style-span" style=3D"font-size: 12px; line-height= : 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spa= cing: 2px; "><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;= , monospace"><br> </font></span></div><div><span class=3D"Apple-style-span" style=3D"font-siz= e: 12px; line-height: 18px; -webkit-border-horizontal-spacing: 2px; -webkit= -border-vertical-spacing: 2px; "><font class=3D"Apple-style-span" face=3D"&= #39;courier new&#39;, monospace">=A0=A0 =A0auto a5 =3D a4.dup; // all prope= rties of an array, such as dup here, should hold and overall</font></span><= /div> <div><span class=3D"Apple-style-span" style=3D"font-size: 12px; line-height= : 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spa= cing: 2px; "><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;= , monospace">=A0=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0// the object sh= ould behave as one would expect from an array</font></span></div> <div><span class=3D"Apple-style-span" style=3D"font-size: 12px; line-height= : 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spa= cing: 2px; "><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;= , monospace"><br> </font></span></div><div><span class=3D"Apple-style-span" style=3D"font-siz= e: 12px; line-height: 18px; -webkit-border-horizontal-spacing: 2px; -webkit= -border-vertical-spacing: 2px; "><font class=3D"Apple-style-span" face=3D"&= #39;courier new&#39;, monospace">=A0=A0 =A0int[] a6 =3D a5; // error: obvio= usly shouldn&#39;t work since a6 could then be used to break the</font></sp= an></div> <div><span class=3D"Apple-style-span" style=3D"font-size: 12px; line-height= : 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spa= cing: 2px; "><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;= , monospace">=A0=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 // UniqueArray contract= </font></span></div> </blockquote><blockquote class=3D"webkit-indent-blockquote" style=3D"margin= : 0 0 0 40px; border: none; padding: 0px;"><div><span class=3D"Apple-style-= span" style=3D"font-size: 12px; line-height: 18px; -webkit-border-horizonta= l-spacing: 2px; -webkit-border-vertical-spacing: 2px; "><font class=3D"Appl= e-style-span" face=3D"&#39;courier new&#39;, monospace">}</font></span></di= v> </blockquote><span class=3D"Apple-style-span" style=3D"font-size: 12px; lin= e-height: 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vert= ical-spacing: 2px; "><div><font class=3D"Apple-style-span" face=3D"arial, h= elvetica, sans-serif"><span class=3D"Apple-style-span" style=3D"font-size: = 12px; line-height: 18px; -webkit-border-horizontal-spacing: 2px; -webkit-bo= rder-vertical-spacing: 2px; "><br> </span></font></div><font class=3D"Apple-style-span" face=3D"arial, helveti= ca, sans-serif">What do you think?</font><br></span><div><br>-- <br>Atencio= samente / Sincerely,<br>Guilherme (&quot;n2liquid&quot;) Vieira<br> </div> --001636416495f9dbf804985f311b--
Dec 26 2010
next sibling parent "Manfred_Nowak" <svv1999 hotmail.com> writes:
Guilherme Vieira wrote:

 dynamic array object which will only accept "unique" elements

What are your requirements so that you call this data structure "array"? -manfred
Dec 27 2010
prev sibling next sibling parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 27.12.2010 10:22, Guilherme Vieira wrote:
 Hi, guys.  said the shy newcomer.

 I've started reading The D Programming Language just yesterday and I'm 
 making my first attempts to dig into D now. I must say I'm loving the 
 language beyond recognition. I never thought there was a language out 
 there that had everything I ever wanted in C++ (I even considered 
 developing my own language before knowing D!).

 Right now I'm wondering how's the best way to create a dynamic array 
 object which will only accept "unique" elements (i.e., elements != 
 from the existing elements in the array).

 I wanted a class that kept all the functionality of an array (e.g. 
 being the right range types so that they can be passed to 
 std.format.formatValue and trigger the right specialization) for 
 maximum integration with the standard library. I thought about writing 
 a class template privately containing an array and redirecting 
 everything but the assignment/insertion operations to it. All ways of 
 placing an object that was already there should throw an exception, 
 but everything else should work the same.

 Doing it this way is a lot of work for a simple thing, so some sort of 
 internal alert in me tell me I might just be "doing-it-wrong". I want 
 to know what your ideas are.

 I want some way to achieve this sort of thing:

     import myproject.helpers.UniqueArray;

     void main()
     {
         auto a0 = [1, 2, 3];

         // I'm not yet sure how to go about the constructor, since:

         auto a1 = UniqueArray!(int)(a0[1 .. $]); // error: should not
     be able to internally hold reference to

I suspect the best would be to copy whatever got passed to constructor, since to provide such contracts UniqueArray needs to own data. Also you could use fancy homogeneous variadic function for ctor: this(T[] args...){ _payload = args.dup;//assume member _payload holds internal array } Then auto a = UniqueArray!(int)(1,2,3,4);//fills array with elements [1,2,3,4] auto a = UniqueArray!(int)([5,6,7]); //works with the same signature
                                                  // a raw array since
     this could be used to break the "unique
                                                  // elements" contract
     promise of UniqueArray
                                                  // copy of elements
     can be considered, but I'd rather
                                                  // have clients copy
     the array themselves so that they
                                                  // know it is happening

         auto a2 = UniqueArray!(int)(a0[1 .. $].dup); // should be fine
     if D had some sort of non-const
                                                      // rvalue
     reference support, but I think it does not;
                                                      // am I wrong?

         auto a3 = UniqueArray!(int)(a0[1 .. $].idup); // semantically
     pleasing at first sight, but
                                                       // suboptimal:
     the constructor would have to copy
                                                       // the passed
     array again to get rid of immutability

         auto a4 = bestOptionOutOf(a1, a2, a3); // (:

         a4[1 .. $] = [3, 4, 5]; // ok: would first construct a
     UniqueArray out of the rvalue (thus ensuring
                                 // "uniqueness" of elements) and then
     would work like a usual slice
                                 // assignment

         a4 ~= 5; // throws exception: 5 is already in the array!
         a4 ~= 6; // ok: 6 is not there

     writeln(a4); // ok, output: [2, 3, 4, 5, 6]
                      // could just implement UniqueArray.toString()
     for this to work, but making UniqueArray
                      // properly model the ranges an array models
     solves this problem and others at the same
                      // time

         auto a5 = a4.dup; // all properties of an array, such as dup
     here, should hold and overall
                           // the object should behave as one would
     expect from an array

         int[] a6 = a5; // error: obviously shouldn't work since a6
     could then be used to break the
                        // UniqueArray contract

     }


 What do you think?

 -- 
 Atenciosamente / Sincerely,
 Guilherme ("n2liquid") Vieira

-- Dmitry Olshansky
Dec 27 2010
prev sibling parent sybrandy <sybrandy gmail.com> writes:
I've done something like this before using associative arrays.  I would 
rely on the fact that the keys have to be unique to produce my unique 
array.  So, in this case, any value you want to store you would save 
like this:

array[value] = 1;

Regardless of whether or not the value already exists, this will work 
and ensure uniqueness of the keys.  To determine if a value is already 
part of the array, you can check for the key like this:

if (value in array)

You can later get the values like this:

values = array.keys;

Not sure if this will do everything you want, but it seems to be cleaner 
than having to ensure an array is unique after every insertion.

Casey
Dec 27 2010