openModeller  Version 1.5.0
Sample.hh
Go to the documentation of this file.
1 
11 #ifndef _SAMPLE_HH_
12 #define _SAMPLE_HH_
13 
14 #include <openmodeller/om_defs.hh>
16 #include <cstddef>
17 #include <iostream>
18 #include <vector>
19 //Needed for using free in template function with gcc4.3.2
20 #include <stdlib.h>
21 
22 // Decl of SExp which is defined in SampleExpr.hh
23 template< typename T > class SExpr;
24 
25 class dllexp Sample
26 {
27 
28 public:
29  // Construct an empty one.
30  Sample();
31 
32  // Construct one of this size initialized with zeros
33  explicit Sample( std::size_t size );
34 
35  // Construct one with the same value in all elements
36  Sample ( std::size_t size, Scalar value );
37 
38  // Construct one with these values.
39  // Copies the contents of the values array.
40  Sample( std::size_t size, Scalar const * values );
41 
42  // Construct one with these values from a std::vector<Scalar>
43  Sample( std::vector<Scalar> );
44 
45 
46  // Copy one.
47  // Currently implemented slowly by doing an
48  // allocation and deep copy.
49  Sample( const Sample & rhs );
50 
51  ~Sample();
52 
53  // Assignment operator.
54  Sample& operator=( const Sample & rhs );
55 
56  // Assignment from an Expression Template.
57  // Evalute the SExpr and assign to this.
58  template< typename T > inline
59  Sample( const SExpr<T>& rhs );
60 
61  template< typename T > inline
62  Sample& operator=( const SExpr<T>& rhs );
63 
64  // Redimensions this.
65  // Slow operation since it does a memcpy.
66  // Any new elements are initialized to 0.
67  void resize( std::size_t size );
68 
69  // First iteration mechanism works with indexing.
70  inline std::size_t size() const { return size_; }
71 
72  // Set the index of the first attribute related to a continuous variable.
73  // When a Sample contains attributes from both categorical and continuous
74  // variables, categorical attributes always come first and their values
75  // should not be changed in most operations.
76  void setCategoricalThreshold( std::size_t index );
77 
78  // Return an lvalue.
79  Scalar& operator[]( std::size_t index );
80 
81  // Return an rvalue from a const reference.
82  Scalar operator[]( std::size_t index ) const;
83 
84  // Second iteration mechanism works with pointers.
85  // Looks like a real iterator;
86  typedef Scalar* iterator;
87  inline iterator begin() { return value_; }
88  inline iterator end() { return value_ + size_; }
89 
90  typedef Scalar const * const_iterator;
91  inline const_iterator begin() const { return value_; }
92  inline const_iterator end() const { return value_ + size_; }
93 
94  // define an equality check
95  bool equals( const Sample& ) const;
96 
97  // dump values
98  void dump() const;
99 
100  // define some mutating operators for convience
101 
102  // pointwise addition
103  Sample& operator+= ( const Sample& );
104  Sample& operator+= ( const Scalar& );
105 
106  // pointwise subtraction
107  Sample& operator-= ( const Sample& );
108  Sample& operator-= ( const Scalar& );
109 
110  // pointwise multiplication
111  Sample& operator*= ( const Sample& );
112  Sample& operator*= ( const Scalar& );
113 
114  // pointwise division
115  Sample& operator/= ( const Sample& );
116  Sample& operator/= ( const Scalar& );
117 
118  // pointwise minimum - this may not seem like a natural
119  // overload but "and" in lattices means minimum.
120  Sample& operator&= ( const Sample& );
121 
122  // pointwise maximum
123  Sample& operator|= ( const Sample& );
124 
125  // Take the square of this and return this
126  Sample& sqr();
127 
128  // Take the root of this and return this
129  Sample& sqrt();
130 
131  // Compute the vector "norm" = sqrt ( sum squares )
132  Scalar norm() const;
133 
134  // Compute the vector dot product with another vector
135  Scalar dotProduct( const Sample& rhs ) const;
136 
137 private:
138 
139  std::size_t size_;
141 
142  std::size_t start_; // index of the first attribute of a continuous variable
143 
144  void alloc( std::size_t size );
145 
146  void copy( std::size_t size, Scalar const * values );
147 
148 };
149 
150 //
151 // IO operator decls
152 //
153 dllexp std::ostream&
154 operator<<(std::ostream&,const Sample&);
155 
156 dllexp std::istream&
157 operator>>(std::istream&, Sample&);
158 
159 
160 // Definitions of inlined functions
161 dllexp bool inline
162 operator==( const Sample& lhs, const Sample& rhs )
163 {
164  return lhs.equals( rhs );
165 }
166 
167 dllexp bool inline
168 operator!=( const Sample& lhs, const Sample& rhs )
169 {
170  return !lhs.equals( rhs );
171 }
172 
173 template< typename T >
174 inline
175 Sample::Sample( const SExpr<T>& rhs ) :
176  size_(0),
177  value_(0)
178 {
179  operator=(rhs);
180 }
181 
182 template< typename T >
183 inline Sample&
185 {
186  if ( this->size_ != rhs.size() ) {
187  if ( value_ ) {
188  free( value_ );
189  }
190  alloc( rhs.size() );
191  }
192  rhs.reset();
193  iterator meIter = begin();
194  while ( meIter != end() ) {
195  *meIter = *rhs;
196  ++meIter;
197  ++rhs;
198  }
199 
200  return *this;
201 
202 }
203 
204 #endif
dllexp std::istream & operator>>(std::istream &, Sample &)
const_iterator begin() const
Definition: Sample.hh:91
iterator end()
Definition: Sample.hh:88
double Scalar
Type of map values.
Definition: om_defs.hh:39
bool equals(const Sample &) const
Definition: Sample.cpp:249
iterator begin()
Definition: Sample.hh:87
std::size_t size_
Definition: Sample.hh:139
dllexp bool operator==(const Sample &lhs, const Sample &rhs)
Definition: Sample.hh:162
dllexp bool operator!=(const Sample &lhs, const Sample &rhs)
Definition: Sample.hh:168
void alloc(std::size_t size)
Definition: Sample.cpp:199
Definition: Sample.hh:23
Scalar * value_
Definition: Sample.hh:140
const_iterator end() const
Definition: Sample.hh:92
Scalar const * const_iterator
Definition: Sample.hh:90
Scalar * iterator
Definition: Sample.hh:86
std::size_t size() const
Definition: Sample.hh:70
std::size_t start_
Definition: Sample.hh:142
Sample & operator=(const Sample &rhs)
Definition: Sample.cpp:132
Sample()
Definition: Sample.cpp:38
dllexp std::ostream & operator<<(std::ostream &, const Sample &)
Definition: Sample.hh:25