1 : #ifndef TAGCOLL_UTILS_SET_H
2 : #define TAGCOLL_UTILS_SET_H
3 :
4 : /** \file
5 : * Extra useful set operations
6 : */
7 :
8 : /*
9 : * Copyright (C) 2003,2004,2005,2006 Enrico Zini <enrico@debian.org>
10 : *
11 : * This library is free software; you can redistribute it and/or
12 : * modify it under the terms of the GNU Lesser General Public
13 : * License as published by the Free Software Foundation; either
14 : * version 2.1 of the License, or (at your option) any later version.
15 : *
16 : * This library is distributed in the hope that it will be useful,
17 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 : * Lesser General Public License for more details.
20 : *
21 : * You should have received a copy of the GNU Lesser General Public
22 : * License along with this library; if not, write to the Free Software
23 : * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 : */
25 :
26 : #include <wibble/operators.h>
27 : #include <set>
28 :
29 : namespace tagcoll {
30 : namespace utils {
31 :
32 : template<typename T>
33 : int set_distance(const std::set<T>& set1, const std::set<T>& set2)
34 : {
35 : int res = 0;
36 : int intCount = 0;
37 :
38 : typename std::set<T>::const_iterator a = set1.begin();
39 : typename std::set<T>::const_iterator b = set2.begin();
40 :
41 : while (a != set1.end() || b != set2.end())
42 : if ((b == set2.end()) || (a != set1.end() && *a < *b))
43 : {
44 : res++;
45 : a++;
46 : }
47 : else if ((a == set1.end()) || (b != set2.end() && *b < *a))
48 : {
49 : res++;
50 : b++;
51 : }
52 : else
53 : {
54 : a++;
55 : b++;
56 : intCount++;
57 : }
58 :
59 : return intCount ? res : -1;
60 : }
61 :
62 : template<typename T>
63 1 : bool set_contains(const std::set<T>& set1, const std::set<T>& set2)
64 : {
65 1 : typename std::set<T>::const_iterator b = set2.begin();
66 :
67 574 : for (typename std::set<T>::const_iterator a = set1.begin(); a != set1.end(); ++a)
68 574 : if (b == set2.end())
69 1 : return true;
70 573 : else if (*a == *b)
71 31 : b++;
72 542 : else if (*b < *a)
73 0 : return false;
74 :
75 0 : return b == set2.end();
76 : }
77 :
78 : template<typename T>
79 4 : bool set_contains(const std::set<T>& set1, const T& item)
80 : {
81 4 : return set1.find(item) != set1.end();
82 : }
83 :
84 : }
85 : }
86 :
87 : // vim:set ts=4 sw=4:
88 : #endif
|