Source: site.view [edit]
Function name: addToTrie
Arguments: trie,term,dataType,data
Description: Adds to an existing trie a string term with dataType=data as the terminator.
Page type: webl
Render function:  
Module: siteutil

Page source:

// Given a string, return a new trie that contains (only) the string values
var newTrie = fun(term, dataType, data)
   var res = [. .];
   if term == "" then
      return [. data = [data] .]
   end;

   var ch = Select(term, 0, 1);
   if Size(term) == 1 then
      var d = [. .];
      d[ dataType ] := [data];
      res[ ch ] := d
   else
      var rest = Select(term, 1, Size(term));
      res[ ch ] :=  newTrie(rest, dataType, data)
   end;
   res
end;

var subTrie = trie;

var i = 0;

while (i < Size(term)) do

   var ch = Select(term, i, i+1);

   var t = subTrie[ch] ? nil;

   if (t == nil) then
      subTrie[ ch ] := newTrie(Select(term, i + 1, Size(term)), dataType, data);
      i = Size(term) + 2
   elsif i + 1 == Size(term) then
      var prevData = t[ dataType] ? [];
      t[ dataType ] := prevData + [data];
   else
      subTrie = t;
   end;

   i = i + 1;
end;

trie;