String Stack class expl

Started by Frank Brübach, February 26, 2024, 07:16:21 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Frank Brübach

Here is another useful oxygen example with class Methode Style

String Stack

'-- string stack is a special form of a linear data struct
'-- like LIFO last in first out, push and pop
'-- here string concatenation

'=================
 class StringStack
'=================
  string buf
  sys pb
  sys lb

    'APPEND STRING AND ITS LENGTH
    '
    method push(string s) 
   '====================
    sys ls=len s
    sys pn=pb+ls+4
    '
    'ELASTIC BUFFER
    '
    if pn>lb then
      buf+=nuls 4000
      lb=len buf
    end if
    '
    'APPEND STRING AND ITS LENGTH
    '
    mid (buf,pb+1)=s 'APPEND STRING
    pb+=ls
    sys p=*buf + pb
    *p=ls 'PATCH IN LENGTH AS LONG BINARY
    pb+=4
  end method
 
 
  method pop() as string
 '=====================
    if pb<4 then return ""
    '
    pb-=4         'POINT TO ITEM LENGTH ENCODING
    sys i=*buf+pb 'LENGTH OF ITEM
    sys ls=*i
    pb-=ls        'POINT TO START OF STRING
    '
    return mid buf,pb+1,ls
  end method
end class
 
StringStack s
 
s.push "Animal World"
s.push "Hello my"
print s.pop +"  "+s.pop

And now its time for a Break

Regards Frank (Hospital visits)
  •  

Theo Gottwald

📚 I'm fascinated by the concept of using Stacks and Queues as data types!

🗂� It's essential to include a method to get the element count.

🔄 Moreover, having two types of POP instructions is clever:
1. 🔥 A destructive version that truly removes the element.
2. 📝 And another that simply makes a "copy" — ensuring functionality and versatility.

Let's embrace the power of structured data! 🚀

#Coding #DataStructures #TechTalk #ProgrammingTips #StacksAndQueues 😎👨�💻👩�💻🔄🗂�💡

Charles Pegge

You could add a get method to read the top of the stack non-destructively:
(also added a destructor)

 =================
 class StringStack
 =================
  bstring buf ' storage buffer
  int pb      ' item offset
  int lb      ' item length

    'APPEND STRING AND ITS LENGTH
    '
    method destructor()
    ===================
    del buf
    pb=0
    lb=0
    end method

    method push(string s)
    =====================
    int ls=len s
    int pn=pb+ls+4
    '
    'ELASTIC BUFFER
    '
    if pn>lb then
      buf+=nuls 4000 'EXPAND BUFFER
      lb=len buf
    end if
    '
    'APPEND STRING AND ITS LENGTH
    '
    mid (buf,pb+1)=s 'APPEND STRING
    pb+=ls
    int le at pb+strptr(buf)
    le=ls 'STORE LENGTH
    pb+=4
  end method
 
 
  method ipop(int mo) as string
  =============================
    if pb<4 then return ""
    '
    int le at pb-4+strptr(buf) 'LENGTH OF ITEM
    int mb=pb-le-4
    method=mid(buf,mb+1,le)
    if mo=0
      pb=mb 'DISALLOCATE SPACE
    endif
  end method
  '
  method pop() as string
  ======================
   return ipop(0)
 end method

  method get() as string
  ======================
   return ipop(1)
 end method

end class
'#recordof StringStack
 
StringStack s
s.push "Animal World"
s.push "Hello my"
print s.pop +"  "+s.get+"  "+s.pop
del s