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
00047
#ifndef Array_decl_INC
00048
#define Array_decl_INC
00049
00050
00051
00052
#include <string>
00053
#include <vector>
00054
#include "TypeTraits.h"
00055
00056
00057
#include <plearn/math/TMat_decl.h>
00058
00059
#include "ms_hash_wrapper.h"
00060 namespace PLearn {
00061
using namespace std;
00062
00063
00064
template <
class T>
00065 class Array:
public TVec<T>
00066 {
00067
public:
00068
00069
00070
00071
00072
using TVec<T>::length_;
00073
using TVec<T>::offset_;
00074
using TVec<T>::storage;
00075
00076
using TVec<T>::data;
00077
using TVec<T>::resize;
00078
00079 typedef T*
iterator;
00080
00081
explicit Array<T>(
int the_size=0,
int extra_space = 10)
00082 :
TVec<T>(the_size+extra_space)
00083 { length_ = the_size; }
00084
00085
Array<T>(
const T& elem1)
00086 :
TVec<T>(1)
00087 { (*this)[0] = elem1; }
00088
00089 Array<T>(
const T& elem1,
const T& elem2)
00090 : TVec<T>(2)
00091 {
00092 (*this)[0] = elem1;
00093 (*this)[1] = elem2;
00094 }
00095
00096 Array<T>(
const Array<T>& other)
00097 : TVec<T>(other.length())
00098 {
00099
length_ = other.size();
00100
offset_ = other.offset();
00101
iterator array =
data();
00102
for(
int i=0; i<
length_; i++)
00103 array[i] = other[i];
00104 }
00105
00106 Array<T>(
const TVec<T>& other)
00107 : TVec<T>(other.copy())
00108 {}
00109
00110 Array<T>(
const vector<T> &other)
00111 : TVec<T>(other.size())
00112 {
00113
iterator array =
data();
00114
for (
int i = 0; i <
length_; ++i)
00115 array[i] = other[i];
00116 }
00117
00119 operator bool()
const
00120
{
return length_>0; }
00121
00123 bool operator!()
const
00124
{
return length_==0; }
00125
00126 Array<T> subArray(
int start,
int len)
00127 {
00128
if (start+len>length_)
00129
PLERROR(
"Array::subArray start(%d)+len(%d)>size(%d)", start,len,length_);
00130
Array<T> newarray(len);
00131 iterator new_ar = newarray.
data();
00132 iterator array =
data();
00133
for (
int i=0;i<len;i++)
00134 new_ar[i] = array[start+i];
00135
return newarray;
00136 }
00137
00138 void clear()
00139 { length_ = 0; }
00140
00141 void operator=(
const Array<T>& other)
00142 {
00143
resize(other.
size());
00144 iterator array =
data();
00145
for(
int i=0; i<length_; i++)
00146 array[i] = other[i];
00147 }
00148
00149 void operator=(
const TVec<T>& other)
00150 {
00151
resize(other.
size());
00152 iterator array =
data();
00153
for(
int i=0; i<length_; i++)
00154 array[i] = other[i];
00155 }
00156
00158 void view(
const TVec<T>& other)
00159 {
00160
TVec<T>::operator=(other);
00161 }
00162
00163 bool operator==(
const Array<T>& other)
const
00164
{
00165
#ifdef BOUNDCHECK
00166
if (this->
size()!=other.
size())
00167
PLERROR(
"Array::operator== works on same-size arguments");
00168
#endif
00169
iterator array =
data();
00170
for(
int i=0; i<length_; i++)
00171
if (array[i] != other[i])
return false;
00172
return true;
00173 }
00174
00175 bool operator<(const Array<T>& other)
const
00176 {
00177
#ifdef BOUNDCHECK
00178
if (this->
size()!=other.size())
00179
PLERROR(
"Array::operator< works on same-size arguments");
00180
#endif
00181
iterator array =
data();
00182
for(
int i=0; i<length_; i++)
00183 {
00184
if (array[i] < other[i])
return true;
00185
else if (array[i] > other[i])
return false;
00186 }
00187
return false;
00188 }
00189
00190 bool operator<=(const Array<T>& other)
const
00191 {
00192
#ifdef BOUNDCHECK
00193
if (this->
size()!=other.size())
00194
PLERROR(
"Array::operator< works on same-size arguments");
00195
#endif
00196
iterator array =
data();
00197
for(
int i=0; i<length_; i++)
00198 {
00199
if (array[i] < other[i])
return true;
00200
else if (array[i] > other[i])
return false;
00201 }
00202
return true;
00203 }
00204
00205
00206 bool operator>(
const Array<T>& other)
const
00207
{
00208
#ifdef BOUNDCHECK
00209
if (this->
size()!=other.
size())
00210
PLERROR(
"Array::operator< works on same-size arguments");
00211
#endif
00212
iterator array =
data();
00213
for(
int i=0; i<length_; i++)
00214 {
00215
if (array[i] > other[i])
return true;
00216
else if (array[i] < other[i])
return false;
00217 }
00218
return false;
00219 }
00220
00221 bool operator>=(
const Array<T>& other)
const
00222
{
00223
#ifdef BOUNDCHECK
00224
if (this->
size()!=other.
size())
00225
PLERROR(
"Array::operator< works on same-size arguments");
00226
#endif
00227
iterator array =
data();
00228
for(
int i=0; i<length_; i++)
00229 {
00230
if (array[i] > other[i])
return true;
00231
else if (array[i] < other[i])
return false;
00232 }
00233
return true;
00234 }
00235
00236 void operator=(
const vector<T> &other)
00237 {
00238
resize(other.size());
00239 iterator array =
data();
00240
for(
int i = 0; i < length_; ++i)
00241 array[i] = other[i];
00242 }
00243
00244 void print(ostream& out)
const
00245
{
00246 iterator array =
data();
00247
for(
int i=0; i<length_; i++)
00248 out << array[i] <<
endl;
00249 }
00250
00251 int findFirstOccurence(
const T& elem)
00252 {
00253
for(
int i=0;i<this->array_size;i++)
00254
if(elem==this->array[i])
00255
return i;
00256
return -1;
00257 }
00258
00261
void makeDeepCopyFromShallowCopy(map<const void*, void*>& copies);
00262
00263
00264
00265 void write(ostream &out_)
const
00266
{
00267
PStream out(&out_);
00268 out << *
this;
00269 }
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281 void read(istream &in_)
00282 {
00283
PStream in(&in_);
00284 in >> *
this;
00285 }
00286
00288 inline operator char*()
const {
if(this->
isNull())
return 0;
else return (
char*)
data(); }
00289
00290
00291
00292
00293 inline size_t
byteLength()
const {
return this->
size()*
sizeof(T); }
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307 };
00308
00309
template<
class T>
00310 class TypeTraits< Array<T> >
00311 {
00312
public:
00313 static inline string name()
00314 {
return string(
"Array< ") +
TypeTraits<T>::name()+
" >"; }
00315
00316 static inline unsigned char little_endian_typecode()
00317 {
return 0xFF; }
00318
00319 static inline unsigned char big_endian_typecode()
00320 {
return 0xFF; }
00321
00322 };
00323
00324
00325
template <
class T>
00326 class Array2ArrayMap :
public PPointable
00327 {
00328
public:
00329 multimap<Array<T>,
Array<T> >
map;
00330 };
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344 }
00345
00346
00347
00348 SET_HASH_FUNCTION(
PLearn::Array<T>, T, a, PLearn::hashbytes((
char*)a.data(),a.size()*
sizeof(T)) )
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
#endif