September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,85 @@
' FB 1.05.0 Win64
' stack_rosetta.bi
' simple generic Stack type
#Define Stack(T) Stack_##T
#Macro Declare_Stack(T)
Type Stack(T)
Public:
Declare Constructor()
Declare Destructor()
Declare Property capacity As Integer
Declare Property count As Integer
Declare Property empty As Boolean
Declare Property top As T
Declare Function pop() As T
Declare Sub push(item As T)
Private:
a(any) As T
count_ As Integer = 0
Declare Function resize(size As Integer) As Integer
End Type
Constructor Stack(T)()
Redim a(0 To 0) '' create a default T instance for various purposes
End Constructor
Destructor Stack(T)()
Erase a
End Destructor
Property Stack(T).capacity As Integer
Return UBound(a)
End Property
Property Stack(T).count As Integer
Return count_
End Property
Property Stack(T).empty As Boolean
Return count_ = 0
End Property
Property Stack(T).top As T
If count_ > 0 Then
Return a(count_)
End If
Print "Error: Attempted to access 'top' element of an empty stack"
Return a(0) '' return default element
End Property
Function Stack(T).pop() As T
If count_ > 0 Then
Dim value As T = a(count_)
a(count_) = a(0) '' zero element to be removed
count_ -= 1
Return value
End If
Print "Error: Attempted to remove 'top' element of an empty stack"
Return a(0) '' return default element
End Function
Sub Stack(T).push(item As T)
Dim size As Integer = UBound(a)
count_ += 1
If count_ > size Then
size = resize(size)
Redim Preserve a(0 to size)
End If
a(count_) = item
End Sub
Function Stack(T).resize(size As Integer) As Integer
If size = 0 Then
size = 4
ElseIf size <= 32 Then
size = 2 * size
Else
size += 32
End If
Return size
End Function
#EndMacro

View file

@ -0,0 +1,49 @@
' FB 1.05.0 Win64
#Include "stack_rosetta.bi"
Type Dog
name As String
age As Integer
Declare Constructor
Declare Constructor(name_ As string, age_ As integer)
Declare Operator Cast() As String
end type
Constructor Dog '' default constructor
End Constructor
Constructor Dog(name_ As String, age_ As Integer)
name = name_
age = age_
End Constructor
Operator Dog.Cast() As String
Return "[" + name + ", " + Str(age) + "]"
End Operator
Declare_Stack(Dog) '' expand Stack type for Dog instances
Dim dogStack As Stack(Dog)
Var cerberus = Dog("Cerberus", 10)
Var rover = Dog("Rover", 3)
Var puppy = Dog("Puppy", 0)
With dogStack '' push these Dog instances onto the stack
.push(cerberus)
.push(rover)
.push(puppy)
End With
Print "Number of dogs on the stack :" ; dogStack.count
Print "Capacity of dog stack :" ; dogStack.capacity
Print "Top dog : "; dogStack.top
dogStack.pop()
Print "Top dog now : "; dogStack.top
Print "Number of dogs on the stack :" ; dogStack.count
dogStack.pop()
Print "Top dog now : "; dogStack.top
Print "Number of dogs on the stack :" ; dogStack.count
Print "Is stack empty now : "; dogStack.empty
Print
Print "Press any key to quit"
Sleep