Skip to content

Instantly share code, notes, and snippets.

@Oxore
Created February 25, 2023 20:54
Show Gist options
  • Save Oxore/4ed98abc609950a8716db8cc9eb39dc9 to your computer and use it in GitHub Desktop.
Save Oxore/4ed98abc609950a8716db8cc9eb39dc9 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <cstdint>
#include <cassert>
template <typename T, T Min, T Len>
struct Range {
using Self = Range<T, Min, Len>;
constexpr Range(T offset, T length): offset(offset), length(length) {}
constexpr T Intersection(Self other) const { return Intersection(*this, other); }
constexpr static T Intersection(Self a, Self b)
{
// TODO
return 0;
}
T offset{}, length{};
};
static void test_int32()
{
using R = Range<int32_t, -100, 200>;
assert(R(-50, 100).Intersection(R(-70, 20)) == 0);
assert(R(-50, 100).Intersection(R(-60, 20)) == 10);
assert(R(-50, 100).Intersection(R(-60, 120)) == 100);
assert(R(-50, 100).Intersection(R(40, 120)) == 20);
}
static void test_uint32()
{
using R = Range<uint32_t, 0, 100>;
assert(R(20, 60).Intersection(R(10, 10)) == 0);
assert(R(20, 60).Intersection(R(10, 20)) == 10);
assert(R(20, 60).Intersection(R(10, 80)) == 60);
assert(R(20, 60).Intersection(R(70, 60)) == 20);
}
int main()
{
test_int32();
test_uint32();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment