simplevector.cpp
Go to the documentation of this file.
1 //======================================================================
28 //======================================================================
29 
30 //----------------------------------------------------------------------
31 // System Includes - include with <>
32 //----------------------------------------------------------------------
33 
34 #include <assert.h>
35 
36 
37 //----------------------------------------------------------------------
38 // Project Includes - include with ""
39 //----------------------------------------------------------------------
40 
41 #include "dbg.h"
42 #include "simplevector.h"
43 
45 
46 //----------------------------------------------------------------------
47 // Defines, enums, unions, structs,
48 //----------------------------------------------------------------------
49 
50 
51 //----------------------------------------------------------------------
52 // Global variables
53 //----------------------------------------------------------------------
54 
55 
56 //----------------------------------------------------------------------
57 // Function implementation (function definitions)
58 //----------------------------------------------------------------------
59 
60 
61 //----------------------------------------------------------------------
62 // Class member function definitions
63 //----------------------------------------------------------------------
64 
65 
67 {
68  for ( int i=0; i < eNUMBER_OF_ELEMENTS; i++ )
69  value[ i ] = 0.0;
70  valid = 0;
71 }
72 //-----------------------------------------------------------------
73 
74 
75 cSimpleVector::cSimpleVector( int nb_values, char const* str )
76 {
77  FromString( nb_values, 0, str );
78 }
79 //-----------------------------------------------------------------
80 
81 cSimpleVector::cSimpleVector( int nb_values, int start_index, float* values )
82 {
83  valid = 0;
84  int mask = (1<<start_index);
85  for ( int i=0; i < nb_values; i++ )
86  {
87  value[ i+start_index ] = values[i];
88  valid |= mask;
89  mask <<= 1;
90  }
91 }
92 //-----------------------------------------------------------------
93 
94 
95 cSimpleVector::cSimpleVector( int nb_values, int start_index, char const* str )
96 {
97  FromString( nb_values, start_index, str );
98 }
99 //-----------------------------------------------------------------
100 
101 
102 void cSimpleVector::FromString( int nb_values, int start_index, char const* str )
103 {
104  assert( start_index + nb_values <= eNUMBER_OF_ELEMENTS );
105 
106  for ( int i = 0; i < nb_values; i++ )
107  {
108  int n; // number of chars scanned
109  int nb_fields; // number of fields successfully scanned
110  int vi = start_index+i; // index in value to write
111  nb_fields = sscanf( str, " %lf%n", &(value[ vi ]), &n );
112 
113  if ( nb_fields != 1 )
114  throw new cSimpleVectorException( cMsg( "cannot init simple vector from string <%s>", str ) );
115 
116  valid |= (1<<vi);
117 
118 
119  str += n;
120 
121  // skip "," separators
122  while ( *str == ',' )
123  str++;
124  }
125 }
126 //-----------------------------------------------------------------
127 
128 
129 double& cSimpleVector::operator[]( unsigned int index )
130 {
131  assert( index < eNUMBER_OF_ELEMENTS );
132  //assert( valid & (1 << index) );
133  valid |= (1<<index);
134  return value[ index ];
135 }
136 //-----------------------------------------------------------------
137 
138 
139 double& cSimpleVector::x(void)
140 {
141  //assert( valid & (1<<0) );
142  valid |= (1<<0);
143  return value[0];
144 }
145 //-----------------------------------------------------------------
146 
147 
148 
149 double& cSimpleVector::y(void)
150 {
151  //assert( valid & (1<<1) );
152  valid |= (1<<1);
153  return value[1];
154 }
155 //-----------------------------------------------------------------
156 
157 
158 
159 double& cSimpleVector::z(void)
160 {
161  //assert( valid & (1<<2) );
162  valid |= (1<<2);
163  return value[2];
164 }
165 //-----------------------------------------------------------------
166 
167 
168 bool cSimpleVector::Valid( unsigned int index ) const
169 {
170  assert( index < eNUMBER_OF_ELEMENTS );
171 
173  return (valid & (1<<index)) != 0;
174 }
175 //-----------------------------------------------------------------
176 
177 
178 //======================================================================
179 /*
180  Here are some settings for the emacs/xemacs editor (and can be safely ignored):
181  (e.g. to explicitely set C++ mode for *.h header files)
182 
183  Local Variables:
184  mode:C++
185  mode:ELSE
186  End:
187 */
188 //======================================================================
189 
double & operator[](unsigned int index)
index operator, return a reference to the index-th element of this
Derived exception class for low-level simple vector related exceptions.
Definition: simplevector.h:74
double & y(void)
Interpret object as x/y/z vector: return x = the first element, if that is valid. ...
double & z(void)
Interpret object as x/y/z vector: return x = the first element, if that is valid. ...
int valid
bit mask which values in value are valid
Definition: simplevector.h:142
void FromString(int nb_values, int start_index, char const *str)
init nb_values starting from index start_index from comma separated values in str ...
This file contains interface and implementation of class #SDH::cDBG, a class for colorfull debug mess...
Interface of class #SDH::cSimpleVector.
#define USING_NAMESPACE_SDH
double value[eNUMBER_OF_ELEMENTS]
Definition: simplevector.h:139
double & x(void)
Interpret object as x/y/z vector: return x = the first element, if that is valid. ...
Class for short, fixed maximum length text messages.
Definition: sdhexception.h:77
cSimpleVector()
Default constructor: init members to zero.
number of elements in vector
Definition: simplevector.h:97
bool Valid(unsigned int index) const
Return true if vector element index is valid (has been accessed at least once)


sdhlibrary_cpp
Author(s): Dirk Osswald
autogenerated on Sun Aug 18 2019 03:42:20