summaryrefslogtreecommitdiff
path: root/src/mt/mset.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/mt/mset.cc')
-rw-r--r--src/mt/mset.cc81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/mt/mset.cc b/src/mt/mset.cc
new file mode 100644
index 0000000..ec5b69e
--- /dev/null
+++ b/src/mt/mset.cc
@@ -0,0 +1,81 @@
1/*************************************************************************
2 *
3 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <mt/mset.h>
21#include <algorithm>
22using namespace std;
23
24mset::mset() : rpile() {}
25mset::mset(const sref& in) : rpile() {
26 sref first,rest=in;
27 while(Split(first,rest)) insert(first);
28}
29
30/////////////////////////////////////////////////////////////////////////////
31
32int mset::exist(const sref& t) const {
33 pair<const_iterator,const_iterator> p=equal_range(begin(),end(),t);
34 return p.first!=p.second;
35}
36int mset::insert(const sref& t) {
37 pair<iterator,iterator> p=equal_range(begin(),end(),t);
38 if(p.first==p.second) { rpile::insert(p.second,t); return 1; }
39 else return 0;
40}
41
42/////////////////////////////////////////////////////////////////////////////
43
44void mset::Union(const mset& a,const mset& b) {
45 clear();
46 for(int i=0;i<a.size();i++) insert(a[i]);
47 for(int i=0;i<b.size();i++) insert(b[i]);
48}
49void mset::Intersection(const mset& a,const mset& b) {
50 clear();
51 for(int i=0;i<a.size();i++) if(b.exist(a[i])) insert(a[i]);
52}
53void mset::Difference(const mset& a,const mset& b) {
54 clear();
55 for(int i=0;i<a.size();i++) if(!b.exist(a[i])) insert(a[i]);
56}
57
58/////////////////////////////////////////////////////////////////////////////
59
60mstream& operator<<(mstream& out,const mset& m) {
61 for(int i=0;i<m.size();i++) { if(i) out.put(' '); out<<m[i]; } return out;
62}
63
64/////////////////////////////////////////////////////////////////////////////
65
66int msset::exist(const mstring& t) const {
67 pair<const_iterator,const_iterator> p=equal_range(begin(),end(),t);
68 return p.first!=p.second;
69}
70int msset::insert(const mstring& t) {
71 pair<iterator,iterator> p=equal_range(begin(),end(),t);
72 if(p.first==p.second) { spile::insert(p.second,t); return 1; }
73 else return 0;
74}
75
76/////////////////////////////////////////////////////////////////////////////
77
78mstream& operator<<(mstream& out,const msset& m) {
79 for(int i=0;i<m.size();i++) { if(i) out.put(' '); out<<m[i]; } return out;
80}
81