queue.h

00001 /* MLPACK 0.2
00002  *
00003  * Copyright (c) 2008, 2009 Alexander Gray,
00004  *                          Garry Boyer,
00005  *                          Ryan Riegel,
00006  *                          Nikolaos Vasiloglou,
00007  *                          Dongryeol Lee,
00008  *                          Chip Mappus, 
00009  *                          Nishant Mehta,
00010  *                          Hua Ouyang,
00011  *                          Parikshit Ram,
00012  *                          Long Tran,
00013  *                          Wee Chin Wong
00014  *
00015  * Copyright (c) 2008, 2009 Georgia Institute of Technology
00016  *
00017  * This program is free software; you can redistribute it and/or
00018  * modify it under the terms of the GNU General Public License as
00019  * published by the Free Software Foundation; either version 2 of the
00020  * License, or (at your option) any later version.
00021  *
00022  * This program is distributed in the hope that it will be useful, but
00023  * WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025  * General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License
00028  * along with this program; if not, write to the Free Software
00029  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00030  * 02110-1301, USA.
00031  */
00038 #ifndef COL_QUEUE_H
00039 #define COL_QUEUE_H
00040 
00048 template<typename T>
00049 class Queue {
00050   FORBID_ACCIDENTAL_COPIES(Queue); // No copy constructor defined (yet)
00051 
00052  private:
00053   struct Node {
00054     T data;
00055     Node *next;
00056 
00057     Node() : next(NULL) { }
00058     Node(const T& value) : data(value), next(NULL) {}
00059   };
00060 
00061  private:
00062   Node *head_;
00063   Node **tailp_;
00064 
00065  public:
00066   Queue() { DEBUG_POISON_PTR(tailp_); }
00067   ~Queue() {
00068     Clear();
00069   }
00070 
00074   void Init() {
00075     head_ = NULL;
00076     tailp_ = &head_;
00077   }
00078 
00084   T *Add() {
00085     Node *node = new Node();
00086     *tailp_ = node;
00087     tailp_ = &node->next;
00088     return &node->data;
00089   }
00090 
00096   T *Add(const T& value) {
00097     Node *node = new Node(value);
00098     node->next = NULL;
00099     *tailp_ = node;
00100     tailp_ = &node->next;
00101     return &node->data;
00102   }
00103 
00107   void PopOnly() {
00108     Node *tmp = head_;
00109     head_ = head_->next;
00110     delete tmp;
00111     if (unlikely(head_ == NULL)) {
00112       tailp_ = &head_;
00113     }
00114   }
00115 
00119   T Pop() {
00120     T val(head_->data);
00121     PopOnly();
00122     return val;
00123   }
00124 
00128   bool is_empty() const {
00129     return head_ == NULL;
00130   }
00131 
00135   const T& top() const {
00136     return head_->data;
00137   }
00138 
00142   T& top() {
00143     return head_->data;
00144   }
00145 
00149   void Clear() {
00150     Node *cur;
00151     Node *next;
00152 
00153     for (cur = head_; cur; cur = next) {
00154       next = cur->next;
00155       delete cur;
00156     }
00157 
00158     head_ = NULL;
00159     tailp_ = &head_;
00160   }
00161 };
00162 
00163 #endif
Generated on Mon Jan 24 12:04:37 2011 for FASTlib by  doxygen 1.6.3