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