update naming style across range module

master
Daniel Kolesa 2017-02-16 20:02:55 +01:00
parent 3edcafd9a6
commit 68c19a80fb
10 changed files with 665 additions and 647 deletions

View File

@ -65,10 +65,10 @@ inline auto is_partitioned(F &&func) {
namespace detail { namespace detail {
template<typename R, typename C> template<typename R, typename C>
static void insort(R range, C &compare) { static void insort(R range, C &compare) {
RangeSize<R> rlen = range.size(); range_size_t<R> rlen = range.size();
for (RangeSize<R> i = 1; i < rlen; ++i) { for (range_size_t<R> i = 1; i < rlen; ++i) {
RangeSize<R> j = i; range_size_t<R> j = i;
RangeValue<R> v(std::move(range[i])); range_value_t<R> v(std::move(range[i]));
while (j > 0 && !compare(range[j - 1], v)) { while (j > 0 && !compare(range[j - 1], v)) {
range[j] = range[j - 1]; range[j] = range[j - 1];
--j; --j;
@ -79,12 +79,12 @@ namespace detail {
template<typename R, typename C> template<typename R, typename C>
static void hs_sift_down( static void hs_sift_down(
R range, RangeSize<R> s, RangeSize<R> e, C &compare R range, range_size_t<R> s, range_size_t<R> e, C &compare
) { ) {
RangeSize<R> r = s; range_size_t<R> r = s;
while ((r * 2 + 1) <= e) { while ((r * 2 + 1) <= e) {
RangeSize<R> ch = r * 2 + 1; range_size_t<R> ch = r * 2 + 1;
RangeSize<R> sw = r; range_size_t<R> sw = r;
if (compare(range[sw], range[ch])) { if (compare(range[sw], range[ch])) {
sw = ch; sw = ch;
} }
@ -103,15 +103,15 @@ namespace detail {
template<typename R, typename C> template<typename R, typename C>
static void heapsort(R range, C &compare) { static void heapsort(R range, C &compare) {
RangeSize<R> len = range.size(); range_size_t<R> len = range.size();
RangeSize<R> st = (len - 2) / 2; range_size_t<R> st = (len - 2) / 2;
for (;;) { for (;;) {
detail::hs_sift_down(range, st, len - 1, compare); detail::hs_sift_down(range, st, len - 1, compare);
if (st-- == 0) { if (st-- == 0) {
break; break;
} }
} }
RangeSize<R> e = len - 1; range_size_t<R> e = len - 1;
while (e > 0) { while (e > 0) {
using std::swap; using std::swap;
swap(range[e], range[0]); swap(range[e], range[0]);
@ -121,7 +121,7 @@ namespace detail {
} }
template<typename R, typename C> template<typename R, typename C>
static void introloop(R range, C &compare, RangeSize<R> depth) { static void introloop(R range, C &compare, range_size_t<R> depth) {
using std::swap; using std::swap;
if (range.size() <= 10) { if (range.size() <= 10) {
detail::insort(range, compare); detail::insort(range, compare);
@ -132,7 +132,7 @@ namespace detail {
return; return;
} }
swap(range[range.size() / 2], range.back()); swap(range[range.size() / 2], range.back());
RangeSize<R> pi = 0; range_size_t<R> pi = 0;
R pr = range; R pr = range;
pr.pop_back(); pr.pop_back();
for (; !pr.empty(); pr.pop_front()) { for (; !pr.empty(); pr.pop_front()) {
@ -151,7 +151,7 @@ namespace detail {
inline void introsort(R range, C &compare) { inline void introsort(R range, C &compare) {
detail::introloop( detail::introloop(
range, compare, range, compare,
static_cast<RangeSize<R>>(2 * (log(range.size()) / log(2))) static_cast<range_size_t<R>>(2 * (log(range.size()) / log(2)))
); );
} }
} /* namespace detail */ } /* namespace detail */
@ -172,7 +172,7 @@ inline auto sort_cmp(C &&compare) {
template<typename R> template<typename R>
inline R sort(R range) { inline R sort(R range) {
return sort_cmp(range, std::less<RangeValue<R>>()); return sort_cmp(range, std::less<range_value_t<R>>());
} }
inline auto sort() { inline auto sort() {
return [](auto &&obj) { return sort(std::forward<decltype(obj)>(obj)); }; return [](auto &&obj) { return sort(std::forward<decltype(obj)>(obj)); };
@ -531,8 +531,8 @@ inline auto find_one_of(R &&values) {
} }
template<typename R, typename T> template<typename R, typename T>
inline RangeSize<R> count(R range, T const &v) { inline range_size_t<R> count(R range, T const &v) {
RangeSize<R> ret = 0; range_size_t<R> ret = 0;
for (; !range.empty(); range.pop_front()) { for (; !range.empty(); range.pop_front()) {
if (range.front() == v) { if (range.front() == v) {
++ret; ++ret;
@ -549,8 +549,8 @@ inline auto count(T &&v) {
} }
template<typename R, typename P> template<typename R, typename P>
inline RangeSize<R> count_if(R range, P pred) { inline range_size_t<R> count_if(R range, P pred) {
RangeSize<R> ret = 0; range_size_t<R> ret = 0;
for (; !range.empty(); range.pop_front()) { for (; !range.empty(); range.pop_front()) {
if (pred(range.front())) { if (pred(range.front())) {
++ret; ++ret;
@ -567,8 +567,8 @@ inline auto count_if(F &&func) {
} }
template<typename R, typename P> template<typename R, typename P>
inline RangeSize<R> count_if_not(R range, P pred) { inline range_size_t<R> count_if_not(R range, P pred) {
RangeSize<R> ret = 0; range_size_t<R> ret = 0;
for (; !range.empty(); range.pop_front()) { for (; !range.empty(); range.pop_front()) {
if (!pred(range.front())) { if (!pred(range.front())) {
++ret; ++ret;
@ -775,52 +775,52 @@ inline auto foldr_f(T &&init, F &&func) {
} }
template<typename T, typename F, typename R> template<typename T, typename F, typename R>
struct MapRange: InputRange<MapRange<T, F, R>> { struct map_range: input_range<map_range<T, F, R>> {
using Category = RangeCategory<T>; using range_category = range_category_t<T>;
using Value = R; using value_type = R;
using Reference = R; using reference = R;
using Size = RangeSize<T>; using size_type = range_size_t<T>;
using Difference = RangeDifference<T>; using difference_type = range_difference_t<T>;
private: private:
T p_range; T p_range;
std::decay_t<F> p_func; std::decay_t<F> p_func;
public: public:
MapRange() = delete; map_range() = delete;
template<typename FF> template<typename FF>
MapRange(T const &range, FF &&func): map_range(T const &range, FF &&func):
p_range(range), p_func(std::forward<FF>(func)) {} p_range(range), p_func(std::forward<FF>(func)) {}
MapRange(MapRange const &it): map_range(map_range const &it):
p_range(it.p_range), p_func(it.p_func) {} p_range(it.p_range), p_func(it.p_func) {}
MapRange(MapRange &&it): map_range(map_range &&it):
p_range(std::move(it.p_range)), p_func(std::move(it.p_func)) {} p_range(std::move(it.p_range)), p_func(std::move(it.p_func)) {}
MapRange &operator=(MapRange const &v) { map_range &operator=(map_range const &v) {
p_range = v.p_range; p_range = v.p_range;
p_func = v.p_func; p_func = v.p_func;
return *this; return *this;
} }
MapRange &operator=(MapRange &&v) { map_range &operator=(map_range &&v) {
p_range = std::move(v.p_range); p_range = std::move(v.p_range);
p_func = std::move(v.p_func); p_func = std::move(v.p_func);
return *this; return *this;
} }
bool empty() const { return p_range.empty(); } bool empty() const { return p_range.empty(); }
RangeSize<T> size() const { return p_range.size(); } range_size_t<T> size() const { return p_range.size(); }
bool equals_front(MapRange const &r) const { bool equals_front(map_range const &r) const {
return p_range.equals_front(r.p_range); return p_range.equals_front(r.p_range);
} }
bool equals_back(MapRange const &r) const { bool equals_back(map_range const &r) const {
return p_range.equals_front(r.p_range); return p_range.equals_front(r.p_range);
} }
RangeDifference<T> distance_front(MapRange const &r) const { range_difference_t<T> distance_front(map_range const &r) const {
return p_range.distance_front(r.p_range); return p_range.distance_front(r.p_range);
} }
RangeDifference<T> distance_back(MapRange const &r) const { range_difference_t<T> distance_back(map_range const &r) const {
return p_range.distance_back(r.p_range); return p_range.distance_back(r.p_range);
} }
@ -830,42 +830,42 @@ public:
bool push_front() { return p_range.pop_front(); } bool push_front() { return p_range.pop_front(); }
bool push_back() { return p_range.push_back(); } bool push_back() { return p_range.push_back(); }
RangeSize<T> pop_front_n(RangeSize<T> n) { range_size_t<T> pop_front_n(range_size_t<T> n) {
p_range.pop_front_n(n); p_range.pop_front_n(n);
} }
RangeSize<T> pop_back_n(RangeSize<T> n) { range_size_t<T> pop_back_n(range_size_t<T> n) {
p_range.pop_back_n(n); p_range.pop_back_n(n);
} }
RangeSize<T> push_front_n(RangeSize<T> n) { range_size_t<T> push_front_n(range_size_t<T> n) {
return p_range.push_front_n(n); return p_range.push_front_n(n);
} }
RangeSize<T> push_back_n(RangeSize<T> n) { range_size_t<T> push_back_n(range_size_t<T> n) {
return p_range.push_back_n(n); return p_range.push_back_n(n);
} }
R front() const { return p_func(p_range.front()); } R front() const { return p_func(p_range.front()); }
R back() const { return p_func(p_range.back()); } R back() const { return p_func(p_range.back()); }
R operator[](RangeSize<T> idx) const { R operator[](range_size_t<T> idx) const {
return p_func(p_range[idx]); return p_func(p_range[idx]);
} }
MapRange slice(RangeSize<T> start, RangeSize<T> end) { map_range slice(range_size_t<T> start, range_size_t<T> end) {
return MapRange(p_range.slice(start, end), p_func); return map_range(p_range.slice(start, end), p_func);
} }
}; };
namespace detail { namespace detail {
template<typename R, typename F> template<typename R, typename F>
using MapReturnType = decltype( using MapReturnType = decltype(
std::declval<F>()(std::declval<RangeReference<R>>()) std::declval<F>()(std::declval<range_reference_t<R>>())
); );
} }
template<typename R, typename F> template<typename R, typename F>
inline MapRange<R, F, detail::MapReturnType<R, F>> map(R range, F func) { inline map_range<R, F, detail::MapReturnType<R, F>> map(R range, F func) {
return MapRange<R, F, detail::MapReturnType<R, F>>(range, std::move(func)); return map_range<R, F, detail::MapReturnType<R, F>>(range, std::move(func));
} }
template<typename F> template<typename F>
@ -876,12 +876,14 @@ inline auto map(F &&func) {
} }
template<typename T, typename F> template<typename T, typename F>
struct FilterRange: InputRange<FilterRange<T, F>> { struct filter_range: input_range<filter_range<T, F>> {
using Category = std::common_type_t<RangeCategory<T>, ForwardRangeTag>; using range_category = std::common_type_t<
using Value = RangeValue<T>; range_category_t<T>, forward_range_tag
using Reference = RangeReference<T>; >;
using Size = RangeSize<T>; using value_type = range_value_t<T>;
using Difference = RangeDifference<T>; using reference = range_reference_t<T>;
using size_type = range_size_t<T>;
using difference_type = range_difference_t<T>;
private: private:
T p_range; T p_range;
@ -894,31 +896,31 @@ private:
} }
public: public:
FilterRange() = delete; filter_range() = delete;
template<typename P> template<typename P>
FilterRange(T const &range, P &&pred): filter_range(T const &range, P &&pred):
p_range(range), p_pred(std::forward<P>(pred)) p_range(range), p_pred(std::forward<P>(pred))
{ {
advance_valid(); advance_valid();
} }
FilterRange(FilterRange const &it): filter_range(filter_range const &it):
p_range(it.p_range), p_pred(it.p_pred) p_range(it.p_range), p_pred(it.p_pred)
{ {
advance_valid(); advance_valid();
} }
FilterRange(FilterRange &&it): filter_range(filter_range &&it):
p_range(std::move(it.p_range)), p_pred(std::move(it.p_pred)) p_range(std::move(it.p_range)), p_pred(std::move(it.p_pred))
{ {
advance_valid(); advance_valid();
} }
FilterRange &operator=(FilterRange const &v) { filter_range &operator=(filter_range const &v) {
p_range = v.p_range; p_range = v.p_range;
p_pred = v.p_pred; p_pred = v.p_pred;
advance_valid(); advance_valid();
return *this; return *this;
} }
FilterRange &operator=(FilterRange &&v) { filter_range &operator=(filter_range &&v) {
p_range = std::move(v.p_range); p_range = std::move(v.p_range);
p_pred = std::move(v.p_pred); p_pred = std::move(v.p_pred);
advance_valid(); advance_valid();
@ -927,7 +929,7 @@ public:
bool empty() const { return p_range.empty(); } bool empty() const { return p_range.empty(); }
bool equals_front(FilterRange const &r) const { bool equals_front(filter_range const &r) const {
return p_range.equals_front(r.p_range); return p_range.equals_front(r.p_range);
} }
@ -937,19 +939,19 @@ public:
return ret; return ret;
} }
RangeReference<T> front() const { return p_range.front(); } range_reference_t<T> front() const { return p_range.front(); }
}; };
namespace detail { namespace detail {
template<typename R, typename P> template<typename R, typename P>
using FilterPred = std::enable_if_t<std::is_same_v< using FilterPred = std::enable_if_t<std::is_same_v<
decltype(std::declval<P>()(std::declval<RangeReference<R>>())), bool decltype(std::declval<P>()(std::declval<range_reference_t<R>>())), bool
>, P>; >, P>;
} }
template<typename R, typename P> template<typename R, typename P>
inline FilterRange<R, detail::FilterPred<R, P>> filter(R range, P pred) { inline filter_range<R, detail::FilterPred<R, P>> filter(R range, P pred) {
return FilterRange<R, P>(range, std::move(pred)); return filter_range<R, P>(range, std::move(pred));
} }
template<typename F> template<typename F>

View File

@ -21,7 +21,7 @@ namespace sdl {
#ifdef OSTD_EXT_SDL_USE_SDL1 #ifdef OSTD_EXT_SDL_USE_SDL1
using SDLRWopsOffset = int; using SDLRWopsOffset = int;
#else #else
using SDLRWopsOffset = Int64; using SDLRWopsOffset = int64_t;
#endif #endif
inline SDL_RWops *stream_to_rwops(Stream &s) { inline SDL_RWops *stream_to_rwops(Stream &s) {

View File

@ -225,11 +225,11 @@ inline void swap(FileInfo &a, FileInfo &b) {
a.swap(b); a.swap(b);
} }
struct DirectoryRange; struct directory_range;
#ifndef OSTD_PLATFORM_WIN32 #ifndef OSTD_PLATFORM_WIN32
struct DirectoryStream { struct DirectoryStream {
friend struct DirectoryRange; friend struct directory_range;
DirectoryStream(): p_d(), p_de(), p_path() {} DirectoryStream(): p_d(), p_de(), p_path() {}
DirectoryStream(DirectoryStream const &) = delete; DirectoryStream(DirectoryStream const &) = delete;
@ -324,7 +324,7 @@ struct DirectoryStream {
swap(p_path, s.p_path); swap(p_path, s.p_path);
} }
DirectoryRange iter(); directory_range iter();
private: private:
static bool pop_front(DIR *d, struct dirent **de) { static bool pop_front(DIR *d, struct dirent **de) {
@ -366,7 +366,7 @@ private:
#else /* OSTD_PLATFORM_WIN32 */ #else /* OSTD_PLATFORM_WIN32 */
struct DirectoryStream { struct DirectoryStream {
friend struct DirectoryRange; friend struct directory_range;
DirectoryStream(): p_handle(INVALID_HANDLE_VALUE), p_data(), p_path() {} DirectoryStream(): p_handle(INVALID_HANDLE_VALUE), p_data(), p_path() {}
DirectoryStream(DirectoryStream const &) = delete; DirectoryStream(DirectoryStream const &) = delete;
@ -495,7 +495,7 @@ struct DirectoryStream {
swap(p_path, s.p_path); swap(p_path, s.p_path);
} }
DirectoryRange iter(); directory_range iter();
private: private:
bool pop_front() { bool pop_front() {
@ -529,18 +529,18 @@ inline void swap(DirectoryStream &a, DirectoryStream &b) {
a.swap(b); a.swap(b);
} }
struct DirectoryRange: InputRange<DirectoryRange> { struct directory_range: input_range<directory_range> {
using Category = InputRangeTag; using range_category = input_range_tag;
using Value = FileInfo; using value_type = FileInfo;
using Reference = FileInfo; using reference = FileInfo;
using Size = size_t; using size_type = size_t;
using Difference = long; using difference_type = long;
DirectoryRange() = delete; directory_range() = delete;
DirectoryRange(DirectoryStream &s): p_stream(&s) {} directory_range(DirectoryStream &s): p_stream(&s) {}
DirectoryRange(DirectoryRange const &r): p_stream(r.p_stream) {} directory_range(directory_range const &r): p_stream(r.p_stream) {}
DirectoryRange &operator=(DirectoryRange const &r) { directory_range &operator=(directory_range const &r) {
p_stream = r.p_stream; p_stream = r.p_stream;
return *this; return *this;
} }
@ -557,7 +557,7 @@ struct DirectoryRange: InputRange<DirectoryRange> {
return p_stream->front(); return p_stream->front();
} }
bool equals_front(DirectoryRange const &s) const { bool equals_front(directory_range const &s) const {
return p_stream == s.p_stream; return p_stream == s.p_stream;
} }
@ -565,8 +565,8 @@ private:
DirectoryStream *p_stream; DirectoryStream *p_stream;
}; };
inline DirectoryRange DirectoryStream::iter() { inline directory_range DirectoryStream::iter() {
return DirectoryRange(*this); return directory_range(*this);
} }
namespace detail { namespace detail {

View File

@ -757,8 +757,8 @@ namespace detail {
template<typename R, typename T> template<typename R, typename T>
size_t write_val(R &writer, bool escape, T const &val) const { size_t write_val(R &writer, bool escape, T const &val) const {
/* stuff fhat can be custom-formatted goes first */ /* stuff fhat can be custom-formatted goes first */
if constexpr(FmtTofmtTest<T, TostrRange<R>>) { if constexpr(FmtTofmtTest<T, tostr_range<R>>) {
TostrRange<R> sink(writer); tostr_range<R> sink(writer);
to_format(val, sink, *this); to_format(val, sink, *this);
return sink.get_written(); return sink.get_written();
} }

View File

@ -152,11 +152,11 @@ static FileStream err(stderr);
namespace detail { namespace detail {
/* lightweight output range for direct stdout */ /* lightweight output range for direct stdout */
struct StdoutRange: OutputRange<StdoutRange> { struct StdoutRange: output_range<StdoutRange> {
using Value = char; using value_type = char;
using Reference = char &; using reference = char &;
using Size = size_t; using size_type = size_t;
using Difference = ptrdiff_t; using difference_type = ptrdiff_t;
StdoutRange() {} StdoutRange() {}
bool put(char c) { bool put(char c) {

File diff suppressed because it is too large Load Diff

View File

@ -31,7 +31,7 @@ enum class StreamSeek {
}; };
template<typename T = char, bool = std::is_pod_v<T>> template<typename T = char, bool = std::is_pod_v<T>>
struct StreamRange; struct stream_range;
struct Stream { struct Stream {
using Offset = StreamOffset; using Offset = StreamOffset;
@ -111,7 +111,7 @@ struct Stream {
} }
template<typename T = char> template<typename T = char>
StreamRange<T> iter(); stream_range<T> iter();
template<typename T> template<typename T>
size_t put(T const *v, size_t count) { size_t put(T const *v, size_t count) {
@ -141,22 +141,22 @@ struct Stream {
}; };
template<typename T> template<typename T>
size_t range_put_n(StreamRange<T> &range, T const *p, size_t n); size_t range_put_n(stream_range<T> &range, T const *p, size_t n);
template<typename T> template<typename T>
struct StreamRange<T, true>: InputRange<StreamRange<T>> { struct stream_range<T, true>: input_range<stream_range<T>> {
using Category = InputRangeTag; using range_category = input_range_tag;
using Value = T; using value_type = T;
using Reference = T; using reference = T;
using Size = size_t; using size_type = size_t;
using Difference = StreamOffset; using difference_type = StreamOffset;
template<typename TT> template<typename TT>
friend size_t range_put_n(StreamRange<TT> &range, TT const *p, size_t n); friend size_t range_put_n(stream_range<TT> &range, TT const *p, size_t n);
StreamRange() = delete; stream_range() = delete;
StreamRange(Stream &s): p_stream(&s), p_size(s.size()) {} stream_range(Stream &s): p_stream(&s), p_size(s.size()) {}
StreamRange(StreamRange const &r): p_stream(r.p_stream), p_size(r.p_size) {} stream_range(stream_range const &r): p_stream(r.p_stream), p_size(r.p_size) {}
bool empty() const { bool empty() const {
return (p_size - p_stream->tell()) < StreamOffset(sizeof(T)); return (p_size - p_stream->tell()) < StreamOffset(sizeof(T));
@ -176,7 +176,7 @@ struct StreamRange<T, true>: InputRange<StreamRange<T>> {
return val; return val;
} }
bool equals_front(StreamRange const &s) const { bool equals_front(stream_range const &s) const {
return p_stream->tell() == s.p_stream->tell(); return p_stream->tell() == s.p_stream->tell();
} }
@ -199,43 +199,43 @@ private:
}; };
template<typename T> template<typename T>
inline size_t range_put_n(StreamRange<T> &range, T const *p, size_t n) { inline size_t range_put_n(stream_range<T> &range, T const *p, size_t n) {
return range.p_stream->put(p, n); return range.p_stream->put(p, n);
} }
template<typename T> template<typename T>
inline StreamRange<T> Stream::iter() { inline stream_range<T> Stream::iter() {
return StreamRange<T>(*this); return stream_range<T>(*this);
} }
namespace detail { namespace detail {
/* lightweight output range for write/writef on streams */ /* lightweight output range for write/writef on streams */
struct FmtStreamRange: OutputRange<FmtStreamRange> { struct fmt_stream_range: output_range<fmt_stream_range> {
using Value = char; using value_type = char;
using Reference = char &; using reference = char &;
using Size = size_t; using size_type = size_t;
using Difference = ptrdiff_t; using difference_type = ptrdiff_t;
FmtStreamRange(Stream &s): p_s(s) {} fmt_stream_range(Stream &s): p_s(s) {}
bool put(char c) { bool put(char c) {
return p_s.write_bytes(&c, 1) == 1; return p_s.write_bytes(&c, 1) == 1;
} }
Stream &p_s; Stream &p_s;
}; };
inline size_t range_put_n(FmtStreamRange &range, char const *p, size_t n) { inline size_t range_put_n(fmt_stream_range &range, char const *p, size_t n) {
return range.p_s.write_bytes(p, n); return range.p_s.write_bytes(p, n);
} }
} }
template<typename T> template<typename T>
inline void Stream::write(T const &v) { inline void Stream::write(T const &v) {
format(detail::FmtStreamRange{*this}, FormatSpec{'s'}, v); format(detail::fmt_stream_range{*this}, FormatSpec{'s'}, v);
} }
template<typename ...A> template<typename ...A>
inline void Stream::writef(string_range fmt, A const &...args) { inline void Stream::writef(string_range fmt, A const &...args) {
format(detail::FmtStreamRange{*this}, fmt, args...); format(detail::fmt_stream_range{*this}, fmt, args...);
} }
} }

View File

@ -23,12 +23,12 @@
namespace ostd { namespace ostd {
template<typename T, typename TR = std::char_traits<std::remove_const_t<T>>> template<typename T, typename TR = std::char_traits<std::remove_const_t<T>>>
struct basic_char_range: InputRange<basic_char_range<T>> { struct basic_char_range: input_range<basic_char_range<T>> {
using Category = ContiguousRangeTag; using range_category = ContiguousRangeTag;
using Value = T; using value_type = T;
using Reference = T &; using reference = T &;
using Size = size_t; using size_type = size_t;
using Difference = ptrdiff_t; using difference_type = ptrdiff_t;
private: private:
struct Nat {}; struct Nat {};
@ -187,7 +187,7 @@ diffsize:
} }
template<typename R> template<typename R>
std::enable_if_t<IsOutputRange<R>, size_t> copy(R &&orange, size_t n = -1) { std::enable_if_t<is_output_range<R>, size_t> copy(R &&orange, size_t n = -1) {
return range_put_n(orange, data(), ostd::min(n, size())); return range_put_n(orange, data(), ostd::min(n, size()));
} }
@ -417,8 +417,8 @@ template<
> >
inline std::basic_string<T, TR, A> make_string(R range, A const &alloc = A{}) { inline std::basic_string<T, TR, A> make_string(R range, A const &alloc = A{}) {
std::basic_string<T, TR, A> ret{alloc}; std::basic_string<T, TR, A> ret{alloc};
using C = RangeCategory<R>; using C = range_category_t<R>;
if constexpr(std::is_convertible_v<C, FiniteRandomAccessRangeTag>) { if constexpr(std::is_convertible_v<C, finite_random_access_range_tag>) {
/* finite random access or contiguous */ /* finite random access or contiguous */
auto h = range.half(); auto h = range.half();
ret.reserve(range.size()); ret.reserve(range.size());
@ -433,13 +433,13 @@ inline std::basic_string<T, TR, A> make_string(R range, A const &alloc = A{}) {
} }
template< template<
typename R, typename TR = std::char_traits<std::remove_cv_t<RangeValue<R>>>, typename R, typename TR = std::char_traits<std::remove_cv_t<range_value_t<R>>>,
typename A = std::allocator<std::remove_cv_t<RangeValue<R>>> typename A = std::allocator<std::remove_cv_t<range_value_t<R>>>
> >
inline std::basic_string<std::remove_cv_t<RangeValue<R>>, TR, A> make_string( inline std::basic_string<std::remove_cv_t<range_value_t<R>>, TR, A> make_string(
R range, A const &alloc = A{} R range, A const &alloc = A{}
) { ) {
return make_string<std::remove_cv_t<RangeValue<R>>, TR, A>( return make_string<std::remove_cv_t<range_value_t<R>>, TR, A>(
std::move(range), alloc std::move(range), alloc
); );
} }
@ -531,17 +531,17 @@ bool concat(R &&sink, std::initializer_list<T> v, string_range sep = " ") {
namespace detail { namespace detail {
template<typename R> template<typename R>
struct TostrRange: OutputRange<TostrRange<R>> { struct tostr_range: output_range<tostr_range<R>> {
using Value = char; using value_type = char;
using Reference = char &; using reference = char &;
using Size = size_t; using size_type = size_t;
using Difference = ptrdiff_t; using difference_type = ptrdiff_t;
template<typename RR> template<typename RR>
friend size_t range_put_n(TostrRange<RR> &range, char const *p, size_t n); friend size_t range_put_n(tostr_range<RR> &range, char const *p, size_t n);
TostrRange() = delete; tostr_range() = delete;
TostrRange(R &out): p_out(out), p_written(0) {} tostr_range(R &out): p_out(out), p_written(0) {}
bool put(char v) { bool put(char v) {
bool ret = p_out.put(v); bool ret = p_out.put(v);
p_written += ret; p_written += ret;
@ -559,7 +559,7 @@ namespace detail {
}; };
template<typename R> template<typename R>
inline size_t range_put_n(TostrRange<R> &range, char const *p, size_t n) { inline size_t range_put_n(tostr_range<R> &range, char const *p, size_t n) {
size_t ret = range_put_n(range.p_out, p, n); size_t ret = range_put_n(range.p_out, p, n);
range.p_written += ret; range.p_written += ret;
return ret; return ret;
@ -603,7 +603,7 @@ struct ToString<T, std::enable_if_t<detail::IterableTest<T>>> {
auto x = appender<std::string>(); auto x = appender<std::string>();
if (concat(x, ostd::iter(v), ", ", ToString< if (concat(x, ostd::iter(v), ", ", ToString<
std::remove_const_t<std::remove_reference_t< std::remove_const_t<std::remove_reference_t<
RangeReference<decltype(ostd::iter(v))> range_reference_t<decltype(ostd::iter(v))>
>> >>
>())) { >())) {
ret += x.get(); ret += x.get();
@ -615,14 +615,14 @@ struct ToString<T, std::enable_if_t<detail::IterableTest<T>>> {
template<typename T> template<typename T>
struct ToString<T, std::enable_if_t< struct ToString<T, std::enable_if_t<
detail::StringifyTest<T, detail::TostrRange<AppenderRange<std::string>>> detail::StringifyTest<T, detail::tostr_range<appender_range<std::string>>>
>> { >> {
using Argument = std::remove_cv_t<std::remove_reference_t<T>>; using Argument = std::remove_cv_t<std::remove_reference_t<T>>;
using Result = std::string; using Result = std::string;
std::string operator()(T const &v) const { std::string operator()(T const &v) const {
auto app = appender<std::string>(); auto app = appender<std::string>();
detail::TostrRange<AppenderRange<std::string>> sink(app); detail::tostr_range<appender_range<std::string>> sink(app);
if (!v.to_string(sink)) { if (!v.to_string(sink)) {
return std::string{}; return std::string{};
} }
@ -803,7 +803,7 @@ std::string to_string(std::initializer_list<T> init) {
template<typename R> template<typename R>
struct TempCString { struct TempCString {
private: private:
std::remove_cv_t<RangeValue<R>> *p_buf; std::remove_cv_t<range_value_t<R>> *p_buf;
bool p_allocated; bool p_allocated;
public: public:
@ -813,13 +813,13 @@ public:
s.p_buf = nullptr; s.p_buf = nullptr;
s.p_allocated = false; s.p_allocated = false;
} }
TempCString(R input, std::remove_cv_t<RangeValue<R>> *sbuf, size_t bufsize) TempCString(R input, std::remove_cv_t<range_value_t<R>> *sbuf, size_t bufsize)
: p_buf(nullptr), p_allocated(false) { : p_buf(nullptr), p_allocated(false) {
if (input.empty()) { if (input.empty()) {
return; return;
} }
if (input.size() >= bufsize) { if (input.size() >= bufsize) {
p_buf = new std::remove_cv_t<RangeValue<R>>[input.size() + 1]; p_buf = new std::remove_cv_t<range_value_t<R>>[input.size() + 1];
p_allocated = true; p_allocated = true;
} else { } else {
p_buf = sbuf; p_buf = sbuf;
@ -838,8 +838,8 @@ public:
return *this; return *this;
} }
operator std::remove_cv_t<RangeValue<R>> const *() const { return p_buf; } operator std::remove_cv_t<range_value_t<R>> const *() const { return p_buf; }
std::remove_cv_t<RangeValue<R>> const *get() const { return p_buf; } std::remove_cv_t<range_value_t<R>> const *get() const { return p_buf; }
void swap(TempCString &s) { void swap(TempCString &s) {
using std::swap; using std::swap;
@ -855,7 +855,7 @@ inline void swap(TempCString<R> &a, TempCString<R> &b) {
template<typename R> template<typename R>
inline TempCString<R> to_temp_cstr( inline TempCString<R> to_temp_cstr(
R input, std::remove_cv_t<RangeValue<R>> *buf, size_t bufsize R input, std::remove_cv_t<range_value_t<R>> *buf, size_t bufsize
) { ) {
return TempCString<R>(input, buf, bufsize); return TempCString<R>(input, buf, bufsize);
} }

View File

@ -36,13 +36,13 @@ inline std::unordered_map<K, T, H, E, A> make_unordered_map(
E const &kequal = E{}, A const &alloc = A{} E const &kequal = E{}, A const &alloc = A{}
) { ) {
static_assert( static_assert(
detail::is_2tuple_like<RangeValue<R>>, detail::is_2tuple_like<range_value_t<R>>,
"the range element must be a pair/2-tuple" "the range element must be a pair/2-tuple"
); );
using MP = std::pair<K const, T>; using MP = std::pair<K const, T>;
using AK = std::tuple_element_t<0, RangeValue<R>>; using AK = std::tuple_element_t<0, range_value_t<R>>;
using AV = std::tuple_element_t<1, RangeValue<R>>; using AV = std::tuple_element_t<1, range_value_t<R>>;
static_assert( static_assert(
std::is_constructible_v<K const, AK> && std::is_constructible_v<T, AV>, std::is_constructible_v<K const, AK> && std::is_constructible_v<T, AV>,
@ -51,21 +51,21 @@ inline std::unordered_map<K, T, H, E, A> make_unordered_map(
std::unordered_map<K, T, H, E, A> ret{bcount, hash, kequal, alloc}; std::unordered_map<K, T, H, E, A> ret{bcount, hash, kequal, alloc};
using C = RangeCategory<R>; using C = range_category_t<R>;
if constexpr(std::is_convertible_v<C, FiniteRandomAccessRangeTag>) { if constexpr(std::is_convertible_v<C, finite_random_access_range_tag>) {
/* at least try to preallocate here... */ /* at least try to preallocate here... */
ret.reserve(range.size()); ret.reserve(range.size());
} }
for (; !range.empty(); range.pop_front()) { for (; !range.empty(); range.pop_front()) {
if constexpr(std::is_constructible_v<MP, RangeValue<R>>) { if constexpr(std::is_constructible_v<MP, range_value_t<R>>) {
ret.emplace(range.front()); ret.emplace(range.front());
} else { } else {
/* store a temporary to prevent calling front() twice; however, /* store a temporary to prevent calling front() twice; however,
* for values that can be used to construct the pair directly * for values that can be used to construct the pair directly
* we can just do the above * we can just do the above
*/ */
RangeValue<R> v{range.front()}; range_value_t<R> v{range.front()};
ret.emplace(std::move(std::get<0>(v)), std::move(std::get<1>(v))); ret.emplace(std::move(std::get<0>(v)), std::move(std::get<1>(v)));
} }
} }
@ -74,27 +74,27 @@ inline std::unordered_map<K, T, H, E, A> make_unordered_map(
template< template<
typename R, typename R,
typename H = std::hash<typename RangeValue<R>::first_type>, typename H = std::hash<typename range_value_t<R>::first_type>,
typename E = std::equal_to<typename RangeValue<R>::first_type>, typename E = std::equal_to<typename range_value_t<R>::first_type>,
typename A = std::allocator<std::pair< typename A = std::allocator<std::pair<
std::tuple_element_t<0, RangeValue<R>>, std::tuple_element_t<0, range_value_t<R>>,
std::tuple_element_t<1, RangeValue<R>> std::tuple_element_t<1, range_value_t<R>>
>> >>
> >
inline std::unordered_map< inline std::unordered_map<
std::tuple_element_t<0, RangeValue<R>>, std::tuple_element_t<0, range_value_t<R>>,
std::tuple_element_t<1, RangeValue<R>>, H, E, A std::tuple_element_t<1, range_value_t<R>>, H, E, A
> make_unordered_map( > make_unordered_map(
R &&range, size_t bcount = 1, H const &hash = H{}, R &&range, size_t bcount = 1, H const &hash = H{},
E const &kequal = E{}, A const &alloc = A{} E const &kequal = E{}, A const &alloc = A{}
) { ) {
static_assert( static_assert(
detail::is_2tuple_like<RangeValue<R>>, detail::is_2tuple_like<range_value_t<R>>,
"the range element must be a pair/2-tuple" "the range element must be a pair/2-tuple"
); );
return make_unordered_map< return make_unordered_map<
std::tuple_element_t<0, RangeValue<R>>, std::tuple_element_t<0, range_value_t<R>>,
std::tuple_element_t<1, RangeValue<R>>, H, E, A std::tuple_element_t<1, range_value_t<R>>, H, E, A
>(std::forward<R>(range), bcount, hash, kequal, alloc); >(std::forward<R>(range), bcount, hash, kequal, alloc);
} }

View File

@ -17,8 +17,8 @@ namespace ostd {
template<typename T, typename A = std::allocator<T>, typename R> template<typename T, typename A = std::allocator<T>, typename R>
inline std::vector<T, A> make_vector(R range, A const &alloc = A{}) { inline std::vector<T, A> make_vector(R range, A const &alloc = A{}) {
std::vector<T, A> ret{alloc}; std::vector<T, A> ret{alloc};
using C = RangeCategory<R>; using C = range_category_t<R>;
if constexpr(std::is_convertible_v<C, FiniteRandomAccessRangeTag>) { if constexpr(std::is_convertible_v<C, finite_random_access_range_tag>) {
/* finite random access or contiguous */ /* finite random access or contiguous */
auto h = range.half(); auto h = range.half();
ret.reserve(range.size()); ret.reserve(range.size());
@ -32,11 +32,11 @@ inline std::vector<T, A> make_vector(R range, A const &alloc = A{}) {
return ret; return ret;
} }
template<typename R, typename A = std::allocator<RangeValue<R>>> template<typename R, typename A = std::allocator<range_value_t<R>>>
inline std::vector<RangeValue<R>, A> make_vector( inline std::vector<range_value_t<R>, A> make_vector(
R &&range, A const &alloc = A{} R &&range, A const &alloc = A{}
) { ) {
return make_vector<RangeValue<R>, A>(std::forward<R>(range), alloc); return make_vector<range_value_t<R>, A>(std::forward<R>(range), alloc);
} }
} /* namespace ostd */ } /* namespace ostd */