Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
20
Task/Queue-Definition/Ada/queue-definition-1.adb
Normal file
20
Task/Queue-Definition/Ada/queue-definition-1.adb
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
generic
|
||||
type Element_Type is private;
|
||||
package Fifo is
|
||||
type Fifo_Type is private;
|
||||
procedure Push(List : in out Fifo_Type; Item : in Element_Type);
|
||||
procedure Pop(List : in out Fifo_Type; Item : out Element_Type);
|
||||
function Is_Empty(List : Fifo_Type) return Boolean;
|
||||
Empty_Error : exception;
|
||||
private
|
||||
type Fifo_Element;
|
||||
type Fifo_Ptr is access Fifo_Element;
|
||||
type Fifo_Type is record
|
||||
Head : Fifo_Ptr := null;
|
||||
Tail : Fifo_Ptr := null;
|
||||
end record;
|
||||
type Fifo_Element is record
|
||||
Value : Element_Type;
|
||||
Next : Fifo_Ptr := null;
|
||||
end record;
|
||||
end Fifo;
|
||||
11
Task/Queue-Definition/Ada/queue-definition-10.adb
Normal file
11
Task/Queue-Definition/Ada/queue-definition-10.adb
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
generic
|
||||
type Element_Type is private;
|
||||
package Asynchronous_Fifo is
|
||||
protected type Fifo is
|
||||
procedure Push(Item : Element_Type);
|
||||
entry Pop(Item : out Element_Type);
|
||||
private
|
||||
Value : Element_Type;
|
||||
Valid : Boolean := False;
|
||||
end Fifo;
|
||||
end Asynchronous_Fifo;
|
||||
30
Task/Queue-Definition/Ada/queue-definition-11.adb
Normal file
30
Task/Queue-Definition/Ada/queue-definition-11.adb
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package body Asynchronous_Fifo is
|
||||
|
||||
----------
|
||||
-- Fifo --
|
||||
----------
|
||||
|
||||
protected body Fifo is
|
||||
|
||||
----------
|
||||
-- Push --
|
||||
----------
|
||||
|
||||
procedure Push (Item : Element_Type) is
|
||||
begin
|
||||
Value := Item;
|
||||
Valid := True;
|
||||
end Push;
|
||||
|
||||
---------
|
||||
-- Pop --
|
||||
---------
|
||||
|
||||
entry Pop (Item : out Element_Type) when Valid is
|
||||
begin
|
||||
Item := Value;
|
||||
end Pop;
|
||||
|
||||
end Fifo;
|
||||
|
||||
end Asynchronous_Fifo;
|
||||
48
Task/Queue-Definition/Ada/queue-definition-12.adb
Normal file
48
Task/Queue-Definition/Ada/queue-definition-12.adb
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
with Asynchronous_Fifo;
|
||||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
|
||||
procedure Asynchronous_Fifo_Test is
|
||||
package Int_Fifo is new Asynchronous_Fifo(Integer);
|
||||
use Int_Fifo;
|
||||
Buffer : Fifo;
|
||||
|
||||
task Writer is
|
||||
entry Stop;
|
||||
end Writer;
|
||||
|
||||
task body Writer is
|
||||
Val : Positive := 1;
|
||||
begin
|
||||
loop
|
||||
select
|
||||
accept Stop;
|
||||
exit;
|
||||
else
|
||||
Buffer.Push(Val);
|
||||
Val := Val + 1;
|
||||
end select;
|
||||
end loop;
|
||||
end Writer;
|
||||
|
||||
task Reader is
|
||||
entry Stop;
|
||||
end Reader;
|
||||
|
||||
task body Reader is
|
||||
Val : Positive;
|
||||
begin
|
||||
loop
|
||||
select
|
||||
accept Stop;
|
||||
exit;
|
||||
else
|
||||
Buffer.Pop(Val);
|
||||
Put_Line(Integer'Image(Val));
|
||||
end select;
|
||||
end loop;<syntaxhighlight lang="ada">
|
||||
end Reader;
|
||||
begin
|
||||
delay 0.1;
|
||||
Writer.Stop;
|
||||
Reader.Stop;
|
||||
end Asynchronous_Fifo_Test;
|
||||
49
Task/Queue-Definition/Ada/queue-definition-2.adb
Normal file
49
Task/Queue-Definition/Ada/queue-definition-2.adb
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
with Ada.Unchecked_Deallocation;
|
||||
|
||||
package body Fifo is
|
||||
|
||||
----------
|
||||
-- Push --
|
||||
----------
|
||||
|
||||
procedure Push (List : in out Fifo_Type; Item : in Element_Type) is
|
||||
Temp : Fifo_Ptr := new Fifo_Element'(Item, null);
|
||||
begin
|
||||
if List.Tail = null then
|
||||
List.Tail := Temp;
|
||||
end if;
|
||||
if List.Head /= null then
|
||||
List.Head.Next := Temp;
|
||||
end if;
|
||||
List.Head := Temp;
|
||||
end Push;
|
||||
|
||||
---------
|
||||
-- Pop --
|
||||
---------
|
||||
|
||||
procedure Pop (List : in out Fifo_Type; Item : out Element_Type) is
|
||||
procedure Free is new Ada.Unchecked_Deallocation(Fifo_Element, Fifo_Ptr);
|
||||
Temp : Fifo_Ptr := List.Tail;
|
||||
begin
|
||||
if List.Head = null then
|
||||
raise Empty_Error;
|
||||
end if;
|
||||
Item := List.Tail.Value;
|
||||
List.Tail := List.Tail.Next;
|
||||
if List.Tail = null then
|
||||
List.Head := null;
|
||||
end if;
|
||||
Free(Temp);
|
||||
end Pop;
|
||||
|
||||
--------------
|
||||
-- Is_Empty --
|
||||
--------------
|
||||
|
||||
function Is_Empty (List : Fifo_Type) return Boolean is
|
||||
begin
|
||||
return List.Head = null;
|
||||
end Is_Empty;
|
||||
|
||||
end Fifo;
|
||||
17
Task/Queue-Definition/Ada/queue-definition-3.adb
Normal file
17
Task/Queue-Definition/Ada/queue-definition-3.adb
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
with Fifo;
|
||||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
|
||||
procedure Fifo_Test is
|
||||
package Int_Fifo is new Fifo(Integer);
|
||||
use Int_Fifo;
|
||||
My_Fifo : Fifo_Type;
|
||||
Val : Integer;
|
||||
begin
|
||||
for I in 1..10 loop
|
||||
Push(My_Fifo, I);
|
||||
end loop;
|
||||
while not Is_Empty(My_Fifo) loop
|
||||
Pop(My_Fifo, Val);
|
||||
Put_Line(Integer'Image(Val));
|
||||
end loop;
|
||||
end Fifo_Test;
|
||||
13
Task/Queue-Definition/Ada/queue-definition-4.adb
Normal file
13
Task/Queue-Definition/Ada/queue-definition-4.adb
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
with Ada.Containers.Doubly_Linked_Lists;
|
||||
generic
|
||||
type Element_Type is private;
|
||||
package Generic_Fifo is
|
||||
type Fifo_Type is tagged private;
|
||||
procedure Push(The_Fifo : in out Fifo_Type; Item : in Element_Type);
|
||||
procedure Pop(The_Fifo : in out Fifo_Type; Item : out Element_Type);
|
||||
Empty_Error : Exception;
|
||||
private
|
||||
package List_Pkg is new Ada.Containers.Doubly_Linked_Lists(Element_Type);
|
||||
use List_Pkg;
|
||||
Type Fifo_Type is new List with null record;
|
||||
end Generic_Fifo;
|
||||
25
Task/Queue-Definition/Ada/queue-definition-5.adb
Normal file
25
Task/Queue-Definition/Ada/queue-definition-5.adb
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package body Generic_Fifo is
|
||||
|
||||
----------
|
||||
-- Push --
|
||||
----------
|
||||
|
||||
procedure Push (The_Fifo : in out Fifo_Type; Item : in Element_Type) is
|
||||
begin
|
||||
The_Fifo.Prepend(Item);
|
||||
end Push;
|
||||
|
||||
---------
|
||||
-- Pop --
|
||||
---------
|
||||
|
||||
procedure Pop (The_Fifo : in out Fifo_Type; Item : out Element_Type) is
|
||||
begin
|
||||
if Is_Empty(The_Fifo) then
|
||||
raise Empty_Error;
|
||||
end if;
|
||||
Item := The_Fifo.Last_Element;
|
||||
The_Fifo.Delete_Last;
|
||||
end Pop;
|
||||
|
||||
end Generic_Fifo;
|
||||
17
Task/Queue-Definition/Ada/queue-definition-6.adb
Normal file
17
Task/Queue-Definition/Ada/queue-definition-6.adb
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
with Generic_Fifo;
|
||||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
|
||||
procedure Generic_Fifo_Test is
|
||||
package Int_Fifo is new Generic_Fifo(Integer);
|
||||
use Int_Fifo;
|
||||
My_Fifo : Fifo_Type;
|
||||
Val : Integer;
|
||||
begin
|
||||
for I in 1..10 loop
|
||||
My_Fifo.Push(I);
|
||||
end loop;
|
||||
while not My_Fifo.Is_Empty loop
|
||||
My_Fifo.Pop(Val);
|
||||
Put_Line(Integer'Image(Val));
|
||||
end loop;
|
||||
end Generic_Fifo_Test;
|
||||
11
Task/Queue-Definition/Ada/queue-definition-7.adb
Normal file
11
Task/Queue-Definition/Ada/queue-definition-7.adb
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
generic
|
||||
type Element_Type is private;
|
||||
package Synchronous_Fifo is
|
||||
protected type Fifo is
|
||||
entry Push(Item : Element_Type);
|
||||
entry Pop(Item : out Element_Type);
|
||||
private
|
||||
Value : Element_Type;
|
||||
Is_New : Boolean := False;
|
||||
end Fifo;
|
||||
end Synchronous_Fifo;
|
||||
31
Task/Queue-Definition/Ada/queue-definition-8.adb
Normal file
31
Task/Queue-Definition/Ada/queue-definition-8.adb
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package body Synchronous_Fifo is
|
||||
|
||||
----------
|
||||
-- Fifo --
|
||||
----------
|
||||
|
||||
protected body Fifo is
|
||||
|
||||
---------
|
||||
-- Push --
|
||||
---------
|
||||
|
||||
entry Push (Item : Element_Type) when not Is_New is
|
||||
begin
|
||||
Value := Item;
|
||||
Is_New := True;
|
||||
end Push;
|
||||
|
||||
---------
|
||||
-- Pop --
|
||||
---------
|
||||
|
||||
entry Pop (Item : out Element_Type) when Is_New is
|
||||
begin
|
||||
Item := Value;
|
||||
Is_New := False;
|
||||
end Pop;
|
||||
|
||||
end Fifo;
|
||||
|
||||
end Synchronous_Fifo;
|
||||
56
Task/Queue-Definition/Ada/queue-definition-9.adb
Normal file
56
Task/Queue-Definition/Ada/queue-definition-9.adb
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
with Synchronous_Fifo;
|
||||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
|
||||
procedure Synchronous_Fifo_Test is
|
||||
package Int_Fifo is new Synchronous_Fifo(Integer);
|
||||
use Int_Fifo;
|
||||
Buffer : Fifo;
|
||||
|
||||
task Writer is
|
||||
entry Stop;
|
||||
end Writer;
|
||||
|
||||
task body Writer is
|
||||
Val : Positive := 1;
|
||||
begin
|
||||
loop
|
||||
select
|
||||
accept Stop;
|
||||
exit;
|
||||
else
|
||||
select
|
||||
Buffer.Push(Val);
|
||||
Val := Val + 1;
|
||||
or
|
||||
delay 1.0;
|
||||
end select;
|
||||
end select;
|
||||
end loop;
|
||||
end Writer;
|
||||
|
||||
task Reader is
|
||||
entry Stop;
|
||||
end Reader;
|
||||
|
||||
task body Reader is
|
||||
Val : Positive;
|
||||
begin
|
||||
loop
|
||||
select
|
||||
accept Stop;
|
||||
exit;
|
||||
else
|
||||
select
|
||||
Buffer.Pop(Val);
|
||||
Put_Line(Integer'Image(Val));
|
||||
or
|
||||
delay 1.0;
|
||||
end select;
|
||||
end select;
|
||||
end loop;
|
||||
end Reader;
|
||||
begin
|
||||
delay 0.1;
|
||||
Writer.Stop;
|
||||
Reader.Stop;
|
||||
end Synchronous_Fifo_Test;
|
||||
9
Task/Queue-Definition/Pluto/queue-definition.pluto
Normal file
9
Task/Queue-Definition/Pluto/queue-definition.pluto
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
require "queue"
|
||||
|
||||
local q = new queue()
|
||||
q:push(42)
|
||||
q:push(43)
|
||||
while !q:empty() do
|
||||
print(q:pop())
|
||||
end
|
||||
print(q:pop())
|
||||
17
Task/Queue-Definition/PowerShell/queue-definition.ps1
Normal file
17
Task/Queue-Definition/PowerShell/queue-definition.ps1
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
$Q = New-Object System.Collections.Queue
|
||||
|
||||
$Q.Enqueue( 1 )
|
||||
$Q.Enqueue( 2 )
|
||||
$Q.Enqueue( 3 )
|
||||
|
||||
$Q.Dequeue()
|
||||
$Q.Dequeue()
|
||||
|
||||
$Q.Count -eq 0
|
||||
$Q.Dequeue()
|
||||
$Q.Count -eq 0
|
||||
|
||||
try
|
||||
{ $Q.Dequeue() }
|
||||
catch [System.InvalidOperationException]
|
||||
{ If ( $_.Exception.Message -eq 'Queue empty.' ) { 'Caught error' } }
|
||||
31
Task/Queue-Definition/Rebol/queue-definition.rebol
Normal file
31
Task/Queue-Definition/Rebol/queue-definition.rebol
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
Rebol [
|
||||
Title: "FIFO"
|
||||
URL: http://rosettacode.org/wiki/FIFO
|
||||
]
|
||||
|
||||
; Define fifo class:
|
||||
|
||||
fifo: make object! [
|
||||
queue: copy []
|
||||
push: func [x][append queue x]
|
||||
pop: func [/local x][ ; Make 'x' local so it won't pollute global namespace.
|
||||
if empty [return none]
|
||||
x: first queue remove queue x]
|
||||
empty: does [empty? queue]
|
||||
]
|
||||
|
||||
; Create and populate a FIFO:
|
||||
|
||||
q: make fifo []
|
||||
q/push 'a
|
||||
q/push 2
|
||||
q/push $12.34 ; Did I mention that REBOL has 'money!' datatype?
|
||||
q/push [Athos Porthos Aramis] ; List elements pushed on one by one.
|
||||
q/push [[Huey Dewey Lewey]] ; This list is preserved as a list.
|
||||
|
||||
; Dump it out, with narrative:
|
||||
|
||||
print rejoin ["Queue is " either q/empty [""]["not "] "empty."]
|
||||
while [not q/empty][print [" " q/pop]]
|
||||
print rejoin ["Queue is " either q/empty [""]["not "] "empty."]
|
||||
print ["Trying to pop an empty queue yields:" q/pop]
|
||||
|
|
@ -1,50 +1,48 @@
|
|||
const max_tail = 256
|
||||
|
||||
struct Queue<T> {
|
||||
mut:
|
||||
struct Queue[T] {
|
||||
mut:
|
||||
data []T
|
||||
tail int
|
||||
tail int
|
||||
head int
|
||||
}
|
||||
|
||||
fn (mut queue Queue<T>) push(value T) {
|
||||
if queue.tail >= max_tail || queue.tail < queue.head {
|
||||
return
|
||||
}
|
||||
println('push: $value')
|
||||
fn (mut queue Queue[T]) push(value T) {
|
||||
if queue.tail >= max_tail || queue.tail < queue.head { return }
|
||||
println("Enqueue: $value")
|
||||
queue.data << value
|
||||
queue.tail++
|
||||
}
|
||||
|
||||
fn (mut queue Queue<T>) pop() !T {
|
||||
fn (mut queue Queue[T]) pop() !T {
|
||||
if queue.tail > 0 && queue.head < queue.tail {
|
||||
result := queue.data[queue.head]
|
||||
queue.head++
|
||||
println('Dequeue: top of Queue was $result')
|
||||
println("Dequeue: top of Queue was $result")
|
||||
return result
|
||||
}
|
||||
return error('Queue Underflow!!')
|
||||
return error("Queue Underflow!!")
|
||||
}
|
||||
|
||||
fn (queue Queue<T>) peek() !T {
|
||||
fn (queue Queue[T]) peek() !T {
|
||||
if queue.tail > 0 && queue.head < queue.tail {
|
||||
result := queue.data[queue.head]
|
||||
println('Peek: top of Queue is $result')
|
||||
println("Peek: top of Queue is $result")
|
||||
return result
|
||||
}
|
||||
return error('Out of Bounds...')
|
||||
return error("Out of Bounds...")
|
||||
}
|
||||
|
||||
fn (queue Queue<T>) empty() bool {
|
||||
fn (queue Queue[T]) empty() bool {
|
||||
return queue.tail == 0
|
||||
}
|
||||
|
||||
fn main() {
|
||||
mut queue := Queue<f64>{}
|
||||
println('Queue is empty? ' + if queue.empty() { 'Yes' } else { 'No' })
|
||||
mut queue := Queue[f64]{}
|
||||
println("Queue is empty? " + if queue.empty() { "Yes" } else { "No" })
|
||||
queue.push(5.0)
|
||||
queue.push(4.2)
|
||||
println('Queue is empty? ' + if queue.empty() { 'Yes' } else { 'No' })
|
||||
println("Queue is empty? " + if queue.empty() { "Yes" } else { "No" })
|
||||
queue.peek() or { return }
|
||||
queue.pop() or { return }
|
||||
queue.pop() or { return }
|
||||
|
|
|
|||
37
Task/Queue-Definition/VBScript/queue-definition.vbs
Normal file
37
Task/Queue-Definition/VBScript/queue-definition.vbs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
' Queue Definition - VBScript
|
||||
Option Explicit
|
||||
Dim queue, i, x
|
||||
Set queue = CreateObject("System.Collections.ArrayList")
|
||||
If Not empty_(queue) Then Wscript.Echo queue.Count
|
||||
push queue, "Banana"
|
||||
push queue, "Apple"
|
||||
push queue, "Pear"
|
||||
push queue, "Strawberry"
|
||||
Wscript.Echo "Count=" & queue.Count
|
||||
Wscript.Echo pull(queue) & " - Count=" & queue.Count '
|
||||
Wscript.Echo "Head=" & queue.Item(0)
|
||||
Wscript.Echo "Tail=" & queue.Item(queue.Count-1)
|
||||
Wscript.Echo queue.IndexOf("Pear", 0)
|
||||
For i=1 To queue.Count
|
||||
Wscript.Echo join(queue.ToArray(), ", ")
|
||||
x = pull(queue)
|
||||
Next 'i
|
||||
|
||||
Sub push(q, what)
|
||||
q.Add what
|
||||
End Sub 'push
|
||||
|
||||
Function pull(q)
|
||||
Dim what
|
||||
If q.Count > 0 Then
|
||||
what = q(0)
|
||||
q.RemoveAt 0
|
||||
Else
|
||||
what = ""
|
||||
End If
|
||||
pull = what
|
||||
End Function 'pull
|
||||
|
||||
Function empty_(q)
|
||||
empty_ = q.Count = 0
|
||||
End Function 'empty_
|
||||
Loading…
Add table
Add a link
Reference in a new issue