epoc32/include/tools/stlport/stl/_slist.c
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100
branchSymbian3
changeset 4 837f303aceeb
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
williamr@4
     1
/*
williamr@4
     2
 *
williamr@4
     3
 * Copyright (c) 1996,1997
williamr@4
     4
 * Silicon Graphics Computer Systems, Inc.
williamr@4
     5
 *
williamr@4
     6
 * Copyright (c) 1999
williamr@4
     7
 * Boris Fomitchev
williamr@4
     8
 *
williamr@4
     9
 * This material is provided "as is", with absolutely no warranty expressed
williamr@4
    10
 * or implied. Any use is at your own risk.
williamr@4
    11
 *
williamr@4
    12
 * Permission to use or copy this software for any purpose is hereby granted
williamr@4
    13
 * without fee, provided the above notices are retained on all copies.
williamr@4
    14
 * Permission to modify the code and to distribute modified code is granted,
williamr@4
    15
 * provided the above notices are retained, and a notice that the code was
williamr@4
    16
 * modified is included with the above copyright notice.
williamr@4
    17
 *
williamr@4
    18
 */
williamr@4
    19
#ifndef _STLP_SLIST_C
williamr@4
    20
#define _STLP_SLIST_C
williamr@4
    21
williamr@4
    22
#ifndef _STLP_INTERNAL_SLIST_H
williamr@4
    23
#  include <stl/_slist.h>
williamr@4
    24
#endif
williamr@4
    25
williamr@4
    26
#ifndef _STLP_CARRAY_H
williamr@4
    27
#  include <stl/_carray.h>
williamr@4
    28
#endif
williamr@4
    29
williamr@4
    30
#ifndef _STLP_RANGE_ERRORS_H
williamr@4
    31
#  include <stl/_range_errors.h>
williamr@4
    32
#endif
williamr@4
    33
williamr@4
    34
#if defined (_STLP_NESTED_TYPE_PARAM_BUG)
williamr@4
    35
#  define size_type size_t
williamr@4
    36
#endif
williamr@4
    37
williamr@4
    38
_STLP_BEGIN_NAMESPACE
williamr@4
    39
williamr@4
    40
_STLP_MOVE_TO_PRIV_NAMESPACE
williamr@4
    41
williamr@4
    42
template <class _Tp, class _Alloc>
williamr@4
    43
_Slist_node_base*
williamr@4
    44
_Slist_base<_Tp,_Alloc>::_M_erase_after(_Slist_node_base* __before_first,
williamr@4
    45
                                        _Slist_node_base* __last_node) {
williamr@4
    46
  _Slist_node_base* __cur = __before_first->_M_next;
williamr@4
    47
  while (__cur != __last_node) {
williamr@4
    48
    _Node* __tmp = __STATIC_CAST(_Node*, __cur);
williamr@4
    49
    __cur = __cur->_M_next;
williamr@4
    50
    _STLP_STD::_Destroy(&__tmp->_M_data);
williamr@4
    51
    _M_head.deallocate(__tmp,1);
williamr@4
    52
  }
williamr@4
    53
  __before_first->_M_next = __last_node;
williamr@4
    54
  return __last_node;
williamr@4
    55
}
williamr@4
    56
williamr@4
    57
#if defined (_STLP_USE_PTR_SPECIALIZATIONS)
williamr@4
    58
#  define slist _STLP_PTR_IMPL_NAME(slist)
williamr@4
    59
#elif defined (_STLP_DEBUG)
williamr@4
    60
#  define slist _STLP_NON_DBG_NAME(slist)
williamr@4
    61
#else
williamr@4
    62
_STLP_MOVE_TO_STD_NAMESPACE
williamr@4
    63
#endif
williamr@4
    64
williamr@4
    65
/* When building STLport lib Digital Mars Compiler complains on the _M_data assignment
williamr@4
    66
 * problem which would be perfertly right if we were using it. Hiding it during build
williamr@4
    67
 * fix this issue.
williamr@4
    68
 */
williamr@4
    69
template <class _Tp, class _Alloc>
williamr@4
    70
slist<_Tp,_Alloc>& slist<_Tp,_Alloc>::operator=(const slist<_Tp,_Alloc>& __x) {
williamr@4
    71
  if (&__x != this) {
williamr@4
    72
    _Node_base* __p1 = &this->_M_head._M_data;
williamr@4
    73
    _Node_base* __n1 = this->_M_head._M_data._M_next;
williamr@4
    74
    const _Node_base* __n2 = __x._M_head._M_data._M_next;
williamr@4
    75
    while (__n1 && __n2) {
williamr@4
    76
      __STATIC_CAST(_Node*, __n1)->_M_data = __STATIC_CAST(const _Node*, __n2)->_M_data;
williamr@4
    77
      __p1 = __n1;
williamr@4
    78
      __n1 = __n1->_M_next;
williamr@4
    79
      __n2 = __n2->_M_next;
williamr@4
    80
    }
williamr@4
    81
    if (__n2 == 0)
williamr@4
    82
      this->_M_erase_after(__p1, 0);
williamr@4
    83
    else
williamr@4
    84
      _M_insert_after_range(__p1, const_iterator(__CONST_CAST(_Node_base*, __n2)),
williamr@4
    85
                                  const_iterator(0));
williamr@4
    86
  }
williamr@4
    87
  return *this;
williamr@4
    88
}
williamr@4
    89
