diff options
| author | Patrick Williams <iawillia@us.ibm.com> | 2012-02-14 16:45:40 -0600 |
|---|---|---|
| committer | A. Patrick Williams III <iawillia@us.ibm.com> | 2012-02-22 16:11:14 -0600 |
| commit | ddb7ed4a1c3da475aa1fd6036171d4bbba75e722 (patch) | |
| tree | 6616d24f32e7ff5154bb25bd9366137848ceeced /src/include/util/impl/iterator.h | |
| parent | c232f7a5a8b38321edae7a02c3148e67b5b4c3c7 (diff) | |
| download | blackbird-hostboot-ddb7ed4a1c3da475aa1fd6036171d4bbba75e722.tar.gz blackbird-hostboot-ddb7ed4a1c3da475aa1fd6036171d4bbba75e722.zip | |
STL advance / distance.
Change-Id: I9cdf9459f2970def812b1681897fe7d0cdda37ac
Reviewed-on: http://gfw160.austin.ibm.com:8080/gerrit/669
Tested-by: Jenkins Server
Reviewed-by: Douglas R. Gilbert <dgilbert@us.ibm.com>
Reviewed-by: Terry J. Opie <opiet@us.ibm.com>
Reviewed-by: MIKE J. JONES <mjjones@us.ibm.com>
Reviewed-by: A. Patrick Williams III <iawillia@us.ibm.com>
Diffstat (limited to 'src/include/util/impl/iterator.h')
| -rw-r--r-- | src/include/util/impl/iterator.h | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/src/include/util/impl/iterator.h b/src/include/util/impl/iterator.h new file mode 100644 index 000000000..de6b445aa --- /dev/null +++ b/src/include/util/impl/iterator.h @@ -0,0 +1,147 @@ +// IBM_PROLOG_BEGIN_TAG +// This is an automatically generated prolog. +// +// $Source: src/include/util/impl/iterator.h $ +// +// IBM CONFIDENTIAL +// +// COPYRIGHT International Business Machines Corp. 2012 +// +// p1 +// +// Object Code Only (OCO) source materials +// Licensed Internal Code Source Materials +// IBM HostBoot Licensed Internal Code +// +// The source code for this program is not published or other- +// wise divested of its trade secrets, irrespective of what has +// been deposited with the U.S. Copyright Office. +// +// Origin: 30 +// +// IBM_PROLOG_END + +#ifndef __UTIL_IMPL_ITERATOR_H +#define __UTIL_IMPL_ITERATOR_H + +/** @file iterator.h + * + * Contains the internal implementation details of the stl <iterator> header. + */ + +#include <util/traits/has_plusequals.H> +#include <util/traits/has_minus.H> + +namespace Util +{ + namespace __Util_Iterator_Impl + { + + /** + * Template definition of an iterator advance functor. + */ + template <typename InputIterator, typename Distance, + bool HasPlusEquals> struct AdvanceImpl; + + /** + * Template specialization of the advance functor for iterators + * which do not support random access. + */ + template <typename InputIterator, typename Distance> + struct AdvanceImpl<InputIterator, Distance, false> + { + static void advance(InputIterator& i, Distance n) + { + while(n--) + ++i; + } + }; + + /** + * Template specialization of the advance functor for iterators + * which do support random access. + */ + template <typename RandomIterator, typename Distance> + struct AdvanceImpl<RandomIterator, Distance, true> + { + static void advance(RandomIterator& i, Distance n) + { + i += n; + } + }; + + /** + * Template wrapper function for the iterator advance. + * + * Uses the existance of a += operator on the iterator to determine + * if the random-access or non-random-access version should be used. + */ + template <typename InputIterator, typename Distance> + void advance(InputIterator& i, Distance n) + { + AdvanceImpl<InputIterator, Distance, + Util::Traits::has_plusequals<InputIterator,Distance, + InputIterator>::value + >::advance(i,n); + } + + /** + * Template definition of an iterator distance functor. + */ + template <typename InputIterator, typename Distance, + bool HasMinus> struct DistanceImpl; + + /** + * Template specialization of the distance functor for iterators + * which do not support random access. + */ + template <typename InputIterator, typename Distance> + struct DistanceImpl<InputIterator, Distance, false> + { + static Distance distance(InputIterator& first, + InputIterator& last) + { + Distance i = 0; + while (first != last) + { + ++i; + ++first; + } + return i; + } + }; + + /** + * Template specialization of the distance functor for iterators + * which do support random access. + */ + template <typename RandomIterator, typename Distance> + struct DistanceImpl<RandomIterator, Distance, true> + { + static Distance distance(RandomIterator& first, + RandomIterator& last) + { + return last - first; + } + }; + + /** + * Template wrapper function for the iterator distance. + * + * Uses the existance of a - operator on the iterator to determine + * if the random-access or non-random-access version should be used. + */ + template <typename InputIterator, typename Distance> + Distance distance(InputIterator& first, + InputIterator& last) + { + return DistanceImpl<InputIterator, Distance, + Util::Traits::has_minus<InputIterator, InputIterator, + Distance>::value + >::distance(first,last); + } + + }; +}; + +#endif |

