00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00048
#ifndef TVec_impl_INC
00049
#define TVec_impl_INC
00050
00051
#include "TVec_decl.h"
00052
#include <plearn/io/pl_io.h>
00053
00054
namespace PLearn {
00055
using namespace std;
00056
00057
00058
00059
00060
00061
00062
00063
template<
class T>
00064 void TVec<T>::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies)
00065 {
00066
deepCopyField(
storage, copies);
00067 }
00068
00069
template<
class T>
00070 TVec<T> TVec<T>::deepCopy(map<const void*, void*>& copies)
const
00071
{
00072
00073
TVec<T> deep_copy = *
this;
00074
00075 deep_copy.
makeDeepCopyFromShallowCopy(copies);
00076
00077
return deep_copy;
00078 }
00079
00080
template <
class T>
00081 inline TVec<T> deepCopy(
const TVec<T>& source)
00082 {
00083
CopiesMap copies;
00084
return deepCopy(source, copies);
00085 }
00086
00087
template <
class T>
00088 inline TVec<T> deepCopy(
const TVec<T>& source, CopiesMap& copies)
00089 {
00090
return source.
deepCopy(copies);
00091 }
00092
00093
template <
class T>
00094 inline void deepCopyField(
TVec<T>& field, CopiesMap& copies)
00095 {
00096 field.
makeDeepCopyFromShallowCopy(copies);
00097 }
00098
00099
00100
template<
class T>
00101 void swap(
TVec<T>& a,
TVec<T>& b)
00102 { swap_ranges(a.
begin(), a.
end(), b.
begin()); }
00103
00105
template<
class T>
00106 inline void operator<<(const TVec<T>& m1,
const TVec<T>& m2)
00107 {
00108
#ifdef BOUNDCHECK
00109
if(m1.size()!=m2.size())
00110
PLERROR(
"In operator<<(m1,m2) the 2 matrices must have the same number of elements (%d != %d)", m1.size(), m2.size());
00111
#endif
00112
copy(m2.begin(), m2.end(), m1.begin());
00113 }
00114
00116
template<
class T,
class U>
00117 void operator<<(const TVec<T>& m1,
const TVec<U>& m2)
00118 {
00119
#ifdef BOUNDCHECK
00120
if(m1.size()!=m2.size())
00121
PLERROR(
"In operator<<(m1,m2) the 2 matrices must have the same number of elements (%d != %d)", m1.size(), m2.size());
00122
#endif
00123
copy_cast(m2.begin(), m2.end(), m1.begin());
00124 }
00125
00127
template<
class T,
class U>
00128 inline void operator>>(
const TVec<T>& m1,
const TVec<U>& m2)
00129 { m2 << m1; }
00130
00131
00132
template<
class T>
00133 void savePVec(
const string& filename,
const TVec<T>& vec)
00134 {
PLERROR(
"savePVec only implemented for float and double"); }
00135
00136
template<
class T>
00137
void loadPVec(
const string& filename, TVec<float>& vec)
00138 {
PLERROR(
"loadPVec only implemented for float and double"); }
00139
00140
00144
00145
template <
class T>
inline PStream &
00146 operator<<(PStream &out, const TVec<T> &v)
00147 {
00148 v.write(out);
00149
return out;
00150 }
00151
00152
template <
class T>
00153 PStream &
operator>>(
PStream &in,
TVec<T> &v)
00154 {
00155 v.
read(in);
00156
return in;
00157 }
00158
00159
00160
template<
class T>
00161 void binwrite(ostream& out,
const TVec<T>& v)
00162 {
00163
int l = v.
length();
00164
PLearn::binwrite(out,l);
00165
if (l<200000)
00166
PLearn::binwrite(out,v.
data(),l);
00167
else for (
int i=0;i<l;i+=200000)
00168
PLearn::binwrite(out,&v[i],std::min(200000,l-i));
00169 }
00170
00171
template<
class T>
00172 void binread(istream& in,
TVec<T>& v)
00173 {
00174
int l;
00175
PLearn::binread(in,l);
00176 v.
resize(l);
00177
if (l<200000)
00178
PLearn::binread(in,v.
data(),l);
00179
else for (
int i=0;i<l;i+=200000)
00180
PLearn::binread(in,&v[i],std::min(200000,l-i));
00181 }
00182
00183
template<
class T>
00184 void binwrite_double(ostream& out,
const TVec<T>& v)
00185 {
00186
int l = v.
length();
00187
PLearn::binwrite(out,l);
00188
if (l<200000)
00189
PLearn::binwrite_double(out,v.
data(),l);
00190
else for (
int i=0;i<l;i+=200000)
00191
PLearn::binwrite_double(out,&v[i],std::min(200000,l-i));
00192 }
00193
00194
template<
class T>
00195 void binread_double(istream& in,
TVec<T>& v)
00196 {
00197
int l;
00198
PLearn::binread(in,l);
00199 v.
resize(l);
00200
if (l<200000)
00201
PLearn::binread_double(in,v.
data(),l);
00202
else for (
int i=0;i<l;i+=200000)
00203
PLearn::binread_double(in,&v[i],std::min(200000,l-i));
00204 }
00205
00206
00207
template<
class T>
00208 inline ostream& operator<<(ostream& out, const TVec<T>& v)
00209 {
00210 v.print(out);
00211
return out;
00212 }
00213
00214
template<
class T>
00215 inline istream&
operator>>(istream& in,
const TVec<T>& v)
00216 {
00217 v.
input(in);
00218
return in;
00219 }
00220
00221
00227
template<
class T,
class I>
00228
void selectElements(
const TVec<T>& source,
const TVec<I>& indices, TVec<T>& destination);
00229
00231
template<
class T>
00232
void elementsEqualTo(
const TVec<T>& source,
const T& value,
const TVec<T>& destination);
00233
00234
template<
class T>
00235 TVec<T>
concat(
const TVec<T>& v1,
const TVec<T>& v2);
00236
00237
00238
00239
00243
template<
class T>
00244 TVec<T>
removeElement(
const TVec<T>& v,
int elemnum);
00245
00246
00248
template <
class T>
00249 bool operator<=(const TVec<T>&
left,
const TVec<T>&
right)
00250 {
00251
if (
left.size() !=
right.size())
00252
PLERROR(
"Left and right vectors must have the same size in operator<=");
00253
return std::inner_product(
left.begin(),
left.end(),
right.begin(),
00254
true, std::logical_and<bool>(),
00255 std::less_equal<T>());
00256 }
00257
00258
template <
class T>
00259 bool operator>=(
const TVec<T>& left,
const TVec<T>& right)
00260 {
00261
if (
left.size() !=
right.size())
00262
PLERROR(
"Left and right vectors must have the same size in operator>=");
00263
return std::inner_product(
left.begin(),
left.end(),
right.begin(),
00264
true, std::logical_and<bool>(),
00265 std::greater_equal<T>());
00266 }
00267
00268
00269
template <
class T>
00270 bool operator<(const TVec<T>&
left,
const TVec<T>&
right)
00271 {
00272
if (
left.size() !=
right.size())
00273
PLERROR(
"Left and right vectors must have the same size in operator<");
00274
int size =
left.size();
00275
const T* ldata =
left.data();
00276
const T* rdata =
right.data();
00277
for ( ; size ; ++ldata, ++rdata, --size) {
00278
if (*ldata < *rdata)
00279
return true;
00280
if (*ldata > *rdata)
00281
return false;
00282
00283 }
00284
return false;
00285
00286 }
00287
00288
00289
template <
class T>
00290 bool operator>(
const TVec<T>& left,
const TVec<T>& right)
00291 {
00292
if (
left.size() !=
right.size())
00293
PLERROR(
"Left and right vectors must have the same size in operator>");
00294
int size =
left.size();
00295
const T* ldata =
left.data();
00296
const T* rdata =
right.data();
00297
for ( ; size ; ++ldata, ++rdata, --size) {
00298
if (*ldata < *rdata)
00299
return false;
00300
if (*ldata > *rdata)
00301
return true;
00302
00303 }
00304
return false;
00305
00306 }
00307
00308 }
00309
00310
#endif