ååã®æçš¿ã®æåŸã«ãæ¢åã®ééã®æ¬ ç¹ããŸãšããŸããã
- åºåãããééãšç¡éã®ééã¯äžè¯ã³ãŒããçæããŸã
- 圌ãã¯åœŒããã§ããããã匱ãæŠå¿µãã¢ãã«åããå¿ èŠããããŸã
- ããªãã¯ãããæ¶åããªãã¢ã«ãŽãªãºã ã«ç°¡åã«ç¡éã®ééãæž¡ãããšãã§ããŸã
- 䜿ãã«ãã
- ç¡éãŸãã¯éåžžã«å€§ããå¯èœæ§ã®ããééã¯ãdifference_typeã§ãªãŒããŒãããŒãåŒãèµ·ããå¯èœæ§ããããŸã
æåã®åé¡ã¯ç¹ã«é£ããã®ã§ãå§ããŸãããã
ééã®æŠå¿µ
ã¯ããã«ãééã®æŠå¿µãå³å¯ã«å®çŸ©ããŸãããã C ++æšæºã§ã¯ããã®åèªã¯ã©ãã§ã䜿çšãããŸãããã©ãã§ãå®çŸ©ãããŠããŸããã ééã¯ãbeginãšendãæœåºã§ãããã®ã§ãããbeginã§éå§ããããšã§endã«å°éã§ããããã«é 眮ãããã€ãã¬ãŒã¿ã®ãã¢ã§ããããšãç解ã§ããŸãã ç§ã®ææ¡ã«é¢ããŠã¯ããã®æŠå¿µã¯æ¬¡ã®ããã«åœ¢åŒåã§ããŸãã
using std::begin; using std::end; template<typename T> using Iterator_type = decltype(begin(std::declval<T>())); template<typename T> concept bool Range = requires(T range) { { begin(range) } -> Iterator_type<T>; { end(range) } -> Iterator_type<T>; requires Iterator<Iterator_type<T>>; };
InputRangeãForwardRangeãªã©ãšåŒã°ããç¯å²ã¹ãã³ã®æŠå¿µãæ¹åãããŠããŸãã å®éã圌ãã¯åã«ã€ãã¬ãŒã¿ããããå€ããå¿ èŠãšããŸãã éå±€å šäœã以äžã«ç€ºããŸãã
ãããã®æŠå¿µã¯ãBoost.Rangeã©ã€ãã©ãªïŒhttp://www.boost.org/libs/rangeïŒã®åºç€ã§ã
åé¡1ïŒäžæ£ãªã³ãŒãçæ
èŠããŠããå Žåããªããã¿ãŒãšç¡éã®äž¡æ¹ã®åºéããã€ãã¬ãŒã¿ãŒã®ãã¢ã®åœ¢ã§å®è£ ããã«ã¯ãçµäºã€ãã¬ãŒã¿ãŒã¯ã·ã°ãã«ã§ãªããã°ãªããŸããã ãããŠããã®ãããªã€ãã¬ãŒã¿ã¯ãã·ãŒã±ã³ã¹å ã®èŠçŽ ã®ç©ççãªäœçœ®ãšãããããæŠå¿µã§ãã äœçœ®ããlast + 1ãã®èŠçŽ ãšããŠæ³åã§ããŸã-å¯äžã®éãã¯ãå°éãããŸã§åœŒã®äœçœ®ãããããªãããšã§ãã ã·ã°ãã«å埩åã®ã¿ã€ãã¯éåžžã®ãã®ãšåãã§ãããããæå®ãããå埩åãã·ã°ãã«ãã©ãããå€æããã«ã¯ãããã°ã©ã å®è¡æ®µéã§ã®ãã§ãã¯ãå¿ èŠã§ãã ããã«ãããã€ãã¬ãŒã¿ã®æ¯èŒãé ããªããã¡ãœããã®å®è£ ãäžäŸ¿ã«ãªããŸãã
ã€ã³ã¯ãªã¡ã³ã¿ãŒã®æŠå¿µïŒå埩å¯èœïŒ
ã€ãã¬ãŒã¿ãŒãšã¯äœã§ããïŒ ããããæ¡å€§ããéåç §ããŠæ¯èŒããŸããïŒ ãããŠãã·ã°ãã«ã€ãã¬ãŒã¿ã§äœãã§ããŸããïŒ ç¹ã«ãããŸããã 圌ã®äœçœ®ã¯å€ãããã圌ã¯éåç §ã§ããŸããã 圌ã®äœçœ®ã¯åžžã«ãlast + 1ãã§ãã ãã ããã€ãã¬ãŒã¿ãšæ¯èŒã§ããŸãã ã·ã°ãã«å埩åã¯åŒ±ãå埩åã§ããããšãããããŸãã
ééã®åé¡ã¯ãã·ã°ãã«ã€ãã¬ãŒã¿ãéåžžã®ã€ãã¬ãŒã¿ã«å€ããããšããŠããããšã§ãã ãããã圌ã¯ããã§ã¯ãããŸããã ãããã£ãŠãæã ã¯ãããããŸãã- ããããç°ãªãã¿ã€ãã«ããŸãã
ç¯å²ã®æŠå¿µã§ã¯ãéå§ãšçµäºãåãã¿ã€ãã§ããããšãå¿ èŠã§ãã ããããç°ãªããã®ã«ã§ãããªããããã¯ãã§ã«ãã匱ãæŠå¿µ-Iterableã®æŠå¿µã«ãªããŸãã ã€ã³ã¯ãªã¡ã³ãã¯ã€ãã¬ãŒã¿ãŒã®ãããªãã®ã§ãéå§ãšçµäºã®ã¿ãç°ãªãã¿ã€ããæã¡ãŸãã ã³ã³ã»ããïŒ
template<typename T> using Sentinel_type = decltype(end(std::declval<T>())); template<typename T> concept bool Iterable = requires(T range) { { begin(range) } -> Iterator_type<T>; { end(range) } -> Sentinel_type<T>; requires Iterator<Iterator_type<T>>; requires EqualityComparable< Iterator_type<T>, Sentinel_type<T>>; }; template<typename T> concept bool Range = Iteratable<T> && Same<Iterator_type<T>, Sentinel_type<T>>;
åœç¶ãRangeã³ã³ã»ããã¯Iterableã³ã³ã»ããã®äžéšã§ãã 圌女ã¯ãéå§ã¿ã€ããšçµäºã¿ã€ãã«çåŒå¶çŽãè¿œå ããããšã«ãããåçŽã«æ¹è¯ããŸãã
ããã¯ãééãã€ã³ã¯ãªã¡ã³ã¿ãŒãããã³ã€ãã¬ãŒã¿ãŒãèæ ®ãããšéå±€ãã©ã®ããã«èŠãããã§ãããããã°ã©ã ã§ãããããã®ããã«å®çŸ©ããå¿ èŠã¯ãŸã£ãããããŸããã ãééããã€ãŸããéå§ã¿ã€ããšçµäºã¿ã€ãã®é¡äŒŒæ§ã¯ãéå§å埩åã®åŒ·åºŠã«çŽäº€ããããšã«æ³šæããŠãã ããã RandomAccessRangeã¢ããªã³ã°ãã³ãŒãã«å«ããå¿ èŠãããå ŽåãRandomAccessIterable && Rangeãå¿ èŠã§ãããã³ã³ã»ããå šäœãå€æŽããã ãã§ãããšèšããŸãã
ããšãã°ãéå§å埩å¯èœã€ãã¬ãŒã¿ãŒã«ãã£ãŠã¢ãã«åãããæŠå¿µã«ãããBidirectionalIterableãšForwardIterableã®éãã
å埩å¯èœããã³STLã¢ã«ãŽãªãºã
ããããã¡ãã£ãšåŸ ã£ãŠãã ãããçµå±ãSTLã¢ã«ãŽãªãºã ã¯ã€ã³ã¯ãªã¡ã³ã¿ãŒã§ã¯åäœããŸããããªããªããéå§ãšçµäºãåãã¿ã€ãã§ããå¿ èŠãããããã§ãã ããã§ãã ããã§ãSTLå šäœã調ã¹ãŠãäœãæžãæãããããã確èªããŸããã ããšãã°ãstd :: find
template<class InputIterator, class Value> InputIterator find(InputIterator first, InputIterator last, Value const & value) { for (; first != last; ++first) if (*first == value) break; return first; }
çŸåšãstd :: findã¯ç¯å²ã䜿çšããŠããŸãã ãã ããã¢ã«ãŽãªãºã ã¯çµäºå埩åã®äœçœ®ãå€æŽããããšããªãããšã«æ³šæããŠãã ããã æ€çŽ¢ã¢ã«ãŽãªãºã ã¯ãç¯å²ã§ã¯ãªãã€ãã©ãã«ã§åäœããããã«ç°¡åã«å€æŽã§ããŸãã
template<class InputIterator, class Sentinel, class Value> InputIterator find(InputIterator first, Sentinel last, Value const & value) { for (; first != last; ++first) if (*first == value) break; return first; }
ããã ãã§ã-å€æŽã¯éåžžã«å°ãããããæ°ã¥ãããšããå°é£ã§ãã
ããã§ã¯ãã©ã®C ++ 98ã¢ã«ãŽãªãºã ãç¯å²ã§ã¯ãªãIterablesã§åäœããããã«é©åãããããšãã§ããŸããïŒ ã»ãšãã©ãã¹ãŠãå€æããŸããã æåŠãããã®ããªã¹ãããæ¹ãç°¡åã§ãã ããã¯ïŒ
- copy_backward
- ã»ãããšãã©ããããæäœããããã®ã¢ã«ãŽãªãºã ïŒpush_heapãpop_heapãmake_heapãsort_heapïŒ
- inplace_merge
- nth_element
- partial_sortããã³partial_sort_copy
- next_permutationããã³prev_permutation
- random_shuffle
- reverseããã³reverse_copy
- sortããã³stable_sort
- stable_partition
æ®ãã®50åã¯ãã³ãŒããçŽç²ã«æ©æ¢°çã«å€æŽããå¿ èŠããããŸãã Rangeã§å®çŸ©ãããŠããIterableã®æŠå¿µãå®çŸ©ããã®ã§ãIterableã§åäœããã¢ã«ãŽãªãºã ã«ãåãæ¹æ³ã§Rangesã§åäœããæ©èœãæäŸããŸãã ããã¯äŸ¿å©ã§éèŠã§ã-ã€ãã¬ãŒã¿ãäœããã®äºææ§ã®ãªãæœè±¡åãè¡ãããã«ãã€ãã¬ãŒã¿ã«å¯ŸããŠèšè¿°ãããã³ãŒããå€ãããŸãã
ããã©ãŒãã³ã¹ã®èšŒæ
ãããŠãç§ãã¡ã¯äœãåŸãŸããïŒ Cã©ã€ã³ã«æ»ããŸãããã c_string_rangeã¯ã©ã¹ã«ã€ããŠèª¬æããŸããããæåã®åæã«ãã£ãŠäžæ£ãªã³ãŒããçæãããããšãããããŸããã Range_facadeã®ã¿ã䜿çšããŠãç¯å²ã§ã¯ãªãIterableããã«ãããŸãã
using namespace ranges; struct c_string_iterable : range_facade<c_string_iterable> { private: friend range_core_access; char const *sz_; char const & current() const { return *sz_; } void next() { ++sz_; } bool done() const { return *sz_ == 0; } bool equal(c_string_iterable const &that) const { return sz_ == that.sz_; } public: c_string_iterable(char const *sz) : sz_(sz) {} };
ã³ãŒãã¯å€ããã®ãããã¯ããã«åçŽã§ãã Range_facadeã¯ãã¹ãŠã®äœæ¥ãè¡ããŸãã ã€ãã¬ãŒã¿ãšã·ã°ãã«ã€ãã¬ãŒã¿ã¯ãããªããã£ããšããŠå®è£ ãããŸãã ããããã¹ãããããã«ã次ã®é¢æ°çšã«æé©åããããã·ã³ã³ãŒããçæããŸããã1ã€ã¯å€ãc_string_rangeã¯ã©ã¹ã䜿çšãããã1ã€ã¯æ°ããc_string_iterableã䜿çšããŸãã
// Range-based int range_strlen( c_string_range::iterator begin, c_string_range::iterator end) { int i = 0; for(; begin != end; ++begin) ++i; return i; } // Iterable-based int iterable_strlen( range_iterator_t<c_string_iterable> begin, range_sentinel_t<c_string_iterable> end) { int i = 0; for(; begin != end; ++begin) ++i; return i; }
ã¢ã»ã³ãã©ãŒãç¥ããªããŠããéããç解ã§ããŸãã
|
|
ã€ã³ã¯ãªã¡ã³ã¿ã䜿çšããã³ãŒãã¯ãã¯ããã«ã¯ãŒã«ã§ãã ãããŠãããã¯ã裞ã®ãCã®ã€ãã¬ãŒã¿ããåŸãããã¢ã»ã³ãã©ãšã»ãšãã©åãã§ãã
å埩åãä¿¡å·å埩åãããã³ããªãã£
ããããç䟡æ§ã«ã€ããŠç°ãªãã¿ã€ãã®2ã€ã®ãªããžã§ã¯ããæ¯èŒããããšã¯ã©ãããæå³ã§ããïŒ
æªçµéšè åãïŒ N3351ã¯ãã©ã®å Žåã«ç°ãªãã¿ã€ãã®æ¯èŒãèš±å¯ãããã決å®ããŸãã æ§æãx == yããæå¹ã§ãããããŒã«å€ãçæããã ãã§ã¯äžååã§ãã xãšyã®åãç°ãªãå Žåããããã®åèªäœã¯EqualityComparableã§ããå¿ èŠããããå€æã§ããå ±éã®åã§ããå¿ èŠããããEqualityComparableã§ããå¿ èŠããããŸãã charãšshortãæ¯èŒãããšããŸãã ããã¯ãEqualityComparableã§ãããEqualityComparableã§ãããintã«å€æã§ããããå¯èœã§ãã
ã€ãã¬ãŒã¿ã¯æ¯èŒã§ããã·ã°ãã«ã€ãã¬ãŒã¿ã¯ç°¡åãªæ¹æ³ã§æ¯èŒãããŸãã å°é£ãªã®ã¯ã圌ãã«å ±éã®ã¿ã€ããèŠã€ããããšã§ãã äžè¬ã«ãåå埩åãšä¿¡å·ã«ã¯å ±éã®ã¿ã€ããããã次ã®ããã«äœæã§ããŸããæ°ããã¿ã€ãã®å埩åIãååšãããšä»®å®ããŸãã ããããæ¯èŒãããšãäž¡æ¹ãšãæåã«ã¿ã€ãIã®2ã€ã®ãªããžã§ã¯ãã«å€æããããã®ããã«æå³çã«åäœããlhsããã³rhsãšåŒã³ã次ã®ãã¬ãŒãã§æ¯èŒããŸãã
lhsã·ã°ãã«ã€ãã¬ãŒã¿ïŒ
| rhsã·ã°ãã«ã€ãã¬ãŒã¿ïŒ
| lhs == rhsïŒ
|
æ¬åœ
| æ¬åœ
| æ¬åœ
|
æ¬åœ
| åœ
| å®äºïŒrhs.iterïŒ
|
åœ
| æ¬åœ
| å®äºïŒlhs.iterïŒ
|
åœ
| åœ
| lhs.iter == rhs.iter
|
ãã®ãã¬ãŒãã¯ãæ¯èŒæŒç®åc_string_range :: iteratorã®åäœãåæãããšãã«åŸããããã¬ãŒãã«äŒŒãŠããŸã ã ããã¯å¶ç¶ã§ã¯ãããŸãã-ããã¯ãã®ããäžè¬çãªã¹ããŒã ã®ç¹å¥ãªã±ãŒã¹ã§ããã ããã¯ã2ã€ã®ã¯ã©ã¹c_string_rangeãšc_string_iterableãèŠããšããã§ã«æ°ä»ããŠãããšããçŽæçãªçµè«ãè£ä»ããŠããŸãã 1ã€ã¯ã€ãã¬ãŒã¿ã®ãã¢ããã1ã€ã¯ã€ãã¬ãŒã¿/ä¿¡å·ã®ãã¢ã§ããããããã®æäœã¹ããŒã ã¯äŒŒãŠããŸãã å°ãã®ããã©ãŒãã³ã¹ãç ç²ã«ãããšãã€ãã©ãã«ããåçã®ç¯å²ãæ§ç¯ããããšãå¯èœã ãšæããŠããŸãã ãããŠä»ãç§ãã¡ã¯ããã®ç¢ºèªãèŠã€ããŸããã
ã€ãã¬ãŒã¿ãšã·ã°ãã«ã€ãã¬ãŒã¿ãçŽæ¥æ¯èŒããæ©èœã«ãããC ++åã·ã¹ãã ã䜿çšããŠå埩ã®å€§ããªã«ããŽãªãæé©åããããªãã£æ¯èŒæŒç®åã®åå²ãåé€ã§ããŸãã
ç°è°
ç°ãªãçš®é¡ã®éå§ãšçµäºãäžãããšããèãã¯æ°ãããã®ã§ã¯ãªãããããçºæããã®ã¯ç§ã§ã¯ãããŸããã§ããã ç§ã¯äœå¹Žãåã«ããã«ã€ããŠãã€ãã»ã¢ãã©ãã ãºããåŠã³ãŸããã æè¿ãåæ§ã®ã¢ã€ãã¢ãRangesã¡ãŒãªã³ã°ãªã¹ãã§Dietmar Kuehl ã«ãã£ãŠæ瀺ãããŸããã圌ã®æçŽã«å¿ããŠãSean Parentã¯å察ããŸããã
ç§ãã¡ã¯ã€ãã¬ãŒã¿ãŒã䜿ããããŠãããšæããŸãã ã·ã°ãã«ã€ãã¬ãŒã¿ã®æ€èšŒãŸãã¯ã«ãŠã³ãã«åºã¥ããšã³ãã£ã³ã°ã§æ©èœããã¢ã«ãŽãªãºã ã¯ãç°ãªããšã³ãã£ãã£ã§ãã copy_nïŒïŒããã³copy_sentinelïŒïŒãåç §
stlab.adobe.com/copy_8hpp.html
ééã«ã€ããŠ-ç§ã¯ããªãããããæ§ç¯ã§ãããšç¢ºä¿¡ããŠããŸãïŒ
- ã€ãã¬ãŒã¿ã®ãã¢
- ã€ãã¬ãŒã¿ãšé
- ã€ãã¬ãŒã¿ãšä¿¡å·
ãã®å ŽåãcopyïŒrãoutïŒã§ç®çã®ã¢ã«ãŽãªãºã ãäœæã§ããŸãã
ç§ã圌ãæ£ããç解ããŠããã°ã圌ã¯ãIteratorRangeãCountedRangeãããã³SentinelRangeã®3ã€ã®äžŠè¡ããééã®æŠå¿µã®ååšã«ã€ããŠè©±ããŠããŸãã ãããŠããããã®éå±€ã¯çžäºã«é¢ä¿ä»ããããšããå¿ èŠã¯ãããŸããã ã³ããŒã¢ã«ãŽãªãºã ã«ã¯ãæŠå¿µããšã«1ã€ãã€ã3ã€ã®ç°ãªãå®è£ ãå«ããå¿ èŠããããŸãã 次ã«ãçŽ50ã®ã¢ã«ãŽãªãºã ã3åã«ããå¿ èŠããããŸããããã¯ãã³ãŒãã®ç¹°ãè¿ããå€ãããŸãã
ããã«æªãããšã«ãäžéšã®ã¢ã«ãŽãªãºã ã¯æŽç·ŽãããæŠå¿µã«åºã¥ããŠããŸãã ããšãã°ã libc ++ã§ã¯ ãrotateã¢ã«ãŽãªãºã 㯠ãçŽæ¥å埩åãåæ¹åå埩åããŸãã¯ã©ã³ãã ã¢ã¯ã»ã¹å埩åãæž¡ããã©ããã«å¿ããŠã3ã€ã®å®è£ ã®ãããããéžæããŸãã ãããŠãIteratorãCountedãããã³SentinelRangesã®3ã€ã®ç°ãªãå®è£ ãå«ããã«ã¯ã9ã€ã®å転ã¢ã«ãŽãªãºã ãå¿ èŠã§ãã ãã¹ãŠã®æ¬æãæã£ãŠãããã¯ã¯ã¬ã€ãžãŒã§ãã
åèš
æçš¿ã®åé ã§ããã¢ã®ã€ãã¬ãŒã¿ãŒãšã®ééã«é¢é£ããåé¡ã®ãªã¹ãã瀺ããŸããã ããã©ãŒãã³ã¹ã®åé¡ãæ±ãæ°ããæŠå¿µIterableã瀺ããééã®å®è£ ã®è€éãã®åé¡ãæèµ·ããŸããã ãããŸã§ã®ãšããããã®æŠå¿µãç¡éã®ééã§ã©ã®ããã«æ©èœãããã«ã€ããŠã¯èª¬æããŠããŸãããããã«ã€ããŠã¯ã4çªç®ã®æçµæçš¿ã§èª¬æããŸãã
ãã¹ãŠã®ã³ãŒãã¯githubãªããžããªã«ãããŸãã
è¬èŸ
Andrew Suttonã«Concpets Liteã®æ§æãæ¯æŽããããŸããŸãªã¿ã€ãã®EqualityComparableã³ã³ã»ããã®èŠä»¶ã説æããå€ãã®ã¢ã€ãã¢ã®äžè¬çãªæ¹åãšåœ¢åŒåã«æè¬ããŸãã 圌ã®å€å€§ãªè²¢ç®ã®ãããã§ãèšäºã¯ãã£ãšè¯ããªããŸããã
ãã®ä»ã®ãµã€ã¯ã«èšäº
C ++ã®ééãããŒã1ïŒåºåãèšå·ä»ãã®éé
C ++ã®ééãããŒã2ïŒç¡éã®éé
C ++ã®ééãããŒã4ïŒç¡é倧ãŸã§