Skip to content

Commit

Permalink
karm-base: Got ride of the weird Cons<> tuple like type.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Jan 8, 2025
1 parent 4afceaf commit 9ec4b18
Show file tree
Hide file tree
Showing 32 changed files with 186 additions and 210 deletions.
54 changes: 0 additions & 54 deletions src/libs/karm-base/cons.h

This file was deleted.

40 changes: 20 additions & 20 deletions src/libs/karm-base/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ namespace Karm {

template <typename K, typename V>
struct Map {
Vec<Cons<K, V>> _els{};
Vec<Pair<K, V>> _els{};

Map() = default;

Map(std::initializer_list<Cons<K, V>> &&list)
Map(std::initializer_list<Pair<K, V>> &&list)
: _els(std::move(list)) {}

void put(K const &key, V value) {
for (auto &i : ::mutIter(_els)) {
if (i.car == key) {
i.cdr = std::move(value);
if (i.v0 == key) {
i.v1 = std::move(value);
return;
}
}

_els.pushBack(Cons<K, V>{key, std::move(value)});
_els.pushBack(Pair<K, V>{key, std::move(value)});
}

bool has(K const &key) const {
for (auto &i : _els) {
if (i.car == key) {
if (i.v0 == key) {
return true;
}
}
Expand All @@ -37,8 +37,8 @@ struct Map {

V &get(K const &key) {
for (auto &i : _els) {
if (i.car == key) {
return i.cdr;
if (i.v0 == key) {
return i.v1;
}
}

Expand All @@ -47,22 +47,22 @@ struct Map {

MutCursor<V> access(K const &key) {
for (auto &i : _els)
if (i.car == key)
return &i.cdr;
if (i.v0 == key)
return &i.v1;
return {};
}

Cursor<V> access(K const &key) const {
for (auto &i : _els)
if (i.car == key)
return &i.cdr;
if (i.v0 == key)
return &i.v1;
return {};
}

V take(K const &key) {
for (usize i = 0; i < _els.len(); i++) {
if (_els[i].car == key) {
V value = std::move(_els[i].cdr);
if (_els[i].v0 == key) {
V value = std::move(_els[i].v1);
_els.removeAt(i);
return value;
}
Expand All @@ -73,8 +73,8 @@ struct Map {

Opt<V> tryGet(K const &key) const {
for (auto &i : _els) {
if (i.car == key) {
return i.cdr;
if (i.v0 == key) {
return i.v1;
}
}

Expand All @@ -83,7 +83,7 @@ struct Map {

bool del(K const &key) {
for (usize i = 0; i < _els.len(); i++) {
if (_els[i].car == key) {
if (_els[i].v0 == key) {
_els.removeAt(i);
return true;
}
Expand All @@ -96,7 +96,7 @@ struct Map {
bool changed = false;

for (usize i = 1; i < _els.len() + 1; i++) {
if (_els[i - 1].cdr == value) {
if (_els[i - 1].v1 == value) {
_els.removeAt(i - 1);
changed = true;
i--;
Expand All @@ -108,7 +108,7 @@ struct Map {

bool removeFirst(V const &value) {
for (usize i = 1; i < _els.len() + 1; i++) {
if (_els[i - 1].cdr == value) {
if (_els[i - 1].v1 == value) {
_els.removeAt(i - 1);
return true;
}
Expand All @@ -126,7 +126,7 @@ struct Map {
}

V at(usize index) const {
return _els[index].cdr;
return _els[index].v1;
}

usize len() const {
Expand Down
4 changes: 2 additions & 2 deletions src/libs/karm-base/range.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "align.h"
#include "clamp.h"
#include "cons.h"
#include "tuple.h"

namespace Karm {

Expand Down Expand Up @@ -96,7 +96,7 @@ struct Range {
return {};
}

constexpr Cons<Range> split(Range other) {
constexpr Pair<Range> split(Range other) {
return {halfUnder(other), halfOver(other)};
}

Expand Down
27 changes: 27 additions & 0 deletions src/libs/karm-base/tuple.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ namespace Karm {
template <typename... Ts>
struct Tuple;

template <typename T0, typename T1 = T0>
using Pair = Tuple<T0, T1>;

template <>
struct Tuple<> {
constexpr static usize len() {
Expand Down Expand Up @@ -84,6 +87,9 @@ struct Tuple<_T0> {
constexpr U into() const {
return U{v0};
}

bool operator==(Tuple const &) const = default;
auto operator<=>(Tuple const &) const = default;
};

template <typename T0>
Expand Down Expand Up @@ -134,6 +140,9 @@ struct Tuple<_T0, _T1> {
constexpr U into() const {
return U{v0, v1};
}

bool operator==(Tuple const &) const = default;
auto operator<=>(Tuple const &) const = default;
};

template <typename _T0, typename _T1>
Expand Down Expand Up @@ -195,6 +204,9 @@ struct Tuple<_T0, _T1, _T2> {
constexpr U into() const {
return U{v0, v1, v2};
}

bool operator==(Tuple const &) const = default;
auto operator<=>(Tuple const &) const = default;
};

template <typename _T0, typename _T1, typename _T2>
Expand Down Expand Up @@ -267,6 +279,9 @@ struct Tuple<_T0, _T1, _T2, _T3> {
constexpr U into() const {
return U{v0, v1, v2, v3};
}

bool operator==(Tuple const &) const = default;
auto operator<=>(Tuple const &) const = default;
};

template <typename _T0, typename _T1, typename _T2, typename _T3>
Expand Down Expand Up @@ -350,6 +365,9 @@ struct Tuple<_T0, _T1, _T2, _T3, _T4> {
constexpr U into() const {
return U{v0, v1, v2, v3, v4};
}

bool operator==(Tuple const &) const = default;
auto operator<=>(Tuple const &) const = default;
};

template <typename _T0, typename _T1, typename _T2, typename _T3, typename _T4>
Expand Down Expand Up @@ -444,6 +462,9 @@ struct Tuple<_T0, _T1, _T2, _T3, _T4, _T5> {
constexpr U into() const {
return U{v0, v1, v2, v3, v4, v5};
}

bool operator==(Tuple const &) const = default;
auto operator<=>(Tuple const &) const = default;
};

template <typename _T0, typename _T1, typename _T2, typename _T3, typename _T4, typename _T5>
Expand Down Expand Up @@ -549,6 +570,9 @@ struct Tuple<_T0, _T1, _T2, _T3, _T4, _T5, _T6> {
constexpr U into() const {
return U{v0, v1, v2, v3, v4, v5, v6};
}

bool operator==(Tuple const &) const = default;
auto operator<=>(Tuple const &) const = default;
};

template <typename _T0, typename _T1, typename _T2, typename _T3, typename _T4, typename _T5, typename _T6>
Expand Down Expand Up @@ -665,6 +689,9 @@ struct Tuple<_T0, _T1, _T2, _T3, _T4, _T5, _T6, _T7> {
constexpr U into() const {
return U{v0, v1, v2, v3, v4, v5, v6, v7};
}

bool operator==(Tuple const &) const = default;
auto operator<=>(Tuple const &) const = default;
};

template <typename _T0, typename _T1, typename _T2, typename _T3, typename _T4, typename _T5, typename _T6, typename _T7>
Expand Down
44 changes: 22 additions & 22 deletions src/libs/karm-gfx/borders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ void Borders::_paintCurveEdge(Gfx::Canvas &c, Pair<Math::Curvef> const &start, P
c.beginPath();

// Outer edge
auto outerStart = start.car, outerEnd = end.car;
auto outerStart = start.v0, outerEnd = end.v0;
c.moveTo(outerStart.a);
c.cubicTo(outerStart.b, outerStart.c, outerStart.d);

c.lineTo(outerEnd.a);
c.cubicTo(outerEnd.b, outerEnd.c, outerEnd.d);

// Inner edge
auto innerStart = start.cdr, innerEnd = end.cdr;
auto innerStart = start.v1, innerEnd = end.v1;
c.lineTo(innerEnd.d);
c.cubicTo(innerEnd.c, innerEnd.b, innerEnd.a);

Expand Down Expand Up @@ -107,12 +107,12 @@ void Borders::_paintCurveEdges(Gfx::Canvas &c, Math::Rectf rect) {
_paintCurveEdge(
c,
{
outerTopStart.split(topStartRatio).cdr,
innerTopStart.split(topStartRatio).cdr,
outerTopStart.split(topStartRatio).v1,
innerTopStart.split(topStartRatio).v1,
},
{
outerTopEnd.split(topEndRatio).car,
innerTopEnd.split(topEndRatio).car,
outerTopEnd.split(topEndRatio).v0,
innerTopEnd.split(topEndRatio).v0,
},
fills[0]
);
Expand All @@ -121,12 +121,12 @@ void Borders::_paintCurveEdges(Gfx::Canvas &c, Math::Rectf rect) {
_paintCurveEdge(
c,
{
outerTopEnd.split(topEndRatio).cdr,
innerTopEnd.split(topEndRatio).cdr,
outerTopEnd.split(topEndRatio).v1,
innerTopEnd.split(topEndRatio).v1,
},
{
outerBottomEnd.split(bottomEndRatio).car,
innerBottomEnd.split(bottomEndRatio).car,
outerBottomEnd.split(bottomEndRatio).v0,
innerBottomEnd.split(bottomEndRatio).v0,
},
fills[1]
);
Expand All @@ -135,12 +135,12 @@ void Borders::_paintCurveEdges(Gfx::Canvas &c, Math::Rectf rect) {
_paintCurveEdge(
c,
{
outerBottomEnd.split(bottomEndRatio).cdr,
innerBottomEnd.split(bottomEndRatio).cdr,
outerBottomEnd.split(bottomEndRatio).v1,
innerBottomEnd.split(bottomEndRatio).v1,
},
{
outerBottomStart.split(bottomStartRatio).car,
innerBottomStart.split(bottomStartRatio).car,
outerBottomStart.split(bottomStartRatio).v0,
innerBottomStart.split(bottomStartRatio).v0,
},
fills[2]
);
Expand All @@ -149,12 +149,12 @@ void Borders::_paintCurveEdges(Gfx::Canvas &c, Math::Rectf rect) {
_paintCurveEdge(
c,
{
outerBottomStart.split(bottomStartRatio).cdr,
innerBottomStart.split(bottomStartRatio).cdr,
outerBottomStart.split(bottomStartRatio).v1,
innerBottomStart.split(bottomStartRatio).v1,
},
{
outerTopStart.split(topStartRatio).car,
innerTopStart.split(topStartRatio).car,
outerTopStart.split(topStartRatio).v0,
innerTopStart.split(topStartRatio).v0,
},
fills[3]
);
Expand All @@ -164,12 +164,12 @@ void Borders::_paintStraightEdge(Gfx::Canvas &c, Pair<Math::Vec2f> const &start,
c.beginPath();

// Outer edge
c.moveTo(start.car);
c.lineTo(end.car);
c.moveTo(start.v0);
c.lineTo(end.v0);

// Inner edge
c.lineTo(end.cdr);
c.lineTo(start.cdr);
c.lineTo(end.v1);
c.lineTo(start.v1);

c.closePath();

Expand Down
Loading

0 comments on commit 9ec4b18

Please sign in to comment.