å 責äºé ïŒãã®èšäºã®ã¢ã€ãã¢ã¯ã以åã®ã¢ã€ãã¢ãããææ©çã§ãã è°è«ãã§ããŠå¬ããã§ãã
以åã®æçš¿ã§è©±ãã解決æžã¿ã®åé¡ã«å ããŠãããã«2ã€ãããŸãã ããã¯ïŒ
- äžéšã®STLã¢ã«ãŽãªãºã ã¯ç¡éã®ééã§æ©èœããŸãã
- ç¡éãŸãã¯ããããç¡éã®ééã¯ãdifference_typeãªãŒããŒãããŒã«ã€ãªãããŸã
ç¡éã€ãã¬ãŒã¿ãŒ
iota_range-ããå€ããå§ãŸããæ°žä¹ ã«ç¶ãç¡éã®æŽæ°ã®ç¯å²ã ããã¯ããœãŒããããåæ¹ã¹ããŒã¹ã§ãã 以äžã¯ããœãŒãããããŸã£ãããªééã§æ©èœãããã€ããªæ€çŽ¢ã¢ã«ãŽãªãºã ã§ãããæ©èœããŸããã åå²ã«ãã£ãŠç¡éãåŸæããããšã¯ã§ããŸããïŒè¯ããã¬ãŒãºãå€æããŸããïŒã
ç¡éã®ééãåä¿¡ãããšãã«å£ããªãããã«STLã¢ã«ãŽãªãºã ãä¿®æ£ããããšã¯å¯èœã§ããïŒ çŸåšã®åœ¢åŒã§ã¯ãããã-ã€ãã¬ãŒã¿ã®äžéšãç¡éã®ééã瀺ããšããã³ã³ãã€ã«æ®µéã§æ å ±ãäŒããããšã¯äžå¯èœã§ãã ããã«ã€ããŠèããŠã¿ãŠãã ããã次ã®ã³ãŒãã¯ç¢ºå®ã«æ©èœããŸãã
// : iota_range<bigint> rng; auto i = std::lower_bound(rng.begin(), std::next(rng.begin(), 10), 5);
ããããããã¯æ°žé ã«æ©èœããŸãïŒ
// «» :'-( iota_range<bigint> rng; auto i = std::lower_bound(rng.begin(), rng.end(), 5);
rng.beginïŒïŒãrng.endïŒïŒãšåãã¿ã€ãã®å Žåã2ã€ã®åŒã³åºãã¯åãlower_boundã€ã³ã¹ã¿ã³ã¹ãè¿ããŸãã é¢æ°ã¯ãæ°žä¹ ã«æ©èœãããã©ãããå€æã§ããŸããã ãã ããã·ã°ãã«ã€ãã¬ãŒã¿ãå¥ã®ã¿ã€ãã«ããããšãã§ããå Žåã¯ãã³ã³ãã€ã«æ®µéã§ãã®ãã§ãã¯ãå®è¡ã§ããŸãã
DenotesInfiniteSequenceãšåŒã°ããåé¢æ°ïŒã¡ã¿é¢æ°ïŒãããããããåã®ãã¢ïŒBeginTypeãEndTypeïŒãåããã·ãŒã±ã³ã¹ãç¡éãã©ãããå€æãããšããŸãã BeginTypeãšEndTypeãåãå ŽåãDenotesInfiniteSequenceã¯falseãè¿ãå¿ èŠãããããšãæ¢ã«æ±ºå®ããŠããŸããããã¯ããããã©ã®ãããªæ¹æ³ã§ãæ€èšŒã§ããªãããã§ãã ããããããããç°ãªãå Žå-EndTypeã®ã¿ã€ããunreachable_sentinelãŸãã¯ãã®ãããªãã®ã«ãªããšãããšãã³ã³ãã€ã«æ®µéã§ã·ãŒã±ã³ã¹ãç¡éã§ããããšãããããŸãã
ç¡éã®éé
äžéšã®ééã¯ãå®çŸ©äžãåãã¿ã€ãã®éå§å埩åãšçµäºå埩åãæã€ããšããããç¡éã«ãªããŸãã
// class zeros : public range_facade<zeros> { friend range_core_access; struct impl { bool sentinel; int current() const { return 0; } void next() {} bool equal(impl that) const { return sentinel == that.sentinel; } }; // begin() end() range_facade // begin_impl end_impl. impl begin_impl() const { return {false}; } impl end_impl() const { return {true}; } }; // Range CONCEPT_ASSERT(Range<zeros>()); int main() { // for_each(zeros(), [](int i) {/*...*/}); }
ãã®ãããªãšã©ãŒãããã«ãã£ãããããã®ã§ãããæããã«ããã€ããªé¢æ°DenotesInfiniteSequenceã¯ããã«å¯ŸåŠã§ããŸããã ãŒãã®å ŽåãBeginTypeã¿ã€ããšEndTypeã¿ã€ãã¯åãã«ãªããããDenotesInfiniteSequenceã¯falseãè¿ããŸãã
ãããã£ãŠãåºéåãåãå ¥ããIsInfiniteåã®åé é¢æ°ãäœæããå¿ èŠããããŸãã
// , Iterable template<typename Iterable> struct is_infinite : std::integral_constant<bool, true-or-false> {};
FiniteIterableã®æŠå¿µãå®çŸ©ããããã«äœ¿çšã§ããŸãã
// Concept Lite template<typename T> concept bool FiniteIterable = Iterable<T> && !is_infinite<T>::value;
åFiniteIterableã¯å埩å¯èœã§ãã æ¹è¯ã®äžŠåéå±€ã芳å¯ãããŸãã
ãŸãããããã®æŠå¿µã¯ãã¹ãŠãRangeãšã®é¡æšã«ãããã³ãŒãã§å®çŸ©ããå¿ èŠã¯ãããŸããã æéæ§ã¯Iterableéå±€ã«çŽäº€ããŠãããåå¥ã«åãåãããããšãã§ããŸãã
ãããããªãInfiniteIterableã§ã¯ãªãFiniteIterableãªã®ã§ããããïŒ ã¢ã«ãŽãªãºã ãšãã®èŠä»¶ããã¹ãŠã§ãã ééåŒæ°ãç¡éã«ããå¿ èŠãããã¢ã«ãŽãªãºã ã¯ãããŸããã ãããã£ãŠãInfiniteIterableãèšè¿°ã§ããŠãæå³ããããŸããã ããããlower_boundã®ãããªã¢ã«ãŽãªãºã ã¯ãééãæéã§ããããšãæã¿ãŸãã ãããšFiniteIterableããã
ããã¯ããã¹ãŠã®å埩åãããã©ã«ãã§FiniteIterableãã¢ãã«åãããã®åãäœããã®åœ¢ã§ç¡éã§ããããšãåŠç¿ããå¿ èŠãããããšãæå³ããŸãã ã©ããã£ãŠïŒ is_infiniteãç¹æ®åããããšãã§ããŸãã 幞ããªããšã«ãã€ã³ã¯ãªã¡ã³ã¿ãŒãšã€ã³ã¿ãŒãã«ãäœæããããŒã«ã¯ãªãã·ã§ã³ã®IsInfiniteãã©ã¡ãŒã¿ãŒãåãå ¥ãããããããã¯ç°¡åã§ãã zeroesã¯ã©ã¹ã¯æ¬¡ã®ããã«ãªããŸãã
// class zeros : public range_facade<zeros, true> { // ... IsInfinite ...................^^^^ // ... , ... }; // zeros â Range, Finite CONCEPT_ASSERT(Range<zeros>()); CONCEPT_ASSERT(!FiniteIterable<zeros>());
FiniteIterableã³ã³ã»ãããè¿œå ããããšã«ãããæéæ§ãå¿ èŠãšããã¢ã«ãŽãªãºã ã«ãã³ã³ãã€ã«æ®µéã§ãããæ€èšŒããæ©äŒãäžããŸãã
ããããç¡éã®éé
次ã«ãééãã«ããŽãªã«åå²ããå¿ èŠããããŸãã çè«çã«ã¯ãç¡éãŸãã¯æéã®ã©ã¡ããã§ããããã§ã¯ãããŸãããïŒ ãã istreamãå©çšããŠãã ããã ããã¯æéãããããªãããããã§ãªããããããªãã éåžžããããŒã¯åæ¢ããŸãããæã«ã¯...
ç¶æ³ã¯è€éã§ãã istreamã®ã¢ã«ãŽãªãºã ãžã®è»¢éãçŠæ¢ããå¿ èŠãããã®ã¯ããããç¡éã§ããå¯èœæ§ãããããã ãã§ããïŒ
ã«ãŠã³ãäžå¯
ç¡éã®ééã§ã¯ããã®ãããªå°é£ããããŸãããã¹ãŠã®ã€ãã¬ãŒã¿ãšãã¹ãŠã®ã€ã³ã¯ãªã¡ã³ã¿ã«ã¯ããããã«é¢é£ä»ããããdifference_typeããããŸãã ã¢ã¬ã¯ãµã³ããŒã»ã¹ããããã¯ããã«ã€ããŠæžããŠããŸãïŒ
DistanceTypeã¯ããã®åã«æå¹ãªåŸç¶ã¢ããªã±ãŒã·ã§ã³ã®ã·ãŒã±ã³ã¹ã枬å®ããã®ã«ååãªå€§ããã®æŽæ°åãè¿ããŸãã
ãããããµã€ãºã®ç¡éãµã€ãºãå¿ èŠã§ããããšãããããŸããã ãã®åé¡ã®è§£æ±ºçã¯ãããŸããïŒ
- C ++ã§ã¯ãbigintãå¿ èŠã§ã-粟床ãç¡éã®åå šäœã§ãã ä»ã®èšèªã§ã¯ããã§ãã C ++ã¯ã©ã€ãã©ãªãäœæããã®ã«æé©ãªèšèªã§ãããã©ã€ãã©ãªã¯ãããæ±ããŠããã ãã§ãã ãã®ãããªã¿ã€ãããã£ãå Žåãããã¯difference_typeãšããŠåãããŸãã
- ç¡éééã§ã¯ãsafe_intãdifference_typeãšããŠäœ¿çšã§ããŸãã safe_intã¯intã®ããã«åäœããŸãããç¡éãè¡šãããšãã§ããŸãã æ··ãã§ããŸããã difference_typeã®ãªãŒããŒãããŒã«é¢ãã2ã€ã®æãé£ããåé¡ã¯ãæªå®çŸ©ã®åäœãšãäœãééã£ãŠããããäºå®ãäŒããããšãã§ããªãããšã§ãã safe_intã䜿çšãããšãäžç¢ºå®æ§ãåé¿ããåŸã§äœãèµ·ããããåé¿ã§ããŸãã
- ãªãŒããŒãããŒæã«äŸå€ãã¹ããŒãã代æ¿ã®safe_intå®è£ ãææ¡ãããå ŽåããããŸãã
- ãŸããã©ã€ãã©ãªãdifference_typeã䜿çšããå Žæã確èªããå¥ã®ã¿ã€ããããã§äœ¿çšãããããã«ãŠãŒã¶ãŒã«æå®ãããããšãã§ããŸãã ããšãã°ãè·é¢èšç®ã¢ã«ãŽãªãºã APIã¯ãééãšãªãã·ã§ã³ã®åæå€ãåãå ŽåããããŸãã ããã©ã«ãã§ã¯ãdifference_type {0}ã§åäœããŸãããbigintãæž¡ããå Žåã¯ãç°ãªããããé ãããããå®å šãªæ¹æ³ã§åäœããŸãã
- åé¡ãç¡èŠã§ããŸãã ãããŠããªãŒããŒãããŒãæ°ã«ãã人ã¯ãã«ãŠã³ãããŠã³ééã¢ããã¿ãŒïŒhttps://github.com/ericniebler/range-v3/blob/master/include/range/v3/view/counted.hppïŒã䜿çšããŠç¢ºèªã§ããŸãããã®ç¹°ãè¿ãã¯ãdifference_typeããªãŒããŒãããŒããåã«åæ¢ããŸãã
- ç§ã«ã¯èµ·ãããªãã£ãäœãã
ãã¡ãããã¢ã«ãŽãªãºã ã®åäœãé ããããã®ãã¹ãŠã奜ãã§ã¯ãªãã®ã§ãstd :: ptrdiff_tã¯ããã©ã«ãã§difference_typeã®ãŸãŸã«ããŠããããšãã§ããŸãã ããã«ããŠãŒã¶ãŒããªãŒããŒãããŒãå¿é ããå Žåã«å¥ã®difference_typeãèšå®ã§ããããã«ãã€ã³ã¿ãŒãã«ã€ã³ã¿ãŒãã§ã€ã¹ãèšèšããå¿ èŠããããŸãã ãããã£ãŠããªãã·ã§ã³4ãš5ã奜ãã§ããç¹å®ã®å¶éãæã€bigintãsafe_intã®ãããªä»ã®ã©ã€ãã©ãªã¿ã€ãã¯ã䜿çšã§ãããšããã§ãããã
çµæãšæ¬¡ã«äœããã¹ãã
ãããããåã®æçš¿ãèªãã§ããéãããªãã¯é床ã«æ¥œèŠ³çã§ããïŒãã¹ãŠãããŸãããå§ããŠããããã§ãä»ããªãã¯æ··ä¹±ããŠããŸãã ããããç§ãå§ããå Žæãšæ¯èŒããŠãç§ãã¡ã¯é 調ã«é²æ©ããŠããããã«æããŸãã ã€ãã¬ãŒã¿ã®ãã¢ã䜿çšããŠãééã«é¢ãã5ã€ã®åé¡ã説æããŸããã ã€ã³ã¯ãªã¡ã³ã¿ãŒã®æ°ããæŠå¿µã¯ãããã®ãã¡3ã€ã解決ãã4çªç®ã®åé¡ïŒç¡éééïŒã¯ãã®æŠå¿µãæ¹åããããšã§çè«çã«è§£æ±ºã§ããŸãã ãŸãã5çªç®ïŒãªãŒããŒãããŒïŒãåŠçããããã®ãªãã·ã§ã³ãããã€ããããŸãã å°ãªããšãéå§ãããŸããã
ç§ã®ã¢ã€ãã¢ãC ++æšæºåå§å¡äŒã«äŒããããšæã人ãããŸãã ãã¡ããã èšèªã®æŠå¿µã®ãµããŒããåŸããããšãç°ãªãåå空éã§åäœããå¯èœæ§ã®ããæŠå¿µåãããããŒãžã§ã³ã®STLãå¿ èŠã«ãªããŸãã
ãããŸã§ã®éãSG9ïŒRangesïŒã¡ãŒãªã³ã°ãªã¹ãïŒhttp://www.open-std.org/mailman/listinfo/rangesïŒã§ãã®åé¡ãè°è«ããŸãã ãã®è°è«ã®ããåé¡ã¯ãæ°ããã¢ã€ãã¢ãçã¿åºãå¯èœæ§ããããŸãã èå³ã®ããæ¹ã¯ããã£ã¹ã«ãã·ã§ã³ã«åå ããããšããå§ãããŸãã
èè ã¯åœŒã®ã¢ã€ãã¢ãå§å¡äŒã«æã¡èŸŒã¿ãçŸåšãããã®å®è£ ã«åãââçµãã§ããŸãã
ãã®ä»ã®ãµã€ã¯ã«èšäº
C ++ã®ééãããŒã1ïŒåºåãèšå·ä»ãã®éé
C ++ã®ééãããŒã2ïŒç¡éã®éé
C ++ã®ééãããŒã3ïŒã€ã³ã¯ãªã¡ã³ãã®å°å ¥ïŒå埩å¯èœïŒ