File:  [LON-CAPA] / loncom / types / Queue.pm
Revision 1.3: download - view: text, annotated - select for diffs
Thu Apr 24 10:57:57 2003 UTC (21 years ago) by foxr
Branches: MAIN
CVS tags: HEAD
Added Count member to fetch the number of elements in the data structure.

    1: #  Implement a simple queue in terms of a list.
    2: #
    3: # $Id: Queue.pm,v 1.3 2003/04/24 10:57:57 foxr Exp $
    4: #
    5: # Copyright Michigan State University Board of Trustees
    6: #
    7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    8: #
    9: # LON-CAPA is free software; you can redistribute it and/or modify
   10: # it under the terms of the GNU General Public License as published by
   11: # the Free Software Foundation; either version 2 of the License, or
   12: # (at your option) any later version.
   13: #
   14: # LON-CAPA is distributed in the hope that it will be useful,
   15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17: # GNU General Public License for more details.
   18: #
   19: # You should have received a copy of the GNU General Public License
   20: # along with LON-CAPA; if not, write to the Free Software
   21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   22: #
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: 
   28: =pod
   29: 
   30: =head1 Queue
   31: 
   32: An object oriented implementation of a queue data structure.  queues
   33: are first in last out data structures.
   34: 
   35: =head1 Member functions:
   36: 
   37: =cut
   38: 
   39: package Queue;
   40: 
   41: 
   42: =pod
   43: 
   44: =head2 new 
   45: 
   46:   Construct a new queue.
   47: 
   48: =cut
   49: 
   50: sub new {
   51:     my $class = shift;		# Class name to bless into.
   52:     my $self  = [];		# Our data is a reference to an anon. array.
   53: 
   54:     bless($self, $class);	# Type this as an object.
   55: 
   56:     return $self;
   57: }
   58: 
   59: =pod
   60: 
   61: =head2 enqueue
   62: 
   63:    Add an element to the tail of the queue.
   64: 
   65: =cut
   66: 
   67: sub enqueue {
   68:     my $self = shift;		# Get object reference.
   69:     my $item = shift;		# Get the item to enqueue...
   70:     push(@$self, $item);		# Push the item to the back of the array.
   71: 
   72: 
   73: }
   74: 
   75: =pod
   76: 
   77: =head2 dequeue
   78: 
   79:    Remove an element from the front of the queue.
   80: 
   81: =cut
   82: 
   83: sub dequeue {
   84:     my $self = shift;		# Get object reference....
   85:     return shift @$self;		# Remove from the front of the queue.
   86: }
   87: 
   88: 
   89: =pod  
   90: =head1 Count
   91: 
   92: Returns number of items in a queue.
   93: 
   94: =cut
   95: 
   96: sub Count {
   97:     my $self = shift;
   98:     my $count = scalar(@$self);
   99:     return $count;
  100: }
  101: 
  102: 1;
  103: 
  104: =pod
  105: 
  106: =head1 Theory
  107: 
  108: The queue is implemented as an array... enqueue is a thinly disguised
  109: push, and dequeue is a thinly disguised shift.  This is probably quite
  110: in efficient for large queues, but should be fine for reasonably sized
  111: queues.
  112: 
  113: Note that since Perl is a dynamically typed language, queues can
  114: contain objects of any data type and can even be heterogenously typed.
  115: 
  116: =cut

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>