parent
2ec0a5ba7a
commit
873fe958fd
@ -0,0 +1,117 @@
|
||||
# Generic C and C++ Makefile project template
|
||||
# Contains targets for `release', `debug', and `clean'.
|
||||
|
||||
PROJECT=project
|
||||
AUTHOR=Avril (Flanchan) <flanchan@cumallover.me>
|
||||
VERSION=0.0.0
|
||||
|
||||
SRC_C = $(wildcard src/*.c)
|
||||
SRC_CXX = $(wildcard src/*.cpp)
|
||||
|
||||
INCLUDE=include
|
||||
|
||||
# Link to these libraries dynamicalls
|
||||
SHARED_LIBS=
|
||||
# Link to these libraries statically
|
||||
STATIC_LIBS=
|
||||
|
||||
override __VERSION_SPLIT:= $(subst ., ,$(VERSION))
|
||||
override __VERSION_REVISION:=$(word 3,$(__VERSION_SPLIT)) 0
|
||||
override __VERSION_SPLIT:= MAJOR:$(word 1,$(__VERSION_SPLIT)) MINOR:$(word 2,$(__VERSION_SPLIT)) BUGFIX:$(word 1,$(subst r, ,$(__VERSION_REVISION))) REVISION:$(word 2,$(subst r, ,$(__VERSION_REVISION))) REVISION_STRING:$(word 3,$(__VERSION_SPLIT))
|
||||
|
||||
COMMON_FLAGS+= -W -Wall -Wstrict-aliasing -fno-strict-aliasing $(addprefix -I,$(INCLUDE))
|
||||
COMMON_FLAGS+= $(addprefix -D_VERSION_,$(subst :,=,$(__VERSION_SPLIT))) '-D_VERSION="$(VERSION)"'
|
||||
|
||||
# Target arch. Set to blank for generic
|
||||
ARCH?=native
|
||||
# Enable OpenMP and loop parallelisation? (dyn-links to openmp)
|
||||
PARALLEL?=yes
|
||||
|
||||
OPT_FLAGS?= -fgraphite \
|
||||
-floop-interchange -ftree-loop-distribution -floop-strip-mine -floop-block \
|
||||
-fno-stack-check
|
||||
|
||||
ifneq ($(ARCH),)
|
||||
OPT_FLAGS+= $(addprefix -march=,$(ARCH))
|
||||
endif
|
||||
|
||||
ifeq ($(PARALLEL),yes)
|
||||
OPT_FLAGS+= -fopenmp -floop-parallelize-all -ftree-parallelize-loops=4
|
||||
endif
|
||||
|
||||
CXX_OPT_FLAGS?= $(OPT_FLAGS) -felide-constructors
|
||||
|
||||
CSTD?=gnu2x
|
||||
CXXSTD?=gnu++23
|
||||
|
||||
CFLAGS += $(COMMON_FLAGS) --std=$(CSTD)
|
||||
CXXFLAGS += $(COMMON_FLAGS) --std=$(CXXSTD)
|
||||
LDFLAGS += $(addsuffix .a,$(addprefix -l:lib,$(STATIC_LIBS))) $(addprefix -l,$(SHARED_LIBS))
|
||||
|
||||
|
||||
STRIP=strip
|
||||
|
||||
RELEASE_COMMON_FLAGS+= -Werror -fno-bounds-check
|
||||
DEBUG_COMMON_FLAGS+= -ggdb -gz -fanalyzer -ftrapv -fbounds-check
|
||||
|
||||
ifneq ($(TARGET_SPEC_FLAGS),no)
|
||||
RELEASE_CFLAGS?= -O3 -flto $(OPT_FLAGS)
|
||||
RELEASE_CXXFLAGS?= -O3 -flto $(CXX_OPT_FLAGS)
|
||||
RELEASE_LDFLAGS?= -Wl,-O3 -Wl,-flto
|
||||
|
||||
DEBUG_CFLAGS?= -Og
|
||||
DEBUG_CXXFLAGS?= -Og
|
||||
|
||||
DEBUG_LDFLAGS?=
|
||||
endif
|
||||
|
||||
DEBUG_CFLAGS+=-DDEBUG $(DEBUG_COMMON_FLAGS)
|
||||
DEBUG_CXXFLAGS+=-DDEBUG $(DEBUG_COMMON_FLAGS) -fasynchronous-unwind-tables
|
||||
|
||||
RELEASE_CFLAGS+=-DRELEASE $(RELEASE_COMMON_FLAGS)
|
||||
RELEASE_CXXFLAGS+=-DRELEASE $(RELEASE_COMMON_FLAGS)
|
||||
|
||||
# Objects
|
||||
|
||||
OBJ_C = $(addprefix obj/c/,$(SRC_C:.c=.o))
|
||||
OBJ_CXX = $(addprefix obj/cxx/,$(SRC_CXX:.cpp=.o))
|
||||
OBJ = $(OBJ_C) $(OBJ_CXX)
|
||||
|
||||
# Phonies
|
||||
|
||||
.PHONY: release
|
||||
release: | dirs $(PROJECT)-release
|
||||
|
||||
.PHONY: debug
|
||||
debug: | dirs $(PROJECT)-debug
|
||||
|
||||
# Targets
|
||||
|
||||
dirs:
|
||||
@mkdir -p obj/c{,xx}/src
|
||||
|
||||
obj/c/%.o: %.c
|
||||
$(CC) -c $< $(CFLAGS) -o $@ $(LDFLAGS)
|
||||
|
||||
obj/cxx/%.o: %.cpp
|
||||
$(CXX) -c $< $(CXXFLAGS) -o $@ $(LDFLAGS)
|
||||
|
||||
$(PROJECT)-release: CFLAGS+= $(RELEASE_CFLAGS)
|
||||
$(PROJECT)-release: CXXFLAGS += $(RELEASE_CXXFLAGS)
|
||||
$(PROJECT)-release: LDFLAGS += $(RELEASE_LDFLAGS)
|
||||
$(PROJECT)-release: $(OBJ)
|
||||
$(CXX) $^ $(CXXFLAGS) -o $@ $(LDFLAGS)
|
||||
$(STRIP) $@
|
||||
|
||||
$(PROJECT)-debug: CFLAGS+= $(DEBUG_CFLAGS)
|
||||
$(PROJECT)-debug: CXXFLAGS += $(DEBUG_CXXFLAGS)
|
||||
$(PROJECT)-debug: LDFLAGS += $(DEBUG_LDFLAGS)
|
||||
$(PROJECT)-debug: $(OBJ)
|
||||
$(CXX) $^ $(CXXFLAGS) -o $@ $(LDFLAGS)
|
||||
|
||||
clean-rebuild:
|
||||
rm -rf obj
|
||||
|
||||
clean: clean-rebuild
|
||||
rm -f $(PROJECT)-{release,debug,pgo}
|
||||
|
@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
struct Point {
|
||||
uint64_t x,y;
|
||||
};
|
||||
|
||||
struct Line {
|
||||
Point start, end;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
using Output = T* __restrict__;
|
||||
|
||||
namespace Input {
|
||||
struct Parser {
|
||||
Parser(const char* string, size_t len) noexcept;
|
||||
template<size_t N>
|
||||
inline Parser(const char (&string)[N]) noexcept
|
||||
: Parser(string, N) {}
|
||||
|
||||
Parser(Parser&&) noexcept;
|
||||
Parser(const Parser&) = delete;
|
||||
|
||||
Parser& operator=(Parser&&) noexcept;
|
||||
Parser& operator=(const Parser&) = delete;
|
||||
|
||||
bool try_read_next(Output<Line>);
|
||||
|
||||
virtual ~Parser();
|
||||
private:
|
||||
struct _impl;
|
||||
|
||||
std::unique_ptr<_impl> state_;
|
||||
};
|
||||
}
|
@ -0,0 +1,120 @@
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include <input.h>
|
||||
|
||||
namespace {
|
||||
template<typename T>
|
||||
[[gnu::pure]]
|
||||
inline T* gmemchr(T* ptr, char ch, size_t sz) noexcept {
|
||||
return (T*)memchr((void*)ptr, ch, sz * sizeof(T));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline size_t gmemchr_s(T* ptr, char ch, size_t sz) noexcept {
|
||||
if(T* p = gmemchr(ptr, ch, sz)) return (size_t)(p - ptr);
|
||||
else return 0;
|
||||
}
|
||||
|
||||
template<typename F> requires(std::is_invocable_v<F>)
|
||||
struct Defer {
|
||||
constexpr explicit Defer(std::nullptr_t) noexcept
|
||||
: f(nullptr) {}
|
||||
constexpr Defer(F&& func) noexcept(std::is_nothrow_move_constructible_v<F>)
|
||||
: f(std::make_unique<F>(std::move(func))) {}
|
||||
constexpr virtual ~Defer() noexcept(std::is_nothrow_invocable_v<F>) { if(f) (*f)(); }
|
||||
|
||||
constexpr Defer(Defer&& m) noexcept
|
||||
: f(std::move(m.f)) {}
|
||||
constexpr Defer(const Defer& p) noexcept requires(std::is_copy_constructible_v<F>)
|
||||
: f(p.f ? *p.f : nullptr) {}
|
||||
constexpr Defer& operator=(Defer&& m) noexcept(std::is_nothrow_move_constructible_v<F>)
|
||||
{
|
||||
if(this != std::addressof(m)) {
|
||||
f = std::move(m.f);
|
||||
} return *this;
|
||||
}
|
||||
constexpr Defer& operator=(const Defer& c) noexcept(std::is_nothrow_copy_constructible_v<F>) requires(std::is_copy_constructible_v<F>)
|
||||
{
|
||||
if(this != std::addressof(m)) {
|
||||
if(c.f) f = std::make_unique<F>(*c.f);
|
||||
else f = nullptr;
|
||||
} return *this;
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
constexpr decltype(auto) operator()(Args&&... args) noexcept(std::is_nothrow_invocable_v<F, Args...>) requires(std::is_invocable_v<F, Args...>)
|
||||
{ return std::forward<std::invoke_result_t<F, Args...>>( (*f)() ); }
|
||||
private:
|
||||
std::unique_ptr<F> f;
|
||||
};
|
||||
template<typename F> requires(std::is_invocable_v<F>)
|
||||
constexpr auto defer_call(F&& func) noexcept(std::is_nothrow_move_constructible_v<F>) { return Defer<F>{std::move(func)}; }
|
||||
#define $CAT_(a, b) a ## b
|
||||
#define $CAT(a, b) $CAT_(a, b)
|
||||
#define $DEFER(...) const auto $CAT(_$__defer_, __LINE__) = ::defer_call( __VA_ARGS__ )
|
||||
}
|
||||
|
||||
namespace Input {
|
||||
struct Parser::_impl {
|
||||
struct {
|
||||
const char* data;
|
||||
size_t len;
|
||||
} src;
|
||||
|
||||
const char* cur_line;
|
||||
size_t cur_len;
|
||||
};
|
||||
Parser::Parser(const char* string, size_t len) noexcept
|
||||
: state_(std::make_unique<_impl>())
|
||||
{
|
||||
state_->cur_line = state_->src.data = string;
|
||||
state_->cur_len = state_->src.len = len;
|
||||
}
|
||||
|
||||
Parser::~Parser() {}
|
||||
|
||||
Parser::Parser(Parser&& p) noexcept
|
||||
: state_(std::move(p.state_)) {}
|
||||
|
||||
Parser& Parser::operator=(Parser&& p) noexcept
|
||||
{
|
||||
if(this != std::addressof(p)) {
|
||||
state_ = std::move(p.state_);
|
||||
} return *this;
|
||||
}
|
||||
|
||||
bool Parser::try_read_next(Output<Line> output)
|
||||
{
|
||||
const char* start = state_->cur_line;
|
||||
const char* end = gmemchr(start, '\n', state_->cur_len);
|
||||
if(!end) return false;
|
||||
|
||||
size_t diff = (size_t)(end - state_->cur_line);
|
||||
|
||||
if(diff > 1) {
|
||||
auto string = std::string_view{ start, gmemchr_s(start, ' ', diff - 1) };
|
||||
|
||||
for(size_t i=0;
|
||||
string.size() && string.data() < end;
|
||||
i++,
|
||||
string = std::string_view{ start += string.size(), // start = start + last size
|
||||
gmemchr_s(start, ' ', diff - 1) // XXX: <-- this should be `start + string.size()`
|
||||
})
|
||||
{
|
||||
switch(i) {
|
||||
case 0: output->start = parse_point(string); break; // First point
|
||||
case 1: continue; // Arrow
|
||||
case 2: output->end = parse_point(string); break; // Second point
|
||||
case 3: continue; // Arrow
|
||||
default: goto le; // Next point, quit
|
||||
}
|
||||
}
|
||||
le:
|
||||
end = ((const char*)string.data()) + string.size();
|
||||
state_->cur_len = (size_t)(end - state_->cur_line); // TODO: Is this the correct length calculation? I don't think it is....
|
||||
state_->cur_line = end;
|
||||
return true;
|
||||
} else return false;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue