aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
argMaxSet.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief This files contains several function objects that are not (yet) defined
25  * in the STL
26  *
27  * Generically, function objects are instances of a class with member function
28  * operator() defined.
29  * This member function allows the object to be used with the same syntax as a
30  * function call.
31  *
32  * @author Jean-Christophe MAGNAN
33  */
34 
35 // =========================================================================
36 #ifndef GUM_ARG_MAX_SET_H
37 #define GUM_ARG_MAX_SET_H
38 // =========================================================================
39 #include <cstdlib>
40 // =========================================================================
41 #include <agrum/tools/core/sequence.h>
42 #include <agrum/tools/core/smallobjectallocator/smallObjectAllocator.h>
43 // =========================================================================
44 
45 namespace gum {
46 
47  /**
48  * @struct ArgMaxSet argMaxSet.h <agrum/tools/multidim/patterns/argMaxSet.h>
49  * @brief A class containing the set of object assoicated to a maximum
50  * @ingroup core
51  *
52  * This set contains the ids of the obj
53  *
54  */
55  /// Class to handle efficiently argMaxSet
56  template < typename GUM_SCALAR_VAL, typename GUM_SCALAR_SEQ >
57  class ArgMaxSet {
58  public:
59  // ###########################################################################
60  /// @name CNL
61  // ###########################################################################
62  /// @{
63 
64  // ============================================================================
65  /// Constructor
66  // ============================================================================
67  ArgMaxSet();
68 
69  // ============================================================================
70  /// Constructor
71  // ============================================================================
72  ArgMaxSet(const GUM_SCALAR_VAL& val, const GUM_SCALAR_SEQ& elem);
73 
74  // ============================================================================
75  /// Copy Constructor
76  // ============================================================================
77  ArgMaxSet(const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >& src);
78 
79  ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >&
80  operator=(const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >& src);
81 
82  // ============================================================================
83  /// Destructor
84  // ============================================================================
85  ~ArgMaxSet();
86 
87  // ============================================================================
88  /// Allocators and Deallocators redefinition
89  // ============================================================================
90  void* operator new(size_t s) {
91  return SmallObjectAllocator::instance().allocate(s);
92  }
93  void operator delete(void* p) {
94  SmallObjectAllocator::instance().deallocate(
95  p,
96  sizeof(ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >));
97  }
98 
99  /// @}
100 
101  // ###########################################################################
102  /// @name Iterators
103  // ###########################################################################
104  /// @{
105 
106  // ============================================================================
107  /// Iterator beginning
108  // ============================================================================
110  return argMaxSeq__->beginSafe();
111  }
112 
113  // ============================================================================
114  /// Iterator end
115  // ============================================================================
117  return argMaxSeq__->endSafe();
118  }
119 
120  /// @}
121 
122  // ###########################################################################
123  /// @name Operators
124  // ###########################################################################
125  /// @{
126 
127  // ============================================================================
128  /// Ajout d'un élément
129  // ============================================================================
130  ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >&
131  operator+=(const GUM_SCALAR_SEQ& elem);
132 
133  // ============================================================================
134  /// Use to insert the content of another set inside this one
135  // ============================================================================
136  ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >&
137  operator+=(const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >& src);
138 
139  // ============================================================================
140  /// Gives the ith element
141  // ============================================================================
142  const GUM_SCALAR_SEQ& operator[](const Idx i) const {
143  return argMaxSeq__->atPos(i);
144  }
145 
146  // ============================================================================
147  /// Compares two ArgMaxSet to check if they are equals
148  // ============================================================================
149  bool operator==(
150  const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >& compared) const;
151  bool operator!=(
152  const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >& compared) const {
153  return !(*this == compared);
154  }
155 
156  // ============================================================================
157  /// Checks if val is lower or higher from the compared ArgMaxSet val
158  // ============================================================================
159  bool operator<(
160  const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >& compared) const {
161  return val__ < compared.value() ? true : false;
162  }
163  bool operator>(
164  const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >& compared) const {
165  return compared < *this;
166  }
167  bool operator<=(
168  const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >& compared) const {
169  return !(*this > compared);
170  }
171  bool operator>=(
172  const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >& compared) const {
173  return !(*this < compared);
174  }
175 
176  /// @}
177 
178  // ============================================================================
179  /// Gives the size
180  // ============================================================================
181  Idx size() const { return argMaxSeq__->size(); }
182 
183  // ============================================================================
184  /// Returns the value on which comparison are made
185  // ============================================================================
186  const GUM_SCALAR_VAL& value() const { return val__; }
187 
188  bool exists(const GUM_SCALAR_SEQ& elem) const {
189  return argMaxSeq__->exists(elem);
190  }
191 
192  private:
193  /// The very bone of the ArgMaxSet
195  GUM_SCALAR_VAL val__;
196 
197  public:
199  streamy << "Value : " << objy.value()
200  << " - Set : " << objy.argMaxSeq__->toString();
201  return streamy;
202  }
203  };
204 
205 } // End of namespace gum
206 
207 #include <agrum/tools/core/argMaxSet_tpl.h>
208 
209 #endif /* GUM_ARG_MAX_SET_H */
ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ > & operator+=(const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ > &src)
Use to insert the content of another set inside this one.
SequenceIteratorSafe< GUM_SCALAR_SEQ > endSafe() const
Iterator end.
Definition: argMaxSet.h:116
~ArgMaxSet()
Destructor.
Definition: argMaxSet_tpl.h:85
ArgMaxSet(const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ > &src)
Copy Constructor.
Definition: argMaxSet_tpl.h:63
bool operator==(const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ > &compared) const
Compares two ArgMaxSet to check if they are equals.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
void * operator new(size_t s)
Allocators and Deallocators redefinition.
Definition: argMaxSet.h:90
GUM_SCALAR_VAL val__
Definition: argMaxSet.h:195
Class to handle efficiently argMaxSet.
Definition: argMaxSet.h:57
bool operator<(const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ > &compared) const
Checks if val is lower or higher from the compared ArgMaxSet val.
Definition: argMaxSet.h:159
bool operator<=(const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ > &compared) const
Ajout d&#39;un élément.
Definition: argMaxSet.h:167
bool operator!=(const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ > &compared) const
Ajout d&#39;un élément.
Definition: argMaxSet.h:151
bool operator>(const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ > &compared) const
Ajout d&#39;un élément.
Definition: argMaxSet.h:163
friend std::ostream & operator<<(std::ostream &streamy, const ArgMaxSet &objy)
Definition: argMaxSet.h:198
SequenceIteratorSafe< GUM_SCALAR_SEQ > beginSafe() const
Iterator beginning.
Definition: argMaxSet.h:109
const GUM_SCALAR_VAL & value() const
Returns the value on which comparison are made.
Definition: argMaxSet.h:186
bool exists(const GUM_SCALAR_SEQ &elem) const
Definition: argMaxSet.h:188
ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ > & operator+=(const GUM_SCALAR_SEQ &elem)
Ajout d&#39;un élément.
Definition: argMaxSet_tpl.h:99
void operator delete(void *p)
Constructor.
Definition: argMaxSet.h:93
ArgMaxSet(const GUM_SCALAR_VAL &val, const GUM_SCALAR_SEQ &elem)
Constructor.
Definition: argMaxSet_tpl.h:50
ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ > & operator=(const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ > &src)
Constructor.
Definition: argMaxSet_tpl.h:73
Idx size() const
Gives the size.
Definition: argMaxSet.h:181
bool operator>=(const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ > &compared) const
Ajout d&#39;un élément.
Definition: argMaxSet.h:171
ArgMaxSet()
Constructor.
Definition: argMaxSet_tpl.h:41
const GUM_SCALAR_SEQ & operator[](const Idx i) const
Gives the ith element.
Definition: argMaxSet.h:142
Sequence< GUM_SCALAR_SEQ > * argMaxSeq__
The very bone of the ArgMaxSet.
Definition: argMaxSet.h:194