epoc32/include/stdapis/stlportv5/stl/_list.c
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:27:01 +0100
branchSymbian2
changeset 3 e1b950c65cb4
parent 2 epoc32/include/stdapis/stlport/stl/_list.c@2fe1408b6811
child 4 837f303aceeb
permissions -rw-r--r--
Attempt to represent the S^2->S^3 header reorganisation as a series of "hg rename" operations
williamr@2
     1
/*
williamr@2
     2
 *
williamr@2
     3
 *
williamr@2
     4
 * Copyright (c) 1994
williamr@2
     5
 * Hewlett-Packard Company
williamr@2
     6
 *
williamr@2
     7
 * Copyright (c) 1996,1997
williamr@2
     8
 * Silicon Graphics Computer Systems, Inc.
williamr@2
     9
 *
williamr@2
    10
 * Copyright (c) 1997
williamr@2
    11
 * Moscow Center for SPARC Technology
williamr@2
    12
 *
williamr@2
    13
 * Copyright (c) 1999 
williamr@2
    14
 * Boris Fomitchev
williamr@2
    15
 *
williamr@2
    16
 * This material is provided "as is", with absolutely no warranty expressed
williamr@2
    17
 * or implied. Any use is at your own risk.
williamr@2
    18
 *
williamr@2
    19
 * Permission to use or copy this software for any purpose is hereby granted 
williamr@2
    20
 * without fee, provided the above notices are retained on all copies.
williamr@2
    21
 * Permission to modify the code and to distribute modified code is granted,
williamr@2
    22
 * provided the above notices are retained, and a notice that the code was
williamr@2
    23
 * modified is included with the above copyright notice.
williamr@2
    24
 *
williamr@2
    25
 */
williamr@2
    26
#ifndef _STLP_LIST_C
williamr@2
    27
#define _STLP_LIST_C
williamr@2
    28
williamr@2
    29
#ifndef _STLP_INTERNAL_LIST_H
williamr@2
    30
# include <stl/_list.h>
williamr@2
    31
#endif
williamr@2
    32
williamr@2
    33
#if defined (__WATCOMC__) || defined (_STLP_USE_TRAP_LEAVE)
williamr@2
    34
#include <vector>
williamr@2
    35
#endif
williamr@2
    36
williamr@2
    37
# undef list
williamr@2
    38
# define  list  __WORKAROUND_DBG_RENAME(list)
williamr@2
    39
williamr@2
    40
_STLP_BEGIN_NAMESPACE
williamr@2
    41
williamr@2
    42
# if defined (_STLP_EXPOSE_GLOBALS_IMPLEMENTATION)
williamr@2
    43
williamr@2
    44
template <class _Dummy>
williamr@2
    45
void _STLP_CALL
williamr@2
    46
_List_global<_Dummy>::_Transfer(_List_node_base* __position, 
williamr@2
    47
				_List_node_base* __first, _List_node_base* __last) {
williamr@2
    48
  if (__position != __last) {
williamr@2
    49
    // Remove [first, last) from its old position.
williamr@2
    50
    ((_Node*) (__last->_M_prev))->_M_next = __position;
williamr@2
    51
    ((_Node*) (__first->_M_prev))->_M_next    = __last;
williamr@2
    52
    ((_Node*) (__position->_M_prev))->_M_next = __first; 
williamr@2
    53
    
williamr@2
    54
    // Splice [first, last) into its new position.
williamr@2
    55
    _Node* __tmp = (_Node*) (__position->_M_prev);
williamr@2
    56
    __position->_M_prev = __last->_M_prev;
williamr@2
    57
    __last->_M_prev      = __first->_M_prev; 
williamr@2
    58
    __first->_M_prev    = __tmp;
williamr@2
    59
  }
williamr@2
    60
}
williamr@2
    61
williamr@2
    62
#endif /* defined (__BUILDING_STLPORT) || ! defined (_STLP_OWN_IOSTREAMS) */
williamr@2
    63
williamr@2
    64
williamr@2
    65
template <class _Tp, class _Alloc>
williamr@2
    66
void 
williamr@2
    67
_List_base<_Tp,_Alloc>::clear() 
williamr@2
    68
{
williamr@2
    69
  _List_node<_Tp>* __cur = this->_M_node._M_data;
williamr@2
    70
  if (!__cur)
williamr@2
    71
	return;
williamr@2
    72
  __cur = (_List_node<_Tp>*)__cur->_M_next;
williamr@2
    73
  while (__cur != this->_M_node._M_data) {
williamr@2
    74
    _List_node<_Tp>* __tmp = __cur;
williamr@2
    75
    __cur = (_List_node<_Tp>*) __cur->_M_next;
williamr@2
    76
    _STLP_STD::_Destroy(&__tmp->_M_data);
williamr@2
    77
    this->_M_node.deallocate(__tmp, 1);
williamr@2
    78
  }
williamr@2
    79
  this->_M_node._M_data->_M_next = this->_M_node._M_data;
williamr@2
    80
  this->_M_node._M_data->_M_prev = this->_M_node._M_data;
williamr@2
    81
}
williamr@2
    82
williamr@2
    83
# if defined (_STLP_NESTED_TYPE_PARAM_BUG) 
williamr@2
    84
#  define size_type      size_t
williamr@2
    85
# endif
williamr@2
    86
williamr@2
    87
template <class _Tp, class _Alloc>
williamr@2
    88
void list<_Tp, _Alloc>::resize(size_type __new_size, _Tp __x)
williamr@2
    89
{
williamr@2
    90
  iterator __i = begin();
williamr@2
    91
  size_type __len = 0;
williamr@2
    92
  for ( ; __i != end() && __len < __new_size; ++__i, ++__len);
williamr@2
    93
williamr@2
    94
  if (__len == __new_size)
williamr@2
    95
    erase(__i, end());
williamr@2
    96
  else                          // __i == end()
williamr@2
    97
    insert(end(), __new_size - __len, __x);
williamr@2
    98
}
williamr@2
    99
williamr@2
   100
template <class _Tp, class _Alloc>
williamr@2
   101
list<_Tp, _Alloc>& list<_Tp, _Alloc>::operator=(const list<_Tp, _Alloc>& __x)
williamr@2
   102
{
williamr@2
   103
  if (this != &__x) {
williamr@2
   104
    iterator __first1 = begin();
williamr@2
   105
    iterator __last1 = end();
williamr@2
   106
    const_iterator __first2 = __x.begin();
williamr@2
   107
    const_iterator __last2 = __x.end();
williamr@2
   108
    while (__first1 != __last1 && __first2 != __last2) 
williamr@2
   109
      *__first1++ = *__first2++;
williamr@2
   110
    if (__first2 == __last2)
williamr@2
   111
      erase(__first1, __last1);
williamr@2
   112
    else
williamr@2
   113
      insert(__last1, __first2, __last2);
williamr@2
   114
  }
williamr@2
   115
  return *this;
williamr@2
   116
}
williamr@2
   117
williamr@2
   118
template <class _Tp, class _Alloc>
williamr@2
   119
void list<_Tp, _Alloc>::_M_fill_assign(size_type __n, const _Tp& __val) {
williamr@2
   120
  iterator __i = begin();
williamr@2
   121
  for ( ; __i != end() && __n > 0; ++__i, --__n)
williamr@2
   122
    *__i = __val;
williamr@2
   123
  if (__n > 0)
williamr@2
   124
    insert(end(), __n, __val);
williamr@2
   125
  else
williamr@2
   126
    erase(__i, end());
williamr@2
   127
}
williamr@2
   128
williamr@2
   129
template <class _Tp, class _Alloc, class _Predicate> 
williamr@2
   130
void _S_remove_if(list<_Tp, _Alloc>& __that, _Predicate __pred)  {
williamr@2
   131
  typename list<_Tp, _Alloc>::iterator __first = __that.begin();
williamr@2
   132
  typename list<_Tp, _Alloc>::iterator __last = __that.end();
williamr@2
   133
  while (__first != __last) {
williamr@2
   134
    typename list<_Tp, _Alloc>::iterator __next = __first;
williamr@2
   135
    ++__next;
williamr@2
   136
    if (__pred(*__first)) __that.erase(__first);
williamr@2
   137
    __first = __next;
williamr@2
   138
  }
williamr@2
   139
}
williamr@2
   140
williamr@2
   141
template <class _Tp, class _Alloc, class _BinaryPredicate>
williamr@2
   142
void _S_unique(list<_Tp, _Alloc>& __that, _BinaryPredicate __binary_pred) {
williamr@2
   143
  typename list<_Tp, _Alloc>::iterator __first = __that.begin();
williamr@2
   144
  typename list<_Tp, _Alloc>::iterator __last = __that.end();
williamr@2
   145
  if (__first == __last) return;
williamr@2
   146
  typename list<_Tp, _Alloc>::iterator __next = __first;
williamr@2
   147
  while (++__next != __last) {
williamr@2
   148
    if (__binary_pred(*__first, *__next))
williamr@2
   149
      __that.erase(__next);
williamr@2
   150
    else
williamr@2
   151
      __first = __next;
williamr@2
   152
    __next = __first;
williamr@2
   153
  }
williamr@2
   154
}
williamr@2
   155
williamr@2
   156
template <class _Tp, class _Alloc, class _StrictWeakOrdering>
williamr@2
   157
void _S_merge(list<_Tp, _Alloc>& __that, list<_Tp, _Alloc>& __x,
williamr@2
   158
	      _StrictWeakOrdering __comp) {
williamr@2
   159
  typedef typename list<_Tp, _Alloc>::iterator _Literator;
williamr@2
   160
  _Literator __first1 = __that.begin();
williamr@2
   161
  _Literator __last1 = __that.end();
williamr@2
   162
  _Literator __first2 = __x.begin();
williamr@2
   163
  _Literator __last2 = __x.end();
williamr@2
   164
  while (__first1 != __last1 && __first2 != __last2)
williamr@2
   165
    if (__comp(*__first2, *__first1)) {
williamr@2
   166
      _Literator __next = __first2;
williamr@2
   167
      _List_global_inst::_Transfer(__first1._M_node, __first2._M_node, (++__next)._M_node);
williamr@2
   168
      __first2 = __next;
williamr@2
   169
    }
williamr@2
   170
    else
williamr@2
   171
      ++__first1;
williamr@2
   172
  if (__first2 != __last2) _List_global_inst::_Transfer(__last1._M_node, __first2._M_node, __last2._M_node);
williamr@2
   173
}
williamr@2
   174
