September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
51
Task/Queue-Definition/ALGOL-W/queue-definition.alg
Normal file
51
Task/Queue-Definition/ALGOL-W/queue-definition.alg
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
begin
|
||||
% define a Queue type that will hold StringQueueElements %
|
||||
record StringQueue ( reference(StringQueueElement) front, back );
|
||||
% define the StringQueueElement type %
|
||||
record StringQueueElement ( string(8) element
|
||||
; reference(StringQueueElement) next
|
||||
);
|
||||
% we would need separate types for other element types %
|
||||
% adds s to the end of the StringQueue q %
|
||||
procedure pushString ( reference(StringQueue) value q
|
||||
; string(8) value e
|
||||
) ;
|
||||
begin
|
||||
reference(StringQueueElement) newElement;
|
||||
newElement := StringQueueElement( e, null );
|
||||
if front(q) = null then begin
|
||||
% adding to an empty queue %
|
||||
front(q) := newElement;
|
||||
back(q) := newElement
|
||||
end
|
||||
else begin
|
||||
% the queue is not empty %
|
||||
next(back(q)) := newElement;
|
||||
back(q) := newElement
|
||||
end
|
||||
end pushString ;
|
||||
% removes an element from the front of the StringQueue q %
|
||||
% asserts the queue is not empty, which will stop the %
|
||||
% program if it is %
|
||||
string(8) procedure popString ( reference(StringQueue) value q ) ;
|
||||
begin
|
||||
string(8) v;
|
||||
assert( not isEmptyStringQueue( q ) );
|
||||
v := element(front(q));
|
||||
front(q) := next(front(q));
|
||||
if front(q) = null then % just popped the last element % back(q) := null;
|
||||
v
|
||||
end popStringQueue ;
|
||||
% returns true if the StringQueue q is empty, false otherwise %
|
||||
logical procedure isEmptyStringQueue ( reference(StringQueue) value q ) ; front(q) = null;
|
||||
|
||||
begin % test the StringQueue operations %
|
||||
reference(StringQueue) q;
|
||||
q := StringQueue( null, null );
|
||||
pushString( q, "fred" );
|
||||
pushString( q, "whilma" );
|
||||
pushString( q, "betty" );
|
||||
pushString( q, "barney" );
|
||||
while not isEmptyStringQueue( q ) do write( popString( q ) )
|
||||
end
|
||||
end.
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
# Implement a fifo as an array of arrays, to
|
||||
# greatly amortize dequeue costs, at some expense of
|
||||
# memory overhead and insertion time. The speedup
|
||||
# depends on the underlying JS implementation, but
|
||||
# it's significant on node.js.
|
||||
Fifo = ->
|
||||
max_chunk = 512
|
||||
arr = [] # array of arrays
|
||||
count = 0
|
||||
|
||||
self =
|
||||
enqueue: (elem) ->
|
||||
if count == 0 or arr[arr.length-1].length >= max_chunk
|
||||
arr.push []
|
||||
count += 1
|
||||
arr[arr.length-1].push elem
|
||||
dequeue: (elem) ->
|
||||
throw Error("queue is empty") if count == 0
|
||||
val = arr[0].shift()
|
||||
count -= 1
|
||||
if arr[0].length == 0
|
||||
arr.shift()
|
||||
val
|
||||
is_empty: (elem) ->
|
||||
count == 0
|
||||
|
||||
# test
|
||||
do ->
|
||||
max = 5000000
|
||||
q = Fifo()
|
||||
for i in [1..max]
|
||||
q.enqueue
|
||||
number: i
|
||||
|
||||
console.log q.dequeue()
|
||||
while !q.is_empty()
|
||||
v = q.dequeue()
|
||||
console.log v
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
> time coffee fifo.coffee
|
||||
{ number: 1 }
|
||||
{ number: 5000000 }
|
||||
|
||||
real 0m2.394s
|
||||
user 0m2.089s
|
||||
sys 0m0.265s
|
||||
89
Task/Queue-Definition/FreeBASIC/queue-definition-1.freebasic
Normal file
89
Task/Queue-Definition/FreeBASIC/queue-definition-1.freebasic
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
' queue_rosetta.bi
|
||||
' simple generic Queue type
|
||||
|
||||
#Define Queue(T) Queue_##T
|
||||
|
||||
#Macro Declare_Queue(T)
|
||||
Type Queue(T)
|
||||
Public:
|
||||
Declare Constructor()
|
||||
Declare Destructor()
|
||||
Declare Property capacity As Integer
|
||||
Declare Property count As Integer
|
||||
Declare Property empty As Boolean
|
||||
Declare Property front 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 Queue(T)()
|
||||
Redim a(0 To 0) '' create a default T instance for various purposes
|
||||
End Constructor
|
||||
|
||||
Destructor Queue(T)()
|
||||
Erase a
|
||||
End Destructor
|
||||
|
||||
Property Queue(T).capacity As Integer
|
||||
Return UBound(a)
|
||||
End Property
|
||||
|
||||
Property Queue(T).count As Integer
|
||||
Return count_
|
||||
End Property
|
||||
|
||||
Property Queue(T).empty As Boolean
|
||||
Return count_ = 0
|
||||
End Property
|
||||
|
||||
Property Queue(T).front As T
|
||||
If count_ > 0 Then
|
||||
Return a(1)
|
||||
End If
|
||||
Print "Error: Attempted to access 'front' element of an empty queue"
|
||||
Return a(0) '' return default element
|
||||
End Property
|
||||
|
||||
Function Queue(T).pop() As T
|
||||
If count_ > 0 Then
|
||||
Dim value As T = a(1)
|
||||
If count_ > 1 Then '' move remaining elements to fill space vacated
|
||||
For i As Integer = 2 To count_
|
||||
a(i - 1) = a(i)
|
||||
Next
|
||||
End If
|
||||
a(count_) = a(0) '' zero last element
|
||||
count_ -= 1
|
||||
Return value
|
||||
End If
|
||||
Print "Error: Attempted to remove 'front' element of an empty queue"
|
||||
Return a(0) '' return default element
|
||||
End Function
|
||||
|
||||
Sub Queue(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 Queue(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
|
||||
53
Task/Queue-Definition/FreeBASIC/queue-definition-2.freebasic
Normal file
53
Task/Queue-Definition/FreeBASIC/queue-definition-2.freebasic
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
#Include "queue_rosetta.bi"
|
||||
|
||||
Type Cat
|
||||
name As String
|
||||
age As Integer
|
||||
Declare Constructor
|
||||
Declare Constructor(name_ As string, age_ As integer)
|
||||
Declare Operator Cast() As String
|
||||
end type
|
||||
|
||||
Constructor Cat '' default constructor
|
||||
End Constructor
|
||||
|
||||
Constructor Cat(name_ As String, age_ As Integer)
|
||||
name = name_
|
||||
age = age_
|
||||
End Constructor
|
||||
|
||||
Operator Cat.Cast() As String
|
||||
Return "[" + name + ", " + Str(age) + "]"
|
||||
End Operator
|
||||
|
||||
Declare_Queue(Cat) '' expand Queue type for Cat instances
|
||||
|
||||
Dim CatQueue As Queue(Cat)
|
||||
|
||||
Var felix = Cat("Felix", 8)
|
||||
Var sheba = Cat("Sheba", 4)
|
||||
Var fluffy = Cat("Fluffy", 2)
|
||||
With CatQueue '' push these Cat instances into the Queue
|
||||
.push(felix)
|
||||
.push(sheba)
|
||||
.push(fluffy)
|
||||
End With
|
||||
Print "Number of Cats in the Queue :" ; CatQueue.count
|
||||
Print "Capacity of Cat Queue :" ; CatQueue.capacity
|
||||
Print "Front Cat : "; CatQueue.front
|
||||
CatQueue.pop()
|
||||
Print "Front Cat now : "; CatQueue.front
|
||||
Print "Number of Cats in the Queue :" ; CatQueue.count
|
||||
CatQueue.pop()
|
||||
Print "Front Cat now : "; CatQueue.front
|
||||
Print "Number of Cats in the Queue :" ; CatQueue.count
|
||||
Print "Is Queue empty now : "; CatQueue.empty
|
||||
catQueue.pop()
|
||||
Print "Number of Cats in the Queue :" ; CatQueue.count
|
||||
Print "Is Queue empty now : "; CatQueue.empty
|
||||
catQueue.pop()
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
47
Task/Queue-Definition/Kotlin/queue-definition.kotlin
Normal file
47
Task/Queue-Definition/Kotlin/queue-definition.kotlin
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
// version 1.1.2
|
||||
|
||||
import java.util.LinkedList
|
||||
|
||||
class Queue<E> {
|
||||
private val data = LinkedList<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 queue")
|
||||
return data.removeFirst()
|
||||
}
|
||||
|
||||
val top: E
|
||||
get() {
|
||||
if (empty) throw RuntimeException("Empty queue can't have a top element")
|
||||
return data.first()
|
||||
}
|
||||
|
||||
fun clear() = data.clear()
|
||||
|
||||
override fun toString() = data.toString()
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val q = Queue<Int>()
|
||||
(1..5).forEach { q.push(it) }
|
||||
println(q)
|
||||
println("Size of queue = ${q.size}")
|
||||
print("Popping: ")
|
||||
(1..3).forEach { print("${q.pop()} ") }
|
||||
println("\nRemaining in queue: $q")
|
||||
println("Top element is now ${q.top}")
|
||||
q.clear()
|
||||
println("After clearing, queue is ${if(q.empty) "empty" else "not empty"}")
|
||||
try {
|
||||
q.pop()
|
||||
}
|
||||
catch (e: Exception) {
|
||||
println(e.message)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
import queues
|
||||
|
||||
# defining push & pop (obviously optional)
|
||||
proc push*[T](q: var TQueue[T]; item: T) =
|
||||
add(q,item)
|
||||
proc pop*[T](q: var TQueue[T]): T =
|
||||
result = dequeue(q)
|
||||
|
||||
var fifo: TQueue[int] = initQueue[int]()
|
||||
|
||||
fifo.push(26)
|
||||
fifo.push(99)
|
||||
fifo.push(2)
|
||||
echo("Fifo size: ", fifo.len())
|
||||
echo("Popping: ", fifo.pop())
|
||||
echo("Popping: ", fifo.pop())
|
||||
echo("Popping: ", fifo.pop())
|
||||
#echo("Popping: ", fifo.pop()) # popping an empty stack raises [EAssertionFailed]
|
||||
6
Task/Queue-Definition/Zkl/queue-definition-1.zkl
Normal file
6
Task/Queue-Definition/Zkl/queue-definition-1.zkl
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
class Queue{
|
||||
var [const] q=List();
|
||||
fcn push { q.append(vm.pasteArgs()) }
|
||||
fcn pop { q.pop(0) }
|
||||
fcn empty { q.len()==0 }
|
||||
}
|
||||
5
Task/Queue-Definition/Zkl/queue-definition-2.zkl
Normal file
5
Task/Queue-Definition/Zkl/queue-definition-2.zkl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
q:=Queue();
|
||||
q.push(1,2,3);
|
||||
q.pop(); //-->1
|
||||
q.empty(); //-->False
|
||||
q.pop();q.pop();q.pop() //-->IndexError thrown
|
||||
Loading…
Add table
Add a link
Reference in a new issue