I have implemented a basic dictionary type.

Here's how to use it. First for convenience we need these:


fun dflt : opt[string] -> string =
| Some ?x => x
| None => "NONE"
;

fun dflt : opt[string * string ] -> string * string =
| Some ?x => x
| None => "NONE","NONE"
;


Now we can do:

/////////
var x = strdict[string] ();
add x "Hello" "World";
println$ dflt$ get x "Hello"; 
println$ "Missing .." + dflt (get x "silly");
////////

you can also write:

x.add "Hello" "World" 

using the operator . style. We get:

~/felix>flx jj
World
Missing ..NONE

The "dflt" function is  a way to map:

        dflt: opt[T] -> T

so you don't have to keep matching against Some/None.

You can delete entries:

/////////
var found = del x "Hello";
println$ "Found="+ str found;
found = del x "Hello";
println$ "Found="+ str found;
//////

which gives:

Found=true
Found=false

Remember, "del" is a generator, you cannot ignore the returned value!
Or Felix will silently remove the result variable, and thence the deletion!

You can make a dictionary from an association list:

///////
var y = strdict$ list $ ("A","1"), ("D","2"), ("B","3");
/////////
and we have iterators:

////////
match ?k,?vv in y do
  println$ k,vv;
done
////////////

We also have the functions:

dict.first 
dict.get_ge key
dict.get_gt key
dict.get_le key
dict.get_lt key

There are many more functions to add, but this should get you going!

You can make a dictionary of any value type.

CAVEAT: The keys are actually NTBS: null terminated byte strings.
If you have a string key with a null character in it, only the text up to the
first null will be counted. This is a restriction imposed by the implementation,
JudySLArray.

CAVEAT: there's a limit of about 10,000 characters for the key length.

JudySLArray is an order of magnitude faster than STL Map.
It is roughly the same speed as a STL unordered map (hash table).
However it is a superior data structure because it is ordered: you can
get keys out in order, which you cannot do with a hash table.
It is therefore an ideal data structure .. much better than
dictionary types used in Perl, Python, C++, or most other languages.

CAVEAT: the naive implementation using C++ strings pays a penalty,
since Judy uses char arrays, and modifies them on sequential searches.
For functional behaviour using C++ strings we're forced to copy up/copy down
the string data to a buffer which has to be malloced and freed on every call.

Faster variants of the search functions using a C buffer are available and
used in the iterator.

TODO: a lot more nice functions can be added, such as filtering keys
on regexps, a specialised version mixing strdict with dsarray to give
a JSON data structure, converting to association lists, etc etc.

Also a comprehension would be nice.

The implementation is in src/lib/std/strdict.flx.

--
john skaller
skal...@users.sourceforge.net
http://felix-lang.org




------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to