MADARA  3.1.8
DoubleVectorVector.cpp
Go to the documentation of this file.
1 #include "DoubleVectorVector.h"
3 
4 
6  const KnowledgeUpdateSettings & settings,
7  const std::string & delimiter)
8  : BaseContainer ("", settings), context_ (0), delimiter_ (delimiter)
9 {
10 }
11 
13  const std::string & name,
15  int size,
16  bool delete_vars,
17  const KnowledgeUpdateSettings & settings,
18  const std::string & delimiter)
19  : BaseContainer (name, settings), context_ (&(knowledge.get_context ())),
20  delimiter_ (delimiter)
21 {
22  size_ = get_size_ref ();
23  resize (size, delete_vars);
24 }
25 
27  const std::string & name,
29  int size,
30  bool delete_vars,
31  const KnowledgeUpdateSettings & settings,
32  const std::string & delimiter)
33  : BaseContainer (name, settings), context_ (knowledge.get_context ()),
34  delimiter_ (delimiter)
35 {
36  size_ = get_size_ref ();
37  resize (size, delete_vars);
38 }
39 
41  const DoubleVectorVector & rhs)
42 : BaseContainer (rhs), context_ (rhs.context_),
43  vector_ (rhs.vector_),
44  size_ (rhs.size_),
46 {
47 }
48 
49 
51 {
52 
53 }
54 
55 void
57 {
58  if (context_ && name_ != "")
59  {
60  ContextGuard context_guard (*context_);
61  for (size_t index = 0; index < vector_.size (); ++index)
62  context_->mark_modified (vector_[index]);
63 
65  }
66 }
67 
70 {
71  std::stringstream result;
72 
73  result << "Double Vector: ";
74 
75  if (context_)
76  {
77  ContextGuard context_guard (*context_);
78  MADARA_GUARD_TYPE guard (mutex_);
79  size_t elements = vector_.size ();
80 
81  result << this->name_;
82  result << " [" << elements << "]";
83  result << " = [";
84 
85  if (elements > 0)
86  {
87  result << context_->get (vector_[0]).to_string ();
88 
89  for (size_t index = 1; index < elements; ++index)
90  {
91  result << ", " << context_->get (vector_[index]).to_string ();
92  }
93  }
94 
95  result << "]";
96  }
97 
98  return result.str ();
99 }
100 
101 void
103 {
104  modify ();
105 }
106 
109 {
110  return get_debug_info ();
111 }
112 
115 {
116  return new DoubleVectorVector (*this);
117 }
118 
119 void
121 {
122  if (context_ && name_ != "" && index < vector_.size ())
123  {
124  ContextGuard context_guard (*context_);
125  context_->mark_modified (vector_[index]);
126  }
127 }
128 
129 void
131  const DoubleVectorVector & rhs)
132 {
133  if (this != &rhs)
134  {
135  MADARA_GUARD_TYPE guard (mutex_), guard2 (rhs.mutex_);
136 
137  this->context_ = rhs.context_;
138  this->name_ = rhs.name_;
139  this->settings_ = rhs.settings_;
140  this->size_ = rhs.size_;
141  this->vector_ = rhs.vector_;
142  this->delimiter_ = rhs.delimiter_;
143  }
144 }
145 
146 void
148 {
149  if (context_ && name_ != "")
150  {
151  ContextGuard context_guard (*context_);
152  MADARA_GUARD_TYPE guard (mutex_);
153 
154  if (!size_.is_valid ())
155  {
156  size_ = get_size_ref ();
157  }
158 
159  size_t i = size ();
160  resize ((int)i + 1);
161  set (i, value);
162  }
163 }
164 
167 {
168  VariableReference ref;
169 
170  if (context_ && name_ != "")
171  {
172  KnowledgeUpdateSettings keep_local (true);
173  std::stringstream buffer;
174 
175  ContextGuard context_guard (*context_);
176  MADARA_GUARD_TYPE guard (mutex_);
177 
178  buffer << name_;
179  buffer << delimiter_;
180  buffer << "size";
181 
182  ref = context_->get_ref (buffer.str (), keep_local);
183  }
184 
185  return ref;
186 }
187 
188 void
190  int size, bool delete_vars)
191 {
192  if (context_ && name_ != "")
193  {
194  ContextGuard context_guard (*context_);
195  MADARA_GUARD_TYPE guard (mutex_);
196 
197  if (!size_.is_valid ())
198  {
199  size_ = get_size_ref ();
200  }
201 
202  if (size >= 0)
203  {
204  size_t old_size = vector_.size ();
205 
206  if (old_size != (size_t)size)
207  {
208  vector_.resize (size);
209 
211 
212  if ((size_t)size > old_size)
213  {
214  for (; old_size < (size_t)size; ++old_size)
215  {
216  std::stringstream buffer;
217  buffer << name_;
218  buffer << delimiter_;
219  buffer << old_size;
220  vector_[old_size] = context_->get_ref (buffer.str (), settings_);
221  }
222  }
223  else if (delete_vars)
224  {
225  for (; (size_t)size < old_size; ++size)
226  {
227  std::stringstream buffer;
228  buffer << name_;
229  buffer << delimiter_;
230  buffer << size;
231 
232  context_->delete_variable (buffer.str (), settings_);
233  }
234  }
235  }
236  }
237  else
238  {
239  // dynamically allocate size from the context
240  size_t cur_size =
241  (size_t) context_->get (size_, settings_).to_integer ();
242 
243  size_t old_size = vector_.size ();
244 
245  if (old_size != cur_size)
246  {
247  vector_.resize (cur_size);
248 
249  if (cur_size > old_size)
250  {
251  for (; old_size < (size_t)cur_size; ++old_size)
252  {
253  std::stringstream buffer;
254  buffer << name_;
255  buffer << delimiter_;
256  buffer << old_size;
257  vector_[old_size] = context_->get_ref (buffer.str (), settings_);
258  }
259  }
260  else if (delete_vars)
261  {
262  for (; (size_t)cur_size < old_size; ++cur_size)
263  {
264  std::stringstream buffer;
265  buffer << name_;
266  buffer << delimiter_;
267  buffer << cur_size;
268 
269  context_->delete_variable (buffer.str (), settings_);
270  }
271  }
272  }
273  }
274  }
275 }
276 
277 size_t
279 {
280  MADARA_GUARD_TYPE guard (mutex_);
281  return vector_.size ();
282 }
283 
284 void
286  const std::string & var_name,
288 {
289  if (context_ != &(knowledge.get_context ()) || name_ != var_name)
290  {
291  context_ = &(knowledge.get_context ());
292 
293  ContextGuard context_guard (*context_);
294  MADARA_GUARD_TYPE guard (mutex_);
295 
296  name_ = var_name;
297 
298  vector_.clear ();
299 
300  size_ = get_size_ref ();
301 
302  resize (size);
303  }
304 }
305 
306 void
308  const std::string & var_name,
309  Variables & knowledge, int size)
310 {
311  if (context_ != knowledge.get_context () || name_ != var_name)
312  {
313  context_ = knowledge.get_context ();
314 
315  ContextGuard context_guard (*context_);
316  MADARA_GUARD_TYPE guard (mutex_);
317 
318  name_ = var_name;
319 
320  vector_.clear ();
321  resize (size);
322  }
323 }
324 
325 void
327  const std::string & var_name,
329 {
330  if (context_ != &knowledge || name_ != var_name)
331  {
332  context_ = &knowledge;
333 
334  ContextGuard context_guard (*context_);
335  MADARA_GUARD_TYPE guard (mutex_);
336 
337  name_ = var_name;
338 
339  vector_.clear ();
340  resize (size);
341  }
342 }
343 
344 void
346 const std::string & delimiter)
347 {
348  delimiter_ = delimiter;
349  if (context_)
350  {
351  ContextGuard context_guard (*context_);
352  MADARA_GUARD_TYPE guard (mutex_);
353 
354  vector_.clear ();
355  resize (-1);
356  }
357 }
358 
359 
362 {
363  return delimiter_;
364 }
365 
366 void
368  DoubleVectorVector & other, bool refresh_keys, bool delete_keys)
369 {
370  if (context_ && other.context_)
371  {
372  ContextGuard context_guard (*context_);
373  ContextGuard other_context_guard (*other.context_);
374  MADARA_GUARD_TYPE guard (mutex_), guard2 (other.mutex_);
375 
376  if (refresh_keys)
377  {
378  other.resize ();
379  this->resize ();
380  }
381 
382  size_t other_size = other.vector_.size ();
383  size_t this_size = this->vector_.size ();
384 
385  for (size_t i = 0; i < this_size; ++i)
386  {
387  // temp = this[i];
389 
390  if (i < other_size)
391  {
392  // this[i] = other[i];
393  context_->set (this->vector_[i],
394  context_->get (other.vector_[i], other.settings_),
395  settings_);
396 
397  // other[i] = temp;
398  other.context_->set (other.vector_[i], temp, other.settings_);
399  }
400  else
401  {
402  if (delete_keys)
403  {
404  std::stringstream buffer;
405  buffer << this->name_;
406  buffer << delimiter_;
407  buffer << i;
408  this->context_->delete_variable (buffer.str (), other.settings_);
409  }
410  else
411  {
413  this->context_->set (this->vector_[i], zero, this->settings_);
414  }
415 
416  {
417  std::stringstream buffer;
418  buffer << other.name_;
419  buffer << delimiter_;
420  buffer << i;
421 
422  // other[i] = temp;
423  other.context_->set (buffer.str (), temp, other.settings_);
424  }
425  }
426 
427  }
428 
429  // copy the other vector's elements to this vector's location
430  for (size_t i = this_size; i < other_size; ++i)
431  {
432  std::stringstream buffer;
433  buffer << this->name_;
434  buffer << delimiter_;
435  buffer << i;
436  context_->set (buffer.str (),
437  other.context_->get (other.vector_[i],
438  other.settings_), this->settings_);
439  }
440 
441  // set the size appropriately
442  this->context_->set (this->size_,
443  knowledge::KnowledgeRecord::Integer (other_size), this->settings_);
444  other.context_->set (other.size_,
446 
447  if (refresh_keys)
448  {
449  this->resize (-1, true);
450  other.resize (-1, true);
451  }
452  }
453 }
454 
455 void
457  DoubleVectorVector & other)
458 {
459  if (context_ && other.context_)
460  {
461  ContextGuard context_guard (*context_);
462  ContextGuard other_context_guard (*other.context_);
463  MADARA_GUARD_TYPE guard (mutex_);
464  MADARA_GUARD_TYPE guard2 (other.mutex_);
465 
466  size_t other_size = other.vector_.size ();
467  size_t this_size = this->vector_.size ();
468 
469  size_t size = other_size + this_size;
470  other.resize ((int)size);
471 
472  for (size_t i = 0, j = other_size; i < this_size; ++i, ++j)
473  {
474  other.context_->set (other.vector_[j], (*this)[i], other.settings_);
475  }
476 
477  this->resize (0, true);
478  }
479 }
480 
481 void
483  KnowledgeVector & target) const
484 {
485  if (context_)
486  {
487  ContextGuard context_guard (*context_);
488  MADARA_GUARD_TYPE guard (mutex_);
489 
490  target.resize (vector_.size ());
491 
492  for (size_t i = 0; i < vector_.size (); ++i)
493  {
494  target[i] = knowledge::KnowledgeRecord((*this)[i]);
495  }
496  }
497 }
498 
499 void
501  std::vector <type > & target) const
502 {
503  if (context_)
504  {
505  ContextGuard context_guard (*context_);
506  MADARA_GUARD_TYPE guard (mutex_);
507 
508  target.resize (vector_.size ());
509 
510  for (size_t i = 0; i < vector_.size (); ++i)
511  {
512  target[i] = (*this)[i];
513  }
514  }
515 }
516 
519  size_t index) const
520 {
522  KnowledgeUpdateSettings keep_local (true);
523 
524  if (index < vector_.size () && context_)
525  {
526  ContextGuard context_guard (*context_);
527  MADARA_GUARD_TYPE guard (mutex_);
528  result = context_->get (vector_[index], keep_local);
529  }
530 
531  return result.to_doubles ();
532 }
533 
534 double
536  Indices index) const
537 {
538  KnowledgeRecord result;
539 
540  KnowledgeUpdateSettings keep_local (true);
541 
542  if (index.x < vector_.size () && context_)
543  {
544  ContextGuard context_guard (*context_);
545  MADARA_GUARD_TYPE guard (mutex_);
546  result = context_->get (
547  vector_[index.x], keep_local).retrieve_index (index.y);
548  }
549 
550  return result.to_double ();
551 }
552 
553 
556  size_t index) const
557 {
559  KnowledgeUpdateSettings keep_local (true);
560 
561  if (index < vector_.size () && context_)
562  {
563  ContextGuard context_guard (*context_);
564  MADARA_GUARD_TYPE guard (mutex_);
565  result = context_->get (vector_[index], keep_local);
566  }
567 
568  return result;
569 }
570 
571 bool
573  size_t index) const
574 {
575  bool result (false);
576 
577  if (index < vector_.size () && context_)
578  {
579  ContextGuard context_guard (*context_);
580  MADARA_GUARD_TYPE guard (mutex_);
581  result = context_->exists (vector_[index]);
582  }
583 
584  return result;
585 }
586 
587 int
589 size_t index,
590 type value)
591 {
592  int result = -1;
593 
594  if (index < vector_.size () && context_)
595  {
596  ContextGuard context_guard (*context_);
597  MADARA_GUARD_TYPE guard (mutex_);
598  result = context_->set (vector_[index], value, settings_);
599  }
600 
601  return result;
602 }
603 
604 
605 int
607 const std::vector <type> & value)
608 {
609  int result = -1;
610 
611  if (context_)
612  {
613  ContextGuard context_guard (*context_);
614  MADARA_GUARD_TYPE guard (mutex_);
615  if (vector_.size () < value.size ())
616  resize ((int)value.size (), false);
617 
618  for (size_t i = 0; i < value.size (); ++i)
619  {
620  context_->set (vector_[i], value[i], settings_);
621  }
622 
623  result = 0;
624  }
625 
626  return result;
627 }
628 
629 int
631  size_t index,
632  type value,
633  const KnowledgeUpdateSettings & settings)
634 {
635  int result = -1;
636 
637  if (index < vector_.size () && context_)
638  {
639  ContextGuard context_guard (*context_);
640  MADARA_GUARD_TYPE guard (mutex_);
641  result = context_->set (vector_[index], value, settings);
642  }
643 
644  return result;
645 }
646 
647 
648 int
650  const std::vector <type> & value,
651  const KnowledgeUpdateSettings & settings)
652 {
653  int result = -1;
654 
655  if (context_)
656  {
657  ContextGuard context_guard (*context_);
658  MADARA_GUARD_TYPE guard (mutex_);
659  if (vector_.size () < value.size ())
660  resize ((int)value.size (), false);
661 
662  for (size_t i = 0; i < value.size (); ++i)
663  {
664  context_->set (vector_[i], value[i], settings);
665  }
666 
667  result = 0;
668  }
669 
670  return result;
671 }
672 
673 void
675  size_t index,
676  uint32_t quality,
677  const KnowledgeReferenceSettings & settings)
678 {
679  if (index < vector_.size () && context_)
680  {
681  ContextGuard context_guard (*context_);
682  MADARA_GUARD_TYPE guard (mutex_);
683  context_->set_quality (vector_[index].get_name (), quality,
684  true, settings);
685  }
686 }
687 
688 bool
690 {
691  bool result (false);
692 
694  "DoubleVectorVector::is_true: Checking for truth\n");
695 
696  if (context_)
697  {
698  ContextGuard context_guard (*context_);
699  MADARA_GUARD_TYPE guard (mutex_);
700 
701  result = true;
702 
704  "DoubleVectorVector::is_true: context was not null. Result changed to %d\n",
705  (int)result);
706 
707  for (size_t index = 0; index < vector_.size (); ++index)
708  {
709 
711  "DoubleVectorVector::is_true: checking index %d, is_false of %d. \n",
712  (int)result, (int)context_->get (vector_[index]).is_false ());
713 
714  if (context_->get (vector_[index]).is_false ())
715  {
717  "DoubleVectorVector::is_true: result is false, breaking\n");
718 
719  result = false;
720  break;
721  }
722  }
723 
724  if (vector_.size () == 0)
725  result = false;
726  }
727 
729  "DoubleVectorVector::is_true: final result is %d\n", (int)result);
730 
731  return result;
732 }
733 
734 bool
736 {
737  return !is_true ();
738 }
739 
740 
741 bool
743 {
744  return is_true ();
745 }
746 
747 bool
749 {
750  return is_false ();
751 }
752 
This class encapsulates an entry in a KnowledgeBase.
This class stores a vector of NativeDoubleVectors.
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
std::string get_delimiter(void)
Gets the delimiter for adding and detecting subvariables.
void set_quality(size_t index, uint32_t quality, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Sets the quality of writing to a certain variable from this entity.
std::string get_name(void) const
Returns the name of the container.
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a variable.
virtual BaseContainer * clone(void) const
Clones this container.
madara::knowledge::KnowledgeRecord KnowledgeRecord
int set(size_t index, type value)
Sets a knowledge variable to a specified value.
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
double to_double(void) const
converts the value to a float/double
std::string name_
Prefix of variable.
void transfer_to(DoubleVectorVector &other)
Transfers elements from this vector to another.
knowledge::KnowledgeRecord to_record(size_t index) const
Retrieves a copy of the record from the vector.
This class stores variables and their values for use by any entity needing state information in a thr...
bool is_true(void) const
Determines if all values in the vector are true.
MADARA_LOCK_TYPE mutex_
guard for access and changes
Optimized reference to a variable within the knowledge base.
#define madara_logger_log(logger, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
bool is_false(void) const
Determines if the value of the vector is false.
type operator[](size_t index) const
Retrieves a copy of the record from the vector.
bool is_valid(void) const
Checks to see if the variable reference has been initialized.
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically checks to see if a variable already exists.
void operator=(const DoubleVectorVector &rhs)
Assignment operator.
::std::vector< KnowledgeRecord > KnowledgeVector
DoubleVectorVector(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(), const std::string &delimiter=".")
Default constructor.
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:23
void exchange(DoubleVectorVector &other, bool refresh_keys=true, bool delete_keys=true)
Exchanges the vector at this location with the vector at another location.
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
std::vector< VariableReference > vector_
Values of the array.
void push_back(const type &value)
Pushes the value to the end of the array after incrementing the array size.
void set_delimiter(const std::string &delimiter)
Sets the delimiter for adding and detecting subvariables.
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:44
std::vector< double > to_doubles(void) const
converts the value to a vector of doubles
void resize(int size=-1, bool delete_vars=true)
Resizes the vector.
void copy_to(KnowledgeVector &target) const
Copies the vector elements to an STL vector of Knowledge Records.
Integer to_integer(void) const
converts the value to an integer
static constexpr struct madara::knowledge::tags::string_t string
virtual bool is_true_(void) const
Polymorphic is true method which can be used to determine if all values in the container are true...
void modify(void)
Mark the vector as modified.
ThreadSafeContext * context_
Variable context that we are modifying.
bool exists(size_t index) const
Checks to see if the index has ever been assigned a value.
std::vector< double > type
trait that describes the value type
ThreadSafeContext & get_context(void)
Returns the ThreadSafeContext associated with this Knowledge Base.
bool delete_variable(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Deletes the key.
size_t size(void) const
Returns the size of the local vector.
virtual void modify_(void)
Polymorphic modify method used by collection containers.
VariableReference get_size_ref(void)
Returns a reference to the size field of the current name.
Provides functions and classes for the distributed knowledge base.
KnowledgeUpdateSettings settings_
Settings for modifications.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
VariableReference size_
Reference to the size field of the vector space.
Settings for applying knowledge updates.
uint32_t set_quality(const std::string &key, uint32_t quality, bool force_update, const KnowledgeReferenceSettings &settings)
Atomically sets quality of this process for a variable.
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
KnowledgeRecord retrieve_index(size_t index) const
retrieves the value at an array index.
bool is_false(void) const
Checks to see if the record is false.
Settings for applying knowledge updates.
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
std::string delimiter_
Delimiter for the prefix to subvars.
Provides an interface for external functions into the MADARA KaRL variable settings.
This class is an abstract base class for all containers.
Definition: BaseContainer.h:33
virtual bool is_false_(void) const
Polymorphic is false method which can be used to determine if at least one value in the container is ...
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
void mark_modified(const VariableReference &variable, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Marks the variable reference as updated for the purposes of sending or checkpointing knowledge (for g...
ThreadSafeContext * get_context(void)
Returns the ThreadSafeContext associated with this Variables facade.