12 const Dimensions& dimensions,
bool delete_vars,
19 resize(dimensions, delete_vars);
31 resize(dimensions, delete_vars);
40 delimiter_(rhs.delimiter_)
54 if (dimensions.
x > 0 && dimensions.
y > 0)
56 for (
size_t i = 0; i < dimensions.
x; ++i)
58 for (
size_t j = 0; j < dimensions.
y; ++j)
60 context_->mark_modified(vector_[i][j]);
71 std::stringstream result;
73 result <<
"IntegerVector2D: ";
81 result << this->name_;
82 result <<
" [" << dimensions.
x <<
"," << dimensions.
y <<
"]";
85 if (dimensions.
x > 0 && dimensions.
y > 0)
87 for (
size_t i = 0; i < dimensions.
x; ++i)
89 result <<
context_->get(vector_[i][0]).to_string();
91 for (
size_t j = 1; j < dimensions.
y; ++j)
93 result <<
", " <<
context_->get(vector_[i][j]).to_string();
113 return get_debug_info();
128 if (index.
x < vector_.size() && index.
y < vector_[index.
x].size())
129 context_->mark_modified(vector_[index.
x][index.
y]);
138 MADARA_GUARD_TYPE guard(mutex_), guard2(rhs.
mutex_);
141 this->name_ = rhs.
name_;
143 this->size_ = rhs.
size_;
157 std::stringstream buffer;
162 buffer << delimiter_;
165 ref =
context_->get_ref(buffer.str(), keep_local);
172 const Dimensions& dimensions,
bool delete_vars)
179 "IntegerVector2D::resize: resizing to [%d,%d]\n", (
int)dimensions.
x,
182 bool is_reset = dimensions.
x == 0 && dimensions.
y == 0;
184 if (!size_.is_valid())
186 size_ = get_size_ref();
194 "IntegerVector2D::resize: old size is [%d,%d]\n", (
int)old_size.
x,
200 "IntegerVector2D::resize: new size is being reset to size in KB\n");
202 new_size.
x = old_size.
x;
203 new_size.
y = old_size.
y;
208 "IntegerVector2D::resize: using dimensions passed in.\n");
211 std::vector<KnowledgeRecord::Integer> update(2);
212 update[0] = dimensions.
x;
213 update[1] = dimensions.
y;
215 context_->set(size_, update, settings_);
219 vector_.resize(new_size.
x);
221 for (
size_t i = 0; i < new_size.
x; ++i)
224 "IntegerVector2D::resize: resizing vector_[%d] to %d.\n", (
int)i,
227 vector_[i].resize(new_size.
y);
231 size_t start = old_size.
y;
234 if (is_reset || (old_size.
x < new_size.
x && i >= old_size.
x))
240 "IntegerVector2D::resize: creating var_refs from %d->%d.\n",
241 (
int)start, (
int)new_size.
y);
244 for (
size_t j = start; j < new_size.
y; ++j)
246 std::stringstream var_name;
247 var_name << this->name_;
248 var_name << delimiter_;
250 var_name << delimiter_;
253 vector_[i][j] =
context_->get_ref(var_name.str(), settings_);
258 if ((new_size.
x < old_size.
x || new_size.
y < old_size.
y) && delete_vars)
261 "IntegerVector2D::resize: deleting refs: rows: 0->%d.\n",
265 for (
size_t i = 0; i < old_size.
x; ++i)
268 size_t start = new_size.
y;
271 if (old_size.
x > new_size.
x && i >= new_size.
x)
277 "IntegerVector2D::resize: deleting refs: %d:%d->%d.\n", (
int)i,
278 (
int)start, (
int)old_size.
x);
281 for (
size_t j = start; j < old_size.
y; ++j)
283 std::stringstream var_name;
284 var_name << this->name_;
285 var_name << delimiter_;
287 var_name << delimiter_;
291 "IntegerVector2D::resize: deleting ref: %s.\n",
292 var_name.str().c_str());
294 context_->delete_variable(var_name.str(), settings_);
316 std::vector<KnowledgeRecord::Integer> sizes(record.
to_integers());
317 cur_size.
x = (size_t)(sizes.size() >= 2 ? sizes[0] : 0);
318 cur_size.
y = (size_t)(sizes.size() >= 2 ? sizes[1] : 0);
338 size_ = get_size_ref();
381 delimiter_ = delimiter;
397 std::vector<std::vector<type> >& target)
const
407 target.resize(dimensions.
x);
409 for (
size_t i = 0; i < dimensions.
x; ++i)
411 target[i].resize(dimensions.
y);
412 for (
size_t j = 0; j < dimensions.
y; ++j)
414 target[i][j] =
context_->get(vector_[i][j], keep_local).to_integer();
432 if (index.
x < vector_.size() && index.
y < vector_[index.
x].size())
435 context_->get(vector_[index.
x][index.
y], keep_local).to_integer();
451 if (index.
x < vector_.size() && index.
y < vector_[index.
x].size())
453 result =
context_->exists(vector_[index.
x][index.
y]);
469 if (index.
x < vector_.size() && index.
y < vector_[index.
x].size())
471 result =
context_->set(vector_[index.
x][index.
y], value, settings_);
479 const std::vector<std::vector<type> >& value)
487 for (
size_t i = 0; i < value.size() && i < vector_.size(); ++i)
489 for (
size_t j = 0; j < value[i].size() && j < vector_[i].size(); ++j)
491 context_->set(vector_[i][j], value[i][j], settings_);
508 if (index.
x < vector_.size() && index.
y < vector_[index.
x].size())
510 result =
context_->set(vector_[index.
x][index.
y], value, settings);
518 const std::vector<std::vector<type> >& value,
527 for (
size_t i = 0; i < value.size() && i < vector_.size(); ++i)
529 for (
size_t j = 0; j < value[i].size() && j < vector_[i].size(); ++j)
531 context_->set(vector_[i][j], value[i][j], settings);
540 const Indices& index, uint32_t quality,
547 if (index.
x < vector_.size() && index.
y < vector_[index.
x].size())
549 vector_[index.
x][index.
y].get_name(), quality,
true, settings);
558 "IntegerVector2D::is_true: Checking for truth\n");
567 "IntegerVector2D::is_true: context was not null. Result changed to "
571 for (
size_t i = 0; i < vector_.size(); ++i)
573 for (
size_t j = 0; j < vector_[i].size(); ++i)
576 "IntegerVector2D::is_true: checking [%d,%d], is_false of %d. \n",
577 (
int)i, (
int)j, (
int)
context_->get(vector_[i][j]).is_false());
579 if (
context_->get(vector_[i][j]).is_false())
582 "IntegerVector2D::is_true: result is false, breaking\n");
590 if (vector_.size() == 0)
595 "IntegerVector2D::is_true: final result is %d\n", (
int)result);
#define madara_logger_log(loggering, level,...)
Fast version of the madara::logger::log method.
const ThreadSafeContext * context_
A thread-safe guard for a context or knowledge base.
This class provides a distributed knowledge base to users.
This class encapsulates an entry in a KnowledgeBase.
std::vector< Integer > to_integers(void) const
converts the value to a vector of integers
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...
Optimized reference to a variable within the knowledge base.
Provides an interface for external functions into the MADARA KaRL variable settings.
This class is an abstract base class for all containers.
KnowledgeUpdateSettings settings_
Settings for modifications.
std::string name_
Prefix of variable.
MADARA_LOCK_TYPE mutex_
guard for access and changes
Manages a 2D array of integers as a virtual overlay in the KnowledgeBase.
std::string get_delimiter(void)
Gets the delimiter for adding and detecting subvariables.
VariableReference size_
Reference to the size of 2D array.
virtual ~IntegerVector2D()
Destructor.
bool exists(const Indices &index) const
Checks to see if the index has ever been assigned a value.
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 of vector.
virtual bool is_true_(void) const
Polymorphic is true method which can be used to determine if all values in the container are true.
virtual void modify_(void)
Polymorphic modify method used by collection containers.
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
type operator[](const Indices &index) const
Retrieves an index from the multi-dimensional array.
int set(const Indices &index, type value)
Sets a knowledge variable to a specified value.
void copy_to(std::vector< std::vector< type > > &target) const
Copies the vector elements to an STL vector.
KnowledgeRecord::Integer type
convenience typedef for element type
ThreadSafeContext * context_
Variable context that we are modifying.
std::vector< std::vector< VariableReference > > vector_
Values of the array.
void operator=(const IntegerVector2D &rhs)
Assignment operator.
bool is_true(void) const
Determines if all values in the vector are true.
IntegerVector2D(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(), const std::string &delimiter=".")
Default constructor.
virtual BaseContainer * clone(void) const
Clones this container.
void modify(void)
Mark the vector as modified.
void resize(const Dimensions &dimensions, bool delete_vars=true)
Resizes 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 ...
std::string delimiter_
Delimiter for the prefix to subvars.
void set_quality(const Indices &index, uint32_t quality, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Sets the quality of writing to a certain variable from this entity.
Dimensions size(void) const
Returns the size of the local vector.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, const Dimensions &dimensions={0, 0})
Sets the variable name that this refers to.
void set_delimiter(const std::string &delimiter)
Sets the delimiter for adding and detecting subvariables.
Provides functions and classes for the distributed knowledge base.