u/historybuf: Tidy code and work around clang-format CI troubles

This commit is contained in:
Jakob Bornecrantz 2021-09-20 10:54:04 +01:00 committed by Moses Turner
parent a10a4ab123
commit d86adce39d
2 changed files with 47 additions and 50 deletions

View file

@ -54,8 +54,6 @@ m_relation_history_create(struct m_relation_history **rh_ptr)
*rh_ptr = U_TYPED_CALLOC(struct m_relation_history);
struct m_relation_history *rh = *rh_ptr;
rh->impl.topIdx = 0;
rh->impl.length = 0;
rh->has_first_sample = false;
os_mutex_init(&rh->mutex);
#if 0
@ -94,7 +92,7 @@ m_relation_history_get(struct m_relation_history *rh, struct xrt_space_relation
}
{
uint64_t oldest_in_buffer = rh->impl[rh->impl.length - 1]->timestamp;
uint64_t oldest_in_buffer = rh->impl[rh->impl.length() - 1]->timestamp;
uint64_t newest_in_buffer = rh->impl[0]->timestamp;
if (at_timestamp_ns > newest_in_buffer) {
@ -118,7 +116,7 @@ m_relation_history_get(struct m_relation_history *rh, struct xrt_space_relation
#ifdef RH_DEBUG
U_LOG_E("Extrapolating %f s before the tail of the buffer!", delta_s);
#endif
m_predict_relation(&rh->impl[rh->impl.length - 1]->relation, delta_s, out_relation);
m_predict_relation(&rh->impl[rh->impl.length() - 1]->relation, delta_s, out_relation);
goto end;
}
#ifdef RH_DEBUG
@ -128,7 +126,7 @@ m_relation_history_get(struct m_relation_history *rh, struct xrt_space_relation
// Very slow - O(n) - but easier to read
int idx = 0;
for (int i = 0; i < rh->impl.length; i++) {
for (int i = 0; i < rh->impl.length(); i++) {
if (rh->impl[i]->timestamp < at_timestamp_ns) {
// If the entry we're looking at is before the input time
idx = i;
@ -154,7 +152,7 @@ m_relation_history_get(struct m_relation_history *rh, struct xrt_space_relation
assert(step == (int)pow(2, i));
#endif
if (idx >= rh->impl.length) {
if (idx >= rh->impl.length()) {
// We'd be looking at an uninitialized value. Go back closer to the head of the buffer.
idx -= step;
continue;

View file

@ -19,56 +19,55 @@
namespace xrt::auxiliary::util {
template <typename T, int maxSize> struct HistoryBuffer
template <typename T, int maxSize> class HistoryBuffer
{
private:
T internalBuffer[maxSize];
int topIdx = 0;
int length = 0;
int mTopIdx = 0;
int mLength = 0;
public:
// clang-format off
int topIdx() { return mTopIdx; }
int length() { return mLength; }
// clang-format on
/* Put something at the top, overwrite whatever was at the back*/
void
push(const T inElement);
push(const T inElement)
{
mTopIdx++;
if (mTopIdx == maxSize) {
mTopIdx = 0;
}
T *operator[](int inIndex);
memcpy(&internalBuffer[mTopIdx], &inElement, sizeof(T));
mLength++;
mLength = std::min(mLength, maxSize);
}
// Lazy convenience.
T *
last();
T * // Hack comment to fix clang-format
operator[](int inIndex)
{
if (mLength == 0) {
return NULL;
}
assert(inIndex <= maxSize);
assert(inIndex >= 0);
int index = mTopIdx - inIndex;
if (index < 0) {
index = maxSize + index;
}
assert(index >= 0);
if (index > maxSize) {
assert(false);
}
return &internalBuffer[index];
}
};
template <typename T, int maxSize>
void
HistoryBuffer<T, maxSize>::push(const T inElement)
{
topIdx++;
if (topIdx == maxSize) {
topIdx = 0;
}
memcpy(&internalBuffer[topIdx], &inElement, sizeof(T));
length++;
length = std::min(length, maxSize);
// U_LOG_E("new length is %zu", length);
}
template <typename T, int maxSize> T *HistoryBuffer<T, maxSize>::operator[](int inIndex)
{
if (length == 0) {
return NULL;
}
assert(inIndex <= maxSize);
assert(inIndex >= 0);
int index = topIdx - inIndex;
if (index < 0) {
index = maxSize + index;
}
assert(index >= 0);
if (index > maxSize) {
assert(false);
}
return &internalBuffer[index];
}
} // namespace xrt::auxiliary::util
} // namespace xrt::auxiliary::util