NCBI C++ ToolKit
njn_vector.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef ALGO_BLAST_GUMBEL_PARAMS__INCLUDED_NJN_VECTOR
2 #define ALGO_BLAST_GUMBEL_PARAMS__INCLUDED_NJN_VECTOR
3 
4 /* $Id: njn_vector.hpp 95054 2021-10-01 16:02:11Z merezhuk $
5 * ===========================================================================
6 *
7 * PUBLIC DOMAIN NOTICE
8 * National Center for Biotechnology Information
9 *
10 * This software/database is a "United States Government Work" under the
11 * terms of the United States Copyright Act. It was written as part of
12 * the author's offical duties as a United States Government employee and
13 * thus cannot be copyrighted. This software/database is freely available
14 * to the public for use. The National Library of Medicine and the U.S.
15 * Government have not placed any restriction on its use or reproduction.
16 *
17 * Although all reasonable efforts have been taken to ensure the accuracy
18 * and reliability of the software and data, the NLM and the U.S.
19 * Government do not and cannot warrant the performance or results that
20 * may be obtained by using this software or data. The NLM and the U.S.
21 * Government disclaim all warranties, express or implied, including
22 * warranties of performance, merchantability or fitness for any particular
23 * purpose.
24 *
25 * Please cite the author in any work or product based on this material.
26 *
27 * ===========================================================================*/
28 
29 /*****************************************************************************
30 
31 File name: njn_vector.hpp
32 
33 Author: John Spouge
34 
35 Contents: Matrix routines
36 
37 ******************************************************************************/
38 
39 #include <corelib/ncbistl.hpp>
40 #include <vector>
41 
42 #include "njn_approx.hpp"
43 
44 
46 BEGIN_SCOPE(blast)
47 
48 BEGIN_SCOPE(Njn)
49 
50  template <typename T>
51  class Vector {
52 
53  public:
54 
55  inline Vector ();
56  inline Vector (const Vector &vector_);
57  inline Vector (const T *begin_, const T *end_); // vector_ [0...m-1]
58  inline Vector (size_t m_, const T &a_); // copies a_ into m_ vector elements
59  virtual inline ~Vector ();
60 
61  inline Vector &operator= (const Vector &vector_);
62  virtual inline void *create (bool isCopy_ = false) const;
63  virtual inline void copy (const T *begin_, const T *end_);
64  virtual inline void copy (size_t m_, const T &a_);
65 
66  virtual inline operator bool () const;
67 
68  virtual inline std::ostream &out (std::ostream &ostr_) const;
69  virtual inline std::istream &in (std::istream &istr_);
70 
71  virtual inline Vector &operator= (const T &a_); // copies a_ into all vector elements
72 
73  virtual inline size_t size () const; // vector size
74  virtual inline T &operator [] (size_t i_); // i-th element
75  virtual inline const T &operator [] (size_t i_) const; // i-th element
76 
77  virtual inline size_t getM () const {return d_m;} // m
78  virtual inline const T *getVector () const {return d_vector_p;} // the vector
79 
80  private:
81 
82  size_t d_m; // m
83  T *d_vector_p; // the vector
84 
85  virtual inline void init (size_t m_);
86  virtual inline void free ();
87 
88  };
89 
90 END_SCOPE(Njn)
91 
92 template <typename T>
93 bool operator== (const Njn::Vector <T> &vector_, const Njn::Vector <T> &vector0_);
94 
95 template <typename T>
96 std::ostream &operator<< (std::ostream &ostr_, const Njn::Vector <T> &vector_);
97 
98 template <typename T>
99 std::istream &operator>> (std::istream &istr_, Njn::Vector <T> &vector_);
100 
101 //
102 // There are no more declarations beyond this point.
103 //
104 
105 BEGIN_SCOPE(Njn)
106 
107  template <typename T>
109  : d_m (0), d_vector_p (0)
110  {}
111 
112  template <typename T>
114  const T *begin_,
115  const T *end_)
116  : d_m (0), d_vector_p (0)
117  {
118  copy (begin_, end_);
119  }
120 
121  template <typename T>
122  Vector <T>::Vector ( // copies a_ into m_ vector elements
123  size_t m_, // m
124  const T &a_) // the vector elements
125  : d_m (0), d_vector_p (0)
126  {
127  copy (m_, a_);
128  }
129 
130  template <typename T>
131  Vector <T>::~Vector () {free ();}
132 
133  template <typename T>
135  {
136  if (this != &vector_) copy (vector_.getVector (), vector_.getVector () + vector_.getM ());
137  return *this;
138  }
139 
140  template <typename T>
141  void *Vector <T>::create (bool isCopy_) const
142  {
143  Vector *vector = new Vector;
144  if (isCopy_) vector->operator= (*this);
145  return vector;
146  }
147 
148  template <typename T>
150  const T *begin_,
151  const T *end_)
152  {
153  assert (begin_ <= end_);
154 
155  size_t m = end_ - begin_;
156  if (m != getM ()) {
157  free ();
158  init (m);
159  }
160 
161  for (size_t i = 0; i < getM (); i++) {
162  d_vector_p [i] = begin_ [i];
163  }
164  }
165 
166  template <typename T>
168  size_t m_, // m
169  const T &a_) // the vector elements
170  {
171  if (m_ != getM ()) {
172  free ();
173  init (m_);
174  }
175 
176  for (size_t i = 0; i < getM (); i++) {
177  d_vector_p [i] = a_;
178  }
179  }
180 
181  template <typename T>
182  Vector <T>::operator bool () const {return getM () > 0;}
183 
184  template <typename T>
185  Vector <T> &Vector <T>::operator= (const T &a_) // copies a_ into all vector elements
186  {
187  copy (getM (), a_);
188  return *this;
189  }
190 
191 
192  template <typename T>
193  std::ostream &Vector <T>::out (std::ostream &ostr_) const
194  {
195  assert (ostr_);
196 
197  USING_SCOPE(Njn);
198  USING_SCOPE(IoUtil);
199 
200  size_t i = 0;
201 
202  switch (getFormat ()) {
203 
204  case MACHINE :
205 
206  for (i = 0; i < getM (); i++) {
207 
208  if (i != 0) ostr_ << endl;
209  ostr_ << getVector () [i];
210  }
211 
212  break;
213 
214  case HUMAN :
215  default :
216 
217  ostr_ << getM () << "\t! dimension of vector\n";
218 
219  for (i = 0; i < getM (); i++) {
220 
221  if (i != 0) ostr_ << '\t';
222  ostr_ << getVector () [i];
223  }
224 
225  ostr_ << "\t! vector elements";
226 
227  break;
228  }
229 
230  clearFormat ();
231 
232  return ostr_;
233  }
234 
235  template <typename T>
236  std::istream &Vector <T>::in (std::istream &istr_)
237  {
238  assert (istr_);
239 
240  std::vector <T> v;
241  T value;
242 
243  USING_SCOPE(Njn);
244  USING_SCOPE(IoUtil);
245 
246  string s;
247  stringstream sstream;
248  size_t m = 0;
249 
250  switch (getFormat ()) {
251 
252  case MACHINE :
253 
254  while (istr_ >> value) v.push_back (value);
255 
256  if (v.size () != getM ()) {
257 
258  free ();
259  init (v.size ());
260  }
261 
262  std::copy (v.begin (), v.end (), d_vector_p);
263 
264  break;
265 
266  case HUMAN :
267  default :
268 
269  getLine (istr_, s);
270  sstream.str ("");
271  sstream << s;
272  sstream >> m;
273  if (sstream.fail ()) IoUtil::abort ("Njn::Vector::in : bad m");
274 
275  if (m != getM ()) {
276 
277  free ();
278  init (m);
279  }
280 
281  sstream.str ("");
282  getLine (istr_, s);
283  sstream << s;
284  for (size_t i = 0; i < getM (); i++) {
285 
286  sstream >> d_vector_p [i];
287 
288  if (sstream.fail ()) {
289  ostringstream sstream;
290  sstream << i;
291  IoUtil::abort ("Njn::Vector::in : bad d_vector_p [" + sstream.str () + "]");
292  }
293  }
294 
295  break;
296  }
297 
298  clearFormat ();
299 
300  return istr_;
301  }
302 
303  template <typename T>
304  size_t Vector <T>::size () const
305  {
306  return getM ();
307  }
308 
309  template <typename T>
310  T &Vector <T>::operator [] (size_t i_)
311  {
312  return d_vector_p [i_];
313  }
314 
315  template <typename T>
316  const T &Vector <T>::operator [] (size_t i_) const
317  {
318  return d_vector_p [i_];
319  }
320 
321  template <typename T>
322  void Vector <T>::init (size_t m_)
323  {
324  if (m_ > 0) d_vector_p = new T [m_];
325 
326  d_m = m_;
327  }
328 
329  template <typename T>
331  {
332  if (getM () > 0) delete [] d_vector_p;
333  d_vector_p = 0;
334 
335  d_m = 0;
336  }
337 
338 END_SCOPE(Njn)
339 
340 template <typename T>
341 bool operator== (const Njn::Vector <T> &vector_, const Njn::Vector <T> &vector0_)
342 {
343  if (vector_.getM () != vector0_.getM ()) return false;
344  for (size_t i = 0; i < vector_.getM (); i++) {
345  if (vector_.getVector () [i] != vector0_.getVector () [i]) return false;
346  }
347 
348  return true;
349 }
350 
351 template <typename T>
352 std::ostream &operator<< (std::ostream &ostr_, const Njn::Vector <T> &vector_)
353 {return vector_.out (ostr_);}
354 
355 template <typename T>
356 std::istream &operator>> (std::istream &istr_, Njn::Vector <T> &vector_)
357 {return vector_.in (istr_);}
358 
359 
360 END_SCOPE(blast)
362 
363 #endif //! ALGO_BLAST_GUMBEL_PARAMS__INCLUDED_NJN_VECTOR
364 
#define bool
Definition: bool.h:34
virtual std::ostream & out(std::ostream &ostr_) const
Definition: njn_vector.hpp:193
virtual void init(size_t m_)
Definition: njn_vector.hpp:322
T * d_vector_p
Definition: njn_vector.hpp:83
virtual void * create(bool isCopy_=false) const
Definition: njn_vector.hpp:141
Vector(const Vector &vector_)
virtual size_t getM() const
Definition: njn_vector.hpp:77
size_t d_m
Definition: njn_vector.hpp:82
virtual size_t size() const
Definition: njn_vector.hpp:304
virtual const T * getVector() const
Definition: njn_vector.hpp:78
virtual void copy(const T *begin_, const T *end_)
Definition: njn_vector.hpp:149
virtual std::istream & in(std::istream &istr_)
Definition: njn_vector.hpp:236
virtual T & operator[](size_t i_)
Definition: njn_vector.hpp:310
virtual void free()
Definition: njn_vector.hpp:330
Vector & operator=(const Vector &vector_)
Definition: njn_vector.hpp:134
virtual ~Vector()
Definition: njn_vector.hpp:131
char value[7]
Definition: config.c:431
#define T(s)
Definition: common.h:230
static void DLIST_NAME() init(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:40
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define USING_SCOPE(ns)
Use the specified namespace.
Definition: ncbistl.hpp:78
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:75
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
int i
The NCBI C++/STL use hints.
Format clearFormat()
Definition: njn_ioutil.cpp:69
std::istream & getLine(std::istream &in_, std::string &str_, const char t_=getTerminator())
@ HUMAN
Definition: njn_ioutil.hpp:52
@ MACHINE
Definition: njn_ioutil.hpp:52
void abort()
Format getFormat()
Definition: njn_ioutil.cpp:59
void copy(Njn::Matrix< S > *matrix_, const Njn::Matrix< T > &matrix0_)
Definition: njn_matrix.hpp:613
bool operator==(const Njn::Vector< T > &vector_, const Njn::Vector< T > &vector0_)
Definition: njn_vector.hpp:341
std::ostream & operator<<(std::ostream &ostr_, const Njn::Vector< T > &vector_)
Definition: njn_vector.hpp:352
std::istream & operator>>(std::istream &istr_, Njn::Vector< T > &vector_)
Definition: njn_vector.hpp:356
#define assert(x)
Definition: srv_diag.hpp:58
void free(voidpf ptr)
Modified on Mon Mar 04 05:13:52 2024 by modify_doxy.py rev. 669887