williamr@4
    90
template <class _Tp, class _Alloc>
williamr@4
    91
void slist<_Tp, _Alloc>::_M_fill_assign(size_type __n, const _Tp& __val) {
williamr@4
    92
  _Node_base* __prev = &this->_M_head._M_data;
williamr@4
    93
  _Node_base* __node = this->_M_head._M_data._M_next;
williamr@4
    94
  for ( ; __node != 0 && __n > 0 ; --__n) {
williamr@4
    95
    __STATIC_CAST(_Node*, __node)->_M_data = __val;
williamr@4
    96
    __prev = __node;
williamr@4
    97
    __node = __node->_M_next;
williamr@4
    98
  }
williamr@4
    99
  if (__n > 0)
williamr@4
   100
    _M_insert_after_fill(__prev, __n, __val);
williamr@4
   101
  else
williamr@4
   102
    this->_M_erase_after(__prev, 0);
williamr@4
   103
}
williamr@4
   104
williamr@4
   105
template <class _Tp, class _Alloc>
williamr@4
   106
void slist<_Tp,_Alloc>::resize(size_type __len, const _Tp& __x) {
williamr@4
   107
  _Node_base* __cur = &this->_M_head._M_data;
williamr@4
   108
  while (__cur->_M_next != 0 && __len > 0) {
williamr@4
   109
    --__len;
williamr@4
   110
    __cur = __cur->_M_next;
williamr@4
   111
  }
williamr@4
   112
  if (__cur->_M_next)
williamr@4
   113
    this->_M_erase_after(__cur, 0);
williamr@4
   114
  else
williamr@4
   115
    _M_insert_after_fill(__cur, __len, __x);
williamr@4
   116
}
williamr@4
   117
williamr@4
   118
template <class _Tp, class _Alloc>
williamr@4
   119
void slist<_Tp,_Alloc>::remove(const _Tp& __val) {
williamr@4
   120
  _Node_base* __cur = &this->_M_head._M_data;
williamr@4
   121
  while (__cur && __cur->_M_next) {
williamr@4
   122
    if (__STATIC_CAST(_Node*, __cur->_M_next)->_M_data == __val)
williamr@4
   123
      this->_M_erase_after(__cur);
williamr@4
   124
    else
williamr@4
   125
      __cur = __cur->_M_next;
williamr@4
   126
  }
williamr@4
   127
}
williamr@4
   128
williamr@4
   129
#if !defined (slist)
williamr@4
   130
_STLP_MOVE_TO_PRIV_NAMESPACE
williamr@4
   131
#endif
williamr@4
   132
williamr@4
   133
template <class _Tp, class _Alloc, class _BinaryPredicate>
williamr@4
   134
void _Slist_unique(slist<_Tp, _Alloc>& __that, _BinaryPredicate __pred) {
williamr@4
   135
  typedef _Slist_node<_Tp> _Node;
williamr@4
   136
  typename slist<_Tp, _Alloc>::iterator __ite(__that.begin());
williamr@4
   137
  if (__ite != __that.end()) {
williamr@4
   138
    while (__ite._M_node->_M_next) {
williamr@4
   139
      if (__pred(*__ite, __STATIC_CAST(_Node*, __ite._M_node->_M_next)->_M_data))
williamr@4
   140
        __that.erase_after(__ite);
williamr@4
   141
      else
williamr@4
   142
        ++__ite;
williamr@4
   143
    }
williamr@4
   144
  }
williamr@4
   145
}
williamr@4
   146
williamr@4
   147
template <class _Tp, class _Alloc, class _StrictWeakOrdering>
williamr@4
   148
