Am I getting this all right?

Chris Nicholson-Sauls ibisbasenji at gmail.com
Thu Dec 14 12:51:04 PST 2006


Jason House wrote:
> An earlier post showed stack with push and pop for an array.  How does one do a
> queue? a set?
> 
> Also, I haven't found a way to check if something is in an associative array or not.
> 
> This may all boil down to me not being able to find a comprehensive list of array
> properties and/or quality documentation on the D language.

Well... I don't know that I've ever done Queue/Set, but it wouldn't be hard.  Quick cheap 
version of a Queue off the top of my head:

# // really only neccessary for completeness
# void qpush (T) (inout T[] haystack, T needle) {
#   haystack ~= needle;
# }
#
# T qpop (T) (inout T[] haystack) {
#   T result = haystack[0];
#
#   haystack = haystack[1 .. $];
#   return result;
# }
#
# // and now in use
# char[][] queue;
#
# queue.qpush("alpha");
# queue.qpush("beta");
# queue.qpush("gamma");
#
# auto item = queue.qpop(); // item == "alpha"

Or if one is using Cashew, you could really just do:

# import cashew .utils .array ;
#
# alias push  qpush ;
# alias shift qpop  ;


As for a Set... well, that might take a class/struct wrapper to do succinctly.  Or, again 
using cashew, you could just make a point of calling .unique() after each insert/concat.

# int[] set = ... ;
#
# set ~= 42, set.unique();

Not very pretty though.

-- Chris Nicholson-Sauls


More information about the Digitalmars-d-learn mailing list