]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/ext/slist
Makefile.am (doxygen, [...]): Tweak targets.
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / slist
1 // Singly-linked list implementation -*- C++ -*-
2
3 // Copyright (C) 2001, 2002 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 /*
31 * Copyright (c) 1997
32 * Silicon Graphics Computer Systems, Inc.
33 *
34 * Permission to use, copy, modify, distribute and sell this software
35 * and its documentation for any purpose is hereby granted without fee,
36 * provided that the above copyright notice appear in all copies and
37 * that both that copyright notice and this permission notice appear
38 * in supporting documentation. Silicon Graphics makes no
39 * representations about the suitability of this software for any
40 * purpose. It is provided "as is" without express or implied warranty.
41 *
42 */
43
44 /** @file ext/slist
45 * This file is a GNU extension to the Standard C++ Library (possibly
46 * containing extensions from the HP/SGI STL subset). You should only
47 * include this header if you are using GCC 3 or later.
48 */
49
50 #ifndef __SGI_STL_INTERNAL_SLIST_H
51 #define __SGI_STL_INTERNAL_SLIST_H
52
53 #include <bits/stl_algobase.h>
54 #include <bits/stl_alloc.h>
55 #include <bits/stl_construct.h>
56 #include <bits/stl_uninitialized.h>
57 #include <bits/concept_check.h>
58
59 namespace __gnu_cxx
60 {
61 using std::size_t;
62 using std::ptrdiff_t;
63 using std::_Alloc_traits;
64 using std::_Construct;
65 using std::_Destroy;
66 using std::allocator;
67
68 struct _Slist_node_base
69 {
70 _Slist_node_base* _M_next;
71 };
72
73 inline _Slist_node_base*
74 __slist_make_link(_Slist_node_base* __prev_node,
75 _Slist_node_base* __new_node)
76 {
77 __new_node->_M_next = __prev_node->_M_next;
78 __prev_node->_M_next = __new_node;
79 return __new_node;
80 }
81
82 inline _Slist_node_base*
83 __slist_previous(_Slist_node_base* __head,
84 const _Slist_node_base* __node)
85 {
86 while (__head && __head->_M_next != __node)
87 __head = __head->_M_next;
88 return __head;
89 }
90
91 inline const _Slist_node_base*
92 __slist_previous(const _Slist_node_base* __head,
93 const _Slist_node_base* __node)
94 {
95 while (__head && __head->_M_next != __node)
96 __head = __head->_M_next;
97 return __head;
98 }
99
100 inline void __slist_splice_after(_Slist_node_base* __pos,
101 _Slist_node_base* __before_first,
102 _Slist_node_base* __before_last)
103 {
104 if (__pos != __before_first && __pos != __before_last) {
105 _Slist_node_base* __first = __before_first->_M_next;
106 _Slist_node_base* __after = __pos->_M_next;
107 __before_first->_M_next = __before_last->_M_next;
108 __pos->_M_next = __first;
109 __before_last->_M_next = __after;
110 }
111 }
112
113 inline void
114 __slist_splice_after(_Slist_node_base* __pos, _Slist_node_base* __head)
115 {
116 _Slist_node_base* __before_last = __slist_previous(__head, 0);
117 if (__before_last != __head) {
118 _Slist_node_base* __after = __pos->_M_next;
119 __pos->_M_next = __head->_M_next;
120 __head->_M_next = 0;
121 __before_last->_M_next = __after;
122 }
123 }
124
125 inline _Slist_node_base* __slist_reverse(_Slist_node_base* __node)
126 {
127 _Slist_node_base* __result = __node;
128 __node = __node->_M_next;
129 __result->_M_next = 0;
130 while(__node) {
131 _Slist_node_base* __next = __node->_M_next;
132 __node->_M_next = __result;
133 __result = __node;
134 __node = __next;
135 }
136 return __result;
137 }
138
139 inline size_t __slist_size(_Slist_node_base* __node)
140 {
141 size_t __result = 0;
142 for ( ; __node != 0; __node = __node->_M_next)
143 ++__result;
144 return __result;
145 }
146
147 template <class _Tp>
148 struct _Slist_node : public _Slist_node_base
149 {
150 _Tp _M_data;
151 };
152
153 struct _Slist_iterator_base
154 {
155 typedef size_t size_type;
156 typedef ptrdiff_t difference_type;
157 typedef std::forward_iterator_tag iterator_category;
158
159 _Slist_node_base* _M_node;
160
161 _Slist_iterator_base(_Slist_node_base* __x) : _M_node(__x) {}
162 void _M_incr() { _M_node = _M_node->_M_next; }
163
164 bool operator==(const _Slist_iterator_base& __x) const {
165 return _M_node == __x._M_node;
166 }
167 bool operator!=(const _Slist_iterator_base& __x) const {
168 return _M_node != __x._M_node;
169 }
170 };
171
172 template <class _Tp, class _Ref, class _Ptr>
173 struct _Slist_iterator : public _Slist_iterator_base
174 {
175 typedef _Slist_iterator<_Tp, _Tp&, _Tp*> iterator;
176 typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
177 typedef _Slist_iterator<_Tp, _Ref, _Ptr> _Self;
178
179 typedef _Tp value_type;
180 typedef _Ptr pointer;
181 typedef _Ref reference;
182 typedef _Slist_node<_Tp> _Node;
183
184 _Slist_iterator(_Node* __x) : _Slist_iterator_base(__x) {}
185 _Slist_iterator() : _Slist_iterator_base(0) {}
186 _Slist_iterator(const iterator& __x) : _Slist_iterator_base(__x._M_node) {}
187
188 reference operator*() const { return ((_Node*) _M_node)->_M_data; }
189 pointer operator->() const { return &(operator*()); }
190
191 _Self& operator++()
192 {
193 _M_incr();
194 return *this;
195 }
196 _Self operator++(int)
197 {
198 _Self __tmp = *this;
199 _M_incr();
200 return __tmp;
201 }
202 };
203
204
205 // Base class that encapsulates details of allocators. Three cases:
206 // an ordinary standard-conforming allocator, a standard-conforming
207 // allocator with no non-static data, and an SGI-style allocator.
208 // This complexity is necessary only because we're worrying about backward
209 // compatibility and because we want to avoid wasting storage on an
210 // allocator instance if it isn't necessary.
211
212 // Base for general standard-conforming allocators.
213 template <class _Tp, class _Allocator, bool _IsStatic>
214 class _Slist_alloc_base {
215 public:
216 typedef typename _Alloc_traits<_Tp,_Allocator>::allocator_type
217 allocator_type;
218 allocator_type get_allocator() const { return _M_node_allocator; }
219
220 _Slist_alloc_base(const allocator_type& __a) : _M_node_allocator(__a) {}
221
222 protected:
223 _Slist_node<_Tp>* _M_get_node()
224 { return _M_node_allocator.allocate(1); }
225 void _M_put_node(_Slist_node<_Tp>* __p)
226 { _M_node_allocator.deallocate(__p, 1); }
227
228 protected:
229 typename _Alloc_traits<_Slist_node<_Tp>,_Allocator>::allocator_type
230 _M_node_allocator;
231 _Slist_node_base _M_head;
232 };
233
234 // Specialization for instanceless allocators.
235 template <class _Tp, class _Allocator>
236 class _Slist_alloc_base<_Tp,_Allocator, true> {
237 public:
238 typedef typename _Alloc_traits<_Tp,_Allocator>::allocator_type
239 allocator_type;
240 allocator_type get_allocator() const { return allocator_type(); }
241
242 _Slist_alloc_base(const allocator_type&) {}
243
244 protected:
245 typedef typename _Alloc_traits<_Slist_node<_Tp>, _Allocator>::_Alloc_type
246 _Alloc_type;
247 _Slist_node<_Tp>* _M_get_node() { return _Alloc_type::allocate(1); }
248 void _M_put_node(_Slist_node<_Tp>* __p) { _Alloc_type::deallocate(__p, 1); }
249
250 protected:
251 _Slist_node_base _M_head;
252 };
253
254
255 template <class _Tp, class _Alloc>
256 struct _Slist_base
257 : public _Slist_alloc_base<_Tp, _Alloc,
258 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
259 {
260 typedef _Slist_alloc_base<_Tp, _Alloc,
261 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
262 _Base;
263 typedef typename _Base::allocator_type allocator_type;
264
265 _Slist_base(const allocator_type& __a)
266 : _Base(__a) { this->_M_head._M_next = 0; }
267 ~_Slist_base() { _M_erase_after(&this->_M_head, 0); }
268
269 protected:
270
271 _Slist_node_base* _M_erase_after(_Slist_node_base* __pos)
272 {
273 _Slist_node<_Tp>* __next = (_Slist_node<_Tp>*) (__pos->_M_next);
274 _Slist_node_base* __next_next = __next->_M_next;
275 __pos->_M_next = __next_next;
276 _Destroy(&__next->_M_data);
277 _M_put_node(__next);
278 return __next_next;
279 }
280 _Slist_node_base* _M_erase_after(_Slist_node_base*, _Slist_node_base*);
281 };
282
283 template <class _Tp, class _Alloc>
284 _Slist_node_base*
285 _Slist_base<_Tp,_Alloc>::_M_erase_after(_Slist_node_base* __before_first,
286 _Slist_node_base* __last_node) {
287 _Slist_node<_Tp>* __cur = (_Slist_node<_Tp>*) (__before_first->_M_next);
288 while (__cur != __last_node) {
289 _Slist_node<_Tp>* __tmp = __cur;
290 __cur = (_Slist_node<_Tp>*) __cur->_M_next;
291 _Destroy(&__tmp->_M_data);
292 _M_put_node(__tmp);
293 }
294 __before_first->_M_next = __last_node;
295 return __last_node;
296 }
297
298 template <class _Tp, class _Alloc = allocator<_Tp> >
299 class slist : private _Slist_base<_Tp,_Alloc>
300 {
301 // concept requirements
302 __glibcpp_class_requires(_Tp, _SGIAssignableConcept)
303
304 private:
305 typedef _Slist_base<_Tp,_Alloc> _Base;
306 public:
307 typedef _Tp value_type;
308 typedef value_type* pointer;
309 typedef const value_type* const_pointer;
310 typedef value_type& reference;
311 typedef const value_type& const_reference;
312 typedef size_t size_type;
313 typedef ptrdiff_t difference_type;
314
315 typedef _Slist_iterator<_Tp, _Tp&, _Tp*> iterator;
316 typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
317
318 typedef typename _Base::allocator_type allocator_type;
319 allocator_type get_allocator() const { return _Base::get_allocator(); }
320
321 private:
322 typedef _Slist_node<_Tp> _Node;
323 typedef _Slist_node_base _Node_base;
324 typedef _Slist_iterator_base _Iterator_base;
325
326 _Node* _M_create_node(const value_type& __x) {
327 _Node* __node = this->_M_get_node();
328 try {
329 _Construct(&__node->_M_data, __x);
330 __node->_M_next = 0;
331 }
332 catch(...)
333 {
334 this->_M_put_node(__node);
335 __throw_exception_again;
336 }
337 return __node;
338 }
339
340 _Node* _M_create_node() {
341 _Node* __node = this->_M_get_node();
342 try {
343 _Construct(&__node->_M_data);
344 __node->_M_next = 0;
345 }
346 catch(...)
347 {
348 this->_M_put_node(__node);
349 __throw_exception_again;
350 }
351 return __node;
352 }
353
354 public:
355 explicit slist(const allocator_type& __a = allocator_type()) : _Base(__a) {}
356
357 slist(size_type __n, const value_type& __x,
358 const allocator_type& __a = allocator_type()) : _Base(__a)
359 { _M_insert_after_fill(&this->_M_head, __n, __x); }
360
361 explicit slist(size_type __n) : _Base(allocator_type())
362 { _M_insert_after_fill(&this->_M_head, __n, value_type()); }
363
364 // We don't need any dispatching tricks here, because _M_insert_after_range
365 // already does them.
366 template <class _InputIterator>
367 slist(_InputIterator __first, _InputIterator __last,
368 const allocator_type& __a = allocator_type()) : _Base(__a)
369 { _M_insert_after_range(&this->_M_head, __first, __last); }
370
371 slist(const slist& __x) : _Base(__x.get_allocator())
372 { _M_insert_after_range(&this->_M_head, __x.begin(), __x.end()); }
373
374 slist& operator= (const slist& __x);
375
376 ~slist() {}
377
378 public:
379 // assign(), a generalized assignment member function. Two
380 // versions: one that takes a count, and one that takes a range.
381 // The range version is a member template, so we dispatch on whether
382 // or not the type is an integer.
383
384 void assign(size_type __n, const _Tp& __val)
385 { _M_fill_assign(__n, __val); }
386
387 void _M_fill_assign(size_type __n, const _Tp& __val);
388
389 template <class _InputIterator>
390 void assign(_InputIterator __first, _InputIterator __last) {
391 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
392 _M_assign_dispatch(__first, __last, _Integral());
393 }
394
395 template <class _Integer>
396 void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
397 { _M_fill_assign((size_type) __n, (_Tp) __val); }
398
399 template <class _InputIterator>
400 void _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
401 __false_type);
402
403 public:
404
405 iterator begin() { return iterator((_Node*)this->_M_head._M_next); }
406 const_iterator begin() const
407 { return const_iterator((_Node*)this->_M_head._M_next);}
408
409 iterator end() { return iterator(0); }
410 const_iterator end() const { return const_iterator(0); }
411
412 // Experimental new feature: before_begin() returns a
413 // non-dereferenceable iterator that, when incremented, yields
414 // begin(). This iterator may be used as the argument to
415 // insert_after, erase_after, etc. Note that even for an empty
416 // slist, before_begin() is not the same iterator as end(). It
417 // is always necessary to increment before_begin() at least once to
418 // obtain end().
419 iterator before_begin() { return iterator((_Node*) &this->_M_head); }
420 const_iterator before_begin() const
421 { return const_iterator((_Node*) &this->_M_head); }
422
423 size_type size() const { return __slist_size(this->_M_head._M_next); }
424
425 size_type max_size() const { return size_type(-1); }
426
427 bool empty() const { return this->_M_head._M_next == 0; }
428
429 void swap(slist& __x)
430 { std::swap(this->_M_head._M_next, __x._M_head._M_next); }
431
432 public:
433
434 reference front() { return ((_Node*) this->_M_head._M_next)->_M_data; }
435 const_reference front() const
436 { return ((_Node*) this->_M_head._M_next)->_M_data; }
437 void push_front(const value_type& __x) {
438 __slist_make_link(&this->_M_head, _M_create_node(__x));
439 }
440 void push_front() { __slist_make_link(&this->_M_head, _M_create_node()); }
441 void pop_front() {
442 _Node* __node = (_Node*) this->_M_head._M_next;
443 this->_M_head._M_next = __node->_M_next;
444 _Destroy(&__node->_M_data);
445 this->_M_put_node(__node);
446 }
447
448 iterator previous(const_iterator __pos) {
449 return iterator((_Node*) __slist_previous(&this->_M_head, __pos._M_node));
450 }
451 const_iterator previous(const_iterator __pos) const {
452 return const_iterator((_Node*) __slist_previous(&this->_M_head,
453 __pos._M_node));
454 }
455
456 private:
457 _Node* _M_insert_after(_Node_base* __pos, const value_type& __x) {
458 return (_Node*) (__slist_make_link(__pos, _M_create_node(__x)));
459 }
460
461 _Node* _M_insert_after(_Node_base* __pos) {
462 return (_Node*) (__slist_make_link(__pos, _M_create_node()));
463 }
464
465 void _M_insert_after_fill(_Node_base* __pos,
466 size_type __n, const value_type& __x) {
467 for (size_type __i = 0; __i < __n; ++__i)
468 __pos = __slist_make_link(__pos, _M_create_node(__x));
469 }
470
471 // Check whether it's an integral type. If so, it's not an iterator.
472 template <class _InIter>
473 void _M_insert_after_range(_Node_base* __pos,
474 _InIter __first, _InIter __last) {
475 typedef typename _Is_integer<_InIter>::_Integral _Integral;
476 _M_insert_after_range(__pos, __first, __last, _Integral());
477 }
478
479 template <class _Integer>
480 void _M_insert_after_range(_Node_base* __pos, _Integer __n, _Integer __x,
481 __true_type) {
482 _M_insert_after_fill(__pos, __n, __x);
483 }
484
485 template <class _InIter>
486 void _M_insert_after_range(_Node_base* __pos,
487 _InIter __first, _InIter __last,
488 __false_type) {
489 while (__first != __last) {
490 __pos = __slist_make_link(__pos, _M_create_node(*__first));
491 ++__first;
492 }
493 }
494
495 public:
496
497 iterator insert_after(iterator __pos, const value_type& __x) {
498 return iterator(_M_insert_after(__pos._M_node, __x));
499 }
500
501 iterator insert_after(iterator __pos) {
502 return insert_after(__pos, value_type());
503 }
504
505 void insert_after(iterator __pos, size_type __n, const value_type& __x) {
506 _M_insert_after_fill(__pos._M_node, __n, __x);
507 }
508
509 // We don't need any dispatching tricks here, because _M_insert_after_range
510 // already does them.
511 template <class _InIter>
512 void insert_after(iterator __pos, _InIter __first, _InIter __last) {
513 _M_insert_after_range(__pos._M_node, __first, __last);
514 }
515
516 iterator insert(iterator __pos, const value_type& __x) {
517 return iterator(_M_insert_after(__slist_previous(&this->_M_head,
518 __pos._M_node),
519 __x));
520 }
521
522 iterator insert(iterator __pos) {
523 return iterator(_M_insert_after(__slist_previous(&this->_M_head,
524 __pos._M_node),
525 value_type()));
526 }
527
528 void insert(iterator __pos, size_type __n, const value_type& __x) {
529 _M_insert_after_fill(__slist_previous(&this->_M_head, __pos._M_node),
530 __n, __x);
531 }
532
533 // We don't need any dispatching tricks here, because _M_insert_after_range
534 // already does them.
535 template <class _InIter>
536 void insert(iterator __pos, _InIter __first, _InIter __last) {
537 _M_insert_after_range(__slist_previous(&this->_M_head, __pos._M_node),
538 __first, __last);
539 }
540
541 public:
542 iterator erase_after(iterator __pos) {
543 return iterator((_Node*) this->_M_erase_after(__pos._M_node));
544 }
545 iterator erase_after(iterator __before_first, iterator __last) {
546 return iterator((_Node*) this->_M_erase_after(__before_first._M_node,
547 __last._M_node));
548 }
549
550 iterator erase(iterator __pos) {
551 return (_Node*) this->_M_erase_after(__slist_previous(&this->_M_head,
552 __pos._M_node));
553 }
554 iterator erase(iterator __first, iterator __last) {
555 return (_Node*) this->_M_erase_after(
556 __slist_previous(&this->_M_head, __first._M_node), __last._M_node);
557 }
558
559 void resize(size_type new_size, const _Tp& __x);
560 void resize(size_type new_size) { resize(new_size, _Tp()); }
561 void clear() { this->_M_erase_after(&this->_M_head, 0); }
562
563 public:
564 // Moves the range [__before_first + 1, __before_last + 1) to *this,
565 // inserting it immediately after __pos. This is constant time.
566 void splice_after(iterator __pos,
567 iterator __before_first, iterator __before_last)
568 {
569 if (__before_first != __before_last)
570 __slist_splice_after(__pos._M_node, __before_first._M_node,
571 __before_last._M_node);
572 }
573
574 // Moves the element that follows __prev to *this, inserting it immediately
575 // after __pos. This is constant time.
576 void splice_after(iterator __pos, iterator __prev)
577 {
578 __slist_splice_after(__pos._M_node,
579 __prev._M_node, __prev._M_node->_M_next);
580 }
581
582
583 // Removes all of the elements from the list __x to *this, inserting
584 // them immediately after __pos. __x must not be *this. Complexity:
585 // linear in __x.size().
586 void splice_after(iterator __pos, slist& __x)
587 {
588 __slist_splice_after(__pos._M_node, &__x._M_head);
589 }
590
591 // Linear in distance(begin(), __pos), and linear in __x.size().
592 void splice(iterator __pos, slist& __x) {
593 if (__x._M_head._M_next)
594 __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
595 &__x._M_head, __slist_previous(&__x._M_head, 0));
596 }
597
598 // Linear in distance(begin(), __pos), and in distance(__x.begin(), __i).
599 void splice(iterator __pos, slist& __x, iterator __i) {
600 __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
601 __slist_previous(&__x._M_head, __i._M_node),
602 __i._M_node);
603 }
604
605 // Linear in distance(begin(), __pos), in distance(__x.begin(), __first),
606 // and in distance(__first, __last).
607 void splice(iterator __pos, slist& __x, iterator __first, iterator __last)
608 {
609 if (__first != __last)
610 __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
611 __slist_previous(&__x._M_head, __first._M_node),
612 __slist_previous(__first._M_node, __last._M_node));
613 }
614
615 public:
616 void reverse() {
617 if (this->_M_head._M_next)
618 this->_M_head._M_next = __slist_reverse(this->_M_head._M_next);
619 }
620
621 void remove(const _Tp& __val);
622 void unique();
623 void merge(slist& __x);
624 void sort();
625
626 template <class _Predicate>
627 void remove_if(_Predicate __pred);
628
629 template <class _BinaryPredicate>
630 void unique(_BinaryPredicate __pred);
631
632 template <class _StrictWeakOrdering>
633 void merge(slist&, _StrictWeakOrdering);
634
635 template <class _StrictWeakOrdering>
636 void sort(_StrictWeakOrdering __comp);
637 };
638
639 template <class _Tp, class _Alloc>
640 slist<_Tp,_Alloc>& slist<_Tp,_Alloc>::operator=(const slist<_Tp,_Alloc>& __x)
641 {
642 if (&__x != this) {
643 _Node_base* __p1 = &this->_M_head;
644 _Node* __n1 = (_Node*) this->_M_head._M_next;
645 const _Node* __n2 = (const _Node*) __x._M_head._M_next;
646 while (__n1 && __n2) {
647 __n1->_M_data = __n2->_M_data;
648 __p1 = __n1;
649 __n1 = (_Node*) __n1->_M_next;
650 __n2 = (const _Node*) __n2->_M_next;
651 }
652 if (__n2 == 0)
653 this->_M_erase_after(__p1, 0);
654 else
655 _M_insert_after_range(__p1, const_iterator((_Node*)__n2),
656 const_iterator(0));
657 }
658 return *this;
659 }
660
661 template <class _Tp, class _Alloc>
662 void slist<_Tp, _Alloc>::_M_fill_assign(size_type __n, const _Tp& __val) {
663 _Node_base* __prev = &this->_M_head;
664 _Node* __node = (_Node*) this->_M_head._M_next;
665 for ( ; __node != 0 && __n > 0 ; --__n) {
666 __node->_M_data = __val;
667 __prev = __node;
668 __node = (_Node*) __node->_M_next;
669 }
670 if (__n > 0)
671 _M_insert_after_fill(__prev, __n, __val);
672 else
673 this->_M_erase_after(__prev, 0);
674 }
675
676 template <class _Tp, class _Alloc> template <class _InputIter>
677 void
678 slist<_Tp, _Alloc>::_M_assign_dispatch(_InputIter __first, _InputIter __last,
679 __false_type)
680 {
681 _Node_base* __prev = &this->_M_head;
682 _Node* __node = (_Node*) this->_M_head._M_next;
683 while (__node != 0 && __first != __last) {
684 __node->_M_data = *__first;
685 __prev = __node;
686 __node = (_Node*) __node->_M_next;
687 ++__first;
688 }
689 if (__first != __last)
690 _M_insert_after_range(__prev, __first, __last);
691 else
692 this->_M_erase_after(__prev, 0);
693 }
694
695 template <class _Tp, class _Alloc>
696 inline bool
697 operator==(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2)
698 {
699 typedef typename slist<_Tp,_Alloc>::const_iterator const_iterator;
700 const_iterator __end1 = _SL1.end();
701 const_iterator __end2 = _SL2.end();
702
703 const_iterator __i1 = _SL1.begin();
704 const_iterator __i2 = _SL2.begin();
705 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2) {
706 ++__i1;
707 ++__i2;
708 }
709 return __i1 == __end1 && __i2 == __end2;
710 }
711
712
713 template <class _Tp, class _Alloc>
714 inline bool
715 operator<(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2)
716 {
717 return std::lexicographical_compare(_SL1.begin(), _SL1.end(),
718 _SL2.begin(), _SL2.end());
719 }
720
721 template <class _Tp, class _Alloc>
722 inline bool
723 operator!=(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
724 return !(_SL1 == _SL2);
725 }
726
727 template <class _Tp, class _Alloc>
728 inline bool
729 operator>(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
730 return _SL2 < _SL1;
731 }
732
733 template <class _Tp, class _Alloc>
734 inline bool
735 operator<=(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
736 return !(_SL2 < _SL1);
737 }
738
739 template <class _Tp, class _Alloc>
740 inline bool
741 operator>=(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
742 return !(_SL1 < _SL2);
743 }
744
745 template <class _Tp, class _Alloc>
746 inline void swap(slist<_Tp,_Alloc>& __x, slist<_Tp,_Alloc>& __y) {
747 __x.swap(__y);
748 }
749
750
751 template <class _Tp, class _Alloc>
752 void slist<_Tp,_Alloc>::resize(size_type __len, const _Tp& __x)
753 {
754 _Node_base* __cur = &this->_M_head;
755 while (__cur->_M_next != 0 && __len > 0) {
756 --__len;
757 __cur = __cur->_M_next;
758 }
759 if (__cur->_M_next)
760 this->_M_erase_after(__cur, 0);
761 else
762 _M_insert_after_fill(__cur, __len, __x);
763 }
764
765 template <class _Tp, class _Alloc>
766 void slist<_Tp,_Alloc>::remove(const _Tp& __val)
767 {
768 _Node_base* __cur = &this->_M_head;
769 while (__cur && __cur->_M_next) {
770 if (((_Node*) __cur->_M_next)->_M_data == __val)
771 this->_M_erase_after(__cur);
772 else
773 __cur = __cur->_M_next;
774 }
775 }
776
777 template <class _Tp, class _Alloc>
778 void slist<_Tp,_Alloc>::unique()
779 {
780 _Node_base* __cur = this->_M_head._M_next;
781 if (__cur) {
782 while (__cur->_M_next) {
783 if (((_Node*)__cur)->_M_data ==
784 ((_Node*)(__cur->_M_next))->_M_data)
785 this->_M_erase_after(__cur);
786 else
787 __cur = __cur->_M_next;
788 }
789 }
790 }
791
792 template <class _Tp, class _Alloc>
793 void slist<_Tp,_Alloc>::merge(slist<_Tp,_Alloc>& __x)
794 {
795 _Node_base* __n1 = &this->_M_head;
796 while (__n1->_M_next && __x._M_head._M_next) {
797 if (((_Node*) __x._M_head._M_next)->_M_data <
798 ((_Node*) __n1->_M_next)->_M_data)
799 __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
800 __n1 = __n1->_M_next;
801 }
802 if (__x._M_head._M_next) {
803 __n1->_M_next = __x._M_head._M_next;
804 __x._M_head._M_next = 0;
805 }
806 }
807
808 template <class _Tp, class _Alloc>
809 void slist<_Tp,_Alloc>::sort()
810 {
811 if (this->_M_head._M_next && this->_M_head._M_next->_M_next) {
812 slist __carry;
813 slist __counter[64];
814 int __fill = 0;
815 while (!empty()) {
816 __slist_splice_after(&__carry._M_head,
817 &this->_M_head, this->_M_head._M_next);
818 int __i = 0;
819 while (__i < __fill && !__counter[__i].empty()) {
820 __counter[__i].merge(__carry);
821 __carry.swap(__counter[__i]);
822 ++__i;
823 }
824 __carry.swap(__counter[__i]);
825 if (__i == __fill)
826 ++__fill;
827 }
828
829 for (int __i = 1; __i < __fill; ++__i)
830 __counter[__i].merge(__counter[__i-1]);
831 this->swap(__counter[__fill-1]);
832 }
833 }
834
835 template <class _Tp, class _Alloc>
836 template <class _Predicate>
837 void slist<_Tp,_Alloc>::remove_if(_Predicate __pred)
838 {
839 _Node_base* __cur = &this->_M_head;
840 while (__cur->_M_next) {
841 if (__pred(((_Node*) __cur->_M_next)->_M_data))
842 this->_M_erase_after(__cur);
843 else
844 __cur = __cur->_M_next;
845 }
846 }
847
848 template <class _Tp, class _Alloc> template <class _BinaryPredicate>
849 void slist<_Tp,_Alloc>::unique(_BinaryPredicate __pred)
850 {
851 _Node* __cur = (_Node*) this->_M_head._M_next;
852 if (__cur) {
853 while (__cur->_M_next) {
854 if (__pred(((_Node*)__cur)->_M_data,
855 ((_Node*)(__cur->_M_next))->_M_data))
856 this->_M_erase_after(__cur);
857 else
858 __cur = (_Node*) __cur->_M_next;
859 }
860 }
861 }
862
863 template <class _Tp, class _Alloc> template <class _StrictWeakOrdering>
864 void slist<_Tp,_Alloc>::merge(slist<_Tp,_Alloc>& __x,
865 _StrictWeakOrdering __comp)
866 {
867 _Node_base* __n1 = &this->_M_head;
868 while (__n1->_M_next && __x._M_head._M_next) {
869 if (__comp(((_Node*) __x._M_head._M_next)->_M_data,
870 ((_Node*) __n1->_M_next)->_M_data))
871 __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
872 __n1 = __n1->_M_next;
873 }
874 if (__x._M_head._M_next) {
875 __n1->_M_next = __x._M_head._M_next;
876 __x._M_head._M_next = 0;
877 }
878 }
879
880 template <class _Tp, class _Alloc> template <class _StrictWeakOrdering>
881 void slist<_Tp,_Alloc>::sort(_StrictWeakOrdering __comp)
882 {
883 if (this->_M_head._M_next && this->_M_head._M_next->_M_next) {
884 slist __carry;
885 slist __counter[64];
886 int __fill = 0;
887 while (!empty()) {
888 __slist_splice_after(&__carry._M_head,
889 &this->_M_head, this->_M_head._M_next);
890 int __i = 0;
891 while (__i < __fill && !__counter[__i].empty()) {
892 __counter[__i].merge(__carry, __comp);
893 __carry.swap(__counter[__i]);
894 ++__i;
895 }
896 __carry.swap(__counter[__i]);
897 if (__i == __fill)
898 ++__fill;
899 }
900
901 for (int __i = 1; __i < __fill; ++__i)
902 __counter[__i].merge(__counter[__i-1], __comp);
903 this->swap(__counter[__fill-1]);
904 }
905 }
906
907 } // namespace __gnu_cxx
908
909 namespace std
910 {
911 // Specialization of insert_iterator so that insertions will be constant
912 // time rather than linear time.
913
914 template <class _Tp, class _Alloc>
915 class insert_iterator<__gnu_cxx::slist<_Tp, _Alloc> > {
916 protected:
917 typedef __gnu_cxx::slist<_Tp, _Alloc> _Container;
918 _Container* container;
919 typename _Container::iterator iter;
920 public:
921 typedef _Container container_type;
922 typedef output_iterator_tag iterator_category;
923 typedef void value_type;
924 typedef void difference_type;
925 typedef void pointer;
926 typedef void reference;
927
928 insert_iterator(_Container& __x, typename _Container::iterator __i)
929 : container(&__x) {
930 if (__i == __x.begin())
931 iter = __x.before_begin();
932 else
933 iter = __x.previous(__i);
934 }
935
936 insert_iterator<_Container>&
937 operator=(const typename _Container::value_type& __value) {
938 iter = container->insert_after(iter, __value);
939 return *this;
940 }
941 insert_iterator<_Container>& operator*() { return *this; }
942 insert_iterator<_Container>& operator++() { return *this; }
943 insert_iterator<_Container>& operator++(int) { return *this; }
944 };
945
946 } // namespace std
947
948 #endif /* __SGI_STL_INTERNAL_SLIST_H */
949
950 // Local Variables:
951 // mode:C++
952 // End: