zip_iterator.h 8.72 KB
Newer Older
1
/*
2
 * Copyright (c) 2014-2015, Siemens AG. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef EMBB_ALGORITHMS_ZIP_ITERATOR_H_
#define EMBB_ALGORITHMS_ZIP_ITERATOR_H_

#include<iterator>
#include <utility>
#include <cassert>

namespace embb {
namespace algorithms {
/**
 * \defgroup CPP_ALGORITHMS_ZIP_ITERATOR Zip Iterator
 * Zip two iterators
 * \ingroup CPP_ALGORITHMS
 * \{
 */

/**
 * Container for the values of two dereferenced iterators.
 * The values contained are of type
 * <tt>std::iterator_traits<Iterator>::reference</tt>.
 * \notthreadsafe
 * \tparam TypeA Type of the first value
 * \tparam TypeB Type of the first value
 *
 */
template<typename TypeA, typename TypeB>
class ZipPair {
 public:
  /**
   * Constructs a pair from two values.
   */
  ZipPair(
    TypeA first,
    /**< [IN] First value*/
    TypeB second
    /**< [IN] Second value*/
    )
  :first_(first), second_(second) {}

  /**
   * Copies a pair.
   */
  ZipPair(
    const ZipPair& other
    /**< [IN] pair to copy */
    )
  :first_(other.first_), second_(other.second_) {}

  /**
   * Returns the first value of the pair.
   *
   * \return The first value of the pair.
   */
  TypeA First() {
    return first_;
  }

  /**
   * Returns the second value of the pair.
   *
   * \return The second value of the pair
   */
  TypeB Second() {
      return second_;
  }

  /**
   * Returns the first value of the pair.
   *
   * \return The first value of the pair.
   */
  const TypeA First() const {
    return first_;
  }

  /**
   * Returns the second value of the pair.
   *
   * \return The second value of the pair
   */
  const TypeB Second() const {
      return second_;
  }

 private:
  TypeA first_;
  TypeB second_;

  /**
   * Disable assignment since references cannot be assigned.
   */
  ZipPair &operator=(const ZipPair&);
};

/**
 * Zip container for two iterators.
 * This container allows zipping two iterators together, thus enabling them
 * to be used in situations where only one iterator can be used. Any operation
 * applied to the zip iterator will subsequently be applied to the contained
 * iterators. Dereferencing the iterator will return a ZipPair containing the
 * values referenced by the iterators.
 * \notthreadsafe
 * \note It is required that the two iterators have the same \c difference_type
 * or that at least the first iterator's \c difference_type can be implicitly
 * converted to the second iterator's \c difference_type. Moreover, when
 * calculating the distance between two ZipIterators, the distances between both
 * pairs of iterators are equal.
 * \see ZipPair
 * \tparam IteratorA First iterator
 * \tparam IteratorB Second iterator
 */
template<typename IteratorA, typename IteratorB>
class  ZipIterator{
 public:
  /**
   * \name Iterator Typedefs
   * Necessary typedefs for iterators (std compatibility).
   * \{
   */
  typedef std::random_access_iterator_tag iterator_category;
  typedef typename std::iterator_traits<IteratorA>::difference_type
      difference_type;
  typedef typename std::iterator_traits<IteratorA>::reference RefA;
  typedef typename std::iterator_traits<IteratorB>::reference RefB;
  typedef typename std::iterator_traits<IteratorA>::value_type ValueA;
  typedef typename std::iterator_traits<IteratorB>::value_type ValueB;
  typedef ZipPair<ValueA, ValueB>  value_type;
  typedef ZipPair<RefA, RefB>  reference;
  typedef ZipPair<RefA, RefB>  pointer;
  /**
   * \}
   */

  /**
   * Constructs a zip iterator from two iterators of any type.
   */
  ZipIterator(
    IteratorA iter_a,
    /**< [IN] First iterator*/
    IteratorB iter_b
    /**< [IN] Second iterator*/
    )
  :iter_a_(iter_a), iter_b_(iter_b) {}

  /**
   * Compares two zip iterators for equality.
   *
   * \return \c true if zip iterators are equal, otherwise \c false
   */
  bool operator==(
    const ZipIterator &other
    /**< [IN] Reference to right-hand side of equality operator */
    ) const {
    return iter_a_ == other.iter_a_ && iter_b_ == other.iter_b_;
  }

  /**
   * Compares two zip iterators for inequality.
   *
   * \return \c true if any iterator doesn't equal the other, otherwise \c false
   */
  bool operator!=(
    const ZipIterator &other
    /**< [IN] Reference to right-hand side of inequality operator */
    ) const {
    return iter_a_ != other.iter_a_ || iter_b_ != other.iter_b_;
  }

  /**
   * Applies prefix increment on both iterators.
   */
  void operator++() {
    ++iter_a_;
    ++iter_b_;
  }

  /**
   * Applies prefix decrement on both iterators.
   */
  void operator--() {
    --iter_a_;
    --iter_b_;
  }

  /**
   * Returns an instance of a zip iterator where both iterators have been
   * advanced by the specified distance.
   *
   * \return New zip iterator containing the advanced iterators
   */
  ZipIterator<IteratorA, IteratorB> operator+(
    difference_type distance
    /**< [IN] Number of elements to advance the iterators */
    ) const {
    ZipIterator<IteratorA, IteratorB> new_iterator(*this);
    new_iterator.iter_a_ += distance;
    new_iterator.iter_b_ += distance;

    return new_iterator;
  }

  /**
   * Returns an instance of a zip iterator where both iterators have been
   * regressed by the specified distance.
   *
   * \return New zip iterator containing the regressed iterators
   */
  ZipIterator<IteratorA, IteratorB> operator-(
    difference_type distance
    /**< [IN] Number of elements to regress the iterators */
    ) const {
    ZipIterator<IteratorA, IteratorB> new_iterator(*this);
    new_iterator.iter_a_ -= distance;
    new_iterator.iter_b_ -= distance;

    return new_iterator;
  }

  /**
   * Advances both iterators by the specified distance.
   *
   * \return Reference to \c *this
   */
  ZipIterator<IteratorA, IteratorB>& operator+=(
    difference_type distance
    /**< [IN] Number of elements to advance the iterators */
    ) {
    iter_a_ += distance;
    iter_b_ += distance;
    return *this;
  }

  /**
   * Regresses both iterators by the specified distance.
   *
   * \return Reference to \c *this
   */
  ZipIterator<IteratorA, IteratorB>& operator-=(
    difference_type distance
    /**< [IN] Number of elements to regress the iterators */
    ) {
    iter_a_ -= distance;
    iter_b_ -= distance;
    return *this;
  }

  /**
   * Computes the distance between two zip iterators.
   * It is assumed that both iterator pairs have the same distance.
   *
   * \return The distance between the zip iterators
   */
  difference_type operator-(
    const ZipIterator<IteratorA,
      IteratorB> &other
      /**< [IN] Reference to right-hand side of subtraction operator */
      ) const {
    assert(iter_a_ - other.iter_a_ == iter_b_ - other.iter_b_);
    return iter_a_ - other.iter_a_;
  }

  /**
   * Dereferences the zip iterator.
   *
   * \return ZipPair containing the dereferenced values.
   */
  reference operator*() const {
    return ZipPair<RefA, RefB>(*iter_a_, *iter_b_);
  }

 private:
  IteratorA iter_a_;
  IteratorB iter_b_;
};

/**
 * Creates a zip iterator from two iterators.
 * This is a convenience function which avoids calling the constructor of the
 * templated class.
 *
 * \return Constructed zip iterator
 * \tparam IteratorA Type of first iterator
 * \tparam IteratorB Type of second iterator
 */
template<typename IteratorA, typename IteratorB>
ZipIterator<IteratorA, IteratorB> Zip(
  IteratorA iter_a,
  /**< [IN] First iterator */
  IteratorB iter_b
  /**< [IN] Second iterator */
  ) {
  return ZipIterator<IteratorA, IteratorB>(iter_a, iter_b);
}

/**
 * \}
 */
}  // namespace algorithms
}  // namespace embb

#endif  //  EMBB_ALGORITHMS_ZIP_ITERATOR_H_