00001
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00046 #ifndef SIGNALSEQUENCE_IMPL_H
00047 #define SIGNALSEQUENCE_IMPL_H
00048
00049
00050 #include <iostream>
00051 #include <sstream>
00052 #include <complex>
00053 #include <vector>
00054 #include <stdexcept>
00055 #include <typeinfo>
00056
00057 #include <simthetic/signals.h>
00058 #include <simthetic/phbib.h>
00059
00060 namespace simth
00061 {
00062
00063 template<class Val, class Attr, class Cont, class Stor, class Accs>
00064 SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::SignalSequenceBase(iterator iterStart,
00065 iterator iterEnd, storage_type &seq)
00066 : storage(&seq)
00067 , iter1(iterStart)
00068 , iter2(iterEnd)
00069 , size_( std::distance(iter1,iter2) ), capacity_(size_)
00070 {
00071 invariante();
00072 }
00073
00074 template<class Val, class Attr, class Cont, class Stor, class Accs>
00075 SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::SignalSequenceBase(storage_type &seq)
00076 : storage(&seq), iter1(seq.begin()), iter2(seq.end()),
00077 size_( std::distance(iter1,iter2) ), capacity_(size_)
00078 {
00079 invariante();
00080 }
00081
00082
00083
00084
00085
00086 template<class Val, class Attr, class Cont, class Stor, class Accs>
00087 SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::SignalSequenceBase( )
00088 : storage(NULL)
00089 {
00090
00091
00092 }
00093
00094 template<class Val, class Attr, class Cont, class Stor, class Accs>
00095 SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::~SignalSequenceBase()
00096 {
00097
00098 }
00099
00100
00101 template<class Val, class Attr, class Cont, class Stor, class Accs>
00102 inline void SignalSequenceBase<Val, Attr, Cont, Stor, Accs>::invariante() const
00103 {
00104 #if DEBUG==1
00105 if(std::distance(iter1,iter2) != int(size())) {
00106 throw std::logic_error("void SignalSequenceBase<T,Sto>::invariante: "
00107 "distance(iter1,iter2)=="
00108 +simth::i2string(std::distance(iter1,iter2))
00109 +" != size()=="+simth::i2string(size()));
00110 }
00111 if(capacity() < size()) {
00112 throw std::runtime_error("void SignalSequenceBase<T,Sto>::invariante: "
00113 "capacity < size");
00114 }
00115 #endif
00116 }
00117
00118 template<class Val, class Attr, class Cont, class Stor, class Accs>
00119 SignalSequenceBase<Val, Attr, Cont, Stor, Accs>&
00120 SignalSequenceBase<Val, Attr, Cont, Stor, Accs>::operator=(const SignalSequenceBase<value_type,attributes_type,container_type,storage_type,access_type>& seq)
00121 {
00122 if(&seq == this) {
00123 return *this;
00124 }
00125
00126 resize( seq.size() );
00127
00128 iterator thisIter = begin();
00129 for(const_iterator fromIter = seq.begin(); fromIter!=seq.end(); ++fromIter) {
00130 (*thisIter) = (*fromIter);
00131 ++thisIter;
00132 }
00133
00134 if(&(rAttributes()) != &(seq.rAttributes()) &&
00135 rAttributes() != seq.rAttributes()) {
00136 wAttributes() = seq.rAttributes();
00137 }
00138
00139 invariante();
00140 return *this;
00141 }
00142
00143 template<class Val, class Attr, class Cont, class Stor, class Accs>
00144 void SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::resize(size_t newSize)
00145 {
00146
00147 if(newSize == size()) {
00148 return;
00149 }
00150
00151 if(newSize > capacity()) {
00152 std::ostringstream ost;
00153 ost<<"void SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::resize(size_t newSize) : Resizing to a size larger ";
00154 ost<<"than the capacity is not possible for a shallow copy sequence. Old size: "<<size()<<", capacity: "<<capacity();
00155 ost<<" new size: "<<newSize;
00156 throw std::runtime_error(ost.str());
00157 }
00158
00159 iter2 = iter1+ newSize;
00160 size_ = newSize;
00161
00162 invariante();
00163 }
00164
00165 template<class Val, class Attr, class Cont, class Stor, class Accs>
00166 void SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::seek(iterator newStart, iterator newEnd)
00167 {
00168 iter1 = newStart;
00169 iter2 = newEnd;
00170 size_ = std::distance(iter1,iter2);
00171 capacity_ = size_;
00172 invariante();
00173 }
00174
00175 template<class Val, class Attr, class Cont, class Stor, class Accs>
00176 void SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::seek(iterator newStart, iterator newEnd,storage_type& storage_)
00177 {
00178 iter1 = newStart;
00179 iter2 = newEnd;
00180 storage = &storage_;
00181 size_ = std::distance(iter1,iter2);
00182 capacity_ = size_;
00183 invariante();
00184 }
00185
00186
00187 template<class Val, class Attr, class Cont, class Stor, class Accs>
00188 inline typename SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::iterator
00189 SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::begin()
00190 {
00191 invariante();
00192 return iter1;
00193 }
00194
00195 template<class Val, class Attr, class Cont, class Stor, class Accs>
00196 inline typename SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::const_iterator
00197 SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::begin() const
00198 {
00199 invariante();
00200 return iter1;
00201 }
00202
00203 template<class Val, class Attr, class Cont, class Stor, class Accs>
00204 inline typename SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::iterator
00205 SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::end()
00206 {
00207 invariante();
00208 return iter2;
00209 }
00210
00211 template<class Val, class Attr, class Cont, class Stor, class Accs>
00212 inline typename SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::const_iterator
00213 SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::end() const
00214 {
00215 invariante();
00216 return iter2;
00217 }
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228 template<class Val, class Attr, class Cont>
00229 SignalSequence<Val,Attr,Cont>::SignalSequence(iterator iterStart,iterator iterEnd, storage_type &seq)
00230 : SignalSequenceBase<Val,Attr,Cont,storage_type,access_type>(iterStart,iterEnd,seq)
00231 { }
00232
00233 template<class Val, class Attr, class Cont>
00234 SignalSequence<Val,Attr,Cont>::SignalSequence(storage_type &seq)
00235 : SignalSequenceBase<Val,Attr,Cont,storage_type,access_type>(seq)
00236 { }
00237
00238
00239
00240
00241
00242 template<class Val, class Attr, class Cont>
00243 SignalSequence<Val,Attr,Cont>::SignalSequence( )
00244 : SignalSequenceBase<Val,Attr,Cont,storage_type,access_type>( )
00245 {
00246
00247 }
00248
00249 template<class Val, class Attr, class Cont>
00250 SignalSequence<Val,Attr,Cont>::~SignalSequence()
00251 {
00252
00253 }
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265 template<class T, class A, class Cont>
00266 SignalSequenceStorage<T,A,Cont>::SignalSequenceStorage()
00267 : base_type( ),ownStorage(0), attributes_(), lock_(false)
00268 {
00269 seek(ownStorage.begin(),ownStorage.end(),*this);
00270 invariante();
00271 }
00272
00273 template<class T, class A, class Cont>
00274 SignalSequenceStorage<T,A,Cont>::SignalSequenceStorage(size_t size, const value_type &val)
00275 : base_type( ),ownStorage(size,val), attributes_(), lock_(false)
00276 {
00277 seek(ownStorage.begin(),ownStorage.end(),*this);
00278 invariante();
00279 }
00280
00281 template<class T, class A, class Cont>
00282 SignalSequenceStorage<T,A,Cont>::SignalSequenceStorage(const SignalSequence<value_type,attributes_type,container_type> &seq)
00283 : base_type( ), ownStorage(seq.size()), attributes_( seq.rAttributes() ), lock_(false)
00284 {
00285 seek(ownStorage.begin(),ownStorage.end(),*this);
00286 invariante();
00287
00288 if(DEBUG) {
00289 if(base_type::size() != seq.size()) {
00290 throw std::logic_error("SignalSequenceStorage<T,A,Cont>::SignalSequenceStorage(..) : error 1a");
00291 }
00292 }
00293 iterator to = ownStorage.begin();
00294 for(const_iterator from = seq.begin();from!=seq.end();++from) {
00295 *to = *from;
00296 ++to;
00297 }
00298 invariante();
00299 }
00300
00301 template<class T, class A, class Cont>
00302 SignalSequenceStorage<T,A,Cont>::~SignalSequenceStorage()
00303 { }
00304
00305 template<class T, class A, class Cont>
00306 inline void SignalSequenceStorage<T,A,Cont>::invariante() const
00307 {
00308 #if DEBUG==1
00309 if(base_type::begin() != ownStorage.begin() ||
00310 base_type::end() != ownStorage.end()) {
00311 throw std::logic_error("inline void SignalSequenceStorage<T,A,Cont>::invariante() const : error 1");
00312 }
00313 #endif
00314 }
00315
00316 template<class T, class A, class Cont>
00317 A& SignalSequenceStorage<T,A,Cont>::wAttributes()
00318 {
00319
00320 if(base_type::isAttributesLocked()) {
00321
00322 throw std::runtime_error("SignalSequenceStorage::wAttributes(): "
00323 "write access to attributes requested "
00324 "while attributes are locked, in class "
00325 + std::string(typeid(*this).name()));
00326 }
00327
00328 return attributes_ ;
00329 }
00330
00331 template<class T, class A, class Cont>
00332 void SignalSequenceStorage<T,A,Cont>::resize(size_t newSize)
00333 {
00334
00335
00336
00337 ownStorage.resize(newSize);
00338
00339 seek(ownStorage.begin(),ownStorage.end());
00340 invariante();
00341 }
00342
00343
00344 }
00345
00346 #endif // SIGNALSEQUENCE_IMPL_H