Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
|
|
@ -4,47 +4,47 @@
|
|||
|
||||
typedef int DATA; /* type of data to store in queue */
|
||||
typedef struct {
|
||||
DATA *buf;
|
||||
size_t head, tail, alloc;
|
||||
DATA *buf;
|
||||
size_t head, tail, alloc;
|
||||
} queue_t, *queue;
|
||||
|
||||
queue q_new()
|
||||
{
|
||||
queue q = malloc(sizeof(queue_t));
|
||||
q->buf = malloc(sizeof(DATA) * (q->alloc = 4));
|
||||
q->head = q->tail = 0;
|
||||
return q;
|
||||
queue q = malloc(sizeof(queue_t));
|
||||
q->buf = malloc(sizeof(DATA) * (q->alloc = 4));
|
||||
q->head = q->tail = 0;
|
||||
return q;
|
||||
}
|
||||
|
||||
int empty(queue q)
|
||||
{
|
||||
return q->tail == q->head;
|
||||
return q->tail == q->head;
|
||||
}
|
||||
|
||||
void enqueue(queue q, DATA n)
|
||||
{
|
||||
if (q->tail >= q->alloc) q->tail = 0;
|
||||
q->buf[q->tail++] = n;
|
||||
if (q->tail == q->head) { /* needs more room */
|
||||
q->buf = realloc(q->buf, sizeof(DATA) * q->alloc * 2);
|
||||
if (q->head) {
|
||||
memcpy(q->buf + q->head + q->alloc, q->buf + q->head,
|
||||
sizeof(DATA) * (q->alloc - q->head));
|
||||
q->head += q->alloc;
|
||||
} else
|
||||
q->tail = q->alloc;
|
||||
q->alloc *= 2;
|
||||
}
|
||||
if (q->tail >= q->alloc) q->tail = 0;
|
||||
q->buf[q->tail++] = n;
|
||||
if (q->tail == q->head) { /* needs more room */
|
||||
q->buf = realloc(q->buf, sizeof(DATA) * q->alloc * 2);
|
||||
if (q->head) {
|
||||
memcpy(q->buf + q->head + q->alloc, q->buf + q->head,
|
||||
sizeof(DATA) * (q->alloc - q->head));
|
||||
q->head += q->alloc;
|
||||
} else
|
||||
q->tail = q->alloc;
|
||||
q->alloc *= 2;
|
||||
}
|
||||
}
|
||||
|
||||
int dequeue(queue q, DATA *n)
|
||||
{
|
||||
if (q->head == q->tail) return 0;
|
||||
*n = q->buf[q->head++];
|
||||
if (q->head >= q->alloc) { /* reduce allocated storage no longer needed */
|
||||
q->head = 0;
|
||||
if (q->alloc >= 512 && q->tail < q->alloc / 2)
|
||||
q->buf = realloc(q->buf, sizeof(DATA) * (q->alloc/=2));
|
||||
}
|
||||
return 1;
|
||||
if (q->head == q->tail) return 0;
|
||||
*n = q->buf[q->head++];
|
||||
if (q->head >= q->alloc) { /* reduce allocated storage no longer needed */
|
||||
q->head = 0;
|
||||
if (q->alloc >= 512 && q->tail < q->alloc / 2)
|
||||
q->buf = realloc(q->buf, sizeof(DATA) * (q->alloc/=2));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,36 +8,36 @@ struct node_t { int val; node prev, next; };
|
|||
#define TAIL(q) q->next
|
||||
queue q_new()
|
||||
{
|
||||
node q = malloc(sizeof(node_t));
|
||||
q->next = q->prev = 0;
|
||||
return q;
|
||||
node q = malloc(sizeof(node_t));
|
||||
q->next = q->prev = 0;
|
||||
return q;
|
||||
}
|
||||
|
||||
int empty(queue q)
|
||||
{
|
||||
return !HEAD(q);
|
||||
return !HEAD(q);
|
||||
}
|
||||
|
||||
void enqueue(queue q, int n)
|
||||
{
|
||||
node nd = malloc(sizeof(node_t));
|
||||
nd->val = n;
|
||||
if (!HEAD(q)) HEAD(q) = nd;
|
||||
nd->prev = TAIL(q);
|
||||
if (nd->prev) nd->prev->next = nd;
|
||||
TAIL(q) = nd;
|
||||
nd->next = 0;
|
||||
node nd = malloc(sizeof(node_t));
|
||||
nd->val = n;
|
||||
if (!HEAD(q)) HEAD(q) = nd;
|
||||
nd->prev = TAIL(q);
|
||||
if (nd->prev) nd->prev->next = nd;
|
||||
TAIL(q) = nd;
|
||||
nd->next = 0;
|
||||
}
|
||||
|
||||
int dequeue(queue q, int *val)
|
||||
{
|
||||
node tmp = HEAD(q);
|
||||
if (!tmp) return 0;
|
||||
*val = tmp->val;
|
||||
node tmp = HEAD(q);
|
||||
if (!tmp) return 0;
|
||||
*val = tmp->val;
|
||||
|
||||
HEAD(q) = tmp->next;
|
||||
if (TAIL(q) == tmp) TAIL(q) = 0;
|
||||
free(tmp);
|
||||
HEAD(q) = tmp->next;
|
||||
if (TAIL(q) == tmp) TAIL(q) = 0;
|
||||
free(tmp);
|
||||
|
||||
return 1;
|
||||
return 1;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,22 +1,22 @@
|
|||
int main()
|
||||
{
|
||||
int i, n;
|
||||
queue q = q_new();
|
||||
int i, n;
|
||||
queue q = q_new();
|
||||
|
||||
for (i = 0; i < 100000000; i++) {
|
||||
n = rand();
|
||||
if (n > RAND_MAX / 2) {
|
||||
// printf("+ %d\n", n);
|
||||
enqueue(q, n);
|
||||
} else {
|
||||
if (!dequeue(q, &n)) {
|
||||
// printf("empty\n");
|
||||
continue;
|
||||
}
|
||||
// printf("- %d\n", n);
|
||||
}
|
||||
}
|
||||
while (dequeue(q, &n));// printf("- %d\n", n);
|
||||
for (i = 0; i < 100000000; i++) {
|
||||
n = rand();
|
||||
if (n > RAND_MAX / 2) {
|
||||
// printf("+ %d\n", n);
|
||||
enqueue(q, n);
|
||||
} else {
|
||||
if (!dequeue(q, &n)) {
|
||||
// printf("empty\n");
|
||||
continue;
|
||||
}
|
||||
// printf("- %d\n", n);
|
||||
}
|
||||
}
|
||||
while (dequeue(q, &n));// printf("- %d\n", n);
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,6 @@
|
|||
{ number: 1 }
|
||||
{ number: 5000000 }
|
||||
|
||||
real 0m2.394s
|
||||
user 0m2.089s
|
||||
sys 0m0.265s
|
||||
real 0m2.394s
|
||||
user 0m2.089s
|
||||
sys 0m0.265s
|
||||
|
|
|
|||
|
|
@ -2,62 +2,62 @@ MODULE Queue;
|
|||
IMPORT StdLog,Boxes;
|
||||
|
||||
TYPE
|
||||
Queue* = POINTER TO RECORD
|
||||
first,last: LONGINT;
|
||||
queue: POINTER TO ARRAY OF Boxes.Object;
|
||||
END;
|
||||
|
||||
PROCEDURE NewQueue*(cap: LONGINT): Queue;
|
||||
VAR
|
||||
q: Queue;
|
||||
BEGIN
|
||||
NEW(q);q.first := 0; q.last := 0;
|
||||
NEW(q.queue,cap);
|
||||
RETURN q
|
||||
END NewQueue;
|
||||
|
||||
PROCEDURE (q: Queue) IsEmpty*(): BOOLEAN, NEW;
|
||||
VAR
|
||||
BEGIN
|
||||
RETURN (q.first = q.last)
|
||||
END IsEmpty;
|
||||
|
||||
PROCEDURE (q: Queue) Push*(o: Boxes.Object),NEW;
|
||||
VAR
|
||||
i,j,oldSize,newSize: LONGINT;
|
||||
queue: POINTER TO ARRAY OF Boxes.Object;
|
||||
BEGIN
|
||||
IF q.IsEmpty() THEN
|
||||
q.queue[q.last] := o;
|
||||
q.last := (q.last + 1) MOD LEN(q.queue)
|
||||
ELSE
|
||||
q.queue[q.last] := o;
|
||||
q.last := (q.last + 1) MOD LEN(q.queue);
|
||||
IF q.first = q.last THEN
|
||||
(* grow queue *)
|
||||
newSize := LEN(q.queue) + (LEN(q.queue) DIV 2);
|
||||
(* new queue*)
|
||||
NEW(queue,newSize);
|
||||
(* move data from old queue to new queue *)
|
||||
i := q.first; j := 0;oldSize := LEN(q.queue) - q.first + q.last;
|
||||
WHILE (j < oldSize ) & (j < LEN(queue) - 1) DO
|
||||
queue[j] := q.queue[i];
|
||||
i := (i + 1) MOD LEN(q.queue);INC(j)
|
||||
END;
|
||||
q.queue := queue;q.first := 0;q.last := j
|
||||
END
|
||||
END;
|
||||
END Push;
|
||||
|
||||
PROCEDURE (q: Queue) Pop*(): Boxes.Object, NEW;
|
||||
VAR
|
||||
o: Boxes.Object;
|
||||
BEGIN
|
||||
ASSERT(~q.IsEmpty());
|
||||
o := q.queue[q.first];
|
||||
q.queue[q.first] := NIL;
|
||||
q.first := (q.first + 1) MOD LEN(q.queue);
|
||||
RETURN o;
|
||||
END Pop;
|
||||
Queue* = POINTER TO RECORD
|
||||
first,last: LONGINT;
|
||||
queue: POINTER TO ARRAY OF Boxes.Object;
|
||||
END;
|
||||
|
||||
PROCEDURE NewQueue*(cap: LONGINT): Queue;
|
||||
VAR
|
||||
q: Queue;
|
||||
BEGIN
|
||||
NEW(q);q.first := 0; q.last := 0;
|
||||
NEW(q.queue,cap);
|
||||
RETURN q
|
||||
END NewQueue;
|
||||
|
||||
PROCEDURE (q: Queue) IsEmpty*(): BOOLEAN, NEW;
|
||||
VAR
|
||||
BEGIN
|
||||
RETURN (q.first = q.last)
|
||||
END IsEmpty;
|
||||
|
||||
PROCEDURE (q: Queue) Push*(o: Boxes.Object),NEW;
|
||||
VAR
|
||||
i,j,oldSize,newSize: LONGINT;
|
||||
queue: POINTER TO ARRAY OF Boxes.Object;
|
||||
BEGIN
|
||||
IF q.IsEmpty() THEN
|
||||
q.queue[q.last] := o;
|
||||
q.last := (q.last + 1) MOD LEN(q.queue)
|
||||
ELSE
|
||||
q.queue[q.last] := o;
|
||||
q.last := (q.last + 1) MOD LEN(q.queue);
|
||||
IF q.first = q.last THEN
|
||||
(* grow queue *)
|
||||
newSize := LEN(q.queue) + (LEN(q.queue) DIV 2);
|
||||
(* new queue*)
|
||||
NEW(queue,newSize);
|
||||
(* move data from old queue to new queue *)
|
||||
i := q.first; j := 0;oldSize := LEN(q.queue) - q.first + q.last;
|
||||
WHILE (j < oldSize ) & (j < LEN(queue) - 1) DO
|
||||
queue[j] := q.queue[i];
|
||||
i := (i + 1) MOD LEN(q.queue);INC(j)
|
||||
END;
|
||||
q.queue := queue;q.first := 0;q.last := j
|
||||
END
|
||||
END;
|
||||
END Push;
|
||||
|
||||
PROCEDURE (q: Queue) Pop*(): Boxes.Object, NEW;
|
||||
VAR
|
||||
o: Boxes.Object;
|
||||
BEGIN
|
||||
ASSERT(~q.IsEmpty());
|
||||
o := q.queue[q.first];
|
||||
q.queue[q.first] := NIL;
|
||||
q.first := (q.first + 1) MOD LEN(q.queue);
|
||||
RETURN o;
|
||||
END Pop;
|
||||
|
||||
END Queue.
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
DEFINITION Queue;
|
||||
|
||||
IMPORT Boxes;
|
||||
IMPORT Boxes;
|
||||
|
||||
TYPE
|
||||
Queue = POINTER TO RECORD
|
||||
(q: Queue) IsEmpty (): BOOLEAN, NEW;
|
||||
(q: Queue) Pop (): Boxes.Object, NEW;
|
||||
(q: Queue) Push (o: Boxes.Object), NEW
|
||||
END;
|
||||
TYPE
|
||||
Queue = POINTER TO RECORD
|
||||
(q: Queue) IsEmpty (): BOOLEAN, NEW;
|
||||
(q: Queue) Pop (): Boxes.Object, NEW;
|
||||
(q: Queue) Push (o: Boxes.Object), NEW
|
||||
END;
|
||||
|
||||
PROCEDURE NewQueue (cap: LONGINT): Queue;
|
||||
PROCEDURE NewQueue (cap: LONGINT): Queue;
|
||||
|
||||
END Queue.
|
||||
|
|
|
|||
16
Task/Queue-Definition/Deja-Vu/queue-definition.djv
Normal file
16
Task/Queue-Definition/Deja-Vu/queue-definition.djv
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
queue:
|
||||
{ :start 0 :end 0 }
|
||||
|
||||
enqueue q item:
|
||||
set-to q q!end item
|
||||
set-to q :end ++ q!end
|
||||
|
||||
dequeue q:
|
||||
if empty q:
|
||||
Raise :value-error "popping from empty queue"
|
||||
q! q!start
|
||||
delete-from q q!start
|
||||
set-to q :start ++ q!start
|
||||
|
||||
empty q:
|
||||
= q!start q!end
|
||||
|
|
@ -15,7 +15,7 @@ begin
|
|||
Push ( queue, element ) =
|
||||
[ exception (Full(queue), "Queue Overflow");
|
||||
queue.length:= queue.length + 1;
|
||||
add (queue.list, element)];
|
||||
add (queue.list, element)];
|
||||
Pull ( queue ) =
|
||||
[ exception (Empty(queue), "Queue Underflow");
|
||||
queue.length:= queue.length - 1;
|
||||
|
|
|
|||
|
|
@ -1,40 +1,40 @@
|
|||
public class Queue<E>{
|
||||
Node<E> head = null, tail = null;
|
||||
Node<E> head = null, tail = null;
|
||||
|
||||
static class Node<E>{
|
||||
E value;
|
||||
Node<E> next;
|
||||
static class Node<E>{
|
||||
E value;
|
||||
Node<E> next;
|
||||
|
||||
Node(E value, Node<E> next){
|
||||
this.value= value;
|
||||
this.next= next;
|
||||
}
|
||||
Node(E value, Node<E> next){
|
||||
this.value= value;
|
||||
this.next= next;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public Queue(){
|
||||
}
|
||||
public Queue(){
|
||||
}
|
||||
|
||||
public void enqueue(E value){ //standard queue name for "push"
|
||||
Node<E> newNode= new Node<E>(value, null);
|
||||
if(empty()){
|
||||
head= newNode;
|
||||
}else{
|
||||
tail.next = newNode;
|
||||
}
|
||||
tail= newNode;
|
||||
}
|
||||
public void enqueue(E value){ //standard queue name for "push"
|
||||
Node<E> newNode= new Node<E>(value, null);
|
||||
if(empty()){
|
||||
head= newNode;
|
||||
}else{
|
||||
tail.next = newNode;
|
||||
}
|
||||
tail= newNode;
|
||||
}
|
||||
|
||||
public E dequeue() throws java.util.NoSuchElementException{//standard queue name for "pop"
|
||||
if(empty()){
|
||||
throw new java.util.NoSuchElementException("No more elements.");
|
||||
}
|
||||
E retVal= head.value;
|
||||
head= head.next;
|
||||
return retVal;
|
||||
}
|
||||
public E dequeue() throws java.util.NoSuchElementException{//standard queue name for "pop"
|
||||
if(empty()){
|
||||
throw new java.util.NoSuchElementException("No more elements.");
|
||||
}
|
||||
E retVal= head.value;
|
||||
head= head.next;
|
||||
return retVal;
|
||||
}
|
||||
|
||||
public boolean empty(){
|
||||
return head == null;
|
||||
}
|
||||
public boolean empty(){
|
||||
return head == null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
myQueue =
|
||||
|
||||
FIFOQueue
|
||||
FIFOQueue
|
||||
|
||||
>> push(myQueue,'jello')
|
||||
>> pop(myQueue)
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
empty(U-V) :-
|
||||
unify_with_occurs_check(U, V).
|
||||
unify_with_occurs_check(U, V).
|
||||
|
||||
push(Queue, Value, NewQueue) :-
|
||||
append_dl(Queue, [Value|X]-X, NewQueue).
|
||||
append_dl(Queue, [Value|X]-X, NewQueue).
|
||||
|
||||
% when queue is empty pop fails.
|
||||
pop([X|V]-U, X, V-U) :-
|
||||
\+empty([X|V]-U).
|
||||
\+empty([X|V]-U).
|
||||
|
||||
append_dl(X-Y, Y-Z, X-Z).
|
||||
|
|
|
|||
|
|
@ -1,27 +1,27 @@
|
|||
# The usual Scheme way : build a function that takes commands as parameters (it's like message passing oriented programming)
|
||||
queue <- function() {
|
||||
v <- list()
|
||||
f <- function(cmd, val=NULL) {
|
||||
if(cmd == "push") {
|
||||
v <<- c(v, val)
|
||||
invisible()
|
||||
} else if(cmd == "pop") {
|
||||
if(length(v) == 0) {
|
||||
stop("empty queue")
|
||||
} else {
|
||||
x <- v[[1]]
|
||||
v[[1]] <<- NULL
|
||||
x
|
||||
}
|
||||
} else if(cmd == "length") {
|
||||
length(v)
|
||||
} else if(cmd == "empty") {
|
||||
length(v) == 0
|
||||
} else {
|
||||
stop("unknown command")
|
||||
}
|
||||
}
|
||||
f
|
||||
v <- list()
|
||||
f <- function(cmd, val=NULL) {
|
||||
if(cmd == "push") {
|
||||
v <<- c(v, val)
|
||||
invisible()
|
||||
} else if(cmd == "pop") {
|
||||
if(length(v) == 0) {
|
||||
stop("empty queue")
|
||||
} else {
|
||||
x <- v[[1]]
|
||||
v[[1]] <<- NULL
|
||||
x
|
||||
}
|
||||
} else if(cmd == "length") {
|
||||
length(v)
|
||||
} else if(cmd == "empty") {
|
||||
length(v) == 0
|
||||
} else {
|
||||
stop("unknown command")
|
||||
}
|
||||
}
|
||||
f
|
||||
}
|
||||
|
||||
# Create two queues
|
||||
|
|
|
|||
|
|
@ -8,12 +8,12 @@ rebol [
|
|||
; 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]
|
||||
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:
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ class Queue[T] {
|
|||
if(isEmpty) head=Some(n) else tail.get.append(n)
|
||||
tail=Some(n)
|
||||
}
|
||||
|
||||
|
||||
def dequeue:T=head match {
|
||||
case Some(item) => head=item.next; item.value
|
||||
case None => throw new java.util.NoSuchElementException()
|
||||
|
|
|
|||
|
|
@ -2,16 +2,16 @@
|
|||
(let ((q (cons '() '())))
|
||||
(lambda (cmd . arg)
|
||||
(case cmd
|
||||
((empty?) (null? (car q)))
|
||||
((put) (let ((a (cons (car arg) '())))
|
||||
(if (null? (car q))
|
||||
(begin (set-car! q a) (set-cdr! q a))
|
||||
(begin (set-cdr! (cdr q) a) (set-cdr! q a)))))
|
||||
((get) (if (null? (car q)) 'empty
|
||||
(let ((x (caar q)))
|
||||
(set-car! q (cdar q))
|
||||
(if (null? (car q)) (set-cdr! q '()))
|
||||
x)))
|
||||
((empty?) (null? (car q)))
|
||||
((put) (let ((a (cons (car arg) '())))
|
||||
(if (null? (car q))
|
||||
(begin (set-car! q a) (set-cdr! q a))
|
||||
(begin (set-cdr! (cdr q) a) (set-cdr! q a)))))
|
||||
((get) (if (null? (car q)) 'empty
|
||||
(let ((x (caar q)))
|
||||
(set-car! q (cdar q))
|
||||
(if (null? (car q)) (set-cdr! q '()))
|
||||
x)))
|
||||
))))
|
||||
|
||||
(define q (make-queue))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue