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