View source code Display the source code in std/uni.d from which this page was generated on github. Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone. Page wiki View or edit the community-maintained wiki page associated with this page.

std.uni.codepoint_trie - multiple declarations

Template codepointTrie

A slightly more general tool for building fixed Trie for the Unicode data.

Specifically unlike codepointSetTrie it's allows creating mappings of dchar to an arbitrary type T.

Arguments

template codepointTrie(T, sizes...);

Functions

Function name Description
codepointTrie

Note

Overload taking CodepointSets will naturally convert only to bool mapping Tries.

Example

// pick characters from the Greek script
auto set = unicode.Greek;

// a user-defined property (or an expensive function)
// that we want to look up
static uint luckFactor(dchar ch)
{
    // here we consider a character lucky
    // if its code point has a lot of identical hex-digits
    // e.g. arabic letter DDAL (\u0688) has a "luck factor" of 2
    ubyte[6] nibbles; // 6 4-bit chunks of code point
    uint value = ch;
    foreach(i; 0..6)
    {
        nibbles[i] = value & 0xF;
        value >>= 4;
    }
    uint luck;
    foreach(n; nibbles)
        luck = cast(uint)max(luck, count(nibbles[], n));
    return luck;
}

// only unsigned built-ins are supported at the moment
alias LuckFactor = BitPacked!(uint, 3);

// create a temporary associative array (AA)
LuckFactor[dchar] map;
foreach(ch; set.byCodepoint)
    map[ch] = luckFactor(ch);

// bits per stage are chosen randomly, fell free to optimize
auto trie = codepointTrie!(LuckFactor, 8, 5, 8)(map);

// from now on the AA is not needed
foreach(ch; set.byCodepoint)
    assert(trie[ch] == luckFactor(ch)); // verify
// CJK is not Greek, thus it has the default value
assert(trie['\u4444'] == 0);
// and here is a couple of quite lucky Greek characters:
// Greek small letter epsilon with dasia
assert(trie['\u1F11'] == 3);
// Ancient Greek metretes sign
assert(trie['\U00010181'] == 3);

Alias CodepointTrie

Type of Trie as generated by codepointTrie function.

Declaration

alias CodepointTrie(T, sizes...) = typeof(TrieBuilder!(T,dchar,lastDchar+1,Prefix)(T.init).build());

Authors

Dmitry Olshansky

License

Boost License 1.0.

Comments