|
||||
File indexing completed on 2025-01-18 09:37:58
0001 /*! 0002 @file 0003 Forward declares `boost::hana::Iterable`. 0004 0005 Copyright Louis Dionne 2013-2022 0006 Distributed under the Boost Software License, Version 1.0. 0007 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) 0008 */ 0009 0010 #ifndef BOOST_HANA_FWD_CONCEPT_ITERABLE_HPP 0011 #define BOOST_HANA_FWD_CONCEPT_ITERABLE_HPP 0012 0013 #include <boost/hana/config.hpp> 0014 0015 0016 namespace boost { namespace hana { 0017 //! @ingroup group-concepts 0018 //! @defgroup group-Iterable Iterable 0019 //! The `Iterable` concept represents data structures supporting external 0020 //! iteration. 0021 //! 0022 //! Intuitively, an `Iterable` can be seen as a kind of container whose 0023 //! elements can be pulled out one at a time. An `Iterable` also provides 0024 //! a way to know when the _container_ is empty, i.e. when there are no 0025 //! more elements to pull out. 0026 //! 0027 //! Whereas `Foldable` represents data structures supporting internal 0028 //! iteration with the ability to accumulate a result, the `Iterable` 0029 //! concept allows inverting the control of the iteration. This is more 0030 //! flexible than `Foldable`, since it allows iterating over only some 0031 //! part of the structure. This, in turn, allows `Iterable` to work on 0032 //! infinite structures, while trying to fold such a structure would 0033 //! never finish. 0034 //! 0035 //! 0036 //! Minimal complete definition 0037 //! --------------------------- 0038 //! `at`, `drop_front` and `is_empty` 0039 //! 0040 //! 0041 //! @anchor Iterable-lin 0042 //! The linearization of an `Iterable` 0043 //! ---------------------------------- 0044 //! Intuitively, for an `Iterable` structure `xs`, the _linearization_ of 0045 //! `xs` is the sequence of all the elements in `xs` as if they had been 0046 //! put in a (possibly infinite) list: 0047 //! @code 0048 //! linearization(xs) = [x1, x2, x3, ...] 0049 //! @endcode 0050 //! 0051 //! The `n`th element of the linearization of an `Iterable` can be 0052 //! accessed with the `at` function. In other words, `at(xs, n) == xn`. 0053 //! 0054 //! Note that this notion is precisely the extension of the [linearization] 0055 //! (@ref Foldable-lin) notion of `Foldable`s to the infinite case. This 0056 //! notion is useful for expressing various properties of `Iterable`s, 0057 //! and is used for that elsewhere in the documentation. 0058 //! 0059 //! 0060 //! Compile-time `Iterable`s 0061 //! ------------------------ 0062 //! A _compile-time_ `Iterable` is an `Iterable` for which `is_empty` 0063 //! returns a compile-time `Logical`. These structures allow iteration 0064 //! to be done at compile-time, in the sense that the "loop" doing the 0065 //! iteration can be unrolled because the total length of the structure 0066 //! is kown at compile-time. 0067 //! 0068 //! In particular, note that being a compile-time `Iterable` has nothing 0069 //! to do with being finite or infinite. For example, it would be possible 0070 //! to create a sequence representing the Pythagorean triples as 0071 //! `integral_constant`s. Such a sequence would be infinite, but iteration 0072 //! on the sequence would still be done at compile-time. However, if one 0073 //! tried to iterate over _all_ the elements of the sequence, the compiler 0074 //! would loop indefinitely, in contrast to your program looping 0075 //! indefinitely if the sequence was a runtime one. 0076 //! 0077 //! __In the current version of the library, only compile-time `Iterable`s 0078 //! are supported.__ While it would be possible in theory to support 0079 //! runtime `Iterable`s, doing it efficiently is the subject of some 0080 //! research. In particular, follow [this issue][1] for the current 0081 //! status of runtime `Iterable`s. 0082 //! 0083 //! 0084 //! Laws 0085 //! ---- 0086 //! First, we require the equality of two `Iterable`s to be related to the 0087 //! equality of the elements in their linearizations. More specifically, 0088 //! if `xs` and `ys` are two `Iterable`s of data type `It`, then 0089 //! @code 0090 //! xs == ys => at(xs, i) == at(ys, i) for all i 0091 //! @endcode 0092 //! 0093 //! This conveys that two `Iterable`s must have the same linearization 0094 //! in order to be considered equal. 0095 //! 0096 //! Secondly, since every `Iterable` is also a `Searchable`, we require 0097 //! the models of `Iterable` and `Searchable` to be consistent. This is 0098 //! made precise by the following laws. For any `Iterable` `xs` with a 0099 //! linearization of `[x1, x2, x3, ...]`, 0100 //! @code 0101 //! any_of(xs, equal.to(z)) <=> xi == z 0102 //! @endcode 0103 //! for some _finite_ index `i`. Furthermore, 0104 //! @code 0105 //! find_if(xs, pred) == just(the first xi such that pred(xi) is satisfied) 0106 //! @endcode 0107 //! or `nothing` if no such `xi` exists. 0108 //! 0109 //! 0110 //! Refined concepts 0111 //! ---------------- 0112 //! 1. `Searchable` (free model)\n 0113 //! Any `Iterable` gives rise to a model of `Searchable`, where the keys 0114 //! and the values are both the elements in the structure. Searching for 0115 //! a key is just doing a linear search through the elements of the 0116 //! structure. 0117 //! @include example/iterable/searchable.cpp 0118 //! 0119 //! 2. `Foldable` for finite `Iterable`s\n 0120 //! Every finite `Iterable` gives rise to a model of `Foldable`. For 0121 //! these models to be consistent, we require the models of both `Foldable` 0122 //! and `Iterable` to have the same linearization. 0123 //! 0124 //! @note 0125 //! As explained above, `Iterable`s are also `Searchable`s and their 0126 //! models have to be consistent. By the laws presented here, it also 0127 //! means that the `Foldable` model for finite `Iterable`s has to be 0128 //! consistent with the `Searchable` model. 0129 //! 0130 //! For convenience, finite `Iterable`s must only provide a definition of 0131 //! `length` to model the `Foldable` concept; defining the more powerful 0132 //! `unpack` or `fold_left` is not necessary (but still possible). The 0133 //! default implementation of `unpack` derived from `Iterable` + `length` 0134 //! uses the fact that `at(xs, i)` denotes the `i`th element of `xs`'s 0135 //! linearization, and that the linearization of a finite `Iterable` must 0136 //! be the same as its linearization as a `Foldable`. 0137 //! 0138 //! 0139 //! Concrete models 0140 //! --------------- 0141 //! `hana::tuple`, `hana::string`, `hana::range` 0142 //! 0143 //! 0144 //! [1]: https://github.com/boostorg/hana/issues/40 0145 template <typename It> 0146 struct Iterable; 0147 }} // end namespace boost::hana 0148 0149 #endif // !BOOST_HANA_FWD_CONCEPT_ITERABLE_HPP
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |