00001
00002
00003 #ifndef GEN_TYPE_VECTOR_H
00004 #define GEN_TYPE_VECTOR_H
00005
00006 #include "Object.h"
00007 #include <vector>
00008 #include "ObjectParser.h"
00009 #include "ObjectRef.h"
00010 #include "binio.h"
00011 #include "typetraits.h"
00012 #include <string>
00013 #include "VectorPool.h"
00014
00015 namespace FD {
00016
00017
00022 class BaseVector : public Object {
00023 protected:
00024
00025 public:
00026
00028 BaseVector(){}
00029
00031 virtual size_t vsize() const = 0;
00032
00034 virtual bool vempty() const = 0;
00035
00042 virtual ObjectRef range(size_t startInd, size_t endInd)=0;
00043
00049 virtual ObjectRef getIndex(int pos) = 0;
00050
00056 virtual void setIndex(int pos, ObjectRef val) = 0;
00057
00058
00062 virtual ObjectRef clone() = 0;
00063
00064 };
00065
00070 template<class T>
00071 class Vector : public BaseVector , public std::vector<T> {
00072 public:
00073
00075 typedef T basicType;
00076
00078 Vector()
00079 : BaseVector()
00080 , std::vector<T> ()
00081 {}
00082
00084 Vector(const Vector<T> &v)
00085 : BaseVector()
00086 , std::vector<T> (v)
00087 {
00088 }
00089
00095 explicit Vector(size_t n, const T &x = T())
00096 : BaseVector()
00097 , std::vector<T> (n, x)
00098 {}
00099
00101 ~Vector()
00102 {
00103 }
00104
00105 virtual size_t vsize() const {return this->size();}
00106
00107 virtual bool vempty() const {return this->empty();}
00108
00113 void prettyPrint(std::ostream &out=std::cout) const;
00114
00120 void printOn(std::ostream &out) const;
00121
00127 void readFrom(std::istream &in=std::cin);
00128
00134 virtual void serialize(std::ostream &out) const;
00135
00141 virtual void unserialize(std::istream &in);
00142
00144 virtual void destroy();
00145
00151 static Vector<T> *alloc(size_t size);
00152
00157 static std::string GetClassName()
00158 {
00159 std::string name = ObjectGetClassName<Vector<T> >();
00160 if (name == "unknown")
00161 return std::string("Vector");
00162
00163 else
00164 return name;
00165 }
00166
00171 std::string getClassName() {return GetClassName();}
00172
00173
00180 ObjectRef range(size_t startInd, size_t endInd)
00181 {
00182 Vector<T> *v = Vector<T>::alloc(endInd-startInd+1);
00183 if (endInd >= v->size() || startInd < 0)
00184 throw new GeneralException("Index out of range in BaseVector::range()", __FILE__, __LINE__);
00185 for (size_t i=startInd;i<=endInd;i++)
00186 {
00187 (*v)[i-startInd]=(*this)[i];
00188 }
00189 return ObjectRef(v);
00190 }
00191
00197 virtual ObjectRef getIndex(int pos);
00198
00204 virtual void setIndex(int pos, ObjectRef val);
00205
00206
00210 virtual ObjectRef clone();
00211 };
00212
00213
00215 template <class T>
00216 inline ObjectRef Vector<T>::clone() {
00217 Vector<T> *cpy = Vector<T>::alloc(this->size());
00218
00219 for (unsigned int i = 0; i < this->size(); i++) {
00220 (*cpy)[i] = (*this)[i];
00221 }
00222 return ObjectRef(cpy);
00223 }
00224
00225
00227 template <>
00228 inline ObjectRef Vector<ObjectRef>::clone() {
00229
00230 Vector<ObjectRef> *cpy = new Vector<ObjectRef>(this->size());
00231
00232 for (unsigned int i = 0; i < this->size(); i++) {
00233
00234 (*cpy)[i] = (*this)[i]->clone();
00235 }
00236
00237 return ObjectRef(cpy);
00238 }
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249 template <class T>
00250 inline void _vector_printOn(const Vector<T> &v, std::ostream &out)
00251 {
00252 out << "<" << v.className();
00253 for (size_t i=0; i < v.size(); i++)
00254 {
00255 out << " " << v[i];
00256 }
00257 out << " > ";
00258 }
00259
00260 template <>
00261 inline void _vector_printOn(const Vector<std::string> &v, std::ostream &out)
00262 {
00263 out << "<Vector<string>";
00264 for (unsigned int n=0; n < v.size(); n++)
00265 {
00266 out << " ";
00267 const std::string &str = v[n];
00268 for (unsigned int i=0;i<str.size();i++)
00269 {
00270 if (str[i] == '>')
00271 {
00272 out.put('\\');
00273 out.put('>');
00274 } else if (str[i] == ' ')
00275 {
00276 out.put('\\');
00277 out.put(' ');
00278 } else if (str[i] == '\\')
00279 {
00280 out.put('\\');
00281 out.put('\\');
00282 } else
00283 out.put(str[i]);
00284 }
00285 }
00286 out << "> ";
00287 }
00288
00289 template <class T>
00290 inline void _vector_printOn(const Vector<T*> &v, std::ostream &out)
00291 {
00292 out << "<" << v.className();
00293 for (size_t i=0; i < v.size(); i++)
00294 {
00295 out << " " << *(v[i]);
00296 }
00297 out << " > ";
00298 }
00299
00300 template <class T>
00301 void Vector<T>::printOn(std::ostream &out) const
00302 {
00303 _vector_printOn(*this, out);
00304 }
00305
00306 template <class T>
00307 void Vector<T>::prettyPrint(std::ostream &out) const
00308 {
00309 _vector_printOn(*this,out);
00310 }
00311
00312 template <class T>
00313 inline void _vector_readFrom(Vector<T> &v, std::istream &in)
00314 {
00315 bool a=false;
00316 v.resize(0);
00317 while (1)
00318 {
00319 char ch=' ';
00320 while (ch == ' ')
00321 {
00322 in >> ch;
00323 if (ch == '>')
00324 {
00325 return;
00326 } else if (ch != ' ') {
00327 in.putback(ch);
00328 }
00329 if (in.fail())
00330 {
00331
00332 a=true;
00333 break;
00334 }
00335 }
00336 T tmp;
00337 in >> tmp;
00338 if (in.fail())
00339 {
00340 throw new GeneralException("Error reading Vector", __FILE__, __LINE__);
00341 }
00342 v.push_back(tmp);
00343 }
00344 if (a)
00345 throw new GeneralException("Error reading Vector: '>' expected", __FILE__, __LINE__);
00346 }
00347
00348
00349 template <>
00350 inline void _vector_readFrom(Vector<std::string> &v, std::istream &in)
00351 {
00352 bool done=false;
00353 while (1)
00354 {
00355 std::string tmp;
00356 int i=0;
00357 while(1)
00358 {
00359 char ch;
00360 in.get(ch);
00361 if (in.eof() || in.fail())
00362 throw new GeneralException("Error reading String: '>' or '}' expected", __FILE__, __LINE__);
00363 if (ch == '\\')
00364 {
00365 in.get(ch);
00366 tmp += ch;
00367 }
00368 else if (ch == ' ')
00369 {
00370 if (i)
00371 {
00372 break;
00373 }
00374 else
00375 continue;
00376 }
00377 else if (ch == '>')
00378 {
00379 done=true;
00380 break;
00381 }
00382 else if (ch == '}')
00383 {
00384 break;
00385 }
00386 else
00387 {
00388 tmp += ch;
00389 }
00390 i++;
00391 }
00392
00393 if (tmp != "")
00394 v.push_back(tmp);
00395 if (done)
00396 break;
00397 }
00398 }
00399
00400 template <class T>
00401 inline void _vector_readFrom(Vector<T*> &v, std::istream &in)
00402 {
00403 v.resize(0);
00404 while (1)
00405 {
00406 char ch=' ';
00407 while (ch == ' ')
00408 {
00409 in >> ch;
00410 if (ch == '>')
00411 {
00412 return;
00413 } else if (ch != ' ') {
00414 in.putback(ch);
00415 }
00416 if (in.fail())
00417 throw new GeneralException("Error reading Vector: '>' expected", __FILE__, __LINE__);
00418 }
00419 T *tmp = new T;
00420 in >> *tmp;
00421 if (in.fail())
00422 throw new GeneralException("Error reading Vector", __FILE__, __LINE__);
00423 v.push_back(tmp);
00424 }
00425 }
00426
00427
00428 template <class T>
00429 inline void Vector<T>::readFrom(std::istream &in)
00430 {
00431 _vector_readFrom(*this, in);
00432 }
00433
00434
00435 template<class T, int I>
00436 struct VecMethod {
00437 static inline void serialize(const Vector<T> &v, std::ostream &out)
00438 {
00439 throw new GeneralException("VecMethod default serialize should never be called", __FILE__, __LINE__);
00440 }
00441 static inline void unserialize(Vector<T> &v, std::istream &in)
00442 {
00443 throw new GeneralException("VecMethod default unserialize should never be called", __FILE__, __LINE__);
00444 }
00445 static inline ObjectRef getIndex(Vector<T> &v, int pos)
00446 {
00447 throw new GeneralException("VecMethod getIndex should never be called", __FILE__, __LINE__);
00448 }
00449 static inline void setIndex(Vector<T> &v, int pos, ObjectRef val)
00450 {
00451 throw new GeneralException("VecMethod setIndex should never be called", __FILE__, __LINE__);
00452 }
00453 };
00454
00455 template<class T>
00456 struct VecMethod<T,TTraits::Object> {
00457 static inline void serialize(const Vector<T> &v, std::ostream &out)
00458 {
00459 out << "{" << v.className() << std::endl;
00460 out << "|";
00461 int tmp=v.size();
00462 BinIO::write(out, &tmp, 1);
00463 for (size_t i=0;i<v.size();i++)
00464 {
00465 v[i].serialize(out);
00466 }
00467 out << "}";
00468 }
00469 static inline void unserialize(Vector<T> &v, std::istream &in)
00470 {
00471 int tmp;
00472 std::string expected = Vector<T>::GetClassName();
00473 BinIO::read(in, &tmp, 1);
00474 v.resize(tmp);
00475 for (size_t i=0;i<v.size();i++)
00476 {
00477 if (!isValidType(in, expected))
00478 throw new ParsingException("Expected type " + expected);
00479 v[i].unserialize(in);
00480 }
00481 char ch;
00482 in >> ch;
00483 }
00484
00485
00486 static inline ObjectRef getIndex(Vector<T> &v, int pos)
00487 {
00488 if (pos < 0 || pos >= v.size()) {
00489 throw new GeneralException("Vector getIndex : index out of bound",__FILE__,__LINE__);
00490 }
00491 return ObjectRef(v[pos].clone());
00492 }
00493
00494 static inline void setIndex(Vector<T> &v, int pos, ObjectRef val)
00495 {
00496 if (pos < 0 || pos >= v.size()) {
00497 throw new GeneralException("Vector getIndex : index out of bound",__FILE__,__LINE__);
00498 }
00499 RCPtr<T> obj = val;
00500 v[pos] = *obj;
00501 }
00502 };
00503
00504 template<class T>
00505 struct VecMethod<T,TTraits::ObjectPointer> {
00506 static inline void serialize(const Vector<T> &v, std::ostream &out)
00507 {
00508 out << "{" << v.className() << std::endl;
00509 out << "|";
00510 int tmp=v.size();
00511 BinIO::write(out, &tmp, 1);
00512 for (size_t i=0;i<v.size();i++)
00513 {
00514 v[i]->serialize(out);
00515 }
00516 out << "}";
00517 }
00518 static inline void unserialize(Vector<T> &v, std::istream &in)
00519 {
00520 int tmp;
00521 BinIO::read(in, &tmp, 1);
00522 v.resize(tmp);
00523 for (size_t i=0;i<v.size();i++)
00524 {
00525 in >> v[i];
00526 }
00527 char ch;
00528 in >> ch;
00529 }
00530
00531 static inline ObjectRef getIndex(Vector<T> &v, unsigned int pos)
00532 {
00533 if (pos >= v.size()) {
00534 throw new GeneralException("Vector getIndex : index out of bound",__FILE__,__LINE__);
00535 }
00536 return v[pos];
00537 }
00538
00539 static inline void setIndex(Vector<T> &v, unsigned int pos, ObjectRef val)
00540 {
00541 if (pos >= v.size()) {
00542 throw new GeneralException("Vector getIndex : index out of bound",__FILE__,__LINE__);
00543 }
00544 v[pos] = val;
00545 }
00546 };
00547
00548
00549 template<class T>
00550 struct VecMethod<T,TTraits::Basic> {
00551 static inline void serialize(const Vector<T> &v, std::ostream &out)
00552 {
00553 out << "{" << v.className() << std::endl;
00554 out << "|";
00555 int tmp=v.size();
00556 BinIO::write(out, &tmp, 1);
00557 BinIO::write(out, &v[0], v.size());
00558 out << "}";
00559 }
00560 static inline void unserialize(Vector<T> &v, std::istream &in)
00561 {
00562 int tmp;
00563 BinIO::read(in, &tmp, 1);
00564 v.resize(tmp);
00565 BinIO::read(in, &v[0], v.size());
00566 char ch;
00567 in >> ch;
00568 }
00569
00570 static inline ObjectRef getIndex(Vector<T> &v, unsigned int pos)
00571 {
00572 if (pos >= v.size()) {
00573 throw new GeneralException("Vector getIndex : index out of bound",__FILE__,__LINE__);
00574 }
00575 return ObjectRef( NetCType<T>::alloc(v[pos]));
00576 }
00577
00578 static inline void setIndex(Vector<T> &v, unsigned int pos, ObjectRef val)
00579 {
00580 if (pos >= v.size()) {
00581 throw new GeneralException("Vector getIndex : index out of bound",__FILE__,__LINE__);
00582 }
00583 RCPtr<NetCType<T> > obj = val;
00584 v[pos] = *obj;
00585 }
00586 };
00587
00588 template<class T>
00589 struct VecMethod<T,TTraits::Unknown> {
00590 static inline void serialize(const Vector<T> &v, std::ostream &out)
00591 {
00592 throw new GeneralException(std::string("Sorry, can't serialize this kind of object (") + typeid(T).name()
00593 + ")", __FILE__, __LINE__);
00594 }
00595 static inline void unserialize(Vector<T> &v, std::istream &in)
00596 {
00597 throw new GeneralException(std::string("Sorry, can't unserialize this kind of object (") + typeid(T).name()
00598 + ")", __FILE__, __LINE__);
00599 }
00600
00601 static inline ObjectRef getIndex(Vector<T> &v, int pos)
00602 {
00603 throw new GeneralException(std::string("Sorry, can't getIndex for this type of vector (") + typeid(T).name()
00604 + ")", __FILE__, __LINE__);
00605 }
00606
00607 static inline void setIndex(Vector<T> &v, int pos, ObjectRef val)
00608 {
00609 throw new GeneralException(std::string("Sorry, can't getIndex for this type of vector (") + typeid(T).name()
00610 + ")", __FILE__, __LINE__);
00611 }
00612 };
00613
00614 template <class T>
00615 inline void Vector<T>::serialize(std::ostream &out) const
00616 {
00617 VecMethod<T, TypeTraits<T>::kind>::serialize(*this, out);
00618 }
00619
00620 template <class T>
00621 inline void Vector<T>::unserialize(std::istream &in)
00622 {
00623 VecMethod<T, TypeTraits<T>::kind>::unserialize(*this, in);
00624 }
00625
00626 template <class T>
00627 inline ObjectRef Vector<T>::getIndex(int pos)
00628 {
00629 return VecMethod<T, TypeTraits<T>::kind>::getIndex(*this,pos);
00630 }
00631
00632 template <class T>
00633 inline void Vector<T>::setIndex(int pos, ObjectRef val)
00634 {
00635 VecMethod<T, TypeTraits<T>::kind>::setIndex(*this,pos,val);
00636 }
00637
00638
00639 template <class T>
00640 inline void Vector<T>::destroy()
00641 {
00642 delete this;
00643 }
00644
00645
00646 extern VectorPool<float> floatVectorPool;
00647 extern VectorPool<double> doubleVectorPool;
00648
00649 template <>
00650 inline void Vector<float>::destroy()
00651 {
00652 floatVectorPool.release(this);
00653 }
00654
00655 template <>
00656 inline void Vector<double>::destroy()
00657 {
00658 doubleVectorPool.release(this);
00659 }
00660
00661 template <class T>
00662 inline Vector<T> *Vector<T>::alloc(size_t size)
00663 {
00664 return new Vector<T> (size);
00665 }
00666
00667
00668 template <>
00669 inline Vector<float> *Vector<float>::alloc(size_t size)
00670 {
00671 return floatVectorPool.newVector(size);
00672 }
00673
00674 template <>
00675 inline Vector<double> *Vector<double>::alloc(size_t size)
00676 {
00677 return doubleVectorPool.newVector(size);
00678 }
00679
00680 inline bool isValidVectorType (std::istream &in, std::string type, const std::string &className)
00681 {
00682 if (type != "Vector" && type != className)
00683 return false;
00684 return true;
00685 }
00686
00687
00688 template<class T>
00689 std::istream &operator >> (std::istream &in, Vector<T> &vec)
00690 {
00691 char ch;
00692 in >> ch;
00693
00694 std::string expected = ObjectGetClassName<Vector<T> >();
00695
00696 if (ch == '<')
00697 {
00698 std::string type;
00699 in >> type;
00700 if (!isValidVectorType(in, type, expected))
00701 throw new ParsingException ("Parser expected type " + expected + " and got " + type);
00702 vec.readFrom(in);
00703 } else if (ch == '{')
00704 {
00705 std::string type;
00706 in >> type;
00707 if (!isValidVectorType(in, type, expected))
00708 throw new ParsingException ("Parser expected type " + expected + " and got " + type);
00709 char dummy;
00710 do {
00711 in >> dummy;
00712 } while(dummy != '|');
00713 vec.unserialize(in);
00714 } else {
00715 throw new ParsingException ("Parser expected < or { while parsing type " + expected);
00716 }
00717 return in;
00718 }
00719
00720
00721
00722
00723
00724
00725 template<class T>
00726 inline Vector<T> &operator+= (Vector<T> &v1, const Vector<T> &v2)
00727 {
00728 if (v1.size() != v2.size())
00729 throw new GeneralException("Vector size mismatch", __FILE__, __LINE__);
00730 for (size_t i=0;i<v1.size();i++)
00731 v1[i] += v2[i];
00732 return v1;
00733 }
00734
00735 template<class T>
00736 inline Vector<T> &operator-= (Vector<T> &v1, const Vector<T> &v2)
00737 {
00738 if (v1.size() != v2.size())
00739 throw new GeneralException("Vector size mismatch", __FILE__, __LINE__);
00740 for (size_t i=0;i<v1.size();i++)
00741 v1[i] -= v2[i];
00742 return v1;
00743 }
00744
00745 template<class T>
00746 inline Vector<T> &operator*= (Vector<T> &v1, const Vector<T> &v2)
00747 {
00748 if (v1.size() != v2.size())
00749 throw new GeneralException("Vector size mismatch", __FILE__, __LINE__);
00750 for (size_t i=0;i<v1.size();i++)
00751 v1[i] *= v2[i];
00752 return v1;
00753 }
00754
00755 template<class T>
00756 inline Vector<T> &operator/= (Vector<T> &v1, const Vector<T> &v2)
00757 {
00758 if (v1.size() != v2.size())
00759 throw new GeneralException("Vector size mismatch", __FILE__, __LINE__);
00760 for (size_t i=0;i<v1.size();i++)
00761 v1[i] /= v2[i];
00762 return v1;
00763 }
00764
00765
00766 template<class T>
00767 inline Vector<T> operator+ (const Vector<T> &v1, const Vector<T> &v2)
00768 {
00769 Vector<T> v(v1);
00770 v += v2;
00771 return v;
00772 }
00773
00774 template<class T>
00775 inline Vector<T> operator- (const Vector<T> &v1, const Vector<T> &v2)
00776 {
00777 Vector<T> v(v1);
00778 v -= v2;
00779 return v;
00780 }
00781
00782 template<class T>
00783 inline Vector<T> operator* (const Vector<T> &v1, const Vector<T> &v2)
00784 {
00785 Vector<T> v(v1);
00786 v *= v2;
00787 return v;
00788 }
00789
00790 template<class T>
00791 inline Vector<T> operator/ (const Vector<T> &v1, const Vector<T> &v2)
00792 {
00793 Vector<T> v(v1);
00794 v /= v2;
00795 return v;
00796 }
00797
00798 template<class T>
00799 inline Vector<T> operator- (Vector<T> &v1)
00800 {
00801 Vector<T> v(v1.size());
00802 for (size_t i=0;i<v1.size();i++)
00803 v[i] = -v1[i];
00804 return v;
00805 }
00806
00807
00808 template<class T>
00809 inline Vector<T> &operator+= (Vector<T> &v1, T scal)
00810 {
00811 for (size_t i=0;i<v1.size();i++)
00812 v1[i] += scal;
00813 return v1;
00814 }
00815
00816 template<class T>
00817 inline Vector<T> &operator-= (Vector<T> &v1, T scal)
00818 {
00819 for (size_t i=0;i<v1.size();i++)
00820 v1[i] -= scal;
00821 return v1;
00822 }
00823
00824 template<class T>
00825 inline Vector<T> &operator*= (Vector<T> &v1, T scal)
00826 {
00827 for (size_t i=0;i<v1.size();i++)
00828 v1[i] *= scal;
00829 return v1;
00830 }
00831
00832 template<class T>
00833 inline Vector<T> &operator/= (Vector<T> &v1, T scal)
00834 {
00835 for (size_t i=0;i<v1.size();i++)
00836 v1[i] /= scal;
00837 return v1;
00838 }
00839
00840
00841 template<class T>
00842 inline Vector<T> operator+ (const Vector<T> &v1, T scal)
00843 {
00844 Vector<T> v(v1);
00845 v += scal;
00846 return v;
00847 }
00848
00849 template<class T>
00850 inline Vector<T> operator- (const Vector<T> &v1, T scal)
00851 {
00852 Vector<T> v(v1);
00853 v -= scal;
00854 return v;
00855 }
00856
00857 template<class T>
00858 inline Vector<T> operator* (const Vector<T> &v1, T scal)
00859 {
00860 Vector<T> v(v1);
00861 v *= scal;
00862 return v;
00863 }
00864
00865 template<class T>
00866 inline Vector<T> operator/ (const Vector<T> &v1, T scal)
00867 {
00868 Vector<T> v(v1);
00869 v /= scal;
00870 return v;
00871 }
00872
00873
00874 }
00875
00876
00877 #endif