williamr@2
   175
template <class _Tp, class _Alloc, class _StrictWeakOrdering>
williamr@2
   176
void _S_sort(list<_Tp, _Alloc>& __that, _StrictWeakOrdering __comp) {
williamr@2
   177
  // Do nothing if the list has length 0 or 1.
williamr@2
   178
  if (__that._M_node._M_data->_M_next != __that._M_node._M_data &&
williamr@2
   179
      (__that._M_node._M_data->_M_next)->_M_next != __that._M_node._M_data) {
williamr@2
   180
williamr@2
   181
#if !defined (__WATCOMC__)
williamr@2
   182
#ifdef _STLP_USE_TRAP_LEAVE
williamr@2
   183
    typedef vector<list<_Tp, _Alloc>*, _Alloc>  _TmpVec;
williamr@2
   184
    _TmpVec* __pTmp = new _TmpVec();
williamr@2
   185
    _TmpVec& __counter = *__pTmp;
williamr@2
   186
    for (int i = 0; 1< 64; ++i) {
williamr@2
   187
      list<_Tp, _Alloc>* __pTmp2 = new list<_Tp, _Alloc>;
williamr@2
   188
      __counter.push_back (__pTmp2);
williamr@2
   189
    }
williamr@2
   190
    list<_Tp, _Alloc>* __pcarry = new list<_Tp, _Alloc>;
williamr@2
   191
    list<_Tp, _Alloc>& __carry = *__pcarry;
williamr@2
   192
#else
williamr@2
   193
    list<_Tp, _Alloc> __counter[64];
williamr@2
   194
    list<_Tp, _Alloc> __carry;
williamr@2
   195
#endif
williamr@2
   196
#else
williamr@2
   197
    list<_Tp, _Alloc> __carry;
williamr@2
   198
    __vector__<list<_Tp, _Alloc>, _Alloc> __counter(64);		
williamr@2
   199
#endif
williamr@2
   200
    int __fill = 0;
williamr@2
   201
#ifdef _STLP_USE_TRAP_LEAVE
williamr@2
   202
    while (!__that.empty()) {
williamr@2
   203
      __carry.splice(__carry.begin(), __that, __that.begin());
williamr@2
   204
      int __i = 0;
williamr@2
   205
williamr@2
   206
      while(__i < __fill && !__counter[__i]->empty()) {
williamr@2
   207
	_S_merge(*__counter[__i], __carry, __comp);
williamr@2
   208
	__carry.swap(*__counter[__i++]);
williamr@2
   209
      }
williamr@2
   210
      __carry.swap(*__counter[__i]);         
williamr@2
   211
      if (__i == __fill) ++__fill;
williamr@2
   212
    } 
williamr@2
   213
    
williamr@2
   214
    for (int __i = 1; __i < __fill; ++__i) 
williamr@2
   215
      _S_merge(*__counter[__i], *__counter[__i-1], __comp);
williamr@2
   216
    __that.swap(*__counter[__fill-1]);
williamr@2
   217
williamr@2
   218
    // those objects won't just go away
williamr@2
   219
    __counter.clear();
williamr@2
   220
    CleanupStack::Pop(66);
williamr@2
   221
  }    
williamr@2
   222
# else
williamr@2
   223
    while (!__that.empty()) {
williamr@2
   224
      __carry.splice(__carry.begin(), __that, __that.begin());
williamr@2
   225
      int __i = 0;
williamr@2
   226
williamr@2
   227
      while(__i < __fill && !__counter[__i].empty()) {
williamr@2
   228
	_S_merge(__counter[__i], __carry, __comp);
williamr@2
   229
	__carry.swap(__counter[__i++]);
williamr@2
   230
      }
williamr@2
   231
      __carry.swap(__counter[__i]);         
williamr@2
   232
      if (__i == __fill) ++__fill;
williamr@2
   233
    } 
williamr@2
   234
    
williamr@2
   235
    for (int __i = 1; __i < __fill; ++__i) 
williamr@2
   236
      _S_merge(__counter[__i], __counter[__i-1], __comp);
williamr@2
   237
    __that.swap(__counter[__fill-1]);
williamr@2
   238
  }
williamr@2
   239
# endif
williamr@2
   240
williamr@2
   241
}
williamr@2
   242
williamr@2
   243
# undef  list
williamr@2
   244
# undef  size_type
williamr@2
   245
williamr@2
   246
_STLP_END_NAMESPACE
williamr@2
   247
williamr@2
   248
#endif /*  _STLP_LIST_C */
williamr@2
   249
williamr@2
   250
// Local Variables:
williamr@2
   251
// mode:C++
williamr@2
   252
// End: