Skip to content

patch: merge points over inserting them #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion include/ctre/first.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,14 +434,64 @@ template <size_t Capacity> class point_set {
obj = tmp;
//std::swap(*it, obj);

used++;
return out;
}
}
constexpr bool can_merge(point lhs, point rhs) noexcept {
point expanded_lhs = lhs;
//expand ranges by 1 in each direction
expanded_lhs.low -= (expanded_lhs.low > std::numeric_limits<int64_t>::min()) ? 1 : 0;
expanded_lhs.high += (expanded_lhs.high < std::numeric_limits<int64_t>::max()) ? 1 : 0;

point expanded_rhs = rhs;
expanded_rhs.low -= (expanded_rhs.low > std::numeric_limits<int64_t>::min()) ? 1 : 0;
expanded_rhs.high += (expanded_rhs.high < std::numeric_limits<int64_t>::max()) ? 1 : 0;
//for example [1,5] and [6,9] can merge into [1,9]
return (expanded_lhs.low <= rhs.high && rhs.low <= expanded_lhs.high) ||
(expanded_rhs.low <= lhs.high && lhs.low <= expanded_rhs.high);
}
constexpr point* insert_or_merge_point(int64_t position, int64_t other) {
point obj{ position, other };
auto it = lower_bound(obj);
if (it == end()) {
if (used && can_merge(*(it-1), obj)) {
auto back = it - 1;
back->low = back->low < obj.low ? back->low : obj.low;
back->high = back->high > obj.high ? back->high : obj.high;
} else {
*it = obj;
used++;
}
return it;
} else {
auto out = it;
//good chance we can merge here
if (can_merge(*it, obj)) {
//merge the points together vs inserting
it->low = it->low < obj.low ? it->low : obj.low;
it->high = it->high > obj.high ? it->high : obj.high;
return out;
}
auto e = end();
while (it != e) {
auto tmp = *it;
*it = obj;
obj = tmp;
it++;
}
auto tmp = *it;
*it = obj;
obj = tmp;

used++;
return out;
}
}
public:
constexpr point_set() { }
constexpr void insert(int64_t low, int64_t high) {
insert_point(low, high);
insert_or_merge_point(low, high);
//insert_point(high, low);
}
constexpr bool check(int64_t low, int64_t high) {
Expand Down
52 changes: 51 additions & 1 deletion single-header/ctre-unicode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3872,14 +3872,64 @@ template <size_t Capacity> class point_set {
obj = tmp;
//std::swap(*it, obj);

used++;
return out;
}
}
constexpr bool can_merge(point lhs, point rhs) noexcept {
point expanded_lhs = lhs;
//expand ranges by 1 in each direction
expanded_lhs.low -= (expanded_lhs.low > std::numeric_limits<int64_t>::min()) ? 1 : 0;
expanded_lhs.high += (expanded_lhs.high < std::numeric_limits<int64_t>::max()) ? 1 : 0;

point expanded_rhs = rhs;
expanded_rhs.low -= (expanded_rhs.low > std::numeric_limits<int64_t>::min()) ? 1 : 0;
expanded_rhs.high += (expanded_rhs.high < std::numeric_limits<int64_t>::max()) ? 1 : 0;
//for example [1,5] and [6,9] can merge into [1,9]
return (expanded_lhs.low <= rhs.high && rhs.low <= expanded_lhs.high) ||
(expanded_rhs.low <= lhs.high && lhs.low <= expanded_rhs.high);
}
constexpr point* insert_or_merge_point(int64_t position, int64_t other) {
point obj{ position, other };
auto it = lower_bound(obj);
if (it == end()) {
if (used && can_merge(*(it-1), obj)) {
auto back = it - 1;
back->low = back->low < obj.low ? back->low : obj.low;
back->high = back->high > obj.high ? back->high : obj.high;
} else {
*it = obj;
used++;
}
return it;
} else {
auto out = it;
//good chance we can merge here
if (can_merge(*it, obj)) {
//merge the points together vs inserting
it->low = it->low < obj.low ? it->low : obj.low;
it->high = it->high > obj.high ? it->high : obj.high;
return out;
}
auto e = end();
while (it != e) {
auto tmp = *it;
*it = obj;
obj = tmp;
it++;
}
auto tmp = *it;
*it = obj;
obj = tmp;

used++;
return out;
}
}
public:
constexpr point_set() { }
constexpr void insert(int64_t low, int64_t high) {
insert_point(low, high);
insert_or_merge_point(low, high);
//insert_point(high, low);
}
constexpr bool check(int64_t low, int64_t high) {
Expand Down
52 changes: 51 additions & 1 deletion single-header/ctre.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3869,14 +3869,64 @@ template <size_t Capacity> class point_set {
obj = tmp;
//std::swap(*it, obj);

used++;
return out;
}
}
constexpr bool can_merge(point lhs, point rhs) noexcept {
point expanded_lhs = lhs;
//expand ranges by 1 in each direction
expanded_lhs.low -= (expanded_lhs.low > std::numeric_limits<int64_t>::min()) ? 1 : 0;
expanded_lhs.high += (expanded_lhs.high < std::numeric_limits<int64_t>::max()) ? 1 : 0;

point expanded_rhs = rhs;
expanded_rhs.low -= (expanded_rhs.low > std::numeric_limits<int64_t>::min()) ? 1 : 0;
expanded_rhs.high += (expanded_rhs.high < std::numeric_limits<int64_t>::max()) ? 1 : 0;
//for example [1,5] and [6,9] can merge into [1,9]
return (expanded_lhs.low <= rhs.high && rhs.low <= expanded_lhs.high) ||
(expanded_rhs.low <= lhs.high && lhs.low <= expanded_rhs.high);
}
constexpr point* insert_or_merge_point(int64_t position, int64_t other) {
point obj{ position, other };
auto it = lower_bound(obj);
if (it == end()) {
if (used && can_merge(*(it-1), obj)) {
auto back = it - 1;
back->low = back->low < obj.low ? back->low : obj.low;
back->high = back->high > obj.high ? back->high : obj.high;
} else {
*it = obj;
used++;
}
return it;
} else {
auto out = it;
//good chance we can merge here
if (can_merge(*it, obj)) {
//merge the points together vs inserting
it->low = it->low < obj.low ? it->low : obj.low;
it->high = it->high > obj.high ? it->high : obj.high;
return out;
}
auto e = end();
while (it != e) {
auto tmp = *it;
*it = obj;
obj = tmp;
it++;
}
auto tmp = *it;
*it = obj;
obj = tmp;

used++;
return out;
}
}
public:
constexpr point_set() { }
constexpr void insert(int64_t low, int64_t high) {
insert_point(low, high);
insert_or_merge_point(low, high);
//insert_point(high, low);
}
constexpr bool check(int64_t low, int64_t high) {
Expand Down