Graph Database from Stanley Durham

Started by Theo Gottwald, May 06, 2024, 11:16:24 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Theo Gottwald

[English]
🌐 **Exploring the Intricacies of Graph Structures and Trie Trees in Today's Tech Landscape** 🌐

---

🔹 **Graph Data Structures** 🔹 
Used in Google Maps, AI, social networks, and much more! A graph data structure includes a finite, possibly mutable set of vertices (or nodes) 📌, linked by edges (also known as links or lines in undirected graphs, and arrows or arcs in directed ones). 🔄

---

🔹 **What is a Trie Tree?** 🔹 
A Trie Tree acts as a key/payload data storage structure, similar to hash tables or AVL trees, but it uniquely operates as a prefix tree. 🌳

---

🔹 **Implementing Graph Databases** 🔹 
To start, you need:
- A unique list of nodes 📊
- Links (edges) between these nodes 🔗
- Sometimes, values associated with nodes or edges 🏷�

---

🔹 **Trie Tree Operations** 🔹 
Adding a prefix to a Trie Tree creates a Unique Ordered Set, allowing traversal of any prefix using the prefix cursor. 🗺�

---

🔹 **Practical Applications** 🔹 
Though a simple sample, graph databases and Trie Trees need custom tailoring for effective use. The tree structure serves as a foundation; its potential is unlocked by the user's needs and creativity. 🛠�

---

🚀 **Fast and Efficient** 🚀 
Comparable to a hash in speed and can be stored to file for efficient data management. 💾

---

#GraphDataStructures #TrieTree #TechInnovation #DataStorage #AI #GoogleMaps #SocialNetworking ✨📈🖥�🌍🔍

[German]
🌐💡 Nutzt du Google Maps, KI, soziale Netzwerke oder ähnliches? Heutzutage sind Graphenstrukturen überall im Einsatz! 🚀

🔍 "Eine Graph-Datenstruktur besteht aus einer endlichen (und möglicherweise veränderbaren) Menge von Knotenpunkten, zusammen mit einem Set von ungeordneten Paaren dieser Knoten für einen ungerichteten Graph oder einem Set von geordneten Paaren für einen gerichteten Graph. Diese Paare werden als Kanten (auch Verbindungen oder Linien genannt) bezeichnet, und für einen gerichteten Graph auch manchmal als Pfeile oder Bögen." – WP

🌳 Ein Trie-Baum ist eine Schlüssel-/Datenstruktur. Daten werden mittels eines einzigartigen Schlüssels gespeichert und abgerufen, ähnlich einer Hashtabelle oder einem AVL-Baum.
Aber es ist auch ein Präfix-Baum.

📊 Um eine Graphendatenbank zu implementieren, benötigen wir:
- Eine einzigartige Liste von Knotenpunkten.
- Eine Liste von Verbindungen (Kanten).
- In manchen Fällen kann ein Knoten oder eine Kante einen Wert haben.

🔗 Im Trie-Baum müssen wir nur ein Präfix hinzufügen. Alles mit demselben Präfix wird zu einem einzigartigen geordneten Set. Mit dem Präfix-Cursor können wir jedes Präfix durchlaufen.

📝 Hier ist ein einfaches Beispiel. Mehr kann man wirklich nicht machen, da Graphendatenbanken maßgeschneidert sein müssen.
Der Baum kann genutzt werden, um die Struktur aufzubauen, was danach passiert, liegt am Nutzer.
Schnell wie eine Hashtabelle und kann in Dateien gespeichert werden.

🔥🌟 Nutze die Kraft der Graphen- und Trie-Strukturen für deine Projekte und optimiere deine Datenverarbeitung! 🚀

#GraphenDatenstrukturen #TrieBaum #Datenbanken #KI #TechInnovation #DatenVerarbeiten 🌐📊🔍🌳


'TrieC.inc
'Public domain, use at own risk. SDurham

    '   Trie Tree

    '   if %MessageOnError defined before including file, message box on lib error
    '   if %HaltOnError defined before including file, app will halt on lib error

    ' ----------------------------------------------------------------------------------
    '
    '   Key/Payload container. Payload stored/retrieved using unique Key.
    '
    '       Extremely Fast:
    '           Fast as hash, Keys always in Key order.
    '       Tree:
    '           Faster than a tree and doesn't need to rebalance.
    '           Like a tree, the Keys are in order.
    '       Prefix Tree:
    '           Can be used for IntelliSense like lookup.
    '           Nothing faster.
    '           Use prefix cursor to find prefixes.
    '           Everything below a position in the tree has the same prefix.
    '       Suffix Tree:
    '           Store refers key before storing, use Prefix cursor.
    '       Unique Ordered Set:
    '          A set is a group of members that share a common attribute.
    '       Store/Restore tree To/From String/File:
    '
    '   Implementation: "Range" Triee Tree
    '       Each node represents a character in a Key.
    '       The character's ASCII value in the node array is a pointer to the next character.
    '       This gets heavy very fast.
    '       In this implementation, the array only holds the range between low and high character.
    '       At some point, there will only be one character in the node.
    '
    '   Keys:
    '       case sensitive, no nulls
    '   Payload:
    '       any kind of data packed into a string
    '
    '   Keys aren't stored in Tree
    '   The Key is the structure of the Tree
    ' ----------------------------------------------------------------------------------
    '   Keys lay on top of each other until a difference is encountered
    '   Keys = (cat, cats, catastrophe, catastrophes, cattle, cattleman, cattlemen, catch, catches, caught)
    '
    '   c
    '   a
    '   t*--------------u
    '   a--c--s*--t     g
    '   s  h*     l     h
    '   t  e      e*    t*
    '   r  s*     m
    '   o         a--e
    '   p         n* n*
    '   h
    '   e*
    '   s*
    ' ----------------------------------------------------------------------------------