void _Slist_merge(slist<_Tp, _Alloc>& __that, slist<_Tp, _Alloc>& __x,
williamr@4
   149
                  _StrictWeakOrdering __comp) {
williamr@4
   150
  typedef _Slist_node<_Tp> _Node;
williamr@4
   151
  typedef _STLP_PRIV _Slist_node_base _Node_base;
williamr@4
   152
  if (__that.get_allocator() == __x.get_allocator()) {
williamr@4
   153
    typename slist<_Tp, _Alloc>::iterator __ite(__that.before_begin());
williamr@4
   154
    while (__ite._M_node->_M_next && !__x.empty()) {
williamr@4
   155
      if (__comp(__x.front(), __STATIC_CAST(_Node*, __ite._M_node->_M_next)->_M_data)) {
williamr@4
   156
        _STLP_VERBOSE_ASSERT(!__comp(__STATIC_CAST(_Node*, __ite._M_node->_M_next)->_M_data, __x.front()),
williamr@4
   157
                             _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
williamr@4
   158
        __that.splice_after(__ite, __x, __x.before_begin());
williamr@4
   159
      }
williamr@4
   160
      ++__ite;
williamr@4
   161
    }
williamr@4
   162
    if (!__x.empty()) {
williamr@4
   163
      __that.splice_after(__ite, __x);
williamr@4
   164
    }
williamr@4
   165
  }
williamr@4
   166
  else {
williamr@4
   167
    typename slist<_Tp, _Alloc>::iterator __i1(__that.before_begin()), __i2(__x.begin());
williamr@4
   168
    while (__i1._M_node->_M_next && __i2._M_node) {
williamr@4
   169
      if (__comp(__STATIC_CAST(_Node*, __i1._M_node->_M_next)->_M_data, *__i2)) {
williamr@4
   170
        _STLP_VERBOSE_ASSERT(!__comp(*__i2, __STATIC_CAST(_Node*, __i1._M_node->_M_next)->_M_data),
williamr@4
   171
                             _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
williamr@4
   172
        ++__i1;
williamr@4
   173
      }
williamr@4
   174
      else {
williamr@4
   175
        __i1 = __that.insert_after(__i1, *(__i2++));
williamr@4
   176
      }
williamr@4
   177
    }
williamr@4
   178
    __that.insert_after(__i1, __i2, __x.end());
williamr@4
   179
    __x.clear();
williamr@4
   180
  }
williamr@4
   181
}
williamr@4
   182
williamr@4
   183
template <class _Tp, class _Alloc, class _StrictWeakOrdering>
williamr@4
   184
void _Slist_sort(slist<_Tp, _Alloc>& __that, _StrictWeakOrdering __comp) {
williamr@4
   185
  if (!__that.begin()._M_node || !__that.begin()._M_node->_M_next)
williamr@4
   186
    return;
williamr@4
   187
williamr@4
   188
  slist<_Tp, _Alloc> __carry(__that.get_allocator());
williamr@4
   189
  const int NB = 64;
williamr@4
   190
  _STLP_PRIV _CArray<slist<_Tp, _Alloc>, NB> __counter(__carry);
williamr@4
   191
  int __fill = 0;
williamr@4
   192
  while (!__that.empty()) {
williamr@4
   193
    __carry.splice_after(__carry.before_begin(), __that, __that.before_begin());
williamr@4
   194
    int __i = 0;
williamr@4
   195
    while (__i < __fill && !__counter[__i].empty()) {
williamr@4
   196
      _STLP_PRIV _Slist_merge(__counter[__i], __carry, __comp);
williamr@4
   197
      __carry.swap(__counter[__i]);
williamr@4
   198
      ++__i;
williamr@4
   199
    }
williamr@4
   200
    __carry.swap(__counter[__i]);
williamr@4
   201
    if (__i == __fill) {
williamr@4
   202
      ++__fill;
williamr@4
   203
      if (__fill >= NB) {
williamr@4
   204
        //Looks like the slist has too many elements to be sorted with this algorithm:
williamr@4
   205
        __stl_throw_overflow_error("slist::sort");
williamr@4
   206
      }
williamr@4
   207
    }
williamr@4
   208
  }
williamr@4
   209
williamr@4
   210
  for (int __i = 1; __i < __fill; ++__i)
williamr@4
   211
    _STLP_PRIV _Slist_merge(__counter[__i], __counter[__i - 1], __comp);
williamr@4
   212
  __that.swap(__counter[__fill-1]);
williamr@4
   213
}
williamr@4
   214
williamr@4
   215
#if defined (slist)
williamr@4
   216
#  undef slist
williamr@4
   217
#endif
williamr@4
   218
williamr@4
   219
_STLP_MOVE_TO_STD_NAMESPACE
williamr@4
   220
williamr@4
   221
_STLP_END_NAMESPACE
williamr@4
   222
williamr@4
   223
#if defined (_STLP_NESTED_TYPE_PARAM_BUG)
williamr@4
   224
#  undef size_type
williamr@4
   225
#endif
williamr@4
   226
williamr@4
   227
#endif /*  _STLP_SLIST_C */
williamr@4
   228
williamr@4
   229
// Local Variables:
williamr@4
   230
// mode:C++
williamr@4
   231
// End: