MADARA  3.1.8
DoubleVector.cpp
Go to the documentation of this file.
1 #include "DoubleVector.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 DoubleVector & 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 DoubleVector (*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 DoubleVector & 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  DoubleVector & 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  DoubleVector & 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 <double> & 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_double ();
532 }
533 
536  size_t index) const
537 {
539  KnowledgeUpdateSettings keep_local (true);
540 
541  if (index < vector_.size () && context_)
542  {
543  ContextGuard context_guard (*context_);
544  MADARA_GUARD_TYPE guard (mutex_);
545  result = context_->get (vector_[index], keep_local);
546  }
547 
548  return result;
549 }
550 
553 {
555  KnowledgeUpdateSettings keep_local (true);
556 
557  // if we have something to actually set
558  if (vector_.size () > 0 && context_)
559  {
560  ContextGuard context_guard (*context_);
561  MADARA_GUARD_TYPE guard (mutex_);
562 
563  // set last element first so we're not constantly resizing
564  result.set_index (vector_.size () - 1,
565  context_->get (vector_[vector_.size () - 1], keep_local).to_double ());
566 
567  for (size_t i = 0; i < vector_.size () - 1; ++i)
568  {
569  result.set_index (i,
570  context_->get (vector_[i], keep_local).to_double ());
571  }
572  }
573 
574  return result;
575 }
576 
577 bool
579  size_t index) const
580 {
581  bool result (false);
582 
583  if (index < vector_.size () && context_)
584  {
585  ContextGuard context_guard (*context_);
586  MADARA_GUARD_TYPE guard (mutex_);
587  result = context_->exists (vector_[index]);
588  }
589 
590  return result;
591 }
592 
593 int
595 size_t index,
596 type value)
597 {
598  int result = -1;
599 
600  if (index < vector_.size () && context_)
601  {
602  ContextGuard context_guard (*context_);
603  MADARA_GUARD_TYPE guard (mutex_);
604  result = context_->set (vector_[index], value, settings_);
605  }
606 
607  return result;
608 }
609 
610 
611 int
613 const std::vector <type> & value)
614 {
615  int result = -1;
616 
617  if (context_)
618  {
619  ContextGuard context_guard (*context_);
620  MADARA_GUARD_TYPE guard (mutex_);
621  if (vector_.size () < value.size ())
622  resize ((int)value.size (), false);
623 
624  for (size_t i = 0; i < value.size (); ++i)
625  {
626  context_->set (vector_[i], value[i], settings_);
627  }
628 
629  result = 0;
630  }
631 
632  return result;
633 }
634 
635 int
637  size_t index,
638  type value,
639  const KnowledgeUpdateSettings & settings)
640 {
641  int result = -1;
642 
643  if (index < vector_.size () && context_)
644  {
645  ContextGuard context_guard (*context_);
646  MADARA_GUARD_TYPE guard (mutex_);
647  result = context_->set (vector_[index], value, settings);
648  }
649 
650  return result;
651 }
652 
653 
654 int
656  const std::vector <type> & value,
657  const KnowledgeUpdateSettings & settings)
658 {
659  int result = -1;
660 
661  if (context_)
662  {
663  ContextGuard context_guard (*context_);
664  MADARA_GUARD_TYPE guard (mutex_);
665  if (vector_.size () < value.size ())
666  resize ((int)value.size (), false);
667 
668  for (size_t i = 0; i < value.size (); ++i)
669  {
670  context_->set (vector_[i], value[i], settings);
671  }
672 
673  result = 0;
674  }
675 
676  return result;
677 }
678 
679 void
681  size_t index,
682  uint32_t quality,
683  const KnowledgeReferenceSettings & settings)
684 {
685  if (index < vector_.size () && context_)
686  {
687  ContextGuard context_guard (*context_);
688  MADARA_GUARD_TYPE guard (mutex_);
689  context_->set_quality (vector_[index].get_name (), quality,
690  true, settings);
691  }
692 }
693 
694 bool
696 {
697  bool result (false);
698 
700  "DoubleVector::is_true: Checking for truth\n");
701 
702  if (context_)
703  {
704  ContextGuard context_guard (*context_);
705  MADARA_GUARD_TYPE guard (mutex_);
706 
707  result = true;
708 
710  "DoubleVector::is_true: context was not null. Result changed to %d\n",
711  (int)result);
712 
713  for (size_t index = 0; index < vector_.size (); ++index)
714  {
715 
717  "DoubleVector::is_true: checking index %d, is_false of %d. \n",
718  (int)result, (int)context_->get (vector_[index]).is_false ());
719 
720  if (context_->get (vector_[index]).is_false ())
721  {
723  "DoubleVector::is_true: result is false, breaking\n");
724 
725  result = false;
726  break;
727  }
728  }
729 
730  if (vector_.size () == 0)
731  result = false;
732  }
733 
735  "DoubleVector::is_true: final result is %d\n", (int)result);
736 
737  return result;
738 }
739 
740 bool
742 {
743  return !is_true ();
744 }
745 
746 
747 bool
749 {
750  return is_true ();
751 }
752 
753 bool
755 {
756  return is_false ();
757 }
This class encapsulates an entry in a KnowledgeBase.
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
void modify(void)
Mark the vector as modified.
double type
trait that describes the value type
Definition: DoubleVector.h:35
VariableReference size_
Reference to the size field of the vector space.
Definition: DoubleVector.h:374
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.
madara::knowledge::KnowledgeRecord KnowledgeRecord
type operator[](size_t index) const
Retrieves a copy of the record from the vector.
double to_double(void) const
converts the value to a float/double
std::string name_
Prefix of variable.
VariableReference get_size_ref(void)
Returns a reference to the size field of the current name.
std::string get_delimiter(void)
Gets the delimiter for adding and detecting subvariables.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
This class stores variables and their values for use by any entity needing state information in a thr...
size_t size(void) const
Returns the size of the local vector.
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
virtual void modify_(void)
Polymorphic modify method used by collection containers.
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_valid(void) const
Checks to see if the variable reference has been initialized.
void resize(int size=-1, bool delete_vars=true)
Resizes the vector.
bool exists(size_t index) const
Checks to see if the index has ever been assigned a value.
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically checks to see if a variable already exists.
knowledge::KnowledgeRecord to_record(void) const
Retrieves the entire vector as a native double array in a record.
::std::vector< KnowledgeRecord > KnowledgeVector
bool is_true(void) const
Determines if all values in the vector are true.
std::string delimiter_
Delimiter for the prefix to subvars.
Definition: DoubleVector.h:379
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:23
bool is_false(void) const
Determines if the value of the vector is false.
void operator=(const DoubleVector &rhs)
Assignment operator.
void exchange(DoubleVector &other, bool refresh_keys=true, bool delete_keys=true)
Exchanges the vector at this location with the vector at another location.
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:44
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::vector< VariableReference > vector_
Values of the array.
Definition: DoubleVector.h:369
void transfer_to(DoubleVector &other)
Transfers elements from this vector to another.
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
void set_index(size_t index, T value)
sets the value at the index to the specified value.
Integer to_integer(void) const
converts the value to an integer
static constexpr struct madara::knowledge::tags::string_t string
int set(size_t index, type value)
Sets a knowledge variable to a specified value.
virtual BaseContainer * clone(void) const
Clones this container.
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 ...
ThreadSafeContext * context_
Variable context that we are modifying.
Definition: DoubleVector.h:364
void set_delimiter(const std::string &delimiter)
Sets the delimiter for adding and detecting subvariables.
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.
void push_back(type value)
Pushes the value to the end of the array after incrementing the array size.
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.
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.
virtual bool is_true_(void) const
Polymorphic is true method which can be used to determine if all values in the container are true...
bool is_false(void) const
Checks to see if the record is false.
Settings for applying knowledge updates.
This class stores a vector of doubles inside of KaRL.
Definition: DoubleVector.h:31
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
DoubleVector(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(), const std::string &delimiter=".")
Default constructor.
Definition: DoubleVector.cpp:5
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
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...
void copy_to(KnowledgeVector &target) const
Copies the vector elements to an STL vector of Knowledge Records.
ThreadSafeContext * get_context(void)
Returns the ThreadSafeContext associated with this Variables facade.