Functions from <algorithm> reimplemented in libuavcan

This commit is contained in:
Pavel Kirienko
2014-05-04 23:13:38 +04:00
parent cf3d6e2c08
commit 7608e4ca08
19 changed files with 152 additions and 46 deletions
+3 -3
View File
@@ -34,7 +34,7 @@ struct UAVCAN_EXPORT CanFrame
: id(0)
, dlc(0)
{
std::fill(data, data + MaxDataLen, 0);
fill(data, data + MaxDataLen, 0);
}
CanFrame(uint32_t arg_id, const uint8_t* arg_data, uint8_t data_len)
@@ -43,13 +43,13 @@ struct UAVCAN_EXPORT CanFrame
{
assert(arg_data != NULL);
assert(data_len == dlc);
(void)std::copy(arg_data, arg_data + dlc, this->data);
(void)copy(arg_data, arg_data + dlc, this->data);
}
bool operator!=(const CanFrame& rhs) const { return !operator==(rhs); }
bool operator==(const CanFrame& rhs) const
{
return (id == rhs.id) && (dlc == rhs.dlc) && std::equal(data, data + dlc, rhs.data);
return (id == rhs.id) && (dlc == rhs.dlc) && equal(data, data + dlc, rhs.data);
}
bool isExtended() const { return id & FlagEFF; }
@@ -45,7 +45,7 @@ public:
StatusCode result = 0;
for (unsigned i = 0; i < NumComponents; i++)
{
result = std::max(result, status_array[i]);
result = max(result, status_array[i]);
}
return result;
}
+5 -5
View File
@@ -148,7 +148,7 @@ private:
{
if (ArrayMode != ArrayModeDynamic)
{
std::fill(data_, data_ + MaxSize, U());
fill(data_, data_ + MaxSize, U());
}
}
template <typename> void initialize(...) { }
@@ -192,7 +192,7 @@ public:
template <typename R>
bool operator<(const R& rhs) const
{
return std::lexicographical_compare(begin(), end(), rhs.begin(), rhs.end());
return ::uavcan::lexicographical_compare(begin(), end(), rhs.begin(), rhs.end());
}
typedef ValueType* iterator;
@@ -437,11 +437,11 @@ class UAVCAN_EXPORT Array : public ArrayImpl<T, ArrayMode, MaxSize_>
}
else if (this->size() == MaxSize)
{
(void)std::copy(this->begin(), this->end(), it);
(void)::uavcan::copy(this->begin(), this->end(), it);
}
else
{
std::fill_n(it, MaxSize, 0);
::uavcan::fill_n(it, MaxSize, 0);
}
}
@@ -628,7 +628,7 @@ public:
using namespace std; // For snprintf()
const int ret = snprintf(reinterpret_cast<char*>(ptr), max_size + 1, format, value);
for (int i = 0; i < std::min(ret, int(max_size)); i++)
for (int i = 0; i < min(ret, int(max_size)); i++)
{
Base::grow();
}
+4 -4
View File
@@ -99,16 +99,16 @@ public:
MonotonicDuration getDeadlineResolution() const { return deadline_resolution_; }
void setDeadlineResolution(MonotonicDuration res)
{
res = std::min(res, MonotonicDuration::fromMSec(MaxDeadlineResolutionMs));
res = std::max(res, MonotonicDuration::fromMSec(MinDeadlineResolutionMs));
res = min(res, MonotonicDuration::fromMSec(MaxDeadlineResolutionMs));
res = max(res, MonotonicDuration::fromMSec(MinDeadlineResolutionMs));
deadline_resolution_ = res;
}
MonotonicDuration getCleanupPeriod() const { return cleanup_period_; }
void setCleanupPeriod(MonotonicDuration period)
{
period = std::min(period, MonotonicDuration::fromMSec(MaxCleanupPeriodMs));
period = std::max(period, MonotonicDuration::fromMSec(MinCleanupPeriodMs));
period = min(period, MonotonicDuration::fromMSec(MaxCleanupPeriodMs));
period = max(period, MonotonicDuration::fromMSec(MinCleanupPeriodMs));
cleanup_period_ = period;
}
};
@@ -168,11 +168,11 @@ public:
MonotonicDuration getRequestTimeout() const { return request_timeout_; }
void setRequestTimeout(MonotonicDuration timeout)
{
timeout = std::max(timeout, getMinRequestTimeout());
timeout = std::min(timeout, getMaxRequestTimeout());
timeout = max(timeout, getMinRequestTimeout());
timeout = min(timeout, getMaxRequestTimeout());
publisher_.setTxTimeout(timeout);
request_timeout_ = std::max(timeout, publisher_.getTxTimeout()); // No less than TX timeout
request_timeout_ = max(timeout, publisher_.getTxTimeout()); // No less than TX timeout
}
};
+3 -3
View File
@@ -42,7 +42,7 @@ public:
int64_t toUSec() const { return usec_; }
int64_t toMSec() const { return usec_ / 1000; }
D getAbs() const { return D::fromUSec(std::abs(usec_)); }
D getAbs() const { return D::fromUSec((usec_ < 0) ? (-usec_) : usec_); }
bool isPositive() const { return usec_ > 0; }
bool isNegative() const { return usec_ < 0; }
@@ -229,8 +229,8 @@ void DurationBase<D>::toString(char buf[StringBufSize]) const
*ptr++ = '-';
}
(void)snprintf(ptr, StringBufSize - 1, "%llu.%06lu",
static_cast<unsigned long long>(std::abs(toUSec() / 1000000L)),
static_cast<unsigned long>(std::abs(toUSec() % 1000000L)));
static_cast<unsigned long long>(getAbs().toUSec() / 1000000L),
static_cast<unsigned long>(getAbs().toUSec() % 1000000L));
}
@@ -136,4 +136,109 @@ template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<49> { enum { Result = 7 }; }
template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<64> { enum { Result = 8 }; };
template <> struct UAVCAN_EXPORT CompileTimeIntSqrt<81> { enum { Result = 9 }; };
/**
* Replacement for std::copy(..)
*/
template <typename InputIt, typename OutputIt>
UAVCAN_EXPORT
OutputIt copy(InputIt first, InputIt last, OutputIt result)
{
while (first != last)
{
*result = *first;
++first;
++result;
}
return result;
}
/**
* Replacement for std::fill(..)
*/
template <typename ForwardIt, typename T>
UAVCAN_EXPORT
void fill(ForwardIt first, ForwardIt last, const T& value)
{
while (first != last)
{
*first = value;
++first;
}
}
/**
* Replacement for std::fill_n(..)
*/
template<typename OutputIt, typename T>
UAVCAN_EXPORT
void fill_n(OutputIt first, std::size_t n, const T& value)
{
while (n--)
{
*first++ = value;
}
}
/**
* Replacement for std::min(..)
*/
template <typename T>
UAVCAN_EXPORT
const T& min(const T& a, const T& b)
{
return (b < a) ? b : a;
}
/**
* Replacement for std::max(..)
*/
template <typename T>
UAVCAN_EXPORT
const T& max(const T& a, const T& b)
{
return (a < b) ? b : a;
}
/**
* Replacement for std::lexicographical_compare(..)
*/
template<class InputIt1, class InputIt2>
UAVCAN_EXPORT
bool lexicographical_compare(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
{
while ((first1 != last1) && (first2 != last2))
{
if (*first1 < *first2)
{
return true;
}
if (*first2 < *first1)
{
return false;
}
++first1;
++first2;
}
return (first1 == last1) && (first2 != last2);
}
/**
* Replacement for std::equal(..)
*/
template<class InputIt1, class InputIt2>
UAVCAN_EXPORT
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2)
{
while (first1 != last1)
{
if (*first1 != *first2)
{
return false;
}
++first1;
++first2;
}
return true;
}
}
@@ -7,6 +7,7 @@
#include <cstdlib>
#include <uavcan/error.hpp>
#include <uavcan/impl_constants.hpp>
#include <uavcan/util/compile_time.hpp>
#ifndef UAVCAN_CPP_VERSION
# error UAVCAN_CPP_VERSION
@@ -53,13 +54,13 @@ public:
LazyConstructor()
: ptr_(NULL)
{
std::fill(data_.pool, data_.pool + sizeof(T), 0);
fill(data_.pool, data_.pool + sizeof(T), 0);
}
LazyConstructor(const LazyConstructor<T>& rhs) // Implicit
: ptr_(NULL)
{
std::fill(data_.pool, data_.pool + sizeof(T), 0);
fill(data_.pool, data_.pool + sizeof(T), 0);
if (rhs)
{
construct<const T&>(*rhs); // Invoke copy constructor
@@ -95,7 +96,7 @@ public:
ptr_->~T();
}
ptr_ = NULL;
std::fill(data_.pool, data_.pool + sizeof(T), 0);
fill(data_.pool, data_.pool + sizeof(T), 0);
}
void construct()