/****************************************************************************** Trick Library 'ax' 補助反復子クラステンプレートヘッダファイル Copyright(C) 2008 Wraith. All rights reserved. Coded by Wraith in Jun 6, 2008. ******************************************************************************/ /////////////////////////////////////////////////////////////////////////////// // // ■ axiterat.h // http://tricklib.com/cxx/ax/axiterat.h // // □ 関連ファイル // http://tricklib.com/cxx/ax/axconfig.h // http://tricklib.com/cxx/ax/axexcept.h // http://tricklib.com/cxx/ax/axmemory.h // http://tricklib.com/cxx/ax/axstring.h // // □ ライセンス情報 // http://tricklib.com/license.htm // // ★ このファイルはC++標準ライブラリの 相当のものになり、規格票 //  (ISO/IEC 14882, JIS X 3014)の 24 章に基づき実装されています。 // #ifndef TRICKLIB_AX_AXITERATOR_H #define TRICKLIB_AX_AXITERATOR_H #include "axconfig.h" #include namespace tricklib { namespace ax { // 24.3 基本的要素 #if defined(TRICKLIB_AX_AVAILABLE_TEMPLATE_OVERLOADS) template struct iterator_traits { typedef typename Iterator::difference_type difference_type; typedef typename Iterator::value_type value_type; typedef typename Iterator::pointer pointer; typedef typename Iterator::reference reference; typedef typename Iterator::iterator_category iterator_category; }; template struct iterator_traits { typedef ptrdiff_t difference_type; typedef T value_type; typedef T* pointer; typedef T& reference; typedef random_access_iterator_tag iterator_category; }; template struct iterator_traits { typedef ptrdiff_t difference_type; typedef T value_type; typedef const T* pointer; typedef const T& reference; typedef random_access_iterator_tag iterator_category; }; #if 0 template struct iterator_traits { typedef long difference_type; typedef T value_type; typedef T __far* pointer; typedef T __far& reference; typedef random_access_iterator_tag iterator_category; }; #endif #endif #if defined(TRICKLIB_AX_AVAILABLE_DEFAULT_TEMPLATE_ARGS) // 24.3.2 基本的な反復子 template struct iterator { typedef T value_type; typedef Distance difference_type; typedef Pointer pointer; typedef Reference reference; typedef Category iterator_category; }; #else // 24.3.2 基本的な反復子 template struct iterator { typedef T value_type; typedef ptrdiff_t difference_type; typedef T * pointer; typedef T & reference; typedef Category iterator_category; }; #endif struct input_iterator_tag {}; struct output_iterator_tag {}; struct forward_iterator_tag: public input_iterator_tag {}; struct bidirectional_iterator_tag: public forward_iterator_tag {}; struct random_access_iterator_tag: public bidirectional_iterator_tag {}; template class ax_array_iterator :public iterator { public: typedef iterator base_type; typedef typename base_type::value_type value_type; typedef typename base_type::difference_type difference_type; typedef typename base_type::reference reference; typedef typename base_type::pointer pointer; typedef ax_array_iterator this_type; public: value_type * value; public: ax_array_iterator(value_type * a_value = NULL) :value(a_value) { } ax_array_iterator(const this_type & a) :value(a.value) { } reference operator * () const { return *value; } pointer operator -> () const { return value; } this_type & operator ++ () { ++value; return *this; } this_type operator ++ (int) { this_type result(*this); ++value; return result; } this_type & operator -- () { --value; return *this; } this_type operator -- (int) { this_type result(*this); --value; return result; } this_type operator + (difference_type n) const { return this_type(value + n); } this_type & operator += (difference_type n) { value += n; return *this; } this_type operator - (difference_type n) const { return this_type(value -n); } this_type & operator -= (difference_type n) { value -= n; return *this; } reference operator [] (difference_type n) const { return value[n]; } }; template inline bool operator==(const ax_array_iterator & x, const ax_array_iterator & y) { return x.value == y.value; } template inline bool operator<(const ax_array_iterator & x, const ax_array_iterator & y) { return x.value < y.value; } template inline bool operator!=(const ax_array_iterator & x, const ax_array_iterator & y) { return x.value != y.value; } template inline bool operator>(const ax_array_iterator & x, const ax_array_iterator & y) { return x.value > y.value; } template inline bool operator>=(const ax_array_iterator & x, const ax_array_iterator & y) { return x.value >= y.value; } template inline bool operator<=(const ax_array_iterator & x, const ax_array_iterator & y) { return x.value <= y.value; } template inline typename ax_array_iterator::difference_type operator-(const ax_array_iterator & x, const ax_array_iterator & y) { return x.value -y.value; } template inline ax_array_iterator operator+(typename ax_array_iterator::difference_type n, const ax_array_iterator& x) { return n +x.value; } // 24.3.4 反復子の演算 template void advance(InputIterator & a_i, Distance a_n); #if defined(TRICKLIB_AX_AVAILABLE_TEMPLATE_OVERLOADS) template typename iterator_traits::difference_type distance(InputIterator first, InputIterator last) #else template typename InputIterator::difference_type distance(InputIterator first, InputIterator last) #endif { ptrdiff_t i = 0; while(last != first) { ++first; ++i; } return i; } // 24.4 あらかじめ定義された反復子 template class reverse_iterator { protected: Iterator current; public: typedef Iterator iterator_type; #if defined(TRICKLIB_AX_AVAILABLE_TEMPLATE_OVERLOADS) typedef typename iterator_traits::difference_type difference_type; typedef typename iterator_traits::reference reference; typedef typename iterator_traits::pointer pointer; #else typedef typename Iterator::difference_type difference_type; typedef typename Iterator::reference reference; typedef typename Iterator::pointer pointer; #endif reverse_iterator(); explicit reverse_iterator(Iterator x); //template reverse_iterator(const reverse_iterator& u); reverse_iterator(const reverse_iterator & u); Iterator base() const; // 明示的 reference operator*() const; pointer operator->() const; reverse_iterator& operator++(); reverse_iterator operator++(int); reverse_iterator& operator--(); reverse_iterator operator--(int); reverse_iterator operator+ (difference_type n) const; reverse_iterator& operator+=(difference_type n); reverse_iterator operator- (difference_type n) const; reverse_iterator& operator-=(difference_type n); reference operator[](difference_type n) const; }; template bool operator==(const reverse_iterator& x, const reverse_iterator& y); template bool operator<(const reverse_iterator& x, const reverse_iterator& y); template bool operator!=(const reverse_iterator& x, const reverse_iterator& y); template bool operator>(const reverse_iterator& x, const reverse_iterator& y); template bool operator>=(const reverse_iterator& x, const reverse_iterator& y); template bool operator<=(const reverse_iterator& x, const reverse_iterator& y); template typename reverse_iterator::difference_type operator-(const reverse_iterator& x, const reverse_iterator& y); template reverse_iterator operator+(typename reverse_iterator::difference_type n, const reverse_iterator& x); template class back_insert_iterator; template back_insert_iterator back_inserter(Container& x); template class front_insert_iterator; template front_insert_iterator front_inserter(Container& x); template class insert_iterator; template insert_iterator inserter(Container& x, Iterator i); // 24.5 ストリーム反復子 //template , class Distance = ptrdiff_t> class istream_iterator; template class istream_iterator; template //bool operator==(const istream_iterator& x, const istream_iterator& y); bool operator==(const istream_iterator& x, const istream_iterator& y); template //bool operator!=(const istream_iterator& x, const istream_iterator& y); bool operator!=(const istream_iterator& x, const istream_iterator& y); //template > class ostream_iterator; template class ostream_iterator; //template > class istreambuf_iterator; template class istreambuf_iterator; template //bool operator==(const istreambuf_iterator& a, const istreambuf_iterator& b); bool operator==(const istreambuf_iterator& a, const istreambuf_iterator& b); template //bool operator!=(const istreambuf_iterator& a, const istreambuf_iterator& b); bool operator!=(const istreambuf_iterator& a, const istreambuf_iterator& b); //template > class ostreambuf_iterator; template class ostreambuf_iterator; } } #endif // TRICKLIB_AX_AXITERATOR_H /****************************************************************************** □■□■ Wraith the Trickster □■□■ ■□■□ 〜I'll go with heaven's advantage and fool's wisdom.〜 ■□■□ ******************************************************************************/