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,51 @@
begin
% define a Stack type that will hold StringStackElements %
% and the StringStackElement type %
% we would need separate types for other element types %
record StringStack ( reference(StringStackElement) top );
record StringStackElement ( string(8) element
; reference(StringStackElement) next
);
% adds e to the end of the StringStack s %
procedure pushString ( reference(StringStack) value s
; string(8) value e
) ;
begin
reference(StringStackElement) newElement;
top(s) := StringStackElement( e, top(s) )
end pushString ;
% removes and returns the top element from the StringStack s %
% asserts the Stack is not empty, which will stop the %
% program if it is %
string(8) procedure popString ( reference(StringStack) value s ) ;
begin
string(8) v;
assert( not isEmptyStringStack( s ) );
v := element(top(s));
top(s):= next(top(s));
v
end popStringStack ;
% returns the top element of the StringStack s %
% asserts the Stack is not empty, which will stop the %
% program if it is %
string(8) procedure peekStringStack ( reference(StringStack) value s ) ;
begin
assert( not isEmptyStringStack( s ) );
element(top(s))
end popStringStack ;
% returns true if the StringStack s is empty, false otherwise %
logical procedure isEmptyStringStack ( reference(StringStack) value s ) ; top(s) = null;
begin % test the StringStack operations %
reference(StringStack) s;
s := StringStack( null );
pushString( s, "up" );
pushString( s, "down" );
pushString( s, "strange" );
pushString( s, "charm" );
while not isEmptyStringStack( s ) do write( popString( s )
, if isEmptyStringStack( s ) then "(empty)"
else peekStringStack( s )
)
end
end.

View file

@ -1,9 +1,9 @@
#var stack := system'collections'Stack new.
var stack := system'collections'Stack new.
stack push:2.
#var isEmpty := stack length == 0.
var isEmpty := stack length == 0.
#var item := stack peek. // Peek without Popping.
var item := stack peek. // Peek without Popping.
item := stack pop.

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

View file

@ -0,0 +1,45 @@
// version 1.1.2
class Stack<E> {
private val data = mutableListOf<E>()
val size get() = data.size
val empty get() = size == 0
fun push(element: E) = data.add(element)
fun pop(): E {
if (empty) throw RuntimeException("Can't pop elements from an empty stack")
return data.removeAt(data.lastIndex)
}
val top: E
get() {
if (empty) throw RuntimeException("Empty stack can't have a top element")
return data.last()
}
fun clear() = data.clear()
override fun toString() = data.toString()
}
fun main(args: Array<String>) {
val s = Stack<Int>()
(1..5).forEach { s.push(it) }
println(s)
println("Size of stack = ${s.size}")
print("Popping: ")
(1..3).forEach { print("${s.pop()} ") }
println("\nRemaining on stack: $s")
println("Top element is now ${s.top}")
s.clear()
println("After clearing, stack is ${if(s.empty) "empty" else "not empty"}")
try {
s.pop()
}
catch (e: Exception) {
println(e.message)
}
}

View file

@ -0,0 +1,14 @@
: cr "\n" . ;
: empty? dup execute length if 0 else -1 then swap drop ;
: pop dup execute length 1 - extract swap drop ;
: push dup execute rot append over ;
: s. stack execute . ;
[] '_ set
: stack '_ ;
stack # local variable
1 swap push set
2 swap push set s. cr # [ 1 2 ]
pop . s. cr # 2 [ 1 ]
pop drop
empty? . # -1

View file

@ -1,73 +0,0 @@
import math
type
EStackEmpty = object of E_Base
TStack* [A] = object
data: seq[A]
count: int
proc initStack*[A](initialSize = 32): TStack[A] =
assert isPowerOfTwo(initialSize)
result.count = 0
newSeq(result.data,initialSize)
proc cap*[A] (s: TStack[A]): int =
result = s.data.len
proc len*[A](stack: TStack[A]): int =
result = stack.count
proc push*[A](s: var TStack[A], item: A) =
if s.count == s.data.len:
# not enough room, make container bigger
var d: Seq[A]
newSeq(d,s.len * 2)
for i in 0 .. s.data.len - 1:
shallowCopy(d[i],s.data[i])
shallowCopy(s.data,d)
s.data[s.count] = item
inc(s.count)
proc pop*[A](s: var TStack[A]): A {.raises: [EStackEmpty].}=
if s.count == 0:
raise newException(EStackEmpty,"the stack is empty")
dec(s.count)
result = s.data[s.count]
proc top*[A](s: TStack[A]): A =
result = s.data[s.count - 1]
proc isEmpty*[A](s: var TStack[A]): bool =
return s.count == 0
#Tests
when isMainModule:
var stk: TStack[char] = initStack[char](4)
stk.push('a')
stk.push('b')
stk.push('c')
stk.push('d')
assert(stk.count == 4)
assert(stk.data.len == 4)
stk.push('e')
assert(stk.cap == 8)
assert(stk.top == 'e')
discard stk.pop
discard stk.pop
discard stk.pop
discard stk.pop
assert(stk.isEmpty == false)
discard stk.pop
assert(stk.isEmpty == true)
try:
discard stk.pop
except:
let
e = getCurrentException()
msg = getCurrentExceptionMsg()
echo "Exception: [[", repr(e), "]] msg: ", msg

View file

@ -0,0 +1,6 @@
stack = .queue~of(123, 234) -- creates a stack with a couple of items
stack~push("Abc") -- pushing
value = stack~pull -- popping
value = stack~peek -- peeking
-- the is empty test
if stack~isEmpty then say "The stack is empty"

View file

@ -0,0 +1,82 @@
; x86_64 linux nasm
struc Stack
maxSize: resb 8
currentSize: resb 8
contents:
endStruc
section .data
soError: db "Stack Overflow Exception", 10
seError: db "Stack Empty Error", 10
section .text
createStack:
; IN: max number of elements (rdi)
; OUT: pointer to new stack (rax)
push rdi
xor rdx, rdx
mov rbx, 8
mul rbx
mov rcx, rax
mov rax, 12
mov rdi, 0
syscall
push rax
mov rdi, rax
add rdi, rcx
mov rax, 12
syscall
pop rax
pop rbx
mov qword [rax + maxSize], rbx
mov qword [rax + currentSize], 0
ret
push:
; IN: stack to operate on (stack argument), element to push (rdi)
; OUT: void
mov rax, qword [rsp + 8]
mov rbx, qword [rax + currentSize]
cmp rbx, qword [rax + maxSize]
je stackOverflow
lea rsi, [rax + contents + 8*rbx]
mov qword [rsi], rdi
add qword [rax + currentSize], 1
ret
pop:
; pop
; IN: stack to operate on (stack argument)
; OUT: element from stack top
mov rax, qword [rsp + 8]
mov rbx, qword [rax + currentSize]
cmp rbx, 0
je stackEmpty
sub rbx, 1
lea rsi, [rax + contents + 8*rbx]
mov qword [rax + currentSize], rbx
mov rax, qword [rsi]
ret
; stack operation exceptions
stackOverflow:
mov rsi, soError
mov rdx, 25
jmp errExit
stackEmpty:
mov rsi, seError
mov rdx, 18
errExit:
mov rax, 1
mov rdi, 1
syscall
mov rax, 60
mov rdi, 1
syscall

7
Task/Stack/Zkl/stack.zkl Normal file
View file

@ -0,0 +1,7 @@
class Stack{
var [const] stack=L();
fcn push(x){stack.append(x); self}
fcn pop {stack.pop()}
fcn empty {(not stack.len())}
var [proxy] isEmpty = empty;
}