#If Not %Def(%TrieTree4)
    %TrieTree4 = 1
    %TrieGoDown = 1
    %TrieGoRight = 2
    %TrieGoUp = 3
    %TriePtrSize = 4
    'error exit macro, errors logged to app folder
    Macro TrieExit(test, message, exitWhat)
        If test Then
            Error 151
            Me.Log(message )
            Exit exitWhat
        End If
    End Macro
    Type StrBldT 'forward reference
        mem As Long
        count As Long
        max As Long
    End Type
    Type TrieStrT
        count As Long
        mem As Long
    End Type
    Type TrieNodeT
        prnt As TrieNodeT Ptr
        char As Byte
        low As Byte
        high As Byte
        count As Byte
        arr As Long Ptr
        payload As TrieStrT Ptr
    End Type

    Class TrieC
        Instance count_ As Long
        Instance root_ As TrieNodeT Ptr
        Instance cursor_ As TrieNodeT Ptr
        Instance index_ As Long
        Instance way_ As Byte
        Instance prefix_ As TrieNodeT Ptr
        Class Method Destroy()
            Me.ClearMe()
        End Method

        ' ------------------------------------------------------------------------------
        '   String Trie Tree
        ' ------------------------------------------------------------------------------
       
        Interface TrieI : Inherit IUnknown

            Property Get Count() As Long
                ' get item count
                Property = count_
            End Property

            Method Clear()
                ' empty container
                Me.ClearMe()
            End Method

            Method Add(key As String, payload As String, Opt ByVal update As Long)
                ' add key/payload to tree : key ignored if already in tree
                ' if 'update' specified then payload replaced if key in tree
                Register x As Long
                Local n As TrieNodeT Ptr
                Local k As Byte Ptr
                Err = 0
                If Len(key) Then
                    If root_ = 0 Then root_ = Me.NodeAllocate(0, 0)
                    If Err Then Exit Method
                    n = root_
                    k = StrPtr(key)
                    While @k
                        x = Me.NodeAddToRange(n, @k)
                        If @n.@arr[x] = 0 Then @n.@arr[x] = Me.NodeAllocate(n, @k)
                        n = @n.@arr[x]
                        Incr k
                    Wend
                    'all of key's characters are now in tree
                    If @n.payload = 0 Then
                        @n.payload = Me.StringAllocate()
                        Me.StringSet(@n.@payload, payload)
                        Incr count_
                    ElseIf IsTrue update Then
                        Me.StringSet(@n.@payload, payload)
                    End If
                End If
            End Method

            Method Set(key As String, payload As String)
                ' replace key's payload
                Local n As TrieNodeT Ptr
                n = Me.Contains(key)
                If n And @n.payload Then Me.StringSet(@n.@payload, payload)
            End Method

            Method Get(key As String) As String
                ' get key's payload
                Local n As TrieNodeT Ptr
                n = Me.Contains(key)
                If n And @n.payload Then Method = Peek$(@n.@payload.mem, @n.@payload.count)
            End Method

            Method Contains(key As String) As Long
                ' return zero if key not in tree
                Register c As Long
                Local n As TrieNodeT Ptr
                Local k As Byte Ptr
                If Len(key) Then
                    k = StrPtr(key)
                    c = @k
                    n = root_
                    While n And c
                        If c < @n.low Or c > @n.high Then Exit Method
                        n = @n.@arr[c - @n.low]
                        Incr k : c = @k
                    Wend
                End If
                If n And @n.payload Then Method = n
            End Method

            Method Remove(key As String)
                ' delete key and payload
                Local n, prnt As TrieNodeT Ptr
                n = Me.Contains(key)
                If n And @n.payload Then
                    @n.payload = Me.StringFree(@n.payload)
                    Decr count_
                    While n
                        prnt = @n.prnt
                        If @n.count Or @n.payload Then Exit Method
                        Me.NodeDisconnect(n)
                        Me.NodeFree(n)
                        n = prnt
                    Wend
                    If count_ = 0 Then Me.ClearMe()
                End If
            End Method

            ' ------------------------------------------------------------------------------
            '   Key Cursor : move through every key in tree
            ' ------------------------------------------------------------------------------

            Method FirstKey() As Long
                ' move to first Key in tree : true/false success
                cursor_ = 0
                prefix_ = 0
                If root_ Then
                    cursor_ = root_
                    index_ = -1 'incr by %TrieGoRight
                    way_ = %TrieGoRight
                    Method = Me.NextKey()
                End If
            End Method

            Method NextKey() As Long
                ' move to next Key in tree : true/false success
                If count_ Then
                    While cursor_
                        Select Case As Const way_
                        Case %TrieGoDown
                            If @cursor_.count = 0 Or index_ >= @cursor_.count Then
                                way_ = %TrieGoUp 'can't go down or right - go up
                            ElseIf @cursor_.@arr[index_] = 0 Then
                                way_ = %TrieGoRight 'no child - can't go down - go right
                            Else 'go down
                                cursor_ = @cursor_.@arr[index_]
                                index_ = 0
                                way_ = %TrieGoDown
                                If @cursor_.payload Then
                                    Method = 1 : Exit Method
                                End If
                            End If
                        Case %TrieGoRight
                            Incr index_ 'go right
                            If index_ >= @cursor_.count Then
                                way_ = %TrieGoUp 'can't go right or down
                            Else
                                way_ = %TrieGoDown
                            End If
                        Case %TrieGoUp
                            If @cursor_.prnt Then index_ = @cursor_.char - @cursor_.@prnt.low
                            cursor_ = @cursor_.prnt 'will exit loop if cursor on root node
                            way_ = %TrieGoRight
                        End Select
                    Wend
                End If
            End Method

            ' ------------------------------------------------------------------------------
            '   Prefix Cursor : move through every key starting with "prefix"
            ' ------------------------------------------------------------------------------

            Method FirstPrefix(keyPrefix As String) As Long
                ' move cursor to first matching prefix : true/false success
                Local c, x As Long
                Local k As Byte Ptr
                Local n As TrieNodeT Ptr
                cursor_ = 0
                prefix_ = 0
                n = root_
                If Len(keyPrefix) Then
                    k = StrPtr(keyPrefix)
                    While @k And n
                        c = @k
                        If c < @n.low Or c > @n.high Then Exit Method
                        x = c - @n.low
                        Incr k
                        n = @n.@arr[x]
                    Wend
                    If n Then
                        prefix_ = @n.prnt
                        cursor_ = n
                        index_ = -1
                        way_ = %TrieGoRight
                        If @n.payload Then
                            Method = 1
                        Else
                            Method = Me.NextPrefix()
                        End If
                    End If
                End If
            End Method

            Method NextPrefix() As Long
                ' move to next Key in tree : true/false success
                If count_ Then
                    While cursor_
                        Select Case As Const way_
                        Case %TrieGoDown
                            If @cursor_.count = 0 Or index_ >= @cursor_.count Then
                                way_ = %TrieGoUp 'can't go down or right - go up
                            ElseIf @cursor_.@arr[index_] = 0 Then
                                way_ = %TrieGoRight 'no child - can't go down - go right
                            Else 'go down
                                cursor_ = @cursor_.@arr[index_]
                                index_ = 0
                                way_ = %TrieGoDown
                                If @cursor_.payload Then
                                    Method = 1 : Exit Method
                                End If
                            End If
                        Case %TrieGoRight
                            Incr index_ 'go right
                            If index_ >= @cursor_.count Then
                                way_ = %TrieGoUp 'can't go right or down
                            Else
                                way_ = %TrieGoDown
                            End If
                        Case %TrieGoUp
                            If @cursor_.prnt = prefix_ Then Exit Method
                            If @cursor_.prnt Then index_ = @cursor_.char - @cursor_.@prnt.low
                            cursor_ = @cursor_.prnt 'will exit loop if cursor on root node
                            way_ = %TrieGoRight
                        End Select
                    Wend
                End If
            End Method

            ' ------------------------------------------------------------------------------
            '   Get values at cursor position
            ' ------------------------------------------------------------------------------

            Property Get Key() As String
                'get Key at current cursor position
                Local s As String
                Local n As TrieNodeT Ptr
                If cursor_ And @cursor_.payload Then
                    n = cursor_
                    While n And @n.char
                        s = Chr$(@n.char) + s
                        n = @n.prnt
                    Wend
                End If
                Property = s
            End Property

            Property Get Payload() As String
                'get Payload at current cursor position
                If cursor_ And @cursor_.payload Then Property = Me.StringGet(@cursor_.@payload)
            End Property

            ' ------------------------------------------------------------------------------
            '   Store/Restore tree To/From String/File
            ' ------------------------------------------------------------------------------

            Method Store() As String
                ' store container to String
                Register i As Long
                Register ok As Long
                Local key$, payload$
                Local sb As StrBldT
                If count_ Then
                    StrBldAdd sb, Mkl$(count_)
                    ok = Me.FirstKey()
                    While ok
                        Incr i
                        key$ = Me.Key
                        payload$ = Me.Payload
                        StrBldAdd sb, Mkl$(Len(key$))
                        StrBldAdd sb, key$
                        StrBldAdd sb, Mkl$(Len(payload$))
                        StrBldAdd sb, payload$
                        ok = Me.NextKey()
                    Wend
                    Method = StrBldGet(sb)
                End If
            End Method

            Method Restore(ByVal stored As String)
                ' restore container from String
                Register i As Long
                Local items, characters As Long
                Local key$, payload$
                Local p As Long Ptr
                Me.ClearMe()
                If Len(stored) Then
                    p = StrPtr(stored)
                    items = @p : Incr p
                    For i = 1 To items
                        characters = @p : Incr p
                        key$ = Peek$(p, characters) : p += characters
                        characters = @p : Incr p
                        payload$ = Peek$(p, characters) : p += characters
                        Me.Add(key$, payload$)
                    Next i
                End If
            End Method

            Method FileStore(ByVal file As WString)
                ' store container to File
                StrToFile file, Me.Store()
            End Method

            Method FileRestore(ByVal file As WString)
                ' restore container from File
                Me.Restore(StrFromFile(file))
            End Method

        End Interface 'TrieI

        ' ----------------------------------------------------------------------------------
        '   Class Methods
        ' ----------------------------------------------------------------------------------
        Class Method ClearMe()
            root_ = Me.NodeFree(root_)
            count_ = 0
            cursor_ = 0
            index_ = -1
            prefix_ = 0
        End Method
        ' ----------------------------------------------------------------------------------
        '   Node
        ' ----------------------------------------------------------------------------------
        Class Method NodeAllocate(ByVal prnt As TrieNodeT Ptr, ByVal char As Byte) As Long
            Local node As TrieNodeT Ptr
            node = MemAllocate(SizeOf(@node))
            TrieExit(node = 0, "TrieC: NodeAllocate: node allocate fail", Method)
            @node.prnt = prnt
            @node.char = char
            Method = node
        End Method
        Class Method NodeFree(ByVal node As TrieNodeT Ptr) As Long
            Register i As Long
            If node Then
                For i = 0 To @node.count - 1
                    If @node.@arr[i] Then @node.@arr[i] = Me.NodeFree(@node.@arr[i])
                Next i
                Me.NodeArrayClear(node)
                If @node.payload Then @node.payload = Me.StringFree(@node.payload)
                MemFree(node)
            End If
        End Method
        Class Method NodeAddToRange(ByVal node As TrieNodeT Ptr, ByVal char As Byte) As Long
            Register i As Long
            Register items As Long
            If node Then
                If @node.count = 0 Then
                    Me.NodeArrayAdd(node, 0)
                    @node.low = char
                    @node.high = char
                    Method = 0 'first in array
                ElseIf char < @node.low Then
                    items = @node.low - char
                    For i = 1 To items
                        Me.NodeArrayInsert(node, 0, 0)
                    Next i
                    @node.low = char
                    Method = 0
                ElseIf char > @node.high Then
                    items = char - @node.high
                    For i = 1 To items
                        Me.NodeArrayAdd(node, 0)
                    Next i
                    @node.high = char
                    Method = @node.count - 1
                Else
                    Method = char - @node.low
                End If
            Else
                TrieExit(1, "TrieC: NodeAddToRange: null node", Method)
            End If
        End Method
        Class Method NodeDisconnect(ByVal node As TrieNodeT Ptr)
            Register x As Long
            Local prnt As TrieNodeT Ptr
            TrieExit(node = 0, "TrieC: NodeDisconnect: null node", Method)
            prnt = @node.prnt
            If prnt Then
                x = @node.char - @prnt.low
                TrieExit(x < 0 Or x >= @prnt.count, "TrieC: NodeDisconnect: out of bunds", Method)
                @prnt.@arr[x] = 0
                'may need to collapse range
                While @prnt.count And @prnt.@arr[0] = 0
                    Me.NodeArrayDelete(prnt, 0)
                    Incr @prnt.low
                Wend
                While @prnt.count And @prnt.@arr[@prnt.count - 1] = 0
                    Me.NodeArrayDelete(prnt, @prnt.count - 1)
                    Decr @prnt.high
                Wend
            End If
        End Method
        ' ----------------------------------------------------------------------------------
        '   Node Array
        ' ----------------------------------------------------------------------------------
        Class Method NodeArrayClear(ByVal node As TrieNodeT Ptr)
            TrieExit(node = 0, "TrieC: NodeArrayClear: null node ptr", Method)
            @node.arr = MemFree(@node.arr)
            @node.count = 0
        End Method
        Class Method NodeArrayReDim(ByVal node As TrieNodeT Ptr, ByVal items As Long)
            TrieExit(node = 0, "TrieC: NodeArrayReDim: null node ptr", Method)
            If items = 0 Then
                Me.NodeArrayClear(node)
            ElseIf items <> @node.count Then
                @node.count = 0
                @node.arr = MemReAllocate(@node.arr, items * %TriePtrSize)
                TrieExit(@node.arr = 0, "TrieC: NodeArrayReDim: memory reallocation fial", Method)
                @node.count = items
            End If
        End Method
        Class Method NodeArrayAdd(ByVal node As TrieNodeT Ptr, ByVal payload As Long)
            TrieExit(node = 0, "TrieC: NodeArrayAdd: null node ptr", Method)
            Me.NodeArrayReDim(node, @node.count + 1)
            TrieExit(@node.count = 0, "TrieC: NodeArrayAdd: NodeArrayReDim fail", Method)
            @node.@arr[@node.count - 1] = payload
        End Method
        Class Method NodeArrayInsert(ByVal node As TrieNodeT Ptr, ByVal index As Byte, ByVal payload As Long)
            TrieExit(node = 0, "TrieC: NodeArrayInsert: null node ptr", Method)
            TrieExit(index >= @node.count, "TrieC: NodeArrayInsert: out of bounds", Method)
            Me.NodeArrayReDim(node, @node.count + 1)
            TrieExit(@node.count = 0, "TrieC: NodeArrayInsert: NodeArrayReDim fail", Method)
            Me.NodeArrayMove(node, index, index + 1, @node.count - index - 1)
            @node.@arr[index] = payload
        End Method
        Class Method NodeArrayDelete(ByVal node As TrieNodeT Ptr, ByVal index As Byte)
            TrieExit(node = 0, "TrieC: NodeArrayDelete: null node ptr", Method)
            TrieExit(index >= @node.count, "TrieC: NodeArrayDelete: out of bounds", Method)
            If index < @node.count - 1 Then
                Me.NodeArrayMove(node, index + 1, index , @node.count - index - 1)
            End If
            Me.NodeArrayReDim(node, @node.count - 1)
        End Method
        Class Method NodeArrayMove(ByVal node As TrieNodeT Ptr, ByVal fromIndex As Long, ByVal toIndex As Long, ByVal Count As Long)
            Memory Copy @node.arr + (fromIndex * %TriePtrSize), @node.arr + (toIndex * %TriePtrSize), Count * %TriePtrSize
        End Method
        ' ----------------------------------------------------------------------------------
        '   String
        ' ----------------------------------------------------------------------------------
        Class Method StringAllocate() As Long
            Local p As TrieStrT Ptr
            p = MemAllocate(SizeOf(@p))
            TrieExit(p = 0, "TrieC: StringAllocate: memory allocation fail", Method)
            Method = p
        End Method
        Class Method StringFree(ByVal p As TrieStrT Ptr) As Long
            If p Then
                Me.StringClear(@p)
                MemFree(p)
            End If
        End Method
        Class Method StringClear(str As TrieStrT)
            str.count = 0
            str.mem = MemFree(str.mem)
        End Method
        Class Method StringSet(str As TrieStrT, payload As String)
            Register strLen As Long
            strLen = Len(payload)
            Me.StringClear(str)
            If strLen Then
                str.mem = MemAllocate(strLen)
                TrieExit(str.mem = 0, "TrieC: StringSet: memory allocation fail", Method)
                str.count = strLen
                Memory Copy StrPtr(payload), str.mem, strLen
            End If
        End Method
        Class Method StringGet(str As TrieStrT) As String
            If str.count Then Method = Peek$(str.mem, str.count)
        End Method
        ' ----------------------------------------------------------------------------------
        '  Error Log
        ' ----------------------------------------------------------------------------------
        Class Method Log(message As String)
            Local h As Long
            h = FreeFile
            Try
                Open Exe.Path$ + "TrieeTree.log" For Append As h
                If Lof(h) < 16000 Then
                    Print# h, Date$ +": "+ Time$ +": "+ Exe.Full$ +": "+ message
                End If
            Catch
            Finally
                Close h
                #If %Def(%MessageOnError)
                    MsgBox message, ,"Error!"
                #EndIf
                #If %Def(%HaltOnError)
                    End
                #EndIf
            End Try
        End Method
    End Class 'TrieC
#EndIf '%TrieTree4

#If Not %Def(%Memory230424)
    %Memory230424 = 1
    Declare Function GlobalAlloc Lib "Kernel32.dll" Alias "GlobalAlloc" (ByVal uFlags As Dword, ByVal dwBytes As Dword) As Dword
    Declare Function GlobalReAlloc Lib "Kernel32.dll" Alias "GlobalReAlloc" (ByVal hMem As Dword, ByVal dwBytes As Dword, ByVal uFlags As Dword) As Dword
    Declare Function GlobalFree Lib "Kernel32.dll" Alias "GlobalFree" (ByVal hMem As Dword) As Dword
    %MEMFIXED = &H0000 : %MEMMOVEABLE = &H0002 : %MEMZEROINIT = &H0040 : %MEMGPTR = (%MEMZEROINIT Or %MEMFIXED)
    Function MemAllocate(ByVal bytes As Long) ThreadSafe As Long
        If bytes Then Function = GlobalAlloc(ByVal %MEMGPTR, ByVal bytes)
    End Function
    Function MemReAllocate(ByVal hMem As Long, ByVal bytes As Long) ThreadSafe As Long
        If hMem And bytes Then
            Function = GlobalReAlloc(ByVal hMem, ByVal bytes, ByVal %MEMMOVEABLE Or %MEMZEROINIT)
        ElseIf bytes Then
            Function = GlobalAlloc(ByVal %MEMGPTR, ByVal bytes)
        ElseIf hMem Then
            Function = GlobalFree(ByVal hMem)
        End If
    End Function
    Function MemFree(ByVal hMem As Long) ThreadSafe As Long
        If hMem Then GlobalFree(ByVal hMem)
    End Function
#EndIf '%Memory230424

#If Not %Def(%StrBld230424)
    %StrBld230424 = 1
    ' String Builder : all memory freed when StrBldGet() called
    ' add 1,000,000 one-character Strings =  0.094 seconds
    ' public domain, use at own risk
    ' SDurham
    %StrBldItemsSize = 1
    %StrBldBuffer = 100000
    Type StrBldT
        mem As Long
        count As Long
        max As Long
    End Type
    Function StrBldCount(t As StrBldT) ThreadSafe As Long
        ' get character count
        Function = t.count
    End Function
    Sub StrBldAdd(t As StrBldT, ByRef value As String) ThreadSafe
        ' append string
        Local currentCount, currentMax, newMax As Long
        Local lenValue As Long : lenValue = Len(value)
        If lenValue Then
            If lenValue > t.max - t.count Then
                currentCount = t.count
                currentMax = t.max
                t.count = 0
                t.max = 0
                newMax = currentCount + lenValue + %StrBldBuffer
                t.mem = MemReAllocate(t.mem, newMax * %StrBldItemsSize)
                If t.mem = 0 Then Exit Sub
                t.count = currentCount
                t.max = newMax
            End If
            Memory Copy StrPtr(value), t.mem + (t.count * %StrBldItemsSize), lenValue * %StrBldItemsSize
            t.count += lenValue
        End If
    End Sub
    Function StrBldGet(t As StrBldT) ThreadSafe As String
        ' get complete string and free all memory
        If t.count Then Function = Peek$(t.mem, t.count)
        t.mem = MemFree(t.mem)
        t.count = 0
        t.max = 0
    End Function
#EndIf '%StrBld230424

#If Not %Def(%FileUtilities230424)
    %FileUtilities230424 = 1
    'File Utilities
    Sub StrToFile(ByRef file As WString, ByRef value As String)
        'store string to File
        Local f As Long
        If Len(file) = 0 Then Exit Sub
        f = FreeFile
        Open file For Binary As f
        SetEof f
        Put$ f, value
        Close f
    End Sub
    Function StrFromFile(ByRef file As WString) As String
        'get file contents as string
        Local f As Long, value As String
        If IsFalse IsFile(file) Then Exit Function
        f = FreeFile
        Open file For Binary As f
        Get$ f, Lof(f), value
        Close f
        Function = value
    End Function
    Function StrFromFileFixed(ByRef file As WString) As String
        'get file contents converted from Unix line endings if any
        Local value As String
        value = StrFromFile(file)
        Replace $CrLf With $Lf In value
        Replace $CrLf With $Lf In value
        Replace $Cr With $Lf In value
        Replace $Cr With $Lf In value
        Replace $Lf With $CrLf In value
        Function = value
    End Function
    Sub WStrToFile(ByRef file As WString, ByRef value As WString)
        'store string to File
        Local f As Long
        If Len(file) = 0 Then Exit Sub
        f = FreeFile
        Open file For Binary As f
        SetEof f
        Put$$ f, value
        Close f
    End Sub
    Function WStrFromFile(ByRef file As WString) As WString
        'get file contents as string
        Local f As Long, value As WString
        If IsFalse IsFile(file) Then Exit Function
        f = FreeFile
        Open file For Binary As f
        Get$$ f, Lof(f), value
        Close f
        Function = value
    End Function
    Sub WStrToTextFile(ByRef file As WString, ByRef value As WString)
        'store string converted to UTF8 to File
        StrToFile file, ChrToUtf8$(value)
    End Sub
    Function WStrFromTextFile(ByRef file As WString) As WString
        'get file contents converted from UTF8
        Function = Utf8ToChr$(StrFromFile(file))
    End Function
    Function WStrFromTextFileFixed(ByRef file As WString) As WString
        'get file contents converted from UTF8 fixing Unix line endings if any
        Local value As WString
        value = Utf8ToChr$(StrFromFile(file))
        Replace $CrLf With $Lf In value
        Replace $CrLf With $Lf In value
        Replace $Cr With $Lf In value
        Replace $Cr With $Lf In value
        Replace $Lf With $CrLf In value
        Function = value
    End Function
#EndIf '%FileUtilities230424


Sample code:

'Graph.bas
#Option LargeMem32
#Dim All
#Compile Exe
#Include Once "WIN32API.INC"

%MessageOnError = 1
%HaltOnError = 1
#Include Once "TrieC.inc"

%TextBox = 101
%BtnID = 102
Global gDlg As Long

Sub SampleCode()
    Local tree As TrieI : tree = Class "TrieC"
    Local more As Long

    Control Set Text gDlg, %TextBox, ""

    SS "add nodes, "
    SS "using 'N:' as a prefix for all nodes"
    tree.Add("N:A", "A")
    tree.Add("N:B", "B")
    tree.Add("N:C", "C")
    tree.Add("N:D", "D")

    SS ""
    SS "list nodes"
    more = tree.FirstPrefix("N:")
    While more
        SS $Dq + tree.Payload() + $Dq
        more = tree.NextPrefix()
    Wend

    SS ""
    SS "link A to B"
    SS "using 'L:' as a prefix for all links, edges"
    tree.Add("L:A:B", "B")
    SS "link A to C"
    tree.Add("L:A:C", "C")

    SS ""
    SS "list A links"
    more = tree.FirstPrefix("L:A:")
    While more
        SS $Dq + tree.Payload() + $Dq
        more = tree.NextPrefix()
    Wend

    SS ""
    SS "link C to D"
    tree.Add("L:C:D", "D")

    SS ""
    SS "list all links"
    more = tree.FirstPrefix("L:")
    While more
        SS $Dq + tree.Key() + $Dq
        more = tree.NextPrefix()
    Wend

    SS ""
    SS "add weights"
    SS "using 'W:' as prefix for weights"
    tree.Add("W:A", "4")
    tree.Add("W:B", "3")
    tree.Add("W:C", "2")
    tree.Add("W:D", "1")

    SS ""
    SS "weight for A = " + tree.Get("W:A")
    SS "weight for B = " + tree.Get("W:B")
    SS "weight for C = " + tree.Get("W:C")
    SS "weight for D = " + tree.Get("W:D")

    SS ""
    SS ""

    Control Send gDlg, %TextBox, %EM_SETSEL, 1, 1
    Control Send gDlg, %TextBox, %EM_SCROLLCARET, 0, 0
End Sub

Sub SS(ByVal value As WString)
    'appends without the overhead of getting the text
    Local characterCount As Long
    Local hWin As Long : hWin = GetDlgItem(gDlg, %TextBox)
    value += $CrLf
    characterCount =  SendMessageW(hWin, %WM_GETTEXTLENGTH, 0, 0)
    SendMessageW(hWin, %EM_SETSEL, characterCount, characterCount)
    SendMessageW(hWin, %EM_REPLACESEL, 1, StrPtr(value))
End Sub

Function PBMain()
    Local clientW, clientH As Long
    Desktop Get Client To clientW, clientH
    Dialog Default Font "consolas", 13, 0, 0
    Dialog New 0, Exe.Name$, 0, 0, clientW \ 7, clientH \ 4, %WS_Caption Or %WS_ClipSiblings Or %WS_MinimizeBox Or %WS_SysMenu Or %WS_ThickFrame Or %DS_Center, %WS_Ex_AppWindow To gDlg
    Control Add TextBox, gDlg, %TextBox, "", 0, 0, 0, 0, %ES_AutoHScroll Or %ES_AutoVScroll Or %ES_MultiLine Or %ES_NoHideSel Or %ES_WantReturn Or %WS_HScroll Or %WS_VScroll, 0
    Control Add Button,  gDlg, %BtnID, "Run", 275, 220, 60, 15, %BS_Flat, 0
    SendMessageW(GetDlgItem(gDlg, %TextBox), %EM_SETLIMITTEXT, 4000000, 0)
    Dialog Show Modeless gDlg, Call DlgCB
    Do
        Dialog DoEvents
    Loop While IsWin(gDlg)
End Function

CallBack Function DlgCB()
    Select Case As Long Cb.Msg
        Case %WM_Size
            WM_Size()
        Case %WM_Command
            Select Case As Long Cb.Ctl
                Case %BtnID : If Cb.CtlMsg = %BN_Clicked Then SampleCode()
            End Select
    End Select
End Function

Sub WM_Size()
    Local clientW, clientH As Long
    Local marg As Long
    Local buttonW, buttonH As Long
    Local txtWidth, txtHeight As Long
    Local fromLeft, fromBottom As Long
    Dialog Get Client gDlg To clientW, clientH
    marg = 3 : buttonW = 25 : buttonH = 10
    fromLeft = clientW - marg - buttonW
    fromBottom = clientH - marg - buttonH
    Control Set Size gDlg, %BtnID, buttonW, buttonH
    Control Set Loc gDlg, %BtnID, fromLeft, fromBottom
    txtWidth = clientW - marg - marg
    txtHeight = clientH - marg - buttonH - marg - marg
    Control Set Size gDlg, %TextBox, txtWidth, txtHeight
    Control Set Loc gDlg, %TextBox, marg, marg
End Sub