uLib-0.2
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Vector.h
Go to the documentation of this file.
1 /*//////////////////////////////////////////////////////////////////////////////
2 // CMT Cosmic Muon Tomography project //////////////////////////////////////////
4 
5  Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
6  All rights reserved
7 
8  Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
9 
10  ------------------------------------------------------------------
11  This library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU Lesser General Public
13  License as published by the Free Software Foundation; either
14  version 3.0 of the License, or (at your option) any later version.
15 
16  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  Lesser General Public License for more details.
20 
21  You should have received a copy of the GNU Lesser General Public
22  License along with this library.
23 
25 
26 
27 
28 #ifndef U_CORE_VECTOR_H
29 #define U_CORE_VECTOR_H
30 
31 #include <vector>
32 #include <iostream>
33 
34 #include <Core/StaticInterface.h>
35 #include <Core/SmartPointer.h>
36 #include <Core/CommaInitializer.h>
37 
38 
39 namespace uLib {
40 
41 // Vector Implemetation ... wraps std::vector
42 template <typename T>
43 class Vector : public std::vector<T, std::allocator<T> >
44 {
45  typedef std::vector< T,std::allocator<T> > BaseClass;
46  typedef std::allocator<T> Allocator;
47 public:
48  typedef T TypeData;
49  typedef __gnu_cxx::__normal_iterator<T*, BaseClass > Iterator;
50  typedef __gnu_cxx::__normal_iterator<const T*, BaseClass> ConstIterator;
51 
52  typedef CommaInitializer< Vector<T> , T > VectorCommaInit;
53 
54  Vector(unsigned int size) : BaseClass(size) {}
55  Vector(unsigned int size, T &value) : BaseClass(size,value) {}
56  Vector() : BaseClass(0) {}
57 
58  inline VectorCommaInit operator <<(T scalar) {
59  return VectorCommaInit(this, scalar);
60  }
61 
62  inline void PrintSelf(std::ostream &o);
63 
64  void remove_element(unsigned int index) {
65  std::swap(this->at(index),this->back());
66  this->pop_back();
67  }
68 
69  void remove_element(T &t) {
70  std::swap(t, this->back());
71  this->pop_back();
72  }
73 
74 };
75 
76 template<typename T>
77 void Vector<T>::PrintSelf(std::ostream &o)
78 {
79  o << " *** uLib Vector *** \n";
80  o << " n. of items = " << this->size() << "\n";
81  for(int i=0; i< this->size(); ++i)
82  o << (T)this->at(i) << " ";
83  o << "\n";
84 }
85 
86 template <typename T>
87 std::ostream & operator << (std::ostream &o, const Vector<T> &v) {
88  for(int i=0; i< v.size(); ++i)
89  o << (T)v.at(i) << " ";
90  o << "\n";
91  return o;
92 }
93 
94 template <typename T>
95 std::ofstream & operator << (std::ofstream &o, const Vector<T> &v) {
96  for(int i=0; i< v.size(); ++i)
97  o << (T)v.at(i) << " ";
98  return o;
99 }
100 
101 
102 template < typename T >
103 std::istream & operator >> (std::istream &is, Vector<T> &v) {
104  T value;
105  while( is >> value ) {
106  if(is.fail()) v.push_back(0);
107  else v.push_back( value );
108  }
109  return is;
110 }
111 
112 
113 
114 // Smart pointer Vector Implementation //
115 
116 
117 template <typename T>
118 class SmartVector : public SmartPointer< Vector<T> > {
119  typedef SmartPointer< Vector<T> > Base;
120 public:
121 
122  SmartVector() : Base(new Vector<T>()) { }
123  SmartVector( const SmartVector &copy) : Base(copy) { }
124  SmartVector(unsigned int size) : Base(new Vector<T>((int)size)) { }
125 
126  virtual ~SmartVector() {}
127 
128  T& operator[](int p) {
129  return Base::get()->at(p);
130  }
131 
132  void swap_elements(unsigned int first, unsigned int second) {
133  std::swap(Base::get()->at(first),Base::get()->at(second));
134  }
135 
136  void remove_element(unsigned int index) {
137  std::swap(Base::get()->at(index),Base::get()->back());
138  Base::get()->pop_back();
139  }
140 
141  void remove_element(T &t) {
142  std::swap(t, Base::get()->back());
143  Base::get()->pop_back();
144  }
145 };
146 
147 
148 
149 
150 // ------ Utils ------------------------------------------------------------- //
151 
152 
153 
154 // RIFARE con iteratore !
155 template <typename _Tp, class _CmpT>
156 inline const unsigned long
157 VectorSplice(const _Tp &_it, const _Tp &_end, const float value, _CmpT _comp)
158 {
159  _Tp it = _it;
160  _Tp end = _end-1;
161  for(it; it != end;)
162  {
163  if (_comp(*it,value)) ++it;
164  else if(_comp(*end,value)) std::swap(*it,*end--);
165  else --end;
166  }
167  return it - _it;
168 }
169 
170 
171 
172 } // uLib
173 
174 
175 
176 
177 
178 #endif // VECTOR_H