DeSiGNAR  0.5a
Data Structures General Library
relation.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 DSGRELATION_H
26 # define DSGRELATION_H
27 
28 # include <map.H>
29 
30 namespace Designar
31 {
32 
34  {
37  nat_t num_blocks;
38 
39  nat_t find(nat_t) const;
40 
41  public:
43 
44  void join(nat_t, nat_t);
45 
46  bool are_connected(nat_t, nat_t) const;
47 
48  nat_t get_num_blocks() const;
49 
50  nat_t size() const;
51  };
52 
53  template <typename T, class Equal = std::equal_to<T>>
55  {
57 
58  mutable Table table;
59 
60  nat_t search_or_insert(const T & item) const
61  {
62  nat_t * result = table.search(item);
63 
64  if (result == nullptr)
65  {
66  if (table.size() == EquivalenceRelation::size())
67  throw std::overflow_error("Cannot add a new item");
68 
69  result = table.insert(item, table.size());
70  }
71 
72  return *result;
73  }
74 
75  void init_from_list(const std::initializer_list<T> &);
76 
77  public:
78  TRelation(nat_t n, Equal & eq)
79  : EquivalenceRelation(n), table(n *1.25, eq)
80  {
81  // empty
82  }
83 
84  TRelation(nat_t n, Equal && eq = Equal())
85  : EquivalenceRelation(n), table(n *1.25, std::forward<Equal>(eq))
86  {
87  // empty
88  }
89 
90  TRelation(const std::initializer_list<T> & list, Equal & eq)
91  : EquivalenceRelation(list.size()), table(list.size() * 1.25, eq)
92  {
93  init_from_list(list);
94  }
95 
96  TRelation(const std::initializer_list<T> & list, Equal && eq = Equal())
97  : EquivalenceRelation(list.size()),
98  table(list.size() * 1.25, std::forward<Equal>(eq))
99  {
100  init_from_list(list);
101  }
102 
103  void join(const T & tp, const T & tq)
104  {
105  nat_t p = search_or_insert(tp);
106  nat_t q = search_or_insert(tq);
108  }
109 
110 
111  bool are_connected(const T & tp, const T & tq) const
112  {
113  nat_t p, q;
114 
115  try
116  {
117  p = search_or_insert(tp);
118  q = search_or_insert(tq);
119  }
120  catch (...)
121  {
122  throw std::domain_error("Both elements must belong to relation");
123  }
124 
126  }
127 
128 
129  };
130 
131  template <typename T, class Equal>
132  void TRelation<T, Equal>::init_from_list(const std::initializer_list<T> & l)
133  {
134  nat_t i = 0;
135 
136  for (const T & item : l)
137  table.insert(item, i++);
138  }
139 
140 } // end namespace Designar
141 
142 # endif // DSGRELATION_H
nat_t size() const
Definition: hash.H:282
TRelation(nat_t n, Equal &&eq=Equal())
Definition: relation.H:84
TRelation(const std::initializer_list< T > &list, Equal &&eq=Equal())
Definition: relation.H:96
bool are_connected(const T &tp, const T &tq) const
Definition: relation.H:111
Definition: relation.H:33
Value * insert(const Key &k, const Value &v)
Definition: map.H:186
TRelation(nat_t n, Equal &eq)
Definition: relation.H:78
Definition: array.H:32
unsigned long int nat_t
Definition: types.H:50
Definition: relation.H:54
Value * search(const Key &k)
Definition: map.H:254
bool are_connected(nat_t, nat_t) const
void join(const T &tp, const T &tq)
Definition: relation.H:103
TRelation(const std::initializer_list< T > &list, Equal &eq)
Definition: relation.H:90