% % create b-tree lookup for alphabetic names % % stack: name-to-add % Tree.Off % % descend the tree by seeking the remaining string % take each letter and seek it as tree member at the % current parse level % keep checking tree membership adding each remaining % letter, if no characters left, this is a new branch to be % inserted into the tree % root\ 0 { % % stack: name-to-add parse-length % % check the next letter combo against the current branch % *alphnpar { % % there was another letter to add to the combo % *alphnpat } { % % there was not another letter to add to the combo % cycle through this levels children to see if this combo % shares the leading part of any of them % % stack: name-to-add parse-length % first\? { % % check remaining seek string against child nodes % % stack: name-to-add parse-length child-node % { % % stack: name-to-add parse-length child-node % *alphnpau { % % there was a match of at least one character % % stack: name-to-add parse-length child-node position % % save the current branch into buffer file % exch % % stack: name-to-add parse-length position child-node % % % 0 - item w/children 1 - item only 2 - children only % dup "CopyItem" do Mep.Remove % % stack: name-to-add parse-length position % % create a new sibling with the common text % rot dup third - left % % stack: parse-length position name-to-add common % newchild % % stack: parse-length position name-to-add common-node % _\ dup "LoadItem" do Mep._Below pop % % stack: parse-length position name-to-add orig-node % dup Tree._Text % % stack: parse-length position name-to-add orig-node orig-text % fourth - fromleft % % stack: parse-length position name-to-add orig-node reduced-orig % exch Tree.!Text % % stack: parse-length position name-to-add % exch - fromleft % % stack: parse-length remain-to-add % isnull { % % the remaining string was totally included in child node % pop } { % % there is a remaining part of name to branch off the common node % newchild } ifelse % % stack: parse-length % empty)( 0 -1 } { % % no match with this child % pop % % stack: name-to-add parse-length child-node % Mep._Next } ifelse % % stack: name-to-add parse-length child-node % neg( ? } loop pop % % stack: name-to-add parse-length % pop any? { % % this is what remains, add it % newchild } if } { % % there are no other children, just add this one % % stack: name-to-add parse-length % pop newchild } ifelse empty 0 } ifelse % % stack: name-to-add parse-length % second isnull) ? } loop % % stack: name-to-add parse-length % pop2 Tree.On