Saving top half ot array/stack

General FreeBASIC programming questions.
Post Reply
IchMagBier
Posts: 52
Joined: Jan 13, 2018 8:47
Location: Germany
Contact:

Saving top half ot array/stack

Post by IchMagBier »

Hello

I have a problem. I wrote a "stack"-class, where you can push and pop strings. This works well, however I want to prevent the stack getting to big. So when the stack reaches the size 6, I want to cut off the lower half, but I don't want to loose the top half. How would I do that?

I tried it like this, but it cuts off the wrong half (see the sub "t_Stack.push"):

Code: Select all

type t_Stack
	private:
		as string __stack(any)
		as long __stack_pointer
		as long __real_ubound
	
	public:
		declare sub push(topush as string)
		declare function pop()as string
		declare sub output()
end type

sub t_Stack.push(topush as string)
	if __stack_pointer>ubound(__stack) then redim preserve __stack(0 to ubound(__stack)+64)
	if __real_ubound>6 then __real_ubound=3 '??
	__real_ubound+=1
	__stack(__stack_pointer)=topush
	__stack_pointer+=1
end sub

function t_Stack.pop()as string
	if __stack_pointer<1 then return ""
	__stack_pointer-=1
	return __stack(__stack_pointer)
end function

sub t_Stack.output()
	for i as integer=0 to __real_ubound-1
		if __stack_pointer-1=i then print ">";
		print str(i)+": "+__stack(i)
	next
end sub

dim as t_Stack test
test.push("A")
test.push("B")
test.push("C")
test.push("D")
test.push("E")
test.push("F")
test.push("G")
test.push("H")
test.output
Greetings, IchMagBier
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Saving top half ot array/stack

Post by fxm »

The "cut" lower part must it be lost forever, or can we recover it gradually by popping (calling 't_Stack.pop()')?
In a word, do you want to just limit the size of the stack viewed from user or outright the size of the internal array?
IchMagBier
Posts: 52
Joined: Jan 13, 2018 8:47
Location: Germany
Contact:

Re: Saving top half ot array/stack

Post by IchMagBier »

The lower part needs to be completly gone. I am saving the "history" of files (I don't know the right word for it. The text you can redo/undo via Ctrl+Z/Y) in that stack and I don't want it to grow to an infinite size.
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Saving top half ot array/stack

Post by fxm »

Small proposed adding (keeping the initial program):

Code: Select all

type t_Stack
   private:
      as string __stack(any)
      as long __stack_pointer
   
   public:
      declare sub push(topush as string)
      declare function pop()as string
      declare sub output()
end type

sub t_Stack.push(topush as string)
   if __stack_pointer>ubound(__stack) then redim preserve __stack(0 to ubound(__stack)+64)
   if __stack_pointer=6 then
      for i as integer=1 to __stack_pointer-1
         __stack(i-1) = __stack(i)
      next
      __stack_pointer-=1
   end if
   __stack(__stack_pointer)=topush
   __stack_pointer+=1
end sub

function t_Stack.pop()as string
   if __stack_pointer<1 then return ""
   __stack_pointer-=1
   return __stack(__stack_pointer)
end function

sub t_Stack.output()
   for i as integer=0 to __stack_pointer-1
      if __stack_pointer-1=i then print ">";
      print str(i)+": "+__stack(i)
   next
end sub

dim as t_Stack test
test.push("A")
test.push("B")
test.push("C")
test.push("D")
test.push("E")
test.push("F")
test.push("G")
test.push("H")
test.output
IchMagBier
Posts: 52
Joined: Jan 13, 2018 8:47
Location: Germany
Contact:

Re: Saving top half ot array/stack

Post by IchMagBier »

Ah nice, thank you!
I knew it could be simple.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Saving top half ot array/stack

Post by badidea »

IchMagBier wrote:The lower part needs to be completly gone. I am saving the "history" of files (I don't know the right word for it. The text you can redo/undo via Ctrl+Z/Y) in that stack and I don't want it to grow to an infinite size.
Sounds like a "Circular buffer"
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Saving top half ot array/stack

Post by dodicat »

For fun, a different way with the potential to go back to a period in history and change it.

Code: Select all

 

 


type t_Stack
   private:
       as string __stack(any)
       dim as long limit=6
   public:
      declare sub push(topush as string,as long=-13)
      declare sub pop(as long=-13)
      declare sub output()
end type


Sub arrayinsert(a() As string,index As Long,insert As string)
    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 Integer= Ubound(a) To Lbound(a)+index2+1 Step -1
            Swap a(x),a(x-1)
        Next x
        a(Lbound(a)+index2)=insert
    End If
End Sub

Sub arraydelete(a() As string,index As Long)
    If index>=Lbound(a) And index<=Ubound(a) Then
        For x As Integer=index To Ubound(a)-1
            a(x)=a(x+1)
        Next x
        Redim Preserve a(Lbound(a) To Ubound(a)-1)
    End If 
End Sub

sub t_Stack.push(topush as string,num as long)
    var t=iif(num=-13,ubound(this.__stack)+1,num)
    arrayinsert(this.__stack(),t,topush)
    if ubound(this.__stack)>=limit then
        arraydelete(this.__stack(),lbound(this.__stack))
        end if
end sub

sub t_Stack.pop(num as long)
     var t=iif(num=-13,ubound(this.__stack),num)
  arraydelete(this.__stack(),t)
end sub

sub t_Stack.output()
   for i as integer=lbound(__stack) to ubound(__stack)
     'if i=ubound(__stack)then print ">";
      print str(i)+": "+__stack(i)
   next
end sub

dim as t_Stack test

for n as long=1 to 50
    test.push(str(n))
cls
locate 3
test.output
sleep 100
next

print

test.pop(3)
test.push("new value",3)

test.output

sleep


 

 
Post Reply