Code: Select all
#include "file.bi"
Sub loadfile(file As String,p() As zstring * 100)
If Fileexists(file)=0 Then Print file;" not found":Sleep:End
Var f=Freefile
Open file For Binary Access Read As #f
If Lof(f) > 0 Then
Get #f, ,p()
End If
Close #f
End Sub
Sub savefile(filename As String,p() As zString * 100 )
Dim As Integer n=Freefile
If Open (filename For Binary Access Write As #n)=0 Then
Put #n,,p()
Close
Else
Print "Unable to save " + filename
End If
End Sub
#define pushtop(a) ubound(a)+1
#define poptop(a) ubound(a)
#define bottom(a) lbound(a)
#macro push(a,index,insert)
If (index)>=Lbound(a) And (index)<=Ubound(a)+1 Then
Var index2=(index)-Lbound(a)
Redim Preserve a(Lbound(a) To Ubound(a)+1)
For x As long= Ubound(a) To Lbound(a)+index2+1 Step -1
Swap a(x),a(x-1)
Next x
a(Lbound(a)+index2)=(insert)
End If
#endmacro
#macro pop(a,index)
If index>=Lbound(a) And (index)<=Ubound(a) Then
For x As long=(index) To Ubound(a)-1
a(x)=a(x+1)
Next x
Redim Preserve a(Lbound(a) To Ubound(a)-1)
End If
#endmacro
#define lim 2000 'limit of elements
Type tree
Declare Constructor
Declare Constructor(As long,As String)
Private:
Declare Sub build()
Declare Sub revamp()
Dim As long idx
Dim As String Name
Static As tree mem(lim)'memory stack
Static As Long inc 'memory increment
Public:
Dim As tree Ptr subtree(Any)
Declare Sub create(As Long)
Declare Sub remove(As Long)
Declare Sub Add(As Long,As String)
Declare Sub set(As long,As String)
Declare Sub Print()
Declare Function find Overload(As String) As long
Declare Function find Overload(As long) As String
Declare Sub save(filename As String)
Declare Sub load(filename As String)
End Type 'activate the memory fields
Dim As tree tree.mem(lim)
Dim As Long tree.inc
Constructor tree
End Constructor
Constructor tree(i As long,g As String)
idx=i
Name=g
End Constructor
Sub tree.build()
If tree.inc>lim Then tree.inc=0
tree.inc+=1
push(subtree,pushtop(subtree),@mem(tree.inc))
End Sub
Sub tree.revamp()
For n As Long=Lbound(subtree) To Ubound(subtree)
*(subtree(n)).idx=n
Next n
End Sub
Sub tree.create(num As Long)
For n As Long=1 To num
build()
Next
Redim Preserve subtree(1 To num)
End Sub
Sub tree.remove(i As Long)
pop(subtree,i)
revamp()
End Sub
Sub tree.add(n As Long,g As String)
If tree.inc>lim Then tree.inc=0
tree.inc+=1
push(subtree,n,@mem(tree.inc))
*(subtree(n)).name=g
revamp()
End Sub
Function tree.find(g As String) As long
For n As Long=Lbound(subtree) To Ubound(subtree)
If *(subtree(n)).name=g Then Return *(subtree(n)).idx
Next
End Function
Function tree.find(g As long) As String
For n As Long=Lbound(subtree) To Ubound(subtree)
If *(subtree(n)).idx=g Then Return *(subtree(n)).name
Next
End Function
Sub tree.print
For n As Long=Lbound(subtree) To Ubound(subtree)
..print *(subtree(n)).idx,*(subtree(n)).name
Next
End Sub
Sub tree.set(n As long,g As String)
*(subtree(n))=Type<tree>(n,g)
revamp
End Sub
Sub tree.save(filename As String)
Redim As zstring * 100 a(Lbound(subtree) To Ubound(subtree))
For n As Long=Lbound(subtree) To Ubound(subtree)
a(n)=*(subtree(n)).name
Next
savefile(filename,a())
End Sub
Sub tree.load(filename As String)
Var L=Filelen("listdata.txt")/Sizeof(zstring * 100)
Dim As zstring * 100 b(1 To L)
loadfile(filename,b())
create(L)
For n As Long=1 To L
set(n,b(n))'set in values from file
Next n
End Sub
'save some data to a file to start
Dim As zstring * 100 a(...)={"Peter","Paul","Mary","Henry","Peggy"}
savefile("listdata.txt",a())
Erase a
'============================================================
'START
Dim As tree x
x.load("listdata.txt")
Print
Print "data from file"
x.print
Print
Print "add ABC at position 2"
x.add(2,"ABC")
x.print
Print
Print "add DEF at position 4"
x.add(4,"DEF")
x.print
Print
Print "remove position 3"
x.remove(3)
x.print
Print
Print x.find(3);" (value at position 3)"
Print x.find("ABC");" (position of ABC)"
Print
Print "push to top On Top"
x.add(pushtop(x.subtree),"On Top")
x.print
Print
print "push to bottom START"
x.add(bottom(x.subtree),"START")
'save to disk
x.save("listdata.txt")
'check
Print
Print "Data saved to file"
Print "Check the disk"
Dim As tree test
test.load("listdata.txt")
test.print
print
Dim As tree bye
bye.create(1)
bye.set(1,"GoodBye!")
bye.print
Sleep
'kill "listdata.txt"