| 1 | /*
|
|---|
| 2 | * Copyright (c) 2018 Jaroslav Jindrak
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #ifndef LIBCPP_INTERNAL_INSERT_ITERATOR
|
|---|
| 30 | #define LIBCPP_INTERNAL_INSERT_ITERATOR
|
|---|
| 31 |
|
|---|
| 32 | namespace std
|
|---|
| 33 | {
|
|---|
| 34 | struct forward_iterator_tag;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | namespace std::aux
|
|---|
| 38 | {
|
|---|
| 39 | /**
|
|---|
| 40 | * Note: Commonly the data structures in this library
|
|---|
| 41 | * contain a set of insert member functions. These
|
|---|
| 42 | * are often both iterator based and copy based.
|
|---|
| 43 | * This iterator can change the copy version into
|
|---|
| 44 | * an iterator version by creating a count based copy
|
|---|
| 45 | * iterator.
|
|---|
| 46 | * Usage:
|
|---|
| 47 | * first == insert_iterator{value}
|
|---|
| 48 | * last == insert_iterator{count}
|
|---|
| 49 | *
|
|---|
| 50 | * So the following code:
|
|---|
| 51 | * while (first != last)
|
|---|
| 52 | * *data_++ = *first++;
|
|---|
| 53 | *
|
|---|
| 54 | * Will insert a copy of value into the data_
|
|---|
| 55 | * iterator exactly count times.
|
|---|
| 56 | * TODO: Apply this to existing containers?
|
|---|
| 57 | */
|
|---|
| 58 | template<class T>
|
|---|
| 59 | class insert_iterator
|
|---|
| 60 | {
|
|---|
| 61 | public:
|
|---|
| 62 | using difference_type = unsigned long long;
|
|---|
| 63 | using value_type = T;
|
|---|
| 64 | using iterator_category = input_iterator_tag;
|
|---|
| 65 | using reference = value_type&;
|
|---|
| 66 | using pointer = value_type*;
|
|---|
| 67 |
|
|---|
| 68 | explicit insert_iterator(difference_type count, const value_type& val = value_type{})
|
|---|
| 69 | : value_{val}, count_{count}
|
|---|
| 70 | { /* DUMMY BODY */ }
|
|---|
| 71 |
|
|---|
| 72 | insert_iterator(const insert_iterator&) = default;
|
|---|
| 73 | insert_iterator& operator=(const insert_iterator&) = default;
|
|---|
| 74 |
|
|---|
| 75 | insert_iterator(insert_iterator&&) = default;
|
|---|
| 76 | insert_iterator& operator=(insert_iterator&&) = default;
|
|---|
| 77 |
|
|---|
| 78 | const value_type& operator*() const
|
|---|
| 79 | {
|
|---|
| 80 | return value_;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | const value_type* operator->() const
|
|---|
| 84 | {
|
|---|
| 85 | return &value_;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | insert_iterator& operator++()
|
|---|
| 89 | {
|
|---|
| 90 | ++count_;
|
|---|
| 91 |
|
|---|
| 92 | return *this;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | insert_iterator operator++(int)
|
|---|
| 96 | {
|
|---|
| 97 | ++count_;
|
|---|
| 98 |
|
|---|
| 99 | return insert_iterator{count_ - 1, value_};
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | bool operator==(const insert_iterator& other)
|
|---|
| 103 | {
|
|---|
| 104 | return count_ == other.count_;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | bool operator!=(const insert_iterator& other)
|
|---|
| 108 | {
|
|---|
| 109 | return count_ != other.count_;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | private:
|
|---|
| 113 | value_type value_;
|
|---|
| 114 | difference_type count_;
|
|---|
| 115 | };
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | #endif
|
|---|