summaryrefslogtreecommitdiff
path: root/src/mt/mset.cc
blob: ec5b69ee1e5c0a93ba655a4ef257d3bdd93cf273 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*************************************************************************
 *
 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <mt/mset.h>
#include <algorithm>
using namespace std;

mset::mset() : rpile() {}
mset::mset(const sref& in) : rpile() {
  sref first,rest=in;
  while(Split(first,rest)) insert(first);
}

/////////////////////////////////////////////////////////////////////////////

int mset::exist(const sref& t) const {
  pair<const_iterator,const_iterator> p=equal_range(begin(),end(),t);
  return p.first!=p.second;
}
int mset::insert(const sref& t) {
  pair<iterator,iterator> p=equal_range(begin(),end(),t);
  if(p.first==p.second) { rpile::insert(p.second,t); return 1; }
  else return 0;
}

/////////////////////////////////////////////////////////////////////////////

void mset::Union(const mset& a,const mset& b) {
  clear();
  for(int i=0;i<a.size();i++) insert(a[i]);
  for(int i=0;i<b.size();i++) insert(b[i]);
}
void mset::Intersection(const mset& a,const mset& b) {
  clear();
  for(int i=0;i<a.size();i++) if(b.exist(a[i])) insert(a[i]);
}
void mset::Difference(const mset& a,const mset& b) {
  clear();
  for(int i=0;i<a.size();i++) if(!b.exist(a[i])) insert(a[i]);
}

/////////////////////////////////////////////////////////////////////////////

mstream& operator<<(mstream& out,const mset& m) {
  for(int i=0;i<m.size();i++) { if(i) out.put(' '); out<<m[i]; } return out;
}

/////////////////////////////////////////////////////////////////////////////

int msset::exist(const mstring& t) const {
  pair<const_iterator,const_iterator> p=equal_range(begin(),end(),t);
  return p.first!=p.second;
}
int msset::insert(const mstring& t) {
  pair<iterator,iterator> p=equal_range(begin(),end(),t);
  if(p.first==p.second) { spile::insert(p.second,t); return 1; }
  else return 0;
}

/////////////////////////////////////////////////////////////////////////////

mstream& operator<<(mstream& out,const msset& m) {
  for(int i=0;i<m.size();i++) { if(i) out.put(' '); out<<m[i]; } return out;
}