MADARA  3.2.3
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  std::lock(*context_, *other.context_, mutex_, other.mutex_);
373 
374  ContextGuard context_guard (*context_, std::adopt_lock);
375  ContextGuard other_context_guard (*other.context_, std::adopt_lock);
376  MADARA_GUARD_TYPE guard (mutex_, std::adopt_lock),
377  guard2 (other.mutex_, std::adopt_lock);
378 
379 
380  if (refresh_keys)
381  {
382  other.resize ();
383  this->resize ();
384  }
385 
386  size_t other_size = other.vector_.size ();
387  size_t this_size = this->vector_.size ();
388 
389  for (size_t i = 0; i < this_size; ++i)
390  {
391  // temp = this[i];
393 
394  if (i < other_size)
395  {
396  // this[i] = other[i];
397  context_->set (this->vector_[i],
398  context_->get (other.vector_[i], other.settings_),
399  settings_);
400 
401  // other[i] = temp;
402  other.context_->set (other.vector_[i], temp, other.settings_);
403  }
404  else
405  {
406  if (delete_keys)
407  {
408  std::stringstream buffer;
409  buffer << this->name_;
410  buffer << delimiter_;
411  buffer << i;
412  this->context_->delete_variable (buffer.str (), other.settings_);
413  }
414  else
415  {
417  this->context_->set (this->vector_[i], zero, this->settings_);
418  }
419 
420  {
421  std::stringstream buffer;
422  buffer << other.name_;
423  buffer << delimiter_;
424  buffer << i;
425 
426  // other[i] = temp;
427  other.context_->set (buffer.str (), temp, other.settings_);
428  }
429  }
430 
431  }
432 
433  // copy the other vector's elements to this vector's location
434  for (size_t i = this_size; i < other_size; ++i)
435  {
436  std::stringstream buffer;
437  buffer << this->name_;
438  buffer << delimiter_;
439  buffer << i;
440  context_->set (buffer.str (),
441  other.context_->get (other.vector_[i],
442  other.settings_), this->settings_);
443  }
444 
445  // set the size appropriately
446  this->context_->set (this->size_,
447  knowledge::KnowledgeRecord::Integer (other_size), this->settings_);
448  other.context_->set (other.size_,
450 
451  if (refresh_keys)
452  {
453  this->resize (-1, true);
454  other.resize (-1, true);
455  }
456  }
457 }
458 
459 void
461  IntegerVectorVector & other)
462 {
463  if (context_ && other.context_)
464  {
465  std::lock(*context_, *other.context_, mutex_, other.mutex_);
466 
467  ContextGuard context_guard (*context_, std::adopt_lock);
468  ContextGuard other_context_guard (*other.context_, std::adopt_lock);
469  MADARA_GUARD_TYPE guard (mutex_, std::adopt_lock),
470  guard2 (other.mutex_, std::adopt_lock);
471 
472 
473  size_t other_size = other.vector_.size ();
474  size_t this_size = this->vector_.size ();
475 
476  size_t size = other_size + this_size;
477  other.resize ((int)size);
478 
479  for (size_t i = 0, j = other_size; i < this_size; ++i, ++j)
480  {
481  other.context_->set (other.vector_[j], (*this)[i], other.settings_);
482  }
483 
484  this->resize (0, true);
485  }
486 }
487 
488 void
490  KnowledgeVector & target) const
491 {
492  if (context_)
493  {
494  ContextGuard context_guard (*context_);
495  MADARA_GUARD_TYPE guard (mutex_);
496 
497  target.resize (vector_.size ());
498 
499  for (size_t i = 0; i < vector_.size (); ++i)
500  {
501  target[i] = knowledge::KnowledgeRecord ((*this)[i]);
502  }
503  }
504 }
505 
506 void
508  std::vector <IntegerVectorVector::type > & target) const
509 {
510  if (context_)
511  {
512  ContextGuard context_guard (*context_);
513  MADARA_GUARD_TYPE guard (mutex_);
514 
515  target.resize (vector_.size ());
516 
517  for (size_t i = 0; i < vector_.size (); ++i)
518  {
519  target[i] = (*this)[i];
520  }
521  }
522 }
523 
526  size_t index) const
527 {
529  KnowledgeUpdateSettings keep_local (true);
530 
531  if (index < vector_.size () && context_)
532  {
533  ContextGuard context_guard (*context_);
534  MADARA_GUARD_TYPE guard (mutex_);
535  result = context_->get (vector_[index], keep_local);
536  }
537 
538  return result.to_integers ();
539 }
540 
541 int64_t
543  Indices index) const
544 {
545  KnowledgeRecord result;
546 
547  KnowledgeUpdateSettings keep_local (true);
548 
549  if (index.x < vector_.size () && context_)
550  {
551  ContextGuard context_guard (*context_);
552  MADARA_GUARD_TYPE guard (mutex_);
553  result = context_->get (
554  vector_[index.x], keep_local).retrieve_index (index.y);
555  }
556 
557  return result.to_integer ();
558 }
559 
562  size_t index) const
563 {
565  KnowledgeUpdateSettings keep_local (true);
566 
567  if (index < vector_.size () && context_)
568  {
569  ContextGuard context_guard (*context_);
570  MADARA_GUARD_TYPE guard (mutex_);
571  result = context_->get (vector_[index], keep_local);
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  "IntegerVectorVector::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  "IntegerVectorVector::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  "IntegerVectorVector::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  "IntegerVectorVector::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  "IntegerVectorVector::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 }
758 
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 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.
static struct madara::knowledge::tags::string_t string
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:45
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.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
Integer to_integer(void) const
converts the value to an integer.
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.
Settings for applying knowledge updates.
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a variable.
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.