Skip to content

Commit

Permalink
Fix misc (#1143)
Browse files Browse the repository at this point in the history
* update nix actions

* misc fix

* use modern c++
  • Loading branch information
pca006132 authored Feb 13, 2025
1 parent f2da5ea commit a857f00
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 67 deletions.
12 changes: 6 additions & 6 deletions include/manifold/linalg.h
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@ functions, as well as a set of standard reductions.
*/
template <class T, int M>
struct mat<T, M, 1> {
typedef vec<T, M> V;
using V = vec<T, M>;
V x;
constexpr mat() : x() {}
constexpr mat(const V &x_) : x(x_) {}
Expand All @@ -1076,8 +1076,8 @@ struct mat<T, M, 1> {
template <class U>
constexpr explicit mat(const mat<U, M, 1> &m) : mat(V(m.x)) {}
constexpr vec<T, 1> row(int i) const { return {x[i]}; }
constexpr const V &operator[](int j) const { return x; }
LINALG_CONSTEXPR14 V &operator[](int j) { return x; }
constexpr const V &operator[](int) const { return x; }
LINALG_CONSTEXPR14 V &operator[](int) { return x; }

template <class U, class = detail::conv_t<mat, U>>
constexpr mat(const U &u) : mat(converter<mat, U>{}(u)) {}
Expand All @@ -1088,7 +1088,7 @@ struct mat<T, M, 1> {
};
template <class T, int M>
struct mat<T, M, 2> {
typedef vec<T, M> V;
using V = vec<T, M>;
V x, y;
constexpr mat() : x(), y() {}
constexpr mat(const V &x_, const V &y_) : x(x_), y(y_) {}
Expand All @@ -1111,7 +1111,7 @@ struct mat<T, M, 2> {
};
template <class T, int M>
struct mat<T, M, 3> {
typedef vec<T, M> V;
using V = vec<T, M>;
V x, y, z;
constexpr mat() : x(), y(), z() {}
constexpr mat(const V &x_, const V &y_, const V &z_) : x(x_), y(y_), z(z_) {}
Expand Down Expand Up @@ -1141,7 +1141,7 @@ struct mat<T, M, 3> {
};
template <class T, int M>
struct mat<T, M, 4> {
typedef vec<T, M> V;
using V = vec<T, M>;
V x, y, z, w;
constexpr mat() : x(), y(), z(), w() {}
constexpr mat(const V &x_, const V &y_, const V &z_, const V &w_)
Expand Down
6 changes: 2 additions & 4 deletions include/manifold/vec_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ class VecView {
size_t length = std::numeric_limits<size_t>::max()) {
if (length == std::numeric_limits<size_t>::max())
length = this->size_ - offset;
ASSERT(length >= 0, std::out_of_range("Vec::view out of range"));
ASSERT(offset + length <= this->size_ && offset >= 0,
ASSERT(offset + length <= this->size_,
std::out_of_range("Vec::view out of range"));
return VecView<T>(this->ptr_ + offset, length);
}
Expand All @@ -117,8 +116,7 @@ class VecView {
size_t length = std::numeric_limits<size_t>::max()) const {
if (length == std::numeric_limits<size_t>::max())
length = this->size_ - offset;
ASSERT(length >= 0, std::out_of_range("Vec::cview out of range"));
ASSERT(offset + length <= this->size_ && offset >= 0,
ASSERT(offset + length <= this->size_,
std::out_of_range("Vec::cview out of range"));
return VecView<const T>(this->ptr_ + offset, length);
}
Expand Down
12 changes: 6 additions & 6 deletions src/boolean_result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,8 @@ void AppendPartialEdges(Manifold::Impl &outR, Vec<char> &wholeHalfedgeP,
// reference is now to the endVert instead of the startVert, which is one
// position advanced CCW. This is only valid if this is a retained vert; it
// will be ignored later if the vert is new.
const TriRef forwardRef = {forward ? 0 : 1, -1, faceLeftP};
const TriRef backwardRef = {forward ? 0 : 1, -1, faceRightP};
const TriRef forwardRef = {forward ? 0 : 1, -1, faceLeftP, -1};
const TriRef backwardRef = {forward ? 0 : 1, -1, faceRightP, -1};

for (Halfedge e : edges) {
const int forwardEdge = facePtrR[faceLeft]++;
Expand Down Expand Up @@ -411,8 +411,8 @@ void AppendNewEdges(
// add halfedges to result
const int faceLeft = facePQ2R[faceP];
const int faceRight = facePQ2R[numFaceP + faceQ];
const TriRef forwardRef = {0, -1, faceP};
const TriRef backwardRef = {1, -1, faceQ};
const TriRef forwardRef = {0, -1, faceP, -1};
const TriRef backwardRef = {1, -1, faceQ, -1};
for (Halfedge e : edges) {
const int forwardEdge = facePtrR[faceLeft]++;
const int backwardEdge = facePtrR[faceRight]++;
Expand Down Expand Up @@ -459,8 +459,8 @@ struct DuplicateHalfedges {
// Negative inclusion means the halfedges are reversed, which means our
// reference is now to the endVert instead of the startVert, which is one
// position advanced CCW.
const TriRef forwardRef = {forward ? 0 : 1, -1, faceLeftP};
const TriRef backwardRef = {forward ? 0 : 1, -1, faceRightP};
const TriRef forwardRef = {forward ? 0 : 1, -1, faceLeftP, -1};
const TriRef backwardRef = {forward ? 0 : 1, -1, faceRightP, -1};

for (int i = 0; i < std::abs(inclusion); ++i) {
int forwardEdge = AtomicAdd(facePtr[newFace], 1);
Expand Down
2 changes: 1 addition & 1 deletion src/collider.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ constexpr int kRoot = 1;
#ifdef _MSC_VER

#ifndef _WINDEF_
typedef unsigned long DWORD;
using DWORD = unsigned long;
#endif

uint32_t inline ctz(uint32_t value) {
Expand Down
31 changes: 15 additions & 16 deletions src/hashtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
#include "./vec.h"

namespace {
typedef unsigned long long int Uint64;
typedef Uint64 (*hash_fun_t)(Uint64);
inline constexpr Uint64 kOpen = std::numeric_limits<Uint64>::max();
using hash_fun_t = uint64_t(uint64_t);
inline constexpr uint64_t kOpen = std::numeric_limits<uint64_t>::max();

template <typename T>
T AtomicCAS(T& target, T compare, T val) {
Expand All @@ -46,7 +45,7 @@ T AtomicLoad(const T& target) {
}

// https://stackoverflow.com/questions/664014/what-integer-hash-function-are-good-that-accepts-an-integer-hash-key
inline Uint64 hash64bit(Uint64 x) {
inline uint64_t hash64bit(uint64_t x) {
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ull;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebull;
x = x ^ (x >> 31);
Expand All @@ -59,7 +58,7 @@ namespace manifold {
template <typename V, hash_fun_t H = hash64bit>
class HashTableD {
public:
HashTableD(Vec<Uint64>& keys, Vec<V>& values, std::atomic<size_t>& used,
HashTableD(Vec<uint64_t>& keys, Vec<V>& values, std::atomic<size_t>& used,
uint32_t step = 1)
: step_{step}, keys_{keys}, values_{values}, used_{used} {}

Expand All @@ -70,12 +69,12 @@ class HashTableD {
static_cast<size_t>(Size());
}

void Insert(Uint64 key, const V& val) {
void Insert(uint64_t key, const V& val) {
uint32_t idx = H(key) & (Size() - 1);
while (1) {
if (Full()) return;
Uint64& k = keys_[idx];
const Uint64 found = AtomicCAS(k, kOpen, key);
uint64_t& k = keys_[idx];
const uint64_t found = AtomicCAS(k, kOpen, key);
if (found == kOpen) {
used_.fetch_add(1, std::memory_order_relaxed);
values_[idx] = val;
Expand All @@ -86,35 +85,35 @@ class HashTableD {
}
}

V& operator[](Uint64 key) {
V& operator[](uint64_t key) {
uint32_t idx = H(key) & (Size() - 1);
while (1) {
const Uint64 k = AtomicLoad(keys_[idx]);
const uint64_t k = AtomicLoad(keys_[idx]);
if (k == key || k == kOpen) {
return values_[idx];
}
idx = (idx + step_) & (Size() - 1);
}
}

const V& operator[](Uint64 key) const {
const V& operator[](uint64_t key) const {
uint32_t idx = H(key) & (Size() - 1);
while (1) {
const Uint64 k = AtomicLoad(keys_[idx]);
const uint64_t k = AtomicLoad(keys_[idx]);
if (k == key || k == kOpen) {
return values_[idx];
}
idx = (idx + step_) & (Size() - 1);
}
}

Uint64 KeyAt(int idx) const { return AtomicLoad(keys_[idx]); }
uint64_t KeyAt(int idx) const { return AtomicLoad(keys_[idx]); }
V& At(int idx) { return values_[idx]; }
const V& At(int idx) const { return values_[idx]; }

private:
uint32_t step_;
VecView<Uint64> keys_;
VecView<uint64_t> keys_;
VecView<V> values_;
std::atomic<size_t>& used_;
};
Expand Down Expand Up @@ -157,10 +156,10 @@ class HashTable {

Vec<V>& GetValueStore() { return values_; }

static Uint64 Open() { return kOpen; }
static uint64_t Open() { return kOpen; }

private:
Vec<Uint64> keys_;
Vec<uint64_t> keys_;
Vec<V> values_;
std::atomic<size_t> used_ = 0;
uint32_t step_;
Expand Down
2 changes: 1 addition & 1 deletion src/impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ Manifold Manifold::ImportMeshGL64(std::istream& stream) {
} else {
// add it back because it is not what we want
int end = 0;
while (tmp[end] != 0 && end < SIZE) end++;
while (end < SIZE && tmp[end] != 0) end++;
while (--end > -1) stream.putback(tmp[end]);
}
c = stream.get();
Expand Down
27 changes: 20 additions & 7 deletions src/iters.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#pragma once

#include <iterator>
#include <optional>
#include <type_traits>

namespace manifold {
Expand All @@ -22,7 +23,7 @@ template <typename F, typename Iter>
struct TransformIterator {
private:
Iter iter;
F f;
std::optional<F> f;

public:
using pointer = void;
Expand All @@ -36,16 +37,28 @@ struct TransformIterator {

constexpr TransformIterator(Iter iter, F f) : iter(iter), f(f) {}

TransformIterator(const TransformIterator& other)
: iter(other.iter), f(other.f) {}

TransformIterator(TransformIterator&& other) : iter(other.iter), f(other.f) {}

TransformIterator& operator=(const TransformIterator& other) {
if (this == &other) return *this;
// don't copy function, should be the same
iter = other.iter;
f.emplace(*other.f);
return *this;
}

TransformIterator& operator=(TransformIterator&& other) {
if (this == &other) return *this;
iter = other.iter;
f.emplace(*other.f);
return *this;
}

constexpr reference operator*() const { return f(*iter); }
constexpr reference operator*() const { return (*f)(*iter); }

constexpr reference operator[](size_t i) const { return f(iter[i]); }
constexpr reference operator[](size_t i) const { return (*f)(iter[i]); }

// prefix increment
TransformIterator& operator++() {
Expand Down Expand Up @@ -74,7 +87,7 @@ struct TransformIterator {
}

constexpr TransformIterator operator+(size_t n) const {
return TransformIterator(iter + n, f);
return TransformIterator(iter + n, *f);
}

TransformIterator& operator+=(size_t n) {
Expand All @@ -83,7 +96,7 @@ struct TransformIterator {
}

constexpr TransformIterator operator-(size_t n) const {
return TransformIterator(iter - n, f);
return TransformIterator(iter - n, *f);
}

TransformIterator& operator-=(size_t n) {
Expand All @@ -108,7 +121,7 @@ struct TransformIterator {
}

constexpr operator TransformIterator<F, const Iter>() const {
return TransformIterator(f, iter);
return TransformIterator(*f, iter);
}
};

Expand Down
27 changes: 20 additions & 7 deletions src/polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ class EarClip {

private:
struct Vert;
typedef std::vector<Vert>::iterator VertItr;
typedef std::vector<Vert>::const_iterator VertItrC;
using VertItr = std::vector<Vert>::iterator;
using VertItrC = std::vector<Vert>::const_iterator;
struct MaxX {
bool operator()(const VertItr &a, const VertItr &b) const {
return a->pos.x > b->pos.x;
Expand All @@ -284,7 +284,7 @@ class EarClip {
return a->cost < b->cost;
}
};
typedef std::set<VertItr, MinCost>::iterator qItr;
using qItr = std::set<VertItr, MinCost>::iterator;

// The flat list where all the Verts are stored. Not used much for traversal.
std::vector<Vert> polygon_;
Expand Down Expand Up @@ -625,9 +625,16 @@ class EarClip {
// Build the circular list polygon structures.
std::vector<VertItr> Initialize(const PolygonsIdx &polys) {
std::vector<VertItr> starts;
const auto invalidItr = polygon_.begin();
for (const SimplePolygonIdx &poly : polys) {
auto vert = poly.begin();
polygon_.push_back({vert->idx, 0.0, earsQueue_.end(), vert->pos});
polygon_.push_back({vert->idx,
0.0,
earsQueue_.end(),
vert->pos,
{0, 0},
invalidItr,
invalidItr});
const VertItr first = std::prev(polygon_.end());

bBox_.Union(first->pos);
Expand All @@ -639,7 +646,13 @@ class EarClip {
for (++vert; vert != poly.end(); ++vert) {
bBox_.Union(vert->pos);

polygon_.push_back({vert->idx, 0.0, earsQueue_.end(), vert->pos});
polygon_.push_back({vert->idx,
0.0,
earsQueue_.end(),
vert->pos,
{0, 0},
invalidItr,
invalidItr});
VertItr next = std::prev(polygon_.end());

Link(last, next);
Expand Down Expand Up @@ -836,7 +849,7 @@ class EarClip {
});

if (itr.empty()) {
return {Collider(), itr};
return {Collider(), itr, {}};
}

const int numVert = itr.size();
Expand All @@ -851,7 +864,7 @@ class EarClip {
Permute(vertBox, vertNew2Old);
Permute(itr, vertNew2Old);

return {Collider(vertBox, vertMorton), itr};
return {Collider(vertBox, vertMorton), itr, {}};
}

// The main ear-clipping loop. This is called once for each simple polygon -
Expand Down
Loading

0 comments on commit a857f00

Please sign in to comment.