DeSiGNAR  0.5a
Data Structures General Library
setalgorithms.H
Go to the documentation of this file.
1 /*
2  This file is part of Designar.
3  Copyright (C) 2017 by Alejandro J. Mujica
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 3 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, see <http://www.gnu.org/licenses/>.
17 
18  Any user request of this software, write to
19 
20  Alejandro Mujica
21 
22  aledrums@gmail.com
23 */
24 
25 # ifndef DSGSETALGORITHMS_H
26 # define DSGSETALGORITHMS_H
27 
28 # include <types.H>
29 # include <typetraits.H>
30 
31 namespace Designar
32 {
33  template <class T> class SLList;
34 
35  template <class SetType, typename Key>
37  {
38 
39  SetType & me()
40  {
41  return static_cast<SetType *>(this);
42  }
43 
44  const SetType & const_me() const
45  {
46  return *static_cast<const SetType *>(this);
47  }
48 
49  public:
50  bool contains(const Key & k) const
51  {
52  return const_me().search(k) != nullptr;
53  }
54 
55  bool has(const Key & k) const
56  {
57  return contains(k);
58  }
59 
60  static SetType join(const SetType & s1, const SetType & s2)
61  {
62  SetType ret_val = s1;
63 
64  s2.for_each([&ret_val] (const Key & item)
65  {
66  ret_val.append(item);
67  });
68 
69  return ret_val;
70  }
71 
72  SetType join(const SetType & s) const
73  {
74  return join(const_me(), s);
75  }
76 
77  static SetType intersect(const SetType & s1, const SetType & s2)
78  {
79  if (s1.size() < s2.size())
80  return s1.template filter<SetType>([&s2] (const Key & item)
81  {
82  return s2.contains(item);
83  });
84 
85  return s2.template filter<SetType>([&s1] (const Key & item)
86  {
87  return s1.contains(item);
88  });
89  }
90 
91  SetType intersect(const SetType & s) const
92  {
93  return intersect(const_me(), s);
94  }
95 
96  static SetType difference(const SetType & s1, const SetType & s2)
97  {
98  return s1.template filter<SetType>([&s2] (const Key & item)
99  {
100  return not s2.contains(item);
101  });
102  }
103 
104  SetType difference(const SetType & s) const
105  {
106  return difference(const_me(), s);
107  }
108 
109  template <class SetType2 = SetType>
111  cartesian_product(const SetType & s1, const SetType2 & s2)
112  {
113  using Key2 = typename SetType2::KeyType;
114 
116 
117  s1.for_each([&ret_val, &s2] (const Key & item1)
118  {
119  s2.for_each([&ret_val, &item1] (const Key2 & item2)
120  {
121  ret_val.append(std::make_pair(item1, item2));
122  });
123  });
124 
125  return ret_val;
126  }
127 
128  template <class SetType2 = SetType>
130  cartesian_product(const SetType2 & s) const
131  {
132  return cartesian_product<SetType2>(const_me(), s);
133  }
134  };
135 
136 } // end namespace Designar
137 
138 # endif // DSGSETALGORITHMS_H
T & append(const T &item)
Definition: list.H:265
bool contains(const Key &k) const
Definition: setalgorithms.H:50
Definition: setalgorithms.H:36
static SLList< std::pair< Key, typename SetType2::KeyType > > cartesian_product(const SetType &s1, const SetType2 &s2)
Definition: setalgorithms.H:111
static SetType intersect(const SetType &s1, const SetType &s2)
Definition: setalgorithms.H:77
void for_each(Op &op) const
Definition: containeralgorithms.H:62
static SetType join(const SetType &s1, const SetType &s2)
Definition: setalgorithms.H:60
SetType difference(const SetType &s) const
Definition: setalgorithms.H:104
SetType join(const SetType &s) const
Definition: setalgorithms.H:72
SLList< std::pair< Key, typename SetType2::KeyType > > cartesian_product(const SetType2 &s) const
Definition: setalgorithms.H:130
Definition: italgorithms.H:33
bool has(const Key &k) const
Definition: setalgorithms.H:55
Definition: array.H:32
SetType intersect(const SetType &s) const
Definition: setalgorithms.H:91
static SetType difference(const SetType &s1, const SetType &s2)
Definition: setalgorithms.H:96