mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-11 23:43:35 +08:00
Functions from <algorithm> reimplemented in libuavcan
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -68,7 +68,7 @@ std::string CanFrame::toString(StringRepresentation mode) const
|
||||
char buf[50];
|
||||
char* wpos = buf;
|
||||
char* const epos = buf + sizeof(buf);
|
||||
std::fill(buf, buf + sizeof(buf), 0);
|
||||
fill(buf, buf + sizeof(buf), 0);
|
||||
|
||||
if (id & FlagEFF)
|
||||
{
|
||||
|
||||
@@ -22,7 +22,7 @@ int BitStream::write(const uint8_t* bytes, const int bitlen)
|
||||
assert(MaxBytesPerRW >= bytelen);
|
||||
tmp[0] = tmp[bytelen - 1] = 0;
|
||||
|
||||
std::fill(tmp, tmp + bytelen, 0);
|
||||
fill(tmp, tmp + bytelen, 0);
|
||||
copyBitArray(bytes, 0, bitlen, tmp, bit_offset_ % 8);
|
||||
|
||||
const int new_bit_offset = bit_offset_ + bitlen;
|
||||
@@ -69,7 +69,7 @@ int BitStream::read(uint8_t* bytes, const int bitlen)
|
||||
return ResultOutOfBuffer;
|
||||
}
|
||||
|
||||
std::fill(bytes, bytes + bitlenToBytelen(bitlen), 0);
|
||||
fill(bytes, bytes + bitlenToBytelen(bitlen), 0);
|
||||
copyBitArray(tmp, bit_offset_ % 8, bitlen, bytes, 0);
|
||||
bit_offset_ += bitlen;
|
||||
return ResultOk;
|
||||
|
||||
@@ -83,7 +83,7 @@ uint16_t IEEE754Converter::nativeNonIeeeToHalf(float value)
|
||||
}
|
||||
const int32_t ival = static_cast<int32_t>(value);
|
||||
hbits |= static_cast<uint16_t>(((ival < 0) ? (-ival) : ival) & 0x3FFU);
|
||||
float diff = std::abs(value - static_cast<float>(ival));
|
||||
float diff = std::fabs(value - static_cast<float>(ival));
|
||||
hbits += diff >= 0.5F;
|
||||
return hbits;
|
||||
}
|
||||
|
||||
@@ -64,8 +64,8 @@ TransferSender* GenericPublisherBase::getTransferSender()
|
||||
|
||||
void GenericPublisherBase::setTxTimeout(MonotonicDuration tx_timeout)
|
||||
{
|
||||
tx_timeout = std::max(tx_timeout, getMinTxTimeout());
|
||||
tx_timeout = std::min(tx_timeout, getMaxTxTimeout());
|
||||
tx_timeout = max(tx_timeout, getMinTxTimeout());
|
||||
tx_timeout = min(tx_timeout, getMaxTxTimeout());
|
||||
tx_timeout_ = tx_timeout;
|
||||
}
|
||||
|
||||
|
||||
@@ -128,7 +128,7 @@ MonotonicTime DeadlineScheduler::getEarliestDeadline() const
|
||||
*/
|
||||
MonotonicTime Scheduler::computeDispatcherSpinDeadline(MonotonicTime spin_deadline) const
|
||||
{
|
||||
const MonotonicTime earliest = std::min(deadline_scheduler_.getEarliestDeadline(), spin_deadline);
|
||||
const MonotonicTime earliest = min(deadline_scheduler_.getEarliestDeadline(), spin_deadline);
|
||||
const MonotonicTime ts = getMonotonicTime();
|
||||
if (earliest > ts)
|
||||
{
|
||||
|
||||
@@ -40,8 +40,8 @@ int Frame::setPayload(const uint8_t* data, unsigned len)
|
||||
{
|
||||
return maxlen;
|
||||
}
|
||||
len = std::min(unsigned(maxlen), len);
|
||||
(void)std::copy(data, data + len, payload_);
|
||||
len = min(unsigned(maxlen), len);
|
||||
(void)copy(data, data + len, payload_);
|
||||
payload_len_ = len;
|
||||
return len;
|
||||
}
|
||||
@@ -85,7 +85,7 @@ bool Frame::parse(const CanFrame& can_frame)
|
||||
{
|
||||
dst_node_id_ = NodeID::Broadcast;
|
||||
payload_len_ = can_frame.dlc;
|
||||
(void)std::copy(can_frame.data, can_frame.data + can_frame.dlc, payload_);
|
||||
(void)copy(can_frame.data, can_frame.data + can_frame.dlc, payload_);
|
||||
break;
|
||||
}
|
||||
case TransferTypeServiceResponse:
|
||||
@@ -102,7 +102,7 @@ bool Frame::parse(const CanFrame& can_frame)
|
||||
}
|
||||
dst_node_id_ = can_frame.data[0] & 0x7F;
|
||||
payload_len_ = can_frame.dlc - 1;
|
||||
(void)std::copy(can_frame.data + 1, can_frame.data + can_frame.dlc, payload_);
|
||||
(void)copy(can_frame.data + 1, can_frame.data + can_frame.dlc, payload_);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@@ -142,7 +142,7 @@ bool Frame::compile(CanFrame& out_can_frame) const
|
||||
case TransferTypeMessageBroadcast:
|
||||
{
|
||||
out_can_frame.dlc = payload_len_;
|
||||
(void)std::copy(payload_, payload_ + payload_len_, out_can_frame.data);
|
||||
(void)copy(payload_, payload_ + payload_len_, out_can_frame.data);
|
||||
break;
|
||||
}
|
||||
case TransferTypeServiceResponse:
|
||||
@@ -152,7 +152,7 @@ bool Frame::compile(CanFrame& out_can_frame) const
|
||||
assert((payload_len_ + 1) <= sizeof(out_can_frame.data));
|
||||
out_can_frame.data[0] = dst_node_id_.get();
|
||||
out_can_frame.dlc = payload_len_ + 1;
|
||||
(void)std::copy(payload_, payload_ + payload_len_, out_can_frame.data + 1);
|
||||
(void)copy(payload_, payload_ + payload_len_, out_can_frame.data + 1);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@@ -192,7 +192,7 @@ bool Frame::operator==(const Frame& rhs) const
|
||||
(transfer_id_ == rhs.transfer_id_) &&
|
||||
(last_frame_ == rhs.last_frame_) &&
|
||||
(payload_len_ == rhs.payload_len_) &&
|
||||
std::equal(payload_, payload_ + payload_len_, rhs.payload_);
|
||||
equal(payload_, payload_ + payload_len_, rhs.payload_);
|
||||
}
|
||||
|
||||
#if UAVCAN_TOSTRING
|
||||
|
||||
@@ -216,7 +216,7 @@ int DynamicTransferBufferManagerEntry::write(unsigned offset, const uint8_t* dat
|
||||
}
|
||||
|
||||
const int actually_written = len - left_to_write;
|
||||
max_write_pos_ = std::max(offset + actually_written, unsigned(max_write_pos_));
|
||||
max_write_pos_ = max(offset + actually_written, unsigned(max_write_pos_));
|
||||
return actually_written;
|
||||
}
|
||||
|
||||
@@ -239,7 +239,7 @@ int StaticTransferBufferImpl::read(unsigned offset, uint8_t* data, unsigned len)
|
||||
len = max_write_pos_ - offset;
|
||||
}
|
||||
assert((offset + len) <= max_write_pos_);
|
||||
(void)std::copy(data_ + offset, data_ + offset + len, data);
|
||||
(void)copy(data_ + offset, data_ + offset + len, data);
|
||||
return len;
|
||||
}
|
||||
|
||||
@@ -259,8 +259,8 @@ int StaticTransferBufferImpl::write(unsigned offset, const uint8_t* data, unsign
|
||||
len = size_ - offset;
|
||||
}
|
||||
assert((offset + len) <= size_);
|
||||
(void)std::copy(data, data + len, data_ + offset);
|
||||
max_write_pos_ = std::max(offset + len, unsigned(max_write_pos_));
|
||||
(void)copy(data, data + len, data_ + offset);
|
||||
max_write_pos_ = max(offset + len, unsigned(max_write_pos_));
|
||||
return len;
|
||||
}
|
||||
|
||||
@@ -268,7 +268,7 @@ void StaticTransferBufferImpl::reset()
|
||||
{
|
||||
max_write_pos_ = 0;
|
||||
#if UAVCAN_DEBUG
|
||||
std::fill(data_, data_ + size_, 0);
|
||||
fill(data_, data_ + size_, 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ int SingleFrameIncomingTransfer::read(unsigned offset, uint8_t* data, unsigned l
|
||||
len = payload_len_ - offset;
|
||||
}
|
||||
assert((offset + len) <= payload_len_);
|
||||
(void)std::copy(payload_ + offset, payload_ + offset + len, data);
|
||||
(void)copy(payload_ + offset, payload_ + offset + len, data);
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
@@ -52,8 +52,8 @@ void TransferReceiver::updateTransferTimings()
|
||||
if ((!prev_prev_ts.isZero()) && (!prev_transfer_ts_.isZero()) && (prev_transfer_ts_ >= prev_prev_ts))
|
||||
{
|
||||
uint64_t interval_usec = (prev_transfer_ts_ - prev_prev_ts).toUSec();
|
||||
interval_usec = std::min(interval_usec, uint64_t(MaxTransferIntervalUSec));
|
||||
interval_usec = std::max(interval_usec, uint64_t(MinTransferIntervalUSec));
|
||||
interval_usec = min(interval_usec, uint64_t(MaxTransferIntervalUSec));
|
||||
interval_usec = max(interval_usec, uint64_t(MinTransferIntervalUSec));
|
||||
transfer_interval_usec_ = static_cast<uint32_t>((uint64_t(transfer_interval_usec_) * 7 + interval_usec) / 8);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ int TransferSender::send(const uint8_t* payload, int payload_len, MonotonicTime
|
||||
|
||||
buf[0] = crc.get() & 0xFF; // Transfer CRC, little endian
|
||||
buf[1] = (crc.get() >> 8) & 0xFF;
|
||||
(void)std::copy(payload, payload + BUFLEN - 2, buf + 2);
|
||||
(void)copy(payload, payload + BUFLEN - 2, buf + 2);
|
||||
|
||||
const int write_res = frame.setPayload(buf, BUFLEN);
|
||||
if (write_res < 2)
|
||||
|
||||
@@ -45,7 +45,7 @@ std::size_t LimitedPoolAllocator::getBlockSize() const
|
||||
|
||||
std::size_t LimitedPoolAllocator::getNumBlocks() const
|
||||
{
|
||||
return std::min(max_blocks_, allocator_.getNumBlocks());
|
||||
return min(max_blocks_, allocator_.getNumBlocks());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user