Ah, yeah. I think you're right. Set is exactly what I need, and the fact that it works with hashes is even better. A pity D still doesn't have it, since it looks very useful, but thanks for your response. I'll take a look at your implementation later.<div>
<br></div><div>Is there any prevision for sets in core D or Phobos?</div><div><br></div><div>-- <br>Atenciosamente / Sincerely,<br>Guilherme ("n2liquid") Vieira</div><div><br></div><div><br><div class="gmail_quote">
On Mon, Dec 27, 2010 at 11:39 AM, spir <span dir="ltr"><<a href="mailto:denis.spir@gmail.com">denis.spir@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">On Mon, 27 Dec 2010 05:22:14 -0200<br>
Guilherme Vieira <<a href="mailto:n2.nitrogen@gmail.com">n2.nitrogen@gmail.com</a>> wrote:<br>
<br>
> Right now I'm wondering how's the best way to create a dynamic array object<br>
> which will only accept "unique" elements (i.e., elements != from the<br>
> existing elements in the array).<br>
<br>
</div>(Take my words with precaution because I don not know D very well myself.)<br>
<br>
Hello Guilherme. If I understand your purpose correctly, what you're trying to define is a set, not an array, in the common sense of the terms in programming. To ensure uniqueness, you'd need to check whether the element exits already, which can only be very slow using an array (O(n)): you need to traverse the array element per element. Sets instead are build using data structures that allow this check to be far faster, by looking up a given element in a more clever way.<br>

<br>
There is no builtin Set type in D yet. The simplest way (and maybe the best) would be use associative arrays where keys would be actual elements and values just fake. (e in set) would tell you what you need. Depending on your requirements, trying to put an existing element would just put it again with no change, or should throw an error. In the latter case, you need to check it yourself or build a wrapper type (struct or class) around builtin associative arrays.<br>

<br>
For instance:<br>
    Existence[Element] set;<br>
    Element[] elements = ['a','c','e'];<br>
    Element[] values = ['a','b','c','d','e'];<br>
    foreach (element ; elements)<br>
        set[element] = EXISTS;<br>
    // Note: 'in' actually return a pointer to element.<br>
    foreach (value ; values)<br>
        writeln(cast(bool)(value in set));<br>
<br>
Note: I have a Set type in stock at <a href="https://bitbucket.org/denispir/denispir-d/src/b543fb352803/collections.d" target="_blank">https://bitbucket.org/denispir/denispir-d/src/b543fb352803/collections.d</a>, but wonder whether it's really necessary given the above. But you can have a look to see how it's done (this would give you some hints about "language methods" called 'opSomething', see TDPL's index).<br>

<br>
<br>
Denis<br>
-- -- -- -- -- -- --<br>
vit esse estrany ☣<br>
<font color="#888888"><br>
<a href="http://spir.wikidot.com" target="_blank">spir.wikidot.com</a><br><br></font></blockquote></div>
</div>