www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.dtl - AA indexing behavior

reply Ben Hinkle <Ben_member pathlink.com> writes:
As an experiment I've changed the MinTL AA indexing behavior for HashAA and
SortedAA to give more flexibility:
1) opIndex returns a settable missing value (the property is called 'missing').
The default missing value is Value.init. The Value take(Key) function also
returns the default missing value on failure.
2) added bool contains(Key key) and bool contains(Key key, out Value value)
3) added Value* get(Key key, int action = NullOnMiss) where action can be one of
the three predefined enum values NullOnMiss, ThrowOnMiss and InsertOnMiss

So the old opIn (equivalent to the builtin 'in') is now get(key). Most of the
time, though, the indexing or contains functions will do. For instance
names[phone] = a_name;
Times when a get() is needed would be for fancy things like
(*count.get(word,InsertOnMiss))++
which is how one can mimic the builtin lvalue indexing behavior. One interesting
question is if the get(word,InsertOnMiss) should insert the missing value or
Value.init. Right now I have it inserting Value.init.

Anyhow, comments welcome.
-Ben
Jul 27 2005
parent "Ben Hinkle" <bhinkle mathworks.com> writes:
 3) added Value* get(Key key, int action = NullOnMiss) where action can be 
 one of
 the three predefined enum values NullOnMiss, ThrowOnMiss and InsertOnMiss
slight modification: instead of the action enum I'll make get take an optional bool to distinguish null vs throw and I'll add a new function put(key) that inserts on missing. That way get(key) never modifies the array and the enum can be removed.
Jul 28 2005