4 * Hewlett-Packard Company
6 * Copyright (c) 1996,1997
7 * Silicon Graphics Computer Systems, Inc.
10 * Moscow Center for SPARC Technology
15 * This material is provided "as is", with absolutely no warranty expressed
16 * or implied. Any use is at your own risk.
18 * Permission to use or copy this software for any purpose is hereby granted
19 * without fee, provided the above notices are retained on all copies.
20 * Permission to modify the code and to distribute modified code is granted,
21 * provided the above notices are retained, and a notice that the code was
22 * modified is included with the above copyright notice.
26 /* NOTE: This is an internal header file, included by other STL headers.
27 * You should not attempt to use it directly.
30 #ifndef _STLP_INTERNAL_LIST_H
31 #define _STLP_INTERNAL_LIST_H
33 # ifndef _STLP_INTERNAL_ALGOBASE_H
34 # include <stl/_algobase.h>
37 # ifndef _STLP_INTERNAL_ALLOC_H
38 # include <stl/_alloc.h>
41 # ifndef _STLP_INTERNAL_ITERATOR_H
42 # include <stl/_iterator.h>
45 # ifndef _STLP_INTERNAL_CONSTRUCT_H
46 # include <stl/_construct.h>
49 # ifndef _STLP_INTERNAL_FUNCTION_BASE_H
50 # include <stl/_function_base.h>
56 # define list __WORKAROUND_DBG_RENAME(list)
58 struct _List_node_base {
59 _List_node_base* _M_next;
60 _List_node_base* _M_prev;
63 template <class _Dummy>
66 typedef _List_node_base _Node;
67 static void _STLP_CALL _Transfer(_List_node_base* __position,
68 _List_node_base* __first, _List_node_base* __last);
71 # if defined (_STLP_USE_TEMPLATE_EXPORT)
72 _STLP_EXPORT_TEMPLATE_CLASS _List_global<bool>;
74 typedef _List_global<bool> _List_global_inst;
77 struct _List_node : public _List_node_base {
79 __TRIVIAL_STUFF(_List_node)
82 // for some reason, Digital Mars C++ needs a constructor...
88 struct _List_iterator_base {
89 typedef size_t size_type;
90 typedef ptrdiff_t difference_type;
91 typedef bidirectional_iterator_tag iterator_category;
93 _List_node_base* _M_node;
95 _List_iterator_base(_List_node_base* __x) : _M_node(__x) {}
96 _List_iterator_base() {}
98 void _M_incr() { _M_node = _M_node->_M_next; }
99 void _M_decr() { _M_node = _M_node->_M_prev; }
100 bool operator==(const _List_iterator_base& __y ) const {
101 return _M_node == __y._M_node;
103 bool operator!=(const _List_iterator_base& __y ) const {
104 return _M_node != __y._M_node;
111 template<class _Tp, class _Traits>
112 struct _List_iterator : public _List_iterator_base {
113 typedef _Tp value_type;
114 typedef typename _Traits::pointer pointer;
115 typedef typename _Traits::reference reference;
117 typedef _List_iterator<_Tp, _Nonconst_traits<_Tp> > iterator;
118 typedef _List_iterator<_Tp, _Const_traits<_Tp> > const_iterator;
119 typedef _List_iterator<_Tp, _Traits> _Self;
121 typedef bidirectional_iterator_tag iterator_category;
122 typedef _List_node<_Tp> _Node;
123 typedef size_t size_type;
124 typedef ptrdiff_t difference_type;
126 _List_iterator(_Node* __x) : _List_iterator_base(__x) {}
128 _List_iterator(const iterator& __x) : _List_iterator_base(__x._M_node) {}
130 reference operator*() const { return ((_Node*)_M_node)->_M_data; }
132 _STLP_DEFINE_ARROW_OPERATOR
134 _Self& operator++() {
138 _Self operator++(int) {
143 _Self& operator--() {
147 _Self operator--(int) {
155 #ifdef _STLP_USE_OLD_HP_ITERATOR_QUERIES
156 template <class _Tp, class _Traits>
157 inline _Tp* value_type(const _List_iterator<_Tp, _Traits>&) { return 0; }
158 inline bidirectional_iterator_tag iterator_category(const _List_iterator_base&) { return bidirectional_iterator_tag();}
159 inline ptrdiff_t* distance_type(const _List_iterator_base&) { return 0; }
163 // Base class that encapsulates details of allocators and helps
166 template <class _Tp, class _Alloc>
170 _STLP_FORCE_ALLOCATORS(_Tp, _Alloc)
171 typedef _List_node<_Tp> _Node;
172 typedef typename _Alloc_traits<_Node, _Alloc>::allocator_type
173 _Node_allocator_type;
175 typedef typename _Alloc_traits<_Tp, _Alloc>::allocator_type
178 allocator_type get_allocator() const {
179 return _STLP_CONVERT_ALLOCATOR((const _Node_allocator_type&)_M_node, _Tp);
182 _List_base(const allocator_type& __a) : _M_node(_STLP_CONVERT_ALLOCATOR(__a, _Node), (_Node*)0) {
183 _Node* __n = _M_node.allocate(1);
186 _M_node._M_data = __n;
190 _M_node.deallocate(_M_node._M_data, 1);
196 _STLP_alloc_proxy<_Node*, _Node, _Node_allocator_type> _M_node;
199 template <class _Tp, _STLP_DEFAULT_ALLOCATOR_SELECT(_Tp) >
202 // helper functions to reduce code duplication
203 template <class _Tp, class _Alloc, class _Predicate>
204 void _S_remove_if(list<_Tp, _Alloc>& __that, _Predicate __pred);
206 template <class _Tp, class _Alloc, class _BinaryPredicate>
207 void _S_unique(list<_Tp, _Alloc>& __that, _BinaryPredicate __binary_pred);
209 template <class _Tp, class _Alloc, class _StrictWeakOrdering>
210 void _S_merge(list<_Tp, _Alloc>& __that, list<_Tp, _Alloc>& __x,
211 _StrictWeakOrdering __comp);
213 template <class _Tp, class _Alloc, class _StrictWeakOrdering>
214 void _S_sort(list<_Tp, _Alloc>& __that, _StrictWeakOrdering __comp);
216 template <class _Tp, class _Alloc>
217 class list : public _List_base<_Tp, _Alloc> {
218 typedef _List_base<_Tp, _Alloc> _Base;
219 typedef list<_Tp, _Alloc> _Self;
221 typedef _Tp value_type;
222 typedef value_type* pointer;
223 typedef const value_type* const_pointer;
224 typedef value_type& reference;
225 typedef const value_type& const_reference;
226 typedef _List_node<_Tp> _Node;
227 typedef size_t size_type;
228 typedef ptrdiff_t difference_type;
229 _STLP_FORCE_ALLOCATORS(_Tp, _Alloc)
230 typedef typename _Base::allocator_type allocator_type;
231 typedef bidirectional_iterator_tag _Iterator_category;
234 typedef _List_iterator<_Tp, _Nonconst_traits<_Tp> > iterator;
235 typedef _List_iterator<_Tp, _Const_traits<_Tp> > const_iterator;
236 _STLP_DECLARE_BIDIRECTIONAL_REVERSE_ITERATORS;
239 _Node* _M_create_node(const _Tp& __x)
241 _Node* __p = this->_M_node.allocate(1);
243 _Construct(&__p->_M_data, __x);
245 _STLP_UNWIND(this->_M_node.deallocate(__p, 1));
249 _Node* _M_create_node()
251 _Node* __p = this->_M_node.allocate(1);
253 _Construct(&__p->_M_data);
255 _STLP_UNWIND(this->_M_node.deallocate(__p, 1));
260 # if !(defined(__MRC__)||(defined(__SC__) && !defined(__DMC__)))
263 list(const allocator_type& __a = allocator_type()) :
264 _List_base<_Tp, _Alloc>(__a) {
268 iterator begin() { return iterator((_Node*)(this->_M_node._M_data->_M_next)); }
269 const_iterator begin() const { return const_iterator((_Node*)(this->_M_node._M_data->_M_next)); }
271 iterator end() { return this->_M_node._M_data; }
272 const_iterator end() const { return this->_M_node._M_data; }
274 reverse_iterator rbegin()
275 { return reverse_iterator(end()); }
276 const_reverse_iterator rbegin() const
277 { return const_reverse_iterator(end()); }
279 reverse_iterator rend()
280 { return reverse_iterator(begin()); }
281 const_reverse_iterator rend() const
282 { return const_reverse_iterator(begin()); }
284 bool empty() const { return this->_M_node._M_data->_M_next == this->_M_node._M_data; }
285 size_type size() const {
286 size_type __result = distance(begin(), end());
289 size_type max_size() const { return size_type(-1); }
291 reference front() { return *begin(); }
292 const_reference front() const { return *begin(); }
293 reference back() { return *(--end()); }
294 const_reference back() const { return *(--end()); }
296 void swap(list<_Tp, _Alloc>& __x) {
297 _STLP_STD::swap(this->_M_node, __x._M_node);
300 iterator insert(iterator __position, const _Tp& __x) {
302 _Node* __tmp = _M_create_node(__x);
303 _List_node_base* __n = __position._M_node;
304 _List_node_base* __p = __n->_M_prev;
305 __tmp->_M_next = __n;
306 __tmp->_M_prev = __p;
307 __p->_M_next = __tmp;
308 __n->_M_prev = __tmp;
312 #ifdef _STLP_MEMBER_TEMPLATES
313 template <class _InputIterator>
314 void insert(iterator __pos, _InputIterator __first, _InputIterator __last) {
315 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
316 _M_insert_dispatch(__pos, __first, __last, _Integral());
318 // Check whether it's an integral type. If so, it's not an iterator.
319 template<class _Integer>
320 void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
321 const __true_type&) {
322 _M_fill_insert(__pos, (size_type) __n, (_Tp) __x);
324 template <class _InputIter>
326 _M_insert_dispatch(iterator __position,
327 _InputIter __first, _InputIter __last,
329 #else /* _STLP_MEMBER_TEMPLATES */
330 void insert(iterator __position, const _Tp* __first, const _Tp* __last) {
331 for ( ; __first != __last; ++__first)
332 insert(__position, *__first);
334 void insert(iterator __position, const_iterator __first, const_iterator __last)
335 #endif /* _STLP_MEMBER_TEMPLATES */
337 for ( ; __first != __last; ++__first)
338 insert(__position, *__first);
340 void insert(iterator __pos, size_type __n, const _Tp& __x) { _M_fill_insert(__pos, __n, __x); }
342 void _M_fill_insert(iterator __pos, size_type __n, const _Tp& __x) {
343 for ( ; __n > 0; --__n)
346 void push_front(const _Tp& __x) { insert(begin(), __x); }
347 void push_back(const _Tp& __x) { insert(end(), __x); }
349 # ifndef _STLP_NO_ANACHRONISMS
350 iterator insert(iterator __position) { return insert(__position, _Tp()); }
351 void push_front() {insert(begin());}
352 void push_back() {insert(end());}
355 iterator erase(iterator __position) {
356 _List_node_base* __next_node = __position._M_node->_M_next;
357 _List_node_base* __prev_node = __position._M_node->_M_prev;
358 _Node* __n = (_Node*) __position._M_node;
359 __prev_node->_M_next = __next_node;
360 __next_node->_M_prev = __prev_node;
361 _STLP_STD::_Destroy(&__n->_M_data);
362 this->_M_node.deallocate(__n, 1);
363 return iterator((_Node*)__next_node);
366 iterator erase(iterator __first, iterator __last) {
367 while (__first != __last)
372 void resize(size_type __new_size, _Tp __x);
373 void resize(size_type __new_size) { this->resize(__new_size, _Tp()); }
375 void pop_front() { erase(begin()); }
377 iterator __tmp = end();
380 list(size_type __n, const _Tp& __val,
381 const allocator_type& __a = allocator_type())
382 : _List_base<_Tp, _Alloc>(__a)
384 _STLP_PUSH_CLEANUP_ITEM(_Base, this)
385 this->insert(begin(), __n, __val);
386 _STLP_POP_CLEANUP_ITEM
388 explicit list(size_type __n)
389 : _List_base<_Tp, _Alloc>(allocator_type())
391 # ifdef _STLP_USE_TRAP_LEAVE
392 _STLP_PUSH_CLEANUP_ITEM(_Base, this)
394 _STLP_PUSH_CLEANUP_ITEM(_Tp, &__p)
395 this->insert(begin(), __n, __p);
396 // unconditional for __p
398 _STLP_POP_CLEANUP_ITEM
400 this->insert(begin(), __n, _Tp());
404 #ifdef _STLP_MEMBER_TEMPLATES
405 // We don't need any dispatching tricks here, because insert does all of
407 # ifdef _STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS
408 template <class _InputIterator>
409 list(_InputIterator __first, _InputIterator __last)
410 : _List_base<_Tp, _Alloc>(allocator_type())
412 _STLP_PUSH_CLEANUP_ITEM(_Base, this)
413 insert(begin(), __first, __last);
414 _STLP_POP_CLEANUP_ITEM
418 template <class _InputIterator>
419 list(_InputIterator __first, _InputIterator __last,
420 const allocator_type& __a _STLP_ALLOCATOR_TYPE_DFL)
421 : _List_base<_Tp, _Alloc>(__a)
423 _STLP_PUSH_CLEANUP_ITEM(_Base, this)
424 insert(begin(), __first, __last);
425 _STLP_POP_CLEANUP_ITEM
428 #else /* _STLP_MEMBER_TEMPLATES */
430 list(const _Tp* __first, const _Tp* __last,
431 const allocator_type& __a = allocator_type())
432 : _List_base<_Tp, _Alloc>(__a)
434 _STLP_PUSH_CLEANUP_ITEM(_Base, this)
435 insert(begin(), __first, __last);
436 _STLP_POP_CLEANUP_ITEM
438 list(const_iterator __first, const_iterator __last,
439 const allocator_type& __a = allocator_type())
440 : _List_base<_Tp, _Alloc>(__a)
442 _STLP_PUSH_CLEANUP_ITEM(_Base, this)
443 insert(begin(), __first, __last);
444 _STLP_POP_CLEANUP_ITEM
447 #endif /* _STLP_MEMBER_TEMPLATES */
448 list(const list<_Tp, _Alloc>& __x) : _List_base<_Tp, _Alloc>(__x.get_allocator())
450 _STLP_PUSH_CLEANUP_ITEM(_Base, this)
451 insert(begin(), __x.begin(), __x.end());
452 _STLP_POP_CLEANUP_ITEM
457 list<_Tp, _Alloc>& operator=(const list<_Tp, _Alloc>& __x);
460 // assign(), a generalized assignment member function. Two
461 // versions: one that takes a count, and one that takes a range.
462 // The range version is a member template, so we dispatch on whether
463 // or not the type is an integer.
465 void assign(size_type __n, const _Tp& __val) { _M_fill_assign(__n, __val); }
467 void _M_fill_assign(size_type __n, const _Tp& __val);
469 #ifdef _STLP_MEMBER_TEMPLATES
471 template <class _InputIterator>
472 void assign(_InputIterator __first, _InputIterator __last) {
473 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
474 _M_assign_dispatch(__first, __last, _Integral());
477 template <class _Integer>
478 void _M_assign_dispatch(_Integer __n, _Integer __val, const __true_type&)
479 { assign((size_type) __n, (_Tp) __val); }
481 template <class _InputIterator>
482 void _M_assign_dispatch(_InputIterator __first2, _InputIterator __last2,
483 const __false_type&) {
484 iterator __first1 = begin();
485 iterator __last1 = end();
486 for ( ; __first1 != __last1 && __first2 != __last2; ++__first1, ++__first2)
487 *__first1 = *__first2;
488 if (__first2 == __last2)
489 erase(__first1, __last1);
491 insert(__last1, __first2, __last2);
494 #endif /* _STLP_MEMBER_TEMPLATES */
497 void splice(iterator __position, _Self& __x) {
499 _List_global_inst::_Transfer(__position._M_node, __x.begin()._M_node, __x.end()._M_node);
501 void splice(iterator __position, _Self&, iterator __i) {
504 if (__position == __i || __position == __j) return;
505 _List_global_inst::_Transfer(__position._M_node, __i._M_node, __j._M_node);
507 void splice(iterator __position, _Self&, iterator __first, iterator __last) {
508 if (__first != __last)
509 _List_global_inst::_Transfer(__position._M_node, __first._M_node, __last._M_node);
512 void remove(const _Tp& __val) {
513 iterator __first = begin();
514 iterator __last = end();
515 while (__first != __last) {
516 iterator __next = __first;
518 if (__val == *__first) erase(__first);
524 _S_unique(*this, equal_to<_Tp>());
527 void merge(_Self& __x) {
528 _S_merge(*this, __x, less<_Tp>());
532 _List_node_base* __p = this->_M_node._M_data;
533 _List_node_base* __tmp = __p;
535 _STLP_STD::swap(__tmp->_M_next, __tmp->_M_prev);
536 __tmp = __tmp->_M_prev; // Old next node is now prev.
537 } while (__tmp != __p);
541 _S_sort(*this, less<_Tp>());
544 #ifdef _STLP_MEMBER_TEMPLATES
545 template <class _Predicate> void remove_if(_Predicate __pred) {
546 _S_remove_if(*this, __pred);
548 template <class _BinaryPredicate>
549 void unique(_BinaryPredicate __binary_pred) {
550 _S_unique(*this, __binary_pred);
553 template <class _StrictWeakOrdering>
554 void merge(list<_Tp, _Alloc>& __x,
555 _StrictWeakOrdering __comp) {
556 _S_merge(*this, __x, __comp);
559 template <class _StrictWeakOrdering>
560 void sort(_StrictWeakOrdering __comp) {
561 _S_sort(*this, __comp);
563 #endif /* _STLP_MEMBER_TEMPLATES */
565 #ifdef _STLP_USE_TRAP_LEAVE
567 static void* operator new (size_t __n, TLeave) { return _STLP_StackHelper<bool>::_NewLC(__n); }
568 static void* operator new (size_t __n) { return _STLP_StackHelper<bool>::_NewLC(__n); }
573 template <class _Tp, class _Alloc>
574 _STLP_INLINE_LOOP bool _STLP_CALL
575 operator==(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
577 typedef typename list<_Tp,_Alloc>::const_iterator const_iterator;
578 const_iterator __end1 = __x.end();
579 const_iterator __end2 = __y.end();
581 const_iterator __i1 = __x.begin();
582 const_iterator __i2 = __y.begin();
583 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2) {
587 return __i1 == __end1 && __i2 == __end2;
590 # define _STLP_EQUAL_OPERATOR_SPECIALIZED
591 # define _STLP_TEMPLATE_HEADER template <class _Tp, class _Alloc>
592 # define _STLP_TEMPLATE_CONTAINER list<_Tp, _Alloc>
593 # include <stl/_relops_cont.h>
594 # undef _STLP_TEMPLATE_CONTAINER
595 # undef _STLP_TEMPLATE_HEADER
596 # undef _STLP_EQUAL_OPERATOR_SPECIALIZED
600 # if !defined (_STLP_LINK_TIME_INSTANTIATION)
601 # include <stl/_list.c>
606 # define __list__ __FULL_NAME(list)
608 #if defined (_STLP_DEBUG)
609 # include <stl/debug/_list.h>
612 #if defined (_STLP_USE_WRAPPER_FOR_ALLOC_PARAM)
613 # include <stl/wrappers/_list.h>
616 #endif /* _STLP_INTERNAL_LIST_H */