Using MakeArray to create a type of (Push/Pop) Array Stack

Started by kevin, June 25, 2020, 10:36:52 AM

Previous topic - Next topic

kevin

  Using MakeArray to create a type of (Push/Pop) Array Stack

  in this example we treat a global Array called INFO of type TINFO as the current record.   If we need to change records we can push the info() handle to a stack,  fill the array with data again and then later retrieve it.    
 
 
PlayBASIC Code: [Select]
      Type tInfo
Name$
EndType

Dim InfoStack(1000)

Makearray info().tInfo



Function getStackCount()
Count=InfoStack(0)
EndFunction Count


// ---------------------------------------------------------
// Push the current HANDLE of our global INFO array
// to our STACK
// ---------------------------------------------------------

Function PushInfo()

// Get to the number of items on our stack
Offset=InfoStack(0)+1

// move the array handle
InfoStack(Offset) = info().tinfo
#print "Handle:"+str$(InfoStack(Offset))

// clear this handle out once it's been moved out
deletearray info()

// Store the stack position
InfoStack(0) = Offset

Endfunction


// ---------------------------------------------------------
// Push the current HANDLE of our global INFO array
// to our STACK
// ---------------------------------------------------------

Function PopInfo()

// Get to the number of items on our stack
Offset=InfoStack(0)
if Offset>0

// release the current array using UNDIM
undim info()

// move the array handle
info()=InfoStack(Offset)

// Store the stack position after the delete
InfoStack(0) = Offset-1

endif
Endfunction



Function ShowCurrentInfoDetails()
Status = getArrayStatus(info())
if Status
count=getArrayElements(info())
print "Info("+str$(count)+") as tInfo"
for lp=0 to count
Message$+=info(lp).Name$+", "
next
print Message$
y=getcursory()
line 0,y,getsurfacewidth(),y
setcursory y+10
else

print "info() is empty"
endif

EndFunction



// ---------------------------------------------------------
// Fill Info
// --------------------------------------------------

Function Fill_Info()
count=rndrange(1,10)
dim info(count) as tINfo

for lp =0 to count
info(lp).Name = "Name:"+str$(lp)
next

EndFunction




// ---------------------------------------------------------
// USAGE EXAMPLE ------------------------------------------
// ---------------------------------------------------------

ink $00ff00
print ""
print "---[Push The Stack]-------------------"
print ""
ink -1


for lp=1 to 5

// Fill info with random junk
Fill_Info()


// Show what's in info currently
ShowCurrentInfoDetails()

// push this array onto our stack
// so we can recall it later
PushInfo()
next


ink $00ff00
print ""
print "---[Info State]-------------------"
print ""
ink -1

ShowCurrentInfoDetails()

print ""
print ""


ink $00ff00
print ""
print "---[POP the stack]-------------------"
print ""
ink -1

while getStackCount()<>0


Login required to view complete source code



Related Articles

       - Array Handles
       - Dynamic Array Allocation
        - PlayBASIC Live:  Array Handles (2017-07-08)