2 /// Compare lists and get differences
4 // Copyright (C) 2013 Thomas Geymayer <tomgey@gmail.com>
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Library General Public
8 // License as published by the Free Software Foundation; either
9 // version 2 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Library General Public License for more details.
16 // You should have received a copy of the GNU Library General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #ifndef SG_LISTDIFF_HXX_
21 #define SG_LISTDIFF_HXX_
24 #include <boost/function.hpp>
32 typedef std::vector<T> List;
33 typedef boost::function<void (T)> Callback;
36 * Perform list diff in-place (modifies both lists) and call cb_add for
37 * every element in new_list not in old_list and cb_remove for every element
38 * in old_list not in new_list.
40 static void inplace( List& old_list,
45 // Check which elements have been removed. (Removing first and adding
46 // second should keep the memory usage lower - not for this function, but
47 // probably for users of this function which use the callbacks to delete
48 // and create objects)
49 while( !old_list.empty() )
51 T& old_el = old_list.front();
52 typename List::iterator new_el =
53 std::find(new_list.begin(), new_list.end(), old_el);
55 if( new_el == new_list.end() )
62 // Element is in both lists -> just ignore
63 *new_el = new_list.back();
67 old_list.front() = old_list.back();
71 // All remaing elements in new_list have not been in old_list, so call
72 // the add callback for every element if required.
75 for( typename List::iterator it = new_list.begin();
83 } // namespace simgear
85 #endif /* SG_LISTDIFF_HXX_ */