• Main Page
  • Classes
  • Files
  • File List

/Users/yzchen/ns/ns-allinone-2.33/ns-2.33/tcp/nilist.h

00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
00002 /*
00003  * Copyright (c) 1997 Regents of the University of California.
00004  * All rights reserved.
00005  * 
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. All advertising materials mentioning features or use of this software
00015  *    must display the following acknowledgement:
00016  *      This product includes software developed by the Daedalus Research
00017  *      Group at the University of California Berkeley.
00018  * 4. Neither the name of the University nor of the Research Group may be
00019  *    used to endorse or promote products derived from this software without
00020  *    specific prior written permission.
00021  * 
00022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  */
00034 
00035 #ifndef NILIST
00036 #define NILIST
00037 
00038 class slink {
00039 public:
00040     slink *next_;
00041     int key_;
00042     slink(int key=0) {next_=0; key_ = key;}
00043     slink(slink *p, int key=0) {next_ = p; key_ = key;}
00044 };
00045 
00046 class slist_base {
00047     slink *last_;
00048     int count_;
00049         int append_count_;
00050         int remove_count_;
00051 public:
00052     slist_base() {last_ = 0;count_=0;append_count_=0;remove_count_=0;}
00053     slist_base(slink *a) {last_ = a->next_ = a;}
00054 
00055     void insert(slink *a);      // add at head
00056     void append(slink *a);      // add at tail
00057     void remove(slink *a, slink *prev);
00058     
00059     slink *get();               // remove from head
00060     slink *find(int key);       // return to matching key
00061 
00062     void clear() {last_ = 0;}
00063     int count() {return count_;}        
00064     slink *last() {return last_;}    /* XXX */
00065     int ac() {return append_count_;} /* XXX */
00066     int rc() {return remove_count_;} /* XXX */
00067     friend class slist_base_iter;
00068 };
00069 
00070 template<class T> class Islist_iter;
00071 
00072 template<class T>
00073 class Islist : private slist_base {
00074         friend class Islist_iter<T>;
00075   public:
00076         void insert(T* a) {slist_base::insert(a);}
00077         void append(T* a) {slist_base::append(a);}
00078         void remove(T* a, T* prev) {slist_base::remove(a,prev);}
00079         T *get() {return (T* ) slist_base::get();}
00080         T *find(int key) {return (T*) slist_base::find(key);}
00081         int count() {return slist_base::count();}
00082 };
00083 
00084 template<class T>
00085 struct Tlink : public slink {
00086     T info;
00087     Tlink(const T& a) : info(a) { }
00088 };
00089 
00090 template<class T> class Slist_iter;
00091 
00092 template<class T>
00093 class Slist: private slist_base {
00094     friend class Slist_iter<T>;
00095 public:
00096     void insert(const T& a)
00097          { slist_base::insert(new Tlink<T>(a));}
00098     void append(const T& a)
00099          { slist_base::append(new Tlink<T>(a));}
00100     T get();
00101 };
00102 
00103 class slist_base_iter 
00104 {
00105     slink *ce;                  
00106     slist_base *cs;
00107 public:
00108     slist_base_iter(slist_base &s);
00109     void set_cur(slink *cur) {ce = cur;}
00110     slink *get_cur() {return ce;}
00111     slink *get_last() {return cs->last_;}
00112     slink *operator() ();
00113     int count() {return cs->count();}
00114 };
00115 
00116 template <class T>
00117 class Islist_iter : public slist_base_iter {
00118 public:
00119     Islist_iter(Islist<T> &s) : slist_base_iter(s) { }
00120     T *operator() ()
00121        { return (T *) slist_base_iter::operator() (); }
00122     T *get_last() {return (T *) slist_base_iter::get_last(); }
00123     T *get_cur() {return (T *) slist_base_iter::get_cur(); }
00124 };
00125 
00126 template <class T>
00127 class Slist_iter : public slist_base_iter {
00128 public:
00129     Slist_iter(Slist<T> &s) : slist_base_iter(s) { }
00130     inline T *operator() ();
00131 };
00132 
00133 
00134 
00135 #endif

Generated on Tue Aug 10 2010 16:16:08 for ns-2.33 by  doxygen 1.7.1