pike.git / lib / modules / ADT.pmod / Queue.pike

version» Context lines:

pike.git/lib/modules/ADT.pmod/Queue.pike:1:      //! A simple FIFO queue.      #pike __REAL_VERSION__ - #define QUEUE_SIZE 100 + #pragma strict_types      array l; - int head; - int tail; +       //! Creates a queue with the initial items @[args] in it.   protected void create(mixed ...args)   { -  l = args + allocate(QUEUE_SIZE); -  head = sizeof(args); -  tail = 0; +  l = args;   }      protected int _sizeof()   { -  return head - tail; +  return sizeof(l);   }      protected array _values()   { -  return l[tail..head-1]; +  return values(l);   }    - void write(mixed ... items) + //! @deprecated put + __deprecated__ void write(mixed ... items)   { -  put(@items); +  l += items;   }    - //! @decl void write(mixed ... items) +    //! @decl void put(mixed ... items)   //! Adds @[items] to the queue. - // +    void put(mixed ... items)   { -  if (sizeof(items) + head > sizeof(l)) { -  l = l[tail ..]; -  head -= tail; -  tail = 0; -  l += allocate(sizeof(l) + max(QUEUE_SIZE, sizeof(items))); +  l += items;   } -  foreach (items; int n; mixed item) { -  l[head++] = item; -  } - // werror(sprintf("Queue->put: %O\n", l[tail..head-1])); - } +     - mixed read() + //! @deprecated get + __deprecated__ mixed read()   {    return get();   }    - //! @decl mixed read() +    //! @decl mixed get() - //! Returns the next element from the queue. - // + //! Returns the next element from the queue, or @expr{UNDEFINED@} if + //! the queue is empty.   mixed get()   { - // werror(sprintf("Queue->get: %O\n", l[tail..head-1])); -  mixed res; -  if (tail == head) -  return UNDEFINED; -  res = l[tail]; -  l[tail++] = 0; +  if( !sizeof(l) ) return UNDEFINED; +  mixed res = l[0]; +  l = l[1..];    return res;   }    - //! Returns the next element from the queue - //! without removing it from the queue. + //! Returns the next element from the queue without removing it from + //! the queue. Returns @expr{0@} if the queue is empty.   mixed peek()   { -  return (tail < head) && l[tail]; +  return sizeof(l) && l[0];   }    - //! Returns true if the queue is empty, - //! otherwise zero. + //! Returns true if the queue is empty, otherwise zero.   int(0..1) is_empty()   { -  return (tail == head); +  return !sizeof(l);   }      //! Empties the queue.   void flush()   { -  create(); +  l = ({});   }      //! It is possible to cast ADT.Queue to an array. - protected mixed cast(string to) { -  switch(to) { -  case "object": return this; -  case "array": return l[tail..head-1]; + protected mixed cast(string to) + { +  if( to=="array" ) +  return l+({}); +  return UNDEFINED;   } -  error("Can not cast ADT.Queue to %s.\n", to); - } +       protected string _sprintf(int t) { -  return t=='O' && l && sprintf("%O%O", this_program, cast("array")); +  return t=='O' && sprintf("%O(%O)", this_program, l);   }