changed interval method names, and added comments and assertions

master
Francisco Paisana 4 years ago
parent b2dd46ccad
commit c1755727d7

@ -26,37 +26,40 @@
namespace srslte { namespace srslte {
/// Representation of an interval between two numeric-types in the math representation [start, stop)
template <typename T> template <typename T>
class interval class interval
{ {
static_assert(std::is_trivially_copyable<T>::value, "Template argument T should be trivially copyable.");
public: public:
T start; T start;
T stop; T stop;
interval() : start(T{}), stop(T{}) {} interval() : start(T{}), stop(T{}) {}
interval(const T& start_, const T& stop_) : start(start_), stop(stop_) {} interval(T start_, T stop_) : start(start_), stop(stop_) {}
bool is_empty() const { return stop <= start; } bool empty() const { return stop <= start; }
T length() const { return stop - start; } T length() const { return stop > start ? stop - start : 0; }
void set_length(const T& len) { stop = start + len; } void set_length(T len) { stop = start + len; }
void add_offset(int offset) void displace_by(int offset)
{ {
start += offset; start += offset;
stop += offset; stop += offset;
} }
void shift_to(int new_start) void displace_to(T start_point)
{ {
stop = new_start + length(); stop = start_point + length();
start = new_start; start = start_point;
} }
bool overlaps(const interval& other) const { return start < other.stop and other.start < stop; } bool overlaps(interval other) const { return start < other.stop and other.start < stop; }
bool contains(const T& point) const { return start <= point and point < stop; } bool contains(T point) const { return start <= point and point < stop; }
std::string to_string() const std::string to_string() const
{ {

@ -23,6 +23,7 @@
#define SRSLTE_TTI_POINT_H #define SRSLTE_TTI_POINT_H
#include "logmap.h" #include "logmap.h"
#include "srslte/adt/interval.h"
#include <cstdint> #include <cstdint>
#include <limits> #include <limits>
@ -130,6 +131,8 @@ inline tti_point to_tx_dl_ack(const srslte::tti_point& t)
return to_tx_ul(t); return to_tx_ul(t);
} }
using tti_interval = srslte::interval<srslte::tti_point>;
} // namespace srslte } // namespace srslte
#endif // SRSLTE_TTI_POINT_H #endif // SRSLTE_TTI_POINT_H

@ -22,6 +22,17 @@
#include "srslte/adt/interval.h" #include "srslte/adt/interval.h"
#include "srslte/common/test_common.h" #include "srslte/common/test_common.h"
int test_interval_init()
{
srslte::interval<int> I{}, I2{12, 15}, I3{12, 12};
TESTASSERT(I.empty() and I.start == 0 and I.stop == 0);
TESTASSERT(not I2.empty() and I2.start == 12 and I2.stop == 15);
TESTASSERT(I3.empty() and I3.start == 12 and I3.stop == 12);
return SRSLTE_SUCCESS;
}
int test_interval_overlaps() int test_interval_overlaps()
{ {
srslte::interval<int> I{10, 15}, I2{9, 11}, I3{11, 14}, I4{9, 16}, I5{14, 16}, I6{4, 10}, I7{15, 17}; srslte::interval<int> I{10, 15}, I2{9, 11}, I3{11, 14}, I4{9, 16}, I5{14, 16}, I6{4, 10}, I7{15, 17};
@ -57,14 +68,15 @@ int test_interval_intersect()
TESTASSERT(srslte::make_intersection(I, I2) == (I & I2)); TESTASSERT(srslte::make_intersection(I, I2) == (I & I2));
TESTASSERT((I & I2) == srslte::interval<int>(5, 6)); TESTASSERT((I & I2) == srslte::interval<int>(5, 6));
TESTASSERT((I & I3) == srslte::interval<int>(9, 10)); TESTASSERT((I & I3) == srslte::interval<int>(9, 10));
TESTASSERT(not(I & I3).is_empty()); TESTASSERT(not(I & I3).empty());
TESTASSERT((I & I4).is_empty()); TESTASSERT((I & I4).empty());
return SRSLTE_SUCCESS; return SRSLTE_SUCCESS;
} }
int main() int main()
{ {
TESTASSERT(test_interval_init() == SRSLTE_SUCCESS);
TESTASSERT(test_interval_overlaps() == SRSLTE_SUCCESS); TESTASSERT(test_interval_overlaps() == SRSLTE_SUCCESS);
TESTASSERT(test_interval_contains() == SRSLTE_SUCCESS); TESTASSERT(test_interval_contains() == SRSLTE_SUCCESS);
TESTASSERT(test_interval_intersect() == SRSLTE_SUCCESS); TESTASSERT(test_interval_intersect() == SRSLTE_SUCCESS);

@ -536,7 +536,7 @@ bool sf_grid_t::find_ul_alloc(uint32_t L, prb_interval* alloc) const
*alloc = {}; *alloc = {};
for (uint32_t n = 0; n < ul_mask.size() && alloc->length() < L; n++) { for (uint32_t n = 0; n < ul_mask.size() && alloc->length() < L; n++) {
if (not ul_mask.test(n) && alloc->length() == 0) { if (not ul_mask.test(n) && alloc->length() == 0) {
alloc->shift_to(n); alloc->displace_to(n);
} }
if (not ul_mask.test(n)) { if (not ul_mask.test(n)) {
alloc->stop++; alloc->stop++;

@ -209,7 +209,7 @@ bool ul_metric_rr::find_allocation(uint32_t L, prb_interval* alloc)
*alloc = {}; *alloc = {};
for (uint32_t n = 0; n < used_rb->size() && alloc->length() < L; n++) { for (uint32_t n = 0; n < used_rb->size() && alloc->length() < L; n++) {
if (not used_rb->test(n) && alloc->length() == 0) { if (not used_rb->test(n) && alloc->length() == 0) {
alloc->shift_to(n); alloc->displace_to(n);
} }
if (not used_rb->test(n)) { if (not used_rb->test(n)) {
alloc->stop++; alloc->stop++;

@ -66,7 +66,7 @@ int output_sched_tester::test_pusch_collisions(const tti_params_t&
auto try_ul_fill = [&](prb_interval alloc, const char* ch_str, bool strict = true) { auto try_ul_fill = [&](prb_interval alloc, const char* ch_str, bool strict = true) {
CONDERROR(alloc.stop > nof_prb, "Allocated RBs %s out-of-bounds\n", alloc.to_string().c_str()); CONDERROR(alloc.stop > nof_prb, "Allocated RBs %s out-of-bounds\n", alloc.to_string().c_str());
CONDERROR(alloc.is_empty(), "Allocations must have at least one PRB\n"); CONDERROR(alloc.empty(), "Allocations must have at least one PRB\n");
if (strict and ul_allocs.any(alloc.start, alloc.stop)) { if (strict and ul_allocs.any(alloc.start, alloc.stop)) {
TESTERROR("Collision Detected of %s alloc=%s and cumulative_mask=0x%s\n", TESTERROR("Collision Detected of %s alloc=%s and cumulative_mask=0x%s\n",
ch_str, ch_str,

Loading…
Cancel
Save