~ubuntu-branches/debian/experimental/libtorrent/experimental

« back to all changes in this revision

Viewing changes to rak/ranges.h

  • Committer: Bazaar Package Importer
  • Author(s): Jose Luis Rivas
  • Date: 2007-03-31 10:31:05 UTC
  • mto: (4.1.4 gutsy) (6.2.1 squeeze) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 6.
  • Revision ID: james.westby@ubuntu.com-20070331103105-jzpp1rml6ud0ff75
Tags: upstream-0.11.4
ImportĀ upstreamĀ versionĀ 0.11.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
template <typename Type>
47
47
class ranges : private std::vector<std::pair<Type, Type> > {
48
48
public:
49
 
  typedef std::vector<std::pair<Type, Type> > Base;
50
 
  typedef typename Base::value_type           value_type;
51
 
  typedef typename Base::reference            reference;
52
 
  typedef typename Base::iterator             iterator;
53
 
  typedef typename Base::const_iterator       const_iterator;
54
 
  typedef typename Base::reverse_iterator     reverse_iterator;
55
 
 
56
 
  using Base::clear;
57
 
  using Base::size;
58
 
  using Base::begin;
59
 
  using Base::end;
60
 
  using Base::rbegin;
61
 
  using Base::rend;
 
49
  typedef std::vector<std::pair<Type, Type> >  base_type;
 
50
  typedef typename base_type::value_type       value_type;
 
51
  typedef typename base_type::reference        reference;
 
52
  typedef typename base_type::iterator         iterator;
 
53
  typedef typename base_type::const_iterator   const_iterator;
 
54
  typedef typename base_type::reverse_iterator reverse_iterator;
 
55
 
 
56
  using base_type::clear;
 
57
  using base_type::size;
 
58
  using base_type::begin;
 
59
  using base_type::end;
 
60
  using base_type::rbegin;
 
61
  using base_type::rend;
 
62
 
 
63
  using base_type::front;
 
64
  using base_type::back;
62
65
 
63
66
  void                insert(Type first, Type last) { insert(std::make_pair(first, last)); }
64
 
//   void                erase(Type first, Type last)  { erase(std::make_pair(first, last)); }
 
67
  void                erase(Type first, Type last)  { erase(std::make_pair(first, last)); }
65
68
 
66
69
  void                insert(value_type r);
67
 
//   void                erase(value_type r);
 
70
  void                erase(value_type r);
68
71
 
69
72
  // Find the first ranges that has an end greater than index.
70
73
  iterator            find(Type index);
72
75
 
73
76
  // Use find with no closest match.
74
77
  bool                has(Type index) const;
75
 
 
76
 
private:
77
 
  void                unify(iterator itr);
78
78
};
79
79
 
80
80
template <typename Type>
88
88
  if (first == end() || r.second < first->first) {
89
89
    // The new range is before the first, after the last or between
90
90
    // two ranges.
91
 
    Base::insert(first, r);
92
 
    return;
93
 
  }
94
 
  
95
 
  first->first = std::min(r.first, first->first);
96
 
  first->second = std::max(r.second, first->second);
97
 
 
98
 
  unify(first);
99
 
}
100
 
 
101
 
// template <typename Type>
102
 
// void
103
 
// ranges<Type>::erase(value_type r) {
104
 
// }
 
91
    base_type::insert(first, r);
 
92
 
 
93
  } else {
 
94
    first->first = std::min(r.first, first->first);
 
95
    first->second = std::max(r.second, first->second);
 
96
 
 
97
    iterator last = std::find_if(first, end(), rak::less(first->second, rak::const_mem_ref(&value_type::second)));
 
98
 
 
99
    if (last != end() && first->second >= last->first)
 
100
      first->second = (last++)->second;
 
101
 
 
102
    base_type::erase(first + 1, last);
 
103
  }
 
104
}
 
105
 
 
106
template <typename Type>
 
107
void
 
108
ranges<Type>::erase(value_type r) {
 
109
  if (r.first >= r.second)
 
110
    return;
 
111
 
 
112
  iterator first = std::find_if(begin(), end(), rak::less(r.first, rak::const_mem_ref(&value_type::second)));
 
113
  iterator last  = std::find_if(first, end(), rak::less(r.second, rak::const_mem_ref(&value_type::second)));
 
114
 
 
115
  if (first == end())
 
116
    return;
 
117
 
 
118
  if (first == last) {
 
119
 
 
120
    if (r.first > first->first) {
 
121
      std::swap(first->first, r.second);
 
122
      base_type::insert(first, value_type(r.second, r.first));
 
123
 
 
124
    } else if (r.second > first->first) {
 
125
      first->first = r.second;
 
126
    }
 
127
 
 
128
  } else {
 
129
 
 
130
    if (r.first > first->first)
 
131
      (first++)->second = r.first;
 
132
    
 
133
    if (last != end() && r.second > last->first)
 
134
      last->first = r.second;
 
135
 
 
136
    base_type::erase(first, last);
 
137
  }
 
138
}
105
139
 
106
140
// Find the first ranges that has an end greater than index.
107
141
template <typename Type>
118
152
 
119
153
// Use find with no closest match.
120
154
template <typename Type>
121
 
inline bool
 
155
bool
122
156
ranges<Type>::has(Type index) const {
123
157
  const_iterator itr = find(index);
124
158
 
125
159
  return itr != end() && index >= itr->first;
126
160
}
127
161
 
128
 
template <typename Type>
129
 
inline void
130
 
ranges<Type>::unify(iterator first) {
131
 
  iterator last = std::find_if((first + 1), end(), rak::less(first->second, rak::const_mem_ref(&value_type::first)));
132
 
 
133
 
  first->second = std::max(first->second, (last - 1)->second);
134
 
  Base::erase((first + 1), last);
135
 
}
136
 
 
137
162
}
138
163
 
139
164
#endif