Today's the day. Renamed platform to framework.
This commit is contained in:
41
globalfunctions/matching/BinaryMatcher.h
Normal file
41
globalfunctions/matching/BinaryMatcher.h
Normal file
@ -0,0 +1,41 @@
|
||||
#ifndef BINARYMATCHER_H_
|
||||
#define BINARYMATCHER_H_
|
||||
|
||||
#include <framework/globalfunctions/matching/MatcherIF.h>
|
||||
|
||||
template<typename T>
|
||||
class BinaryMatcher: public MatcherIF<T> {
|
||||
public:
|
||||
bool inverted;
|
||||
T mask, matchField;
|
||||
|
||||
BinaryMatcher() :
|
||||
inverted(false), mask(0), matchField(0) {
|
||||
}
|
||||
|
||||
BinaryMatcher(T mask, T match, bool inverted = false) :
|
||||
inverted(inverted), mask(mask), matchField(match) {
|
||||
}
|
||||
|
||||
bool match(T input) {
|
||||
if (inverted) {
|
||||
return ~doMatch(input, mask, matchField);
|
||||
} else {
|
||||
return doMatch(input, mask, matchField);
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
bool doMatch(T input, T mask, T match) {
|
||||
match = match & mask;
|
||||
input = input & mask;
|
||||
if (input == match) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* BINARYMATCHER_H_ */
|
50
globalfunctions/matching/DecimalMatcher.h
Normal file
50
globalfunctions/matching/DecimalMatcher.h
Normal file
@ -0,0 +1,50 @@
|
||||
#ifndef DECIMALMATCHER_H_
|
||||
#define DECIMALMATCHER_H_
|
||||
|
||||
#include <framework/globalfunctions/matching/MatcherIF.h>
|
||||
|
||||
template<typename T>
|
||||
class DecimalMatcher: public MatcherIF<T> {
|
||||
public:
|
||||
bool inverted;
|
||||
T mask, matchField;
|
||||
|
||||
DecimalMatcher() :
|
||||
inverted(false), mask(0), matchField(0) {
|
||||
}
|
||||
|
||||
DecimalMatcher(T mask, T match, bool inverted = false) :
|
||||
inverted(inverted), mask(mask), matchField(match) {
|
||||
}
|
||||
|
||||
bool match(T input) {
|
||||
if (inverted) {
|
||||
return ~doMatch(input, mask, matchField);
|
||||
} else {
|
||||
return doMatch(input, mask, matchField);
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
bool doMatch(T input, T mask, T match) {
|
||||
T decimal = 1, remainderMask, remainderMatch, remainderInput;
|
||||
|
||||
while (mask != 0) {
|
||||
remainderMask = mask % (decimal * 10);
|
||||
remainderMatch = match % (decimal * 10);
|
||||
remainderInput = input % (decimal * 10);
|
||||
if (remainderMask != 0) {
|
||||
if (remainderMatch != remainderInput) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
mask -= remainderMask;
|
||||
match -= remainderMatch;
|
||||
input -= remainderInput;
|
||||
decimal *= 10;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* DECIMALMATCHER_H_ */
|
208
globalfunctions/matching/MatchTree.h
Normal file
208
globalfunctions/matching/MatchTree.h
Normal file
@ -0,0 +1,208 @@
|
||||
/*
|
||||
* MatchTree.h
|
||||
*
|
||||
* Created on: 09.03.2015
|
||||
* Author: baetz
|
||||
*/
|
||||
|
||||
#ifndef FRAMEWORK_GLOBALFUNCTIONS_MATCHING_MATCHTREE_H_
|
||||
#define FRAMEWORK_GLOBALFUNCTIONS_MATCHING_MATCHTREE_H_
|
||||
|
||||
#include <framework/container/BinaryTree.h>
|
||||
#include <framework/globalfunctions/matching/SerializeableMatcherIF.h>
|
||||
#include <framework/serialize/SerializeAdapter.h>
|
||||
|
||||
template<typename T>
|
||||
class MatchTree: public SerializeableMatcherIF<T>, public BinaryTree<
|
||||
SerializeableMatcherIF<T>> {
|
||||
public:
|
||||
|
||||
static const uint8_t INTERFACE_ID = MATCH_TREE_CLASS;
|
||||
static const ReturnValue_t TOO_DETAILED_REQUEST = MAKE_RETURN_CODE(1);
|
||||
static const ReturnValue_t TOO_GENERAL_REQUEST = MAKE_RETURN_CODE(2);
|
||||
static const ReturnValue_t NO_MATCH = MAKE_RETURN_CODE(3);
|
||||
static const ReturnValue_t FULL = MAKE_RETURN_CODE(4);
|
||||
|
||||
typedef typename BinaryTree<SerializeableMatcherIF<T>>::iterator iterator;
|
||||
typedef BinaryNode<SerializeableMatcherIF<T>> Node;
|
||||
static const bool AND = true; //LEFT
|
||||
static const bool OR = false; //RIGHT
|
||||
MatchTree(BinaryNode<SerializeableMatcherIF<T>>* root,
|
||||
uint8_t maxDepth = -1) :
|
||||
BinaryTree<SerializeableMatcherIF<T>>(root), maxDepth(maxDepth) {
|
||||
}
|
||||
MatchTree(iterator root, uint8_t maxDepth = -1) :
|
||||
BinaryTree<SerializeableMatcherIF<T>>(root.element), maxDepth(
|
||||
maxDepth) {
|
||||
}
|
||||
MatchTree() :
|
||||
BinaryTree<SerializeableMatcherIF<T>>(), maxDepth(-1) {
|
||||
}
|
||||
virtual ~MatchTree() {
|
||||
}
|
||||
virtual bool match(T number) {
|
||||
return matchesTree(number, NULL, NULL);
|
||||
}
|
||||
bool matchesTree(T number, iterator* lastTest, uint8_t* hierarchyLevel) {
|
||||
bool match = false;
|
||||
iterator iter = this->begin();
|
||||
while (iter != this->end()) {
|
||||
if (lastTest != NULL) {
|
||||
*lastTest = iter;
|
||||
}
|
||||
match = iter->match(number);
|
||||
if (match) {
|
||||
iter = iter.left();
|
||||
if (hierarchyLevel != NULL) {
|
||||
(*hierarchyLevel)++;
|
||||
}
|
||||
} else {
|
||||
iter = iter.right();
|
||||
}
|
||||
}
|
||||
return match;
|
||||
}
|
||||
|
||||
ReturnValue_t serialize(uint8_t** buffer, uint32_t* size,
|
||||
const uint32_t max_size, bool bigEndian) const {
|
||||
iterator iter = this->begin();
|
||||
uint8_t count = this->countRight(iter);
|
||||
ReturnValue_t result = SerializeAdapter<uint8_t>::serialize(&count,
|
||||
buffer, size, max_size, bigEndian);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
if (iter == this->end()) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
result = iter->serialize(buffer, size, max_size, bigEndian);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
if (maxDepth > 0) {
|
||||
MatchTree<T> temp(iter.left(), maxDepth - 1);
|
||||
result = temp.serialize(buffer, size, max_size, bigEndian);
|
||||
}
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
iter = iter.right();
|
||||
while (iter != this->end()) {
|
||||
result = iter->serialize(buffer, size, max_size, bigEndian);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
if (maxDepth > 0) {
|
||||
MatchTree<T> temp(iter.left(), maxDepth - 1);
|
||||
result = temp.serialize(buffer, size, max_size, bigEndian);
|
||||
}
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
iter = iter.right();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
uint32_t getSerializedSize() const {
|
||||
//Analogous to serialize!
|
||||
uint32_t size = 1; //One for count
|
||||
iterator iter = this->begin();
|
||||
if (iter == this->end()) {
|
||||
return size;
|
||||
}
|
||||
//Count object itself
|
||||
size += iter->getSerializedSize();
|
||||
//Handle everything below on AND side
|
||||
if (maxDepth > 0) {
|
||||
MatchTree<T> temp(iter.left(), maxDepth - 1);
|
||||
size += temp.getSerializedSize();
|
||||
}
|
||||
//Handle everything on OR side
|
||||
iter = iter.right();
|
||||
//Iterate over every object on the OR branch
|
||||
while (iter != this->end()) {
|
||||
size += iter->getSerializedSize();
|
||||
if (maxDepth > 0) {
|
||||
//If we are allowed to go deeper, handle AND elements.
|
||||
MatchTree<T> temp(iter.left(), maxDepth - 1);
|
||||
size += temp.getSerializedSize();
|
||||
}
|
||||
iter = iter.right();
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
ReturnValue_t deSerialize(const uint8_t** buffer, int32_t* size,
|
||||
bool bigEndian) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
bool isOnAndBranch(iterator position) {
|
||||
if ((position == this->end()) || (position.up() == this->end())) {
|
||||
return false;
|
||||
}
|
||||
if (position.up().left() == position) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: What to do if insertion/deletion fails. Throw event?
|
||||
ReturnValue_t removeElementAndAllChildren(iterator position) {
|
||||
auto children = erase(position);
|
||||
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
|
||||
if (children.first != this->end()) {
|
||||
result = removeElementAndAllChildren(children.first);
|
||||
}
|
||||
if (children.second != this->end()) {
|
||||
result = removeElementAndAllChildren(children.second);
|
||||
}
|
||||
//Delete element itself.
|
||||
return cleanUpElement(position);
|
||||
}
|
||||
|
||||
ReturnValue_t removeElementAndReconnectChildren(iterator position) {
|
||||
if (position == this->end()) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
//Delete everything from the AND branch.
|
||||
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
|
||||
if (position.left() != this->end()) {
|
||||
result = removeElementAndAllChildren(position.left());
|
||||
}
|
||||
if (position.right() != this->end()) {
|
||||
//There's something at the OR branch, reconnect to parent.
|
||||
if (isOnAndBranch(position)) {
|
||||
//Either one hierarchy up AND branch...
|
||||
insert(AND, position.up(), position.right().element);
|
||||
} else {
|
||||
//or on another OR'ed element (or install new root node).
|
||||
insert(OR, position.up(), position.right().element);
|
||||
}
|
||||
} else {
|
||||
if (isOnAndBranch(position)) {
|
||||
//Recursively delete parent node as well, because it is not expected to be there anymore.
|
||||
return removeElementAndReconnectChildren(position.up());
|
||||
} else {
|
||||
//simply delete self.
|
||||
erase(position);
|
||||
}
|
||||
|
||||
}
|
||||
//Delete element itself.
|
||||
return cleanUpElement(position);
|
||||
}
|
||||
|
||||
virtual ReturnValue_t cleanUpElement(iterator position) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
private:
|
||||
uint8_t maxDepth;
|
||||
};
|
||||
|
||||
#endif /* FRAMEWORK_GLOBALFUNCTIONS_MATCHING_MATCHTREE_H_ */
|
16
globalfunctions/matching/MatcherIF.h
Normal file
16
globalfunctions/matching/MatcherIF.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef MATCHERIF_H_
|
||||
#define MATCHERIF_H_
|
||||
|
||||
template<typename T>
|
||||
class MatcherIF {
|
||||
public:
|
||||
virtual ~MatcherIF() {
|
||||
}
|
||||
|
||||
virtual bool match(T number) {
|
||||
return false;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif /* MATCHERIF_H_ */
|
65
globalfunctions/matching/RangeMatcher.h
Normal file
65
globalfunctions/matching/RangeMatcher.h
Normal file
@ -0,0 +1,65 @@
|
||||
#ifndef RANGEMATCHER_H_
|
||||
#define RANGEMATCHER_H_
|
||||
|
||||
#include <framework/globalfunctions/matching/SerializeableMatcherIF.h>
|
||||
#include <framework/serialize/SerializeAdapter.h>
|
||||
|
||||
|
||||
template<typename T>
|
||||
class RangeMatcher: public SerializeableMatcherIF<T> {
|
||||
public:
|
||||
bool inverted;
|
||||
T lowerBound;
|
||||
T upperBound;
|
||||
|
||||
RangeMatcher() :
|
||||
inverted(true), lowerBound(1), upperBound(0) {
|
||||
}
|
||||
RangeMatcher(T lowerBound, T upperBound, bool inverted = false) :
|
||||
inverted(inverted), lowerBound(lowerBound), upperBound(upperBound) {
|
||||
}
|
||||
|
||||
bool match(T input) {
|
||||
if (inverted) {
|
||||
return ~doMatch(input);
|
||||
} else {
|
||||
return doMatch(input);
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t serialize(uint8_t** buffer, uint32_t* size,
|
||||
const uint32_t max_size, bool bigEndian) const {
|
||||
ReturnValue_t result = SerializeAdapter<T>::serialize(&lowerBound, buffer, size, max_size, bigEndian);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
result = SerializeAdapter<T>::serialize(&upperBound, buffer, size, max_size, bigEndian);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
return SerializeAdapter<bool>::serialize(&inverted, buffer, size, max_size, bigEndian);
|
||||
}
|
||||
|
||||
uint32_t getSerializedSize() const {
|
||||
return sizeof(lowerBound) + sizeof(upperBound) + sizeof(bool);
|
||||
}
|
||||
|
||||
ReturnValue_t deSerialize(const uint8_t** buffer, int32_t* size,
|
||||
bool bigEndian) {
|
||||
ReturnValue_t result = SerializeAdapter<T>::deSerialize(&lowerBound, buffer, size, bigEndian);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
result = SerializeAdapter<T>::deSerialize(&upperBound, buffer, size, bigEndian);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
return SerializeAdapter<bool>::deSerialize(&inverted, buffer, size, bigEndian);
|
||||
}
|
||||
protected:
|
||||
bool doMatch(T input) {
|
||||
return (input >= lowerBound) && (input <= upperBound);
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* RANGEMATCHER_H_ */
|
20
globalfunctions/matching/SerializeableMatcherIF.h
Normal file
20
globalfunctions/matching/SerializeableMatcherIF.h
Normal file
@ -0,0 +1,20 @@
|
||||
/*
|
||||
* SerializeableMatcherIF.h
|
||||
*
|
||||
* Created on: 09.03.2015
|
||||
* Author: baetz
|
||||
*/
|
||||
|
||||
#ifndef FRAMEWORK_GLOBALFUNCTIONS_MATCHING_SERIALIZEABLEMATCHERIF_H_
|
||||
#define FRAMEWORK_GLOBALFUNCTIONS_MATCHING_SERIALIZEABLEMATCHERIF_H_
|
||||
|
||||
#include <framework/globalfunctions/matching/MatcherIF.h>
|
||||
#include <framework/serialize/SerializeIF.h>
|
||||
|
||||
template<typename T>
|
||||
class SerializeableMatcherIF : public MatcherIF<T>, public SerializeIF {
|
||||
public:
|
||||
virtual ~SerializeableMatcherIF() {}
|
||||
};
|
||||
|
||||
#endif /* FRAMEWORK_GLOBALFUNCTIONS_MATCHING_SERIALIZEABLEMATCHERIF_H_ */
|
Reference in New Issue
Block a user