JH-Toolkit v1.4.1
An engineering-oriented C++20 toolkit with duck-typed concepts, static design, async coroutines, and semantic containers — header-only, RTTI-free, and concurrency-friendly.
Loading...
Searching...
No Matches
common.h File Reference

A unified common view adaptor for both std::ranges::range and jh::concepts::sequence types. More...

#include <ranges>
#include "jh/conceptual/sequence.h"

Go to the source code of this file.

Namespaces

namespace  jh::ranges
 Semantic pipeline namespace for JH range operations.
namespace  jh::ranges::views
 Range view adaptor implementations with explicit semantic control.

Variables

constexpr detail::common_fn jh::ranges::views::common {}
 User-facing common adaptor instance.

Detailed Description

A unified common view adaptor for both std::ranges::range and jh::concepts::sequence types.

Author
JeongHan-Bae mastr.nosp@m.opse.nosp@m.udo@g.nosp@m.mail.nosp@m..com

This header provides jh::ranges::views::common — a lightweight and compatible wrapper around the standard std::views::common adaptor (C++20). It extends the adaptor to support any type modeling jh::concepts::sequence, enabling third-party or user-defined sequence types to interoperate naturally with the standard range pipeline.

The adaptor normalizes iterator/sentinel categories without altering data ownership semantics. For standard ranges, it behaves identically to std::views::common. For types satisfying jh::concepts::sequence, it performs a promotion through jh::to_range() to obtain a valid range representation. Non-copyable or non-movable sequences are automatically proxied via std::ranges::subrange when required.

Behavior summary:

  • If the source models jh::concepts::sequence, it is promoted using jh::to_range() and wrapped by std::ranges::common_view.
  • If the source models only std::ranges::range, the adaptor forwards to std::views::common().
  • Existing common_range types remain unchanged, except when proxied for non-copyable sources.

Usage examples:

using namespace jh::ranges::views;
// 1. Standard range usage
auto v1 = std::views::iota(0, 5) | common();
// equivalent to std::views::common(iota(...))
// 2. A user-defined type modeling jh::concepts::sequence
MySequence seq = ...; // satisfies jh::concepts::sequence
auto v2 = seq | common();
// promoted through jh::to_range() and std::views::common()
// 3. A non-copyable but reentrant range
jh::runtime_arr<int> arr{1, 2, 3};
auto v3 = arr | common();
// already a common_range; produces a subrange proxy if needed
// 4. Composed pipeline example
auto combined =
ids | enumerate(100)
| zip_pipe(names)
| flatten()
| common();
A move-only, fixed-capacity array with runtime-determined length and RAII-based ownership.
Definition runtime_arr.h:367
Range view adaptor implementations with explicit semantic control.
Definition common.h:99
constexpr detail::common_fn common
User-facing common adaptor instance.
Definition common.h:270
constexpr detail::zip_pipe_fn zip_pipe
Extended multi-argument zip adaptor for pipe syntax.
Definition zip.h:317
constexpr detail::flatten_fn flatten
User-facing flatten adaptor.
Definition flatten.h:255
constexpr detail::enumerate_fn enumerate
The user-facing enumerate adaptor.
Definition enumerate.h:149

Notes:

  • Applying common() to a temporary object results in a dangling view. Always ensure the source object outlives the resulting view.
  • If the input already models std::ranges::common_range, common() performs no semantic change; it may return a subrange proxy for non-copyable or non-movable sources.
  • For consuming (single-pass) ranges, the adaptor directly invokes std::views::common().
Version
1.3.x
Date
2025