I re-read your original post. There are iterators available for
Associative KeyIterator{T<:Associative}, ValueIterator{T<:Associative}
and the usual iterator which returns key and value as a tuple. These
are all also available for the dicts in DataStructures.jl. So you can
do
for (key, val) in dict
...
end
or
for key in KeyIterator(dict)
...
end
The OrderedDict there keeps to order of insertion. I think there is no
dict which keeps the keys sorted.
On Wed, 2014-06-18 at 12:41,
vav...@uwaterloo.ca wrote:
> Dear Julia colleagues,
>
> Earlier I asked whether there is a datatype similar to "map<K,D>" from C++,
> and nobody answered, so I decided as my first nontrivial Julia program to
> write such a type. This is an associative array (i.e., a dictionary), with
> the added property that the keys have a sort-order, and it is possible to
> loop over the items in the array in this order. I am using a 2-3 tree as
> the implementation. My main data structure looks like this:
>
> type Map{K,D}
> ...
> function Map{K,D}()
> ....
> end
> end
Probably better use a name containing `Dict` as that is the convention
in Julia. Also, probably you want to make it a subtype of
Associative(K,V}. Have a look at base/dict.jl.
> But I am stuck right at the outset with two very basic Julia questions.
> First, what is the syntax for a user of my datatype to declare a variable
> to be an empty map of a certain type? In other words, suppose the user
> wants a map from strings to strings:
>
> function test1()
> m1 = Map{ASCIIString, ASCIIString}()
> m1["hello"] = "jello"
> end
>
>
> This gave the error: ERROR: no method Map{ASCIIString, ASCIIString}().
> What is the right syntax to invoke the default constructor for an empty
> map? I couldn't figure this out from the manual; in the manual, the
> constructors either take arguments or else they are constructors for
> built-ins (like arrays) that have a special syntax like a = Int[].
Presumably you've read:
http://docs.julialang.org/en/latest/manual/constructors/
Anyway, this is a often encountered problem. Have a look through the
mailing list. Basically, if you define an inner constructor you also
have to define an outer one.
> Second, at various points in the code, I need to use a default or blank
> instance of either K (the key type) or D (the datatype). In other words, I
> need to initialize an array element whose type is a composite type that has
> some K or D fields; these need to be initialized to some arbitrary K or D
> value that won't ever be used. What is the proper syntax to initialize a
> field k to have a value of type K (where K is a parameter type in my code),
> where the value doesn't matter (can be any default).
Array(Int, 10)
or
Array(String, 10)