MADARA  3.4.1
DoubleVectorVector.cpp
Go to the documentation of this file.
1 #include "DoubleVectorVector.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 DoubleVectorVector& 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 DoubleVectorVector(*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 DoubleVectorVector& 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  DoubleVectorVector& other, bool refresh_keys, bool delete_keys)
344 {
345  if (context_ && other.context_)
346  {
347  ContextGuard context_guard(*context_);
348  ContextGuard other_context_guard(*other.context_);
349  MADARA_GUARD_TYPE guard(mutex_), guard2(other.mutex_);
350 
351  if (refresh_keys)
352  {
353  other.resize();
354  this->resize();
355  }
356 
357  size_t other_size = other.vector_.size();
358  size_t this_size = this->vector_.size();
359 
360  for (size_t i = 0; i < this_size; ++i)
361  {
362  // temp = this[i];
364  context_->get(this->vector_[i], settings_);
365 
366  if (i < other_size)
367  {
368  // this[i] = other[i];
369  context_->set(this->vector_[i],
370  context_->get(other.vector_[i], other.settings_), settings_);
371 
372  // other[i] = temp;
373  other.context_->set(other.vector_[i], temp, other.settings_);
374  }
375  else
376  {
377  if (delete_keys)
378  {
379  std::stringstream buffer;
380  buffer << this->name_;
381  buffer << delimiter_;
382  buffer << i;
383  this->context_->delete_variable(buffer.str(), other.settings_);
384  }
385  else
386  {
388  this->context_->set(this->vector_[i], zero, this->settings_);
389  }
390 
391  {
392  std::stringstream buffer;
393  buffer << other.name_;
394  buffer << delimiter_;
395  buffer << i;
396 
397  // other[i] = temp;
398  other.context_->set(buffer.str(), temp, other.settings_);
399  }
400  }
401  }
402 
403  // copy the other vector's elements to this vector's location
404  for (size_t i = this_size; i < other_size; ++i)
405  {
406  std::stringstream buffer;
407  buffer << this->name_;
408  buffer << delimiter_;
409  buffer << i;
410  context_->set(buffer.str(),
411  other.context_->get(other.vector_[i], other.settings_),
412  this->settings_);
413  }
414 
415  // set the size appropriately
416  this->context_->set(this->size_,
417  knowledge::KnowledgeRecord::Integer(other_size), this->settings_);
418  other.context_->set(other.size_,
420 
421  if (refresh_keys)
422  {
423  this->resize(-1, true);
424  other.resize(-1, true);
425  }
426  }
427 }
428 
430  DoubleVectorVector& other)
431 {
432  if (context_ && other.context_)
433  {
434  std::lock(*context_, *other.context_, mutex_, other.mutex_);
435 
436  ContextGuard context_guard(*context_, std::adopt_lock);
437  ContextGuard other_context_guard(*other.context_, std::adopt_lock);
438  MADARA_GUARD_TYPE guard(mutex_, std::adopt_lock),
439  guard2(other.mutex_, std::adopt_lock);
440 
441  size_t other_size = other.vector_.size();
442  size_t this_size = this->vector_.size();
443 
444  size_t size = other_size + this_size;
445  other.resize((int)size);
446 
447  for (size_t i = 0, j = other_size; i < this_size; ++i, ++j)
448  {
449  other.context_->set(other.vector_[j], (*this)[i], other.settings_);
450  }
451 
452  this->resize(0, true);
453  }
454 }
455 
457  KnowledgeVector& target) const
458 {
459  if (context_)
460  {
461  ContextGuard context_guard(*context_);
462  MADARA_GUARD_TYPE guard(mutex_);
463 
464  target.resize(vector_.size());
465 
466  for (size_t i = 0; i < vector_.size(); ++i)
467  {
468  target[i] = knowledge::KnowledgeRecord((*this)[i]);
469  }
470  }
471 }
472 
474  std::vector<type>& target) const
475 {
476  if (context_)
477  {
478  ContextGuard context_guard(*context_);
479  MADARA_GUARD_TYPE guard(mutex_);
480 
481  target.resize(vector_.size());
482 
483  for (size_t i = 0; i < vector_.size(); ++i)
484  {
485  target[i] = (*this)[i];
486  }
487  }
488 }
489 
492  size_t index) const
493 {
495  KnowledgeUpdateSettings keep_local(true);
496 
497  if (index < vector_.size() && context_)
498  {
499  ContextGuard context_guard(*context_);
500  MADARA_GUARD_TYPE guard(mutex_);
501  result = context_->get(vector_[index], keep_local);
502  }
503 
504  return result.to_doubles();
505 }
506 
508  Indices index) const
509 {
510  KnowledgeRecord result;
511 
512  KnowledgeUpdateSettings keep_local(true);
513 
514  if (index.x < vector_.size() && context_)
515  {
516  ContextGuard context_guard(*context_);
517  MADARA_GUARD_TYPE guard(mutex_);
518  result =
519  context_->get(vector_[index.x], keep_local).retrieve_index(index.y);
520  }
521 
522  return result.to_double();
523 }
524 
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;
539 }
540 
542  size_t index) const
543 {
544  bool result(false);
545 
546  if (index < vector_.size() && context_)
547  {
548  ContextGuard context_guard(*context_);
549  MADARA_GUARD_TYPE guard(mutex_);
550  result = context_->exists(vector_[index]);
551  }
552 
553  return result;
554 }
555 
557  size_t index, type value)
558 {
559  int result = -1;
560 
561  if (index < vector_.size() && context_)
562  {
563  ContextGuard context_guard(*context_);
564  MADARA_GUARD_TYPE guard(mutex_);
565  result = context_->set(vector_[index], value, settings_);
566  }
567 
568  return result;
569 }
570 
572  const std::vector<type>& value)
573 {
574  int result = -1;
575 
576  if (context_)
577  {
578  ContextGuard context_guard(*context_);
579  MADARA_GUARD_TYPE guard(mutex_);
580  if (vector_.size() < value.size())
581  resize((int)value.size(), false);
582 
583  for (size_t i = 0; i < value.size(); ++i)
584  {
585  context_->set(vector_[i], value[i], settings_);
586  }
587 
588  result = 0;
589  }
590 
591  return result;
592 }
593 
595  size_t index, type value, const KnowledgeUpdateSettings& settings)
596 {
597  int result = -1;
598 
599  if (index < vector_.size() && context_)
600  {
601  ContextGuard context_guard(*context_);
602  MADARA_GUARD_TYPE guard(mutex_);
603  result = context_->set(vector_[index], value, settings);
604  }
605 
606  return result;
607 }
608 
610  const std::vector<type>& value, const KnowledgeUpdateSettings& settings)
611 {
612  int result = -1;
613 
614  if (context_)
615  {
616  ContextGuard context_guard(*context_);
617  MADARA_GUARD_TYPE guard(mutex_);
618  if (vector_.size() < value.size())
619  resize((int)value.size(), false);
620 
621  for (size_t i = 0; i < value.size(); ++i)
622  {
623  context_->set(vector_[i], value[i], settings);
624  }
625 
626  result = 0;
627  }
628 
629  return result;
630 }
631 
633  size_t index, uint32_t quality, const KnowledgeReferenceSettings& settings)
634 {
635  if (index < vector_.size() && context_)
636  {
637  ContextGuard context_guard(*context_);
638  MADARA_GUARD_TYPE guard(mutex_);
639  context_->set_quality(vector_[index].get_name(), quality, true, settings);
640  }
641 }
642 
644 {
645  bool result(false);
646 
648  "DoubleVectorVector::is_true: Checking for truth\n");
649 
650  if (context_)
651  {
652  ContextGuard context_guard(*context_);
653  MADARA_GUARD_TYPE guard(mutex_);
654 
655  result = true;
656 
658  "DoubleVectorVector::is_true: context was not null. Result changed to "
659  "%d\n",
660  (int)result);
661 
662  for (size_t index = 0; index < vector_.size(); ++index)
663  {
665  "DoubleVectorVector::is_true: checking index %d, is_false of %d. \n",
666  (int)result, (int)context_->get(vector_[index]).is_false());
667 
668  if (context_->get(vector_[index]).is_false())
669  {
671  "DoubleVectorVector::is_true: result is false, breaking\n");
672 
673  result = false;
674  break;
675  }
676  }
677 
678  if (vector_.size() == 0)
679  result = false;
680  }
681 
683  "DoubleVectorVector::is_true: final result is %d\n", (int)result);
684 
685  return result;
686 }
687 
689 {
690  return !is_true();
691 }
692 
694 {
695  return is_true();
696 }
697 
699 {
700  return is_false();
701 }
#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.
double to_double(void) const
converts the value to a float/double.
std::vector< double > to_doubles(void) const
converts the value to a vector of doubles
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 NativeDoubleVectors.
size_t size(void) const
Returns the size of the local vector.
std::vector< VariableReference > vector_
Values of the array.
std::string get_delimiter(void)
Gets the delimiter for adding and detecting subvariables.
knowledge::KnowledgeRecord to_record(size_t index) const
Retrieves a copy of the record from the vector.
type operator[](size_t index) const
Retrieves a copy of the record from the vector.
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 ...
virtual void modify_(void)
Polymorphic modify method used by collection containers.
void operator=(const DoubleVectorVector &rhs)
Assignment operator.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
void modify(void)
Mark the vector as modified.
std::vector< double > type
trait that describes the value type
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.
bool is_true(void) const
Determines if all values in the vector are true.
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.
void exchange(DoubleVectorVector &other, bool refresh_keys=true, bool delete_keys=true)
Exchanges the vector at this location with the vector at another location.
virtual BaseContainer * clone(void) const
Clones this container.
void resize(int size=-1, bool delete_vars=true)
Resizes the vector.
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.
std::string delimiter_
Delimiter for the prefix to subvars.
bool exists(size_t index) const
Checks to see if the index has ever been assigned a value.
void set_delimiter(const std::string &delimiter)
Sets the delimiter for adding and detecting subvariables.
ThreadSafeContext * context_
Variable context that we are modifying.
DoubleVectorVector(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(), const std::string &delimiter=".")
Default constructor.
void copy_to(KnowledgeVector &target) const
Copies the vector elements to an STL vector of Knowledge Records.
VariableReference size_
Reference to the size field of the vector space.
void transfer_to(DoubleVectorVector &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.
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.
constexpr string_t string
Provides functions and classes for the distributed knowledge base.
::std::vector< KnowledgeRecord > KnowledgeVector