00001 /*****************************************************************************/ 00002 /*! 00003 * \file cdlist.h 00004 * 00005 * Author: Clark Barrett 00006 * 00007 * Created: Wed Feb 12 18:45:26 2003 00008 * 00009 * <hr> 00010 * Copyright (C) 2003 by the Board of Trustees of Leland Stanford 00011 * Junior University and by New York University. 00012 * 00013 * License to use, copy, modify, sell and/or distribute this software 00014 * and its documentation for any purpose is hereby granted without 00015 * royalty, subject to the terms and conditions defined in the \ref 00016 * LICENSE file provided with this distribution. In particular: 00017 * 00018 * - The above copyright notice and this permission notice must appear 00019 * in all copies of the software and related documentation. 00020 * 00021 * - THE SOFTWARE IS PROVIDED "AS-IS", WITHOUT ANY WARRANTIES, 00022 * EXPRESSED OR IMPLIED. USE IT AT YOUR OWN RISK. 00023 * 00024 * <hr> 00025 * 00026 */ 00027 /*****************************************************************************/ 00028 00029 #ifndef _cvcl__include__cdlist_h_ 00030 #define _cvcl__include__cdlist_h_ 00031 00032 #include "context.h" 00033 #include <deque> 00034 00035 namespace CVCL { 00036 00037 /////////////////////////////////////////////////////////////////////////////// 00038 // // 00039 // Class: CDList (Context Dependent List) // 00040 // Author: Clark Barrett // 00041 // Created: Wed Feb 12 17:28:25 2003 // 00042 // Description: Generic templated class for list which grows monotonically // 00043 // over time (if the context is not popped) but must also be // 00044 // saved and restored as contexts are pushed and popped. // 00045 // // 00046 /////////////////////////////////////////////////////////////////////////////// 00047 // TODO: more efficient implementation 00048 template <class T> 00049 class CDList :public ContextObj { 00050 //! The actual data. 00051 /*! Use deque because it doesn't create/destroy data on resize. 00052 This pointer is only non-NULL in the master copy. */ 00053 std::deque<T>* d_list; // 00054 unsigned d_size; 00055 00056 virtual ContextObj* makeCopy(void) { return new CDList<T>(*this); } 00057 virtual void restoreData(ContextObj* data) 00058 { d_size = ((CDList<T>*)data)->d_size; 00059 while (d_list->size() > d_size) d_list->pop_back(); } 00060 virtual void setNull(void) 00061 { while (d_list->size()) d_list->pop_back(); d_size = 0; } 00062 00063 // Copy constructor (private). Do NOT copy d_list. It's not used 00064 // in restore, and it will be deleted in destructor. 00065 CDList(const CDList<T>& l): ContextObj(l), d_list(NULL), d_size(l.d_size) { } 00066 public: 00067 CDList(Context* context) : ContextObj(context), d_size(0) { 00068 d_list = new std::deque<T>(); 00069 IF_DEBUG(setName("CDList")); 00070 } 00071 virtual ~CDList() { if(d_list != NULL) delete d_list; } 00072 unsigned size() const { return d_size; } 00073 bool empty() const { return d_size == 0; } 00074 T& push_back(const T& data, int scope = -1) 00075 { makeCurrent(scope); d_list->push_back(data); d_size++; return d_list->back(); } 00076 const T& operator[](unsigned i) const { 00077 DebugAssert(i < size(), 00078 "CDList["+int2string(i)+"]: i < size="+int2string(size())); 00079 return (*d_list)[i]; 00080 } 00081 const T& at(unsigned i) const { 00082 DebugAssert(i < size(), 00083 "CDList["+int2string(i)+"]: i < size="+int2string(size())); 00084 return (*d_list)[i]; 00085 } 00086 const T& back() const { 00087 DebugAssert(size() > 0, 00088 "CDList::back(): size="+int2string(size())); 00089 return d_list->back(); 00090 } 00091 typedef typename std::deque<T>::const_iterator const_iterator; 00092 const_iterator begin() const { 00093 return d_list->begin(); 00094 } 00095 const_iterator end() const { 00096 return begin() + d_size; 00097 } 00098 }; 00099 00100 } 00101 00102 #endif