www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Compile-time sets and hash tables

reply "Philippe Sigaud" <philippe.sigaud gmail.com> writes:
Hi,

I'm playing with parsers right now, and have to use a lot of 
(small) sets,  sets of sets and hash tables. I'm getting some 
strange behaviour from built-in AAs and I'm a bit blocked by them 
not working at compile-time.

I think I'm ready to code some small set and hash table for my 
own needs, but since I'm no expert in this, I just wanted to 
check if anyone here as some readily available code. My criteria:

- I'm not using millions of elements. The data I'm manipulating 
is more in the 10-1000s range. So much so that I might use 
T*[ubyte.max] as the underlying type.
- I need sets of sets.
- I'd greatly like for those sets or hash tables to be 
CTFE-compatible.

Does anyone have this?
Nov 13 2013
next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 11/14/13, Philippe Sigaud <philippe.sigaud gmail.com> wrote:
 - I'd greatly like for those sets or hash tables to be
 CTFE-compatible.
Well if it's only used in CTFE, I'd imagine a simple struct wrapping an array and doing "!canFind" when adding elements would do the job, no?
Nov 14 2013
prev sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Thu, Nov 14, 2013 at 9:31 PM, Andrej Mitrovic
<andrej.mitrovich gmail.com> wrote:
 On 11/14/13, Philippe Sigaud <philippe.sigaud gmail.com> wrote:
  - I'd greatly like for those sets or hash tables to be
 CTFE-compatible.
Well if it's only used in CTFE, I'd imagine a simple struct wrapping an array and doing "!canFind" when adding elements would do the job, no?
The trick is, I don't use them only during CT evaluation. In that case, yes, linear search would do the trick. But my code is used at runtime and also, from time to time, during compilation (a new Pegged parser engine). Hence my need. But no worries, I spent the day writing and testing code for a small set/hash implementation. It supports user-defined types (it automatically creates a reasonable hashing function), support sets of sets (unlike AA which do not accept other AA as keys) and is both runtime and compile-time compatible. It's a bit slower than the builtin, but I'm using the code only to generate other code, not in performance-critical code. I also wrote a small string mixin that generates a big switch statement and it's 40% faster than a standard AA. That's fun. Not useful, mind, but fun.
Nov 14 2013