sc_list.cpp

Go to the documentation of this file.
00001 /*****************************************************************************
00002 
00003   The following code is derived, directly or indirectly, from the SystemC
00004   source code Copyright (c) 1996-2006 by all Contributors.
00005   All Rights reserved.
00006 
00007   The contents of this file are subject to the restrictions and limitations
00008   set forth in the SystemC Open Source License Version 2.4 (the "License");
00009   You may not use this file except in compliance with such restrictions and
00010   limitations. You may obtain instructions on how to receive a copy of the
00011   License at http://www.systemc.org/. Software distributed by Contributors
00012   under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
00013   ANY KIND, either express or implied. See the License for the specific
00014   language governing rights and limitations under the License.
00015 
00016  *****************************************************************************/
00017 
00018 /*****************************************************************************
00019 
00020   sc_list.cpp -- Simple implementation of a doubly linked list.
00021 
00022   Original Author: Stan Y. Liao, Synopsys, Inc.
00023 
00024  *****************************************************************************/
00025 
00026 /*****************************************************************************
00027 
00028   MODIFICATION LOG - modifiers, enter your name, affiliation, date and
00029   changes you are making here.
00030 
00031       Name, Affiliation, Date:
00032   Description of Modification:
00033 
00034  *****************************************************************************/
00035 
00036 
00037 // $Log: sc_list.cpp,v $
00038 // Revision 1.1.1.1  2006/12/15 20:31:39  acg
00039 // SystemC 2.2
00040 //
00041 // Revision 1.3  2006/01/13 18:53:10  acg
00042 // Andy Goodrich: Added $Log command so that CVS comments are reproduced in
00043 // the source.
00044 //
00045 
00046 #include <assert.h>
00047 
00048 #include "sysc/kernel/sc_cmnhdr.h"
00049 #include "sysc/utils/sc_iostream.h"
00050 #include "sysc/utils/sc_list.h"
00051 #include "sysc/utils/sc_mempool.h"
00052 #include "sysc/utils/sc_report.h"
00053 #include "sysc/utils/sc_utils_ids.h"
00054 
00055 namespace sc_core {
00056 
00057 class sc_plist_elem {
00058     friend class sc_plist_base_iter;
00059     friend class sc_plist_base;
00060 
00061 private:
00062     sc_plist_elem() { prev = 0; next = 0; }
00063     sc_plist_elem( void* d, sc_plist_elem* p, sc_plist_elem* n )
00064     {
00065         data = d; prev = p; next = n;
00066     }
00067     ~sc_plist_elem()
00068     {
00069 
00070     }
00071 
00072     static void* operator new(std::size_t sz)            { return sc_mempool::allocate(sz); }
00073     static void operator delete(void* p, std::size_t sz) { sc_mempool::release(p, sz);      }
00074     
00075     void* data;
00076     sc_plist_elem* prev;
00077     sc_plist_elem* next;
00078 };
00079 
00080 sc_plist_base::sc_plist_base()
00081 {
00082     head = 0;
00083     tail = 0;
00084 }
00085 
00086 sc_plist_base::~sc_plist_base()
00087 {
00088     handle_t p;
00089     for( handle_t h = head; h != 0; h = p ) {
00090         p = h->next;
00091         delete h;
00092     }
00093 }
00094 
00095 void
00096 sc_plist_base::erase_all()
00097 {
00098     handle_t p;
00099     for( handle_t h = head; h != 0; h = p ) {
00100         p = h->next;
00101         delete h;
00102     }
00103     head = 0;
00104     tail = 0;
00105 }
00106 
00107 int
00108 sc_plist_base::size() const
00109 {
00110     int n = 0;
00111     for( handle_t h = head; h != 0; h = h->next ) {
00112         n++;
00113     }
00114     return n;
00115 }
00116 
00117 sc_plist_base::handle_t
00118 sc_plist_base::push_back( void* d )
00119 {
00120     handle_t q = new sc_plist_elem( d, tail, 0 );
00121     if (tail) {
00122         tail->next = q;
00123         tail = q;
00124     }
00125     else {
00126         head = tail = q;
00127     }
00128     return q;
00129 }
00130 
00131 sc_plist_base::handle_t
00132 sc_plist_base::push_front( void* d )
00133 {
00134     handle_t q = new sc_plist_elem( d, (sc_plist_elem*) 0, head );
00135     if (head) {
00136         head->prev = q;
00137         head = q;
00138     }
00139     else {
00140         head = tail = q;
00141     }
00142     return q;
00143 }
00144 
00145 void*
00146 sc_plist_base::pop_back()
00147 {
00148     handle_t q = tail;
00149     void* d = q->data;
00150     tail = tail->prev;
00151     delete q;
00152     if (tail != 0) {
00153         tail->next = 0;
00154     }
00155     else {
00156         head = 0;
00157     }
00158     return d;
00159 }
00160 
00161 void*
00162 sc_plist_base::pop_front()
00163 {
00164     handle_t q = head;
00165     void* d = q->data;
00166     head = head->next;
00167     delete q;
00168     if (head != 0) {
00169         head->prev = 0;
00170     }
00171     else {
00172         tail = 0;
00173     }
00174     return d;
00175 }
00176 
00177 sc_plist_base::handle_t
00178 sc_plist_base::insert_before( handle_t h, void* d )
00179 {
00180     if (h == 0) {
00181         return push_back(d);
00182     }
00183     else {
00184         handle_t q = new sc_plist_elem( d, h->prev, h );
00185         h->prev->next = q;
00186         h->prev = q;
00187         return q;
00188     }
00189 }
00190 
00191 sc_plist_base::handle_t
00192 sc_plist_base::insert_after( handle_t h, void* d )
00193 {
00194     if (h == 0) {
00195         return push_front(d);
00196     }
00197     else {
00198         handle_t q = new sc_plist_elem( d, h, h->next );
00199         h->next->prev = q;
00200         h->next = q;
00201         return q;
00202     }
00203 }
00204 
00205 void*
00206 sc_plist_base::remove( handle_t h )
00207 {
00208     if (h == head)
00209         return pop_front();
00210     else if (h == tail)
00211         return pop_back();
00212     else {
00213         void* d = h->data;
00214         h->prev->next = h->next;
00215         h->next->prev = h->prev;
00216         delete h;
00217         return d;
00218     }
00219 }
00220 
00221 void*
00222 sc_plist_base::get( handle_t h ) const
00223 {
00224     return h->data;
00225 }
00226 
00227 void
00228 sc_plist_base::set( handle_t h, void* d )
00229 {
00230     h->data = d;
00231 }
00232 
00233 void
00234 sc_plist_base::mapcar( sc_plist_map_fn f, void* arg )
00235 {
00236     for (handle_t h = head; h != 0; h = h->next) {
00237         f( h->data, arg );
00238     }
00239 }
00240 
00241 void*
00242 sc_plist_base::front() const
00243 {
00244 
00245    if (head) {          
00246         return head->data;
00247     }               
00248     else {
00249       SC_REPORT_ERROR( SC_ID_FRONT_ON_EMPTY_LIST_ , 0 );
00250       // never reached
00251       return 0;
00252     }
00253 }
00254 
00255 void*
00256 sc_plist_base::back() const
00257 {
00258    if (tail) {
00259         return tail->data;
00260     }
00261     else {
00262       SC_REPORT_ERROR( SC_ID_BACK_ON_EMPTY_LIST_, 0 );
00263       // never reached
00264       return 0;
00265     }
00266 }
00267 
00268 
00269 
00270 sc_plist_base_iter::sc_plist_base_iter( sc_plist_base* l, bool from_tail )
00271 {
00272     reset( l, from_tail );
00273 }
00274 
00275 void
00276 sc_plist_base_iter::reset( sc_plist_base* l, bool from_tail )
00277 {
00278     lst = l;
00279     if (from_tail) {
00280         ptr = l->tail;
00281     }
00282     else {
00283         ptr = l->head;
00284     }
00285 }
00286 
00287 sc_plist_base_iter::~sc_plist_base_iter()
00288 {
00289 
00290 }
00291 
00292 bool
00293 sc_plist_base_iter::empty() const
00294 {
00295     return ptr == 0;
00296 }
00297 
00298 void
00299 sc_plist_base_iter::operator++(int)
00300 {
00301     ptr = ptr->next;
00302 }
00303 
00304 void
00305 sc_plist_base_iter::operator--(int)
00306 {
00307     ptr = ptr->prev;
00308 }
00309 
00310 void*
00311 sc_plist_base_iter::get() const
00312 {
00313     return ptr->data;
00314 }
00315 
00316 void
00317 sc_plist_base_iter::set( void* d )
00318 {
00319     ptr->data = d;
00320 }
00321 
00322 void
00323 sc_plist_base_iter::remove()
00324 {
00325     sc_plist_base::handle_t nptr = ptr->next;
00326     lst->remove(ptr);
00327     ptr = nptr;
00328 }
00329 
00330 void
00331 sc_plist_base_iter::remove(int direction)
00332 {
00333     sc_plist_base::handle_t nptr = (direction == 1) ? ptr->next : ptr->prev;
00334     lst->remove(ptr);
00335     ptr = nptr;
00336 }
00337 
00338 void
00339 sc_plist_base_iter::set_handle( sc_plist_elem* h )
00340 {
00341     ptr = h;
00342 }
00343 
00344 } // namespace sc_core

Generated on Wed Jan 21 15:32:10 2009 for SystemC by  doxygen 1.5.5