set_assign.h

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
00002 // vim:tabstop=4:shiftwidth=4:expandtab:
00003 
00004 /*
00005  * Copyright (C) 2004-2007 Wu Yongwei <adah at users dot sourceforge dot net>
00006  *
00007  * This software is provided 'as-is', without any express or implied
00008  * warranty.  In no event will the authors be held liable for any
00009  * damages arising from the use of this software.
00010  *
00011  * Permission is granted to anyone to use this software for any purpose,
00012  * including commercial applications, and to alter it and redistribute
00013  * it freely, subject to the following restrictions:
00014  *
00015  * 1. The origin of this software must not be misrepresented; you must
00016  *    not claim that you wrote the original software. If you use this
00017  *    software in a product, an acknowledgement in the product
00018  *    documentation would be appreciated but is not required.
00019  * 2. Altered source versions must be plainly marked as such, and must
00020  *    not be misrepresented as being the original software.
00021  * 3. This notice may not be removed or altered from any source
00022  *    distribution.
00023  *
00024  * This file is part of Stones of Nvwa:
00025  *      http://sourceforge.net/projects/nvwa
00026  *
00027  */
00028 
00039 #ifndef _SET_ASSIGN_H
00040 #define _SET_ASSIGN_H
00041 
00042 #include <algorithm>
00043 
00044 template <class _Container, class _InputIter>
00045 _Container& set_assign_union(_Container& __dest,
00046                              _InputIter __first,
00047                              _InputIter __last)
00048 {
00049     typename _Container::iterator __first_dest = __dest.begin();
00050     typename _Container::iterator  __last_dest = __dest.end();
00051     while (__first_dest != __last_dest && __first != __last)
00052     {
00053         if (*__first_dest < *__first)
00054             ++__first_dest;
00055         else if (*__first < *__first_dest)
00056         {
00057             __dest.insert(__first_dest, *__first);
00058             ++__first;
00059         }
00060         else    // *__first_dest == *__first
00061         {
00062             ++__first_dest;
00063             ++__first;
00064         }
00065     }
00066     if (__first != __last)
00067         std::copy(__first, __last, inserter(__dest, __last_dest));
00068     return __dest;
00069 }
00070 
00071 template <class _Container, class _InputIter, class _Compare>
00072 _Container& set_assign_union(_Container& __dest,
00073                              _InputIter __first,
00074                              _InputIter __last,
00075                              _Compare __comp)
00076 {
00077     typename _Container::iterator __first_dest = __dest.begin();
00078     typename _Container::iterator  __last_dest = __dest.end();
00079     while (__first_dest != __last_dest && __first != __last)
00080     {
00081         if (__comp(*__first_dest, *__first))
00082             ++__first_dest;
00083         else if (__comp(*__first, *__first_dest))
00084         {
00085             __dest.insert(__first_dest, *__first);
00086             ++__first;
00087         }
00088         else    // *__first_dest is equivalent to *__first
00089         {
00090             ++__first_dest;
00091             ++__first;
00092         }
00093     }
00094     if (__first != __last)
00095         std::copy(__first, __last, inserter(__dest, __last_dest));
00096     return __dest;
00097 }
00098 
00099 template <class _Container, class _InputIter>
00100 _Container& set_assign_difference(_Container& __dest,
00101                                   _InputIter __first,
00102                                   _InputIter __last)
00103 {
00104     typename _Container::iterator __first_dest = __dest.begin();
00105     typename _Container::iterator  __last_dest = __dest.end();
00106     while (__first_dest != __last_dest && __first != __last)
00107     {
00108         if (*__first_dest < *__first)
00109             ++__first_dest;
00110         else if (*__first < *__first_dest)
00111             ++__first;
00112         else    // *__first_dest == *__first
00113         {
00114             __dest.erase(__first_dest++);
00115             ++__first;
00116         }
00117     }
00118     return __dest;
00119 }
00120 
00121 template <class _Container, class _InputIter, class _Compare>
00122 _Container& set_assign_difference(_Container& __dest,
00123                                   _InputIter __first,
00124                                   _InputIter __last,
00125                                   _Compare __comp)
00126 {
00127     typename _Container::iterator __first_dest = __dest.begin();
00128     typename _Container::iterator  __last_dest = __dest.end();
00129     while (__first_dest != __last_dest && __first != __last)
00130     {
00131         if (__comp(*__first_dest, *__first))
00132             ++__first_dest;
00133         else if (__comp(*__first, *__first_dest))
00134             ++__first;
00135         else    // *__first_dest is equivalent to *__first
00136         {
00137             __dest.erase(__first_dest++);
00138             ++__first;
00139         }
00140     }
00141     return __dest;
00142 }
00143 
00144 #endif // _SET_ASSIGN_H

Generated on Mon Dec 31 15:07:24 2007 for Nvwa by  doxygen 1.5.1