www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Fast linear search for non-null key in slice of Nullable(T, T.max)

reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
If I have

     alias N = Nullable!(T, T nullValue)

fed as template parameter to another template how can I

at compile-time get the `nullValue`

?

I need this to implement fast linear search over a slice of type

     Nullable!(ulong, ulong.max)[]

searching for a non-null key without needing to call a 
combination of Nullable's `isNull` and `get` inside the loop. 
Which is significantly slower than knowing the non-null value to 
search for.  Which also can be implemented using sentinel-based 
search.

In my case it would have been convenient to have public access to

     Nullable!(T, T nullValue)._value

I need this in a hash table of mine that uses open addressing 
that makes use of Nullable to indicate empty slot.
Sep 16 2018
parent reply Neia Neutuladh <neia ikeran.org> writes:
On Sunday, 16 September 2018 at 16:28:20 UTC, Per Nordlöw wrote:
 If I have

     alias N = Nullable!(T, T nullValue)

 fed as template parameter to another template how can I
 at compile-time get the `nullValue` ?
import std.traits; enum nullValue = TemplateArgsOf!(N)[1];
Sep 16 2018
parent Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Sunday, 16 September 2018 at 16:50:32 UTC, Neia Neutuladh 
wrote:
 On Sunday, 16 September 2018 at 16:28:20 UTC, Per Nordlöw wrote:
 If I have

     alias N = Nullable!(T, T nullValue)

 fed as template parameter to another template how can I
 at compile-time get the `nullValue` ?
import std.traits; enum nullValue = TemplateArgsOf!(N)[1];
Thanks. However, I just realized that I need to cast the array of nullables to an array of underlying unnulled types and search that array using an unnulled key. So I have no use for the nullValue, anyway.
Sep 16 2018