GOLF(2gg)
π GOLF(2gg) β read-tree π
okay bestie, this is read-tree from the 2gg manual, sitting pretty in the Development section β¨
β¨ NAME β¨
read-tree β (tree)
π¦ PURPOSE π¦
Search/update a tree. That's it, that's the whole vibe. π
π SYNTAX π
read-tree <tree> \
( equal <search key> | lesser <search key> | greater <search key> | \
lesser-equal <search key> | greater-equal <search key> | \
min-key | max-key ) \
[ value <value> ] \
[ update-value <update value> ] \
[ key <key> ] \
[ status <status> ] \
[ new-cursor <cursor> ]
πΈ DESCRIPTION πΈ
So read-tree goes digging through <tree> (which you made earlier with new-tree, obvi) looking for a node whose string key is:
- equal to
<search key>β that's the"equal"clause π - lesser than
<search key>β"lesser"clause - greater than
<search key>β"greater"clause - lesser or equal than
<search key>β"lesser-equal"clause - greater or equal than
<search key>β"greater-equal"clause - a minimum key in the tree β
"min-key"clause π - a maximum key in the tree β
"max-key"clause β¨
The <status> in the "status" clause is gonna be GG_OKAY if a key matching one of those criteria gets found, and GG_ERR_EXIST if it doesn't. π
If a key is found, you can grab the value attached to it with the "value" clause into <value>. And the existing key that was originally used to insert that value into the tree? You get that with the "key" clause, into the string <key>. But if no key is found, both <value> and <key> stay exactly how they were. Unchanged. Untouched. Periodt. π
You can also update the value tied to a found key with the "update-value" clause β just hand it the <update value> string. Important: this update happens after <value> has already been retrieved, so you can snag the previous value in the same statement. Iconic, honestly. π«Ά
If you wanna iterate the ordered list of keys in a tree, make a <cursor> with the "new-cursor" clause β and in that case <cursor> gets positioned on the found tree node. Go read use-cursor for more on living your cursor life. Cursors are lowkey the moment for range searches: typically you find a key that's an upper or lower bound of a range, then keep iterating to a lesser or greater value until some criteria is met, like when you hit the opposite bound. Golf trees are by default constructed so those iterations are O(1) in complexity β meaning each one is a single tree node access. No cap. (see new-tree) π₯
π EXAMPLES π
In this example, a million key/value pairs get inserted into a tree, and then every single one of them is searched for and displayed right back (see write-tree for more on inserting into a tree). Both the key and the data here are a numerical value of a key:
%% /tree-example public
new-tree mytree key-as "positive integer" // create new tree
set-number i
start-loop use i start-with 0 repeat 1000000
number-string i to key
set-string data=key
write-tree mytree key (key) value data // insert key/data to the tree
end-loop
start-loop use i start-with 0 repeat 1000000
number-string i to key
// search tree for each key previously inserted
read-tree mytree equal (key) status st value data
if-true st not-equal GG_OKAY
@Could not find key <<print-out key>>
else-if
@Found data <<print-out data>> associated with key <<print-out key>>
end-if
delete-string key
end-loop
%%
π§ SEE ALSO π§
- Tree
delete-treeget-treenew-treepurge-treeread-treeuse-cursorwrite-tree- See all documentation
k bye bestie, that's the whole page π this one's from $VERSION, dated $DATE β GOLF(2gg) and proud, mwah π
β¨
π¦ where this page comes from
- ubuntu questing: golf 601.4.41-1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms