source: mainline/uspace/lib/cpp/include/__bits/insert_iterator.hpp@ d1ef5efe

Last change on this file since d1ef5efe was 78050c7, checked in by Jiri Svoboda <jiri@…>, 2 months ago

Add missing input_iterator_tag declaration to insert_iterator.hpp.

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