Implement union() and intersection() functions.

Fixes #5801
This commit is contained in:
Gunnar Beutner 2014-03-20 14:25:40 +01:00
parent 8e472211cb
commit 012c0caeec
7 changed files with 76 additions and 15 deletions

View File

@ -190,14 +190,16 @@ Functions can be called using the `()` operator:
check_interval = len(MyGroups) * 1m
}
Function | Description
---------------------|-----------------------
regex(pattern, text) | Returns true if the regex pattern matches the text, false otherwise.
match(pattern, text) | Returns true if the wildcard pattern matches the text, false otherwise.
len(value) | Returns the length of the value, i.e. the number of elements for an array or dictionary, or the length of the string in bytes.
string(value) | Converts the value to a string.
number(value) | Converts the value to a number.
bool(value) | Converts to value to a bool.
Function | Description
--------------------------------|-----------------------
regex(pattern, text) | Returns true if the regex pattern matches the text, false otherwise.
match(pattern, text) | Returns true if the wildcard pattern matches the text, false otherwise.
len(value) | Returns the length of the value, i.e. the number of elements for an array or dictionary, or the length of the string in bytes.
union(array, array, ...) | Returns an array containing all unique elements from the specified arrays.
intersection(array, array, ...) | Returns an array containing all unique elements which are common to all specified arrays.
string(value) | Converts the value to a string.
number(value) | Converts the value to a number.
bool(value) | Converts to value to a bool.
### <a id="operators"></a> Dictionary Operators

View File

@ -149,6 +149,14 @@ void Array::Remove(Array::Iterator it)
m_Data.erase(it);
}
void Array::Resize(size_t new_size)
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
m_Data.resize(new_size);
}
void Array::CopyTo(const Array::Ptr& dest) const
{
ASSERT(!OwnsLock());

View File

@ -55,6 +55,8 @@ public:
void Remove(unsigned int index);
void Remove(Iterator it);
void Resize(size_t new_size);
void CopyTo(const Array::Ptr& dest) const;
Array::Ptr ShallowClone(void) const;

View File

@ -32,8 +32,3 @@ boost::function<Value (const std::vector<Value>& arguments)> icinga::WrapScriptF
{
return boost::bind(&ScriptFunctionWrapperVV, function, _1);
}
boost::function<Value (const std::vector<Value>& arguments)> icinga::WrapScriptFunction(Value (*function)(const std::vector<Value>&))
{
return boost::bind(function, _1);
}

View File

@ -267,7 +267,11 @@ boost::function<Value (const std::vector<Value>& arguments)> WrapScriptFunction(
return boost::bind(&ScriptFunctionWrapperR<TR, T0, T1, T2, T3, T4, T5>, function, _1);
}
boost::function<Value (const std::vector<Value>& arguments)> I2_BASE_API WrapScriptFunction(Value (*function)(const std::vector<Value>&));
template<typename TR>
boost::function<TR (const std::vector<Value>& arguments)> WrapScriptFunction(TR (*function)(const std::vector<Value>&))
{
return boost::bind(function, _1);
}
}

View File

@ -24,12 +24,15 @@
#include "base/array.h"
#include "base/dictionary.h"
#include <boost/regex.hpp>
#include <algorithm>
using namespace icinga;
REGISTER_SCRIPTFUNCTION(regex, &UtilityFuncs::Regex);
REGISTER_SCRIPTFUNCTION(match, &Utility::Match);
REGISTER_SCRIPTFUNCTION(len, &UtilityFuncs::Len);
REGISTER_SCRIPTFUNCTION(union, &UtilityFuncs::Union);
REGISTER_SCRIPTFUNCTION(intersection, &UtilityFuncs::Intersection);
bool UtilityFuncs::Regex(const String& pattern, const String& text)
{
@ -49,4 +52,48 @@ int UtilityFuncs::Len(const Value& value)
} else {
return Convert::ToString(value).GetLength();
}
}
}
Array::Ptr UtilityFuncs::Union(const std::vector<Value>& arguments)
{
std::set<Value> values;
BOOST_FOREACH(const Value& varr, arguments) {
Array::Ptr arr = varr;
BOOST_FOREACH(const Value& value, arr) {
values.insert(value);
}
}
Array::Ptr result = make_shared<Array>();
BOOST_FOREACH(const Value& value, values) {
result->Add(value);
}
return result;
}
Array::Ptr UtilityFuncs::Intersection(const std::vector<Value>& arguments)
{
if (arguments.size() == 0)
return make_shared<Array>();
Array::Ptr result = make_shared<Array>();
Array::Ptr arr1 = static_cast<Array::Ptr>(arguments[0])->ShallowClone();
for (int i = 1; i < arguments.size(); i++) {
std::sort(arr1->Begin(), arr1->End());
Array::Ptr arr2 = static_cast<Array::Ptr>(arguments[i])->ShallowClone();
std::sort(arr2->Begin(), arr2->End());
result->Resize(std::max(arr1->GetLength(), arr2->GetLength()));
Array::Iterator it = std::set_intersection(arr1->Begin(), arr1->End(), arr2->Begin(), arr2->End(), result->Begin());
result->Resize(it - result->Begin());
arr1 = result;
}
return result;
}

View File

@ -22,6 +22,7 @@
#include "methods/i2-methods.h"
#include "base/qstring.h"
#include "base/array.h"
namespace icinga
{
@ -34,6 +35,8 @@ class I2_METHODS_API UtilityFuncs
public:
static bool Regex(const String& pattern, const String& text);
static int Len(const Value& value);
static Array::Ptr Union(const std::vector<Value>& arguments);
static Array::Ptr Intersection(const std::vector<Value>& arguments);
private:
UtilityFuncs(void);