Nvwa  1.1
set_assign.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
2 // vim:tabstop=4:shiftwidth=4:expandtab:
3 
4 /*
5  * Copyright (C) 2004-2013 Wu Yongwei <adah at users dot sourceforge dot net>
6  *
7  * This software is provided 'as-is', without any express or implied
8  * warranty. In no event will the authors be held liable for any
9  * damages arising from the use of this software.
10  *
11  * Permission is granted to anyone to use this software for any purpose,
12  * including commercial applications, and to alter it and redistribute
13  * it freely, subject to the following restrictions:
14  *
15  * 1. The origin of this software must not be misrepresented; you must
16  * not claim that you wrote the original software. If you use this
17  * software in a product, an acknowledgement in the product
18  * documentation would be appreciated but is not required.
19  * 2. Altered source versions must be plainly marked as such, and must
20  * not be misrepresented as being the original software.
21  * 3. This notice may not be removed or altered from any source
22  * distribution.
23  *
24  * This file is part of Stones of Nvwa:
25  * http://sourceforge.net/projects/nvwa
26  *
27  */
28 
37 #ifndef NVWA_SET_ASSIGN_H
38 #define NVWA_SET_ASSIGN_H
39 
40 #include <algorithm> // std::copy
41 #include "_nvwa.h" // NVWA_NAMESPACE_*
42 
43 NVWA_NAMESPACE_BEGIN
44 
45 template <class _Container, class _InputIter>
46 _Container& set_assign_union(_Container& dest,
47  _InputIter first,
48  _InputIter last)
49 {
50  typename _Container::iterator first_dest = dest.begin();
51  typename _Container::iterator last_dest = dest.end();
52  while (first_dest != last_dest && first != last)
53  {
54  if (*first_dest < *first)
55  ++first_dest;
56  else if (*first < *first_dest)
57  {
58  dest.insert(first_dest, *first);
59  ++first;
60  }
61  else // *first_dest == *first
62  {
63  ++first_dest;
64  ++first;
65  }
66  }
67  if (first != last)
68  std::copy(first, last, inserter(dest, last_dest));
69  return dest;
70 }
71 
72 template <class _Container, class _InputIter, class _Compare>
73 _Container& set_assign_union(_Container& dest,
74  _InputIter first,
75  _InputIter last,
76  _Compare comp)
77 {
78  typename _Container::iterator first_dest = dest.begin();
79  typename _Container::iterator last_dest = dest.end();
80  while (first_dest != last_dest && first != last)
81  {
82  if (comp(*first_dest, *first))
83  ++first_dest;
84  else if (comp(*first, *first_dest))
85  {
86  dest.insert(first_dest, *first);
87  ++first;
88  }
89  else // *first_dest is equivalent to *first
90  {
91  ++first_dest;
92  ++first;
93  }
94  }
95  if (first != last)
96  std::copy(first, last, inserter(dest, last_dest));
97  return dest;
98 }
99 
100 template <class _Container, class _InputIter>
101 _Container& set_assign_difference(_Container& dest,
102  _InputIter first,
103  _InputIter last)
104 {
105  typename _Container::iterator first_dest = dest.begin();
106  typename _Container::iterator last_dest = dest.end();
107  while (first_dest != last_dest && first != last)
108  {
109  if (*first_dest < *first)
110  ++first_dest;
111  else if (*first < *first_dest)
112  ++first;
113  else // *first == *first_dest
114  {
115  dest.erase(first_dest++);
116  ++first;
117  }
118  }
119  return dest;
120 }
121 
122 template <class _Container, class _InputIter, class _Compare>
123 _Container& set_assign_difference(_Container& dest,
124  _InputIter first,
125  _InputIter last,
126  _Compare comp)
127 {
128  typename _Container::iterator first_dest = dest.begin();
129  typename _Container::iterator last_dest = dest.end();
130  while (first_dest != last_dest && first != last)
131  {
132  if (comp(*first_dest, *first))
133  ++first_dest;
134  else if (comp(*first, *first_dest))
135  ++first;
136  else // *first_dest is equivalent to *first
137  {
138  dest.erase(first_dest++);
139  ++first;
140  }
141  }
142  return dest;
143 }
144 
145 NVWA_NAMESPACE_END
146 
147 #endif // NVWA_SET_ASSIGN_H
Common definitions for preprocessing.