1 | /*
|
---|
2 | * Copyright (c) 2017 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_LIST
|
---|
30 | #define LIBCPP_LIST
|
---|
31 |
|
---|
32 | #include <iterator>
|
---|
33 | #include <memory>
|
---|
34 | #include <utility>
|
---|
35 |
|
---|
36 | namespace cpp
|
---|
37 | {
|
---|
38 | namespace aux
|
---|
39 | {
|
---|
40 | template<class T>
|
---|
41 | struct list_node
|
---|
42 | {
|
---|
43 | T value;
|
---|
44 | list_node* next;
|
---|
45 | list_node* prev;
|
---|
46 | };
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * 23.3.5, class template list:
|
---|
51 | */
|
---|
52 |
|
---|
53 | template<class T, class Allocator = allocator<T>>
|
---|
54 | class list
|
---|
55 | {
|
---|
56 | public:
|
---|
57 | using value_type = T;
|
---|
58 | using reference = value_type&;
|
---|
59 | using const_reference = value_type&;
|
---|
60 | using allocator_type = Allocator;
|
---|
61 |
|
---|
62 | using iterator = void;
|
---|
63 | using const_iterator = void;
|
---|
64 | using size_type = void;
|
---|
65 | using difference_type = void;
|
---|
66 |
|
---|
67 | using pointer = typename allocator_traits<allocator_type>::pointer;
|
---|
68 | using const_pointer = typename allocator_traits<allocator_type>::const_pointer;
|
---|
69 |
|
---|
70 | using reverse_iterator = std::reverse_iterator<iterator>;
|
---|
71 | using const_reverse_iterator = std::const_reverse_iterator<iterator>;
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * 23.3.5.2, construct/copy/destroy:
|
---|
75 | */
|
---|
76 |
|
---|
77 | list()
|
---|
78 | : list{allocator_type{}}
|
---|
79 | { /* DUMMY BODY */ }
|
---|
80 |
|
---|
81 | explicit list(const allocator_type& alloc)
|
---|
82 | : allocator_{alloc}, head_{nullptr}
|
---|
83 | { /* DUMMY BODY */ }
|
---|
84 |
|
---|
85 | explicit list(size_type n, const allocator_type& alloc = allocator_type{})
|
---|
86 | : allocator_{alloc}, head_{nullptr}
|
---|
87 | {
|
---|
88 | for (size_type i = 0; i < n; ++i)
|
---|
89 | {
|
---|
90 | auto node = append_new_();
|
---|
91 | allocator_.construct(&node->value);
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | list(size_type n, const value_type& val,
|
---|
96 | const allocator_type& alloc = allocator_type{})
|
---|
97 | : allocator_{alloc}, head_{nullptr}
|
---|
98 | {
|
---|
99 | for (size_type i = 0; i < n; ++i)
|
---|
100 | {
|
---|
101 | auto node = append_new_();
|
---|
102 | allocator_.construct(&node->value, val);
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | template<class InputIterator>
|
---|
107 | list(InputIterator first, InputIterator last,
|
---|
108 | const allocator_type& alloc = allocator_type{})
|
---|
109 | : allocator_{alloc}, head_{nullptr}
|
---|
110 | {
|
---|
111 | while (first != last)
|
---|
112 | {
|
---|
113 | auto node = append_new_();
|
---|
114 | allocator_.construct(&node->value, *first++);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | list(const list& other)
|
---|
119 | : allocator_{other.allocator_}, head_{nullptr}
|
---|
120 | {
|
---|
121 | auto tmp = other.head_;
|
---|
122 |
|
---|
123 | while (tmp->next != other.head_)
|
---|
124 | {
|
---|
125 | auto node = append_new_();
|
---|
126 | allocator_.construct(&node->value, tmp->value);
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | list(list&& other)
|
---|
131 | {
|
---|
132 | // TODO:
|
---|
133 | }
|
---|
134 |
|
---|
135 | private:
|
---|
136 | allocator_type allocator_;
|
---|
137 | list_node<T>* head_;
|
---|
138 |
|
---|
139 | list_node<T>* append_new_()
|
---|
140 | {
|
---|
141 | auto node = new aux::list_node<T>{};
|
---|
142 | auto last = get_last_();
|
---|
143 |
|
---|
144 | if (!last)
|
---|
145 | head_ = node;
|
---|
146 | else
|
---|
147 | {
|
---|
148 | last->next = node;
|
---|
149 | node->prev = last;
|
---|
150 |
|
---|
151 | node->next = head_;
|
---|
152 | head_->prev = node;
|
---|
153 | }
|
---|
154 |
|
---|
155 | return node;
|
---|
156 | }
|
---|
157 |
|
---|
158 | list_node<T>* get_last_()
|
---|
159 | {
|
---|
160 | if (!head_)
|
---|
161 | return nullptr;
|
---|
162 |
|
---|
163 | auto node = head_;
|
---|
164 |
|
---|
165 | while (node->next != head_)
|
---|
166 | node = node->next;
|
---|
167 | }
|
---|
168 | };
|
---|
169 | }
|
---|
170 |
|
---|
171 | #endif
|
---|