aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
argMaxSet.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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) { return SmallObjectAllocator::instance().allocate(s); }
91  void operator delete(void* p) {
92  SmallObjectAllocator::instance().deallocate(
93  p,
94  sizeof(ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >));
95  }
96 
97  /// @}
98 
99  // ###########################################################################
100  /// @name Iterators
101  // ###########################################################################
102  /// @{
103 
104  // ============================================================================
105  /// Iterator beginning
106  // ============================================================================
107  SequenceIteratorSafe< GUM_SCALAR_SEQ > beginSafe() const { return _argMaxSeq_->beginSafe(); }
108 
109  // ============================================================================
110  /// Iterator end
111  // ============================================================================
112  SequenceIteratorSafe< GUM_SCALAR_SEQ > endSafe() const { return _argMaxSeq_->endSafe(); }
113 
114  /// @}
115 
116  // ###########################################################################
117  /// @name Operators
118  // ###########################################################################
119  /// @{
120 
121  // ============================================================================
122  /// Ajout d'un élément
123  // ============================================================================
124  ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >& operator+=(const GUM_SCALAR_SEQ& elem);
125 
126  // ============================================================================
127  /// Use to insert the content of another set inside this one
128  // ============================================================================
129  ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >&
130  operator+=(const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >& src);
131 
132  // ============================================================================
133  /// Gives the ith element
134  // ============================================================================
135  const GUM_SCALAR_SEQ& operator[](const Idx i) const { return _argMaxSeq_->atPos(i); }
136 
137  // ============================================================================
138  /// Compares two ArgMaxSet to check if they are equals
139  // ============================================================================
140  bool operator==(const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >& compared) const;
141  bool operator!=(const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >& compared) const {
142  return !(*this == compared);
143  }
144 
145  // ============================================================================
146  /// Checks if val is lower or higher from the compared ArgMaxSet val
147  // ============================================================================
148  bool operator<(const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >& compared) const {
149  return _val_ < compared.value() ? true : false;
150  }
151  bool operator>(const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >& compared) const {
152  return compared < *this;
153  }
154  bool operator<=(const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >& compared) const {
155  return !(*this > compared);
156  }
157  bool operator>=(const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ >& compared) const {
158  return !(*this < compared);
159  }
160 
161  /// @}
162 
163  // ============================================================================
164  /// Gives the size
165  // ============================================================================
166  Idx size() const { return _argMaxSeq_->size(); }
167 
168  // ============================================================================
169  /// Returns the value on which comparison are made
170  // ============================================================================
171  const GUM_SCALAR_VAL& value() const { return _val_; }
172 
173  bool exists(const GUM_SCALAR_SEQ& elem) const { return _argMaxSeq_->exists(elem); }
174 
175  private:
176  /// The very bone of the ArgMaxSet
178  GUM_SCALAR_VAL _val_;
179 
180  public:
182  streamy << "Value : " << objy.value() << " - Set : " << objy._argMaxSeq_->toString();
183  return streamy;
184  }
185  };
186 
187 } // End of namespace gum
188 
189 #include <agrum/tools/core/argMaxSet_tpl.h>
190 
191 #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:112
~ArgMaxSet()
Destructor.
Definition: argMaxSet_tpl.h:84
ArgMaxSet(const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ > &src)
Copy Constructor.
Definition: argMaxSet_tpl.h:62
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:643
void * operator new(size_t s)
Allocators and Deallocators redefinition.
Definition: argMaxSet.h:90
Sequence< GUM_SCALAR_SEQ > * _argMaxSeq_
The very bone of the ArgMaxSet.
Definition: argMaxSet.h:177
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:148
bool operator<=(const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ > &compared) const
Ajout d&#39;un élément.
Definition: argMaxSet.h:154
bool operator!=(const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ > &compared) const
Ajout d&#39;un élément.
Definition: argMaxSet.h:141
bool operator>(const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ > &compared) const
Ajout d&#39;un élément.
Definition: argMaxSet.h:151
friend std::ostream & operator<<(std::ostream &streamy, const ArgMaxSet &objy)
Definition: argMaxSet.h:181
SequenceIteratorSafe< GUM_SCALAR_SEQ > beginSafe() const
Iterator beginning.
Definition: argMaxSet.h:107
GUM_SCALAR_VAL _val_
Definition: argMaxSet.h:178
const GUM_SCALAR_VAL & value() const
Returns the value on which comparison are made.
Definition: argMaxSet.h:171
bool exists(const GUM_SCALAR_SEQ &elem) const
Definition: argMaxSet.h:173
ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ > & operator+=(const GUM_SCALAR_SEQ &elem)
Ajout d&#39;un élément.
Definition: argMaxSet_tpl.h:98
void operator delete(void *p)
Constructor.
Definition: argMaxSet.h:91
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:72
Idx size() const
Gives the size.
Definition: argMaxSet.h:166
bool operator>=(const ArgMaxSet< GUM_SCALAR_VAL, GUM_SCALAR_SEQ > &compared) const
Ajout d&#39;un élément.
Definition: argMaxSet.h:157
ArgMaxSet()
Constructor.
Definition: argMaxSet_tpl.h:41
const GUM_SCALAR_SEQ & operator[](const Idx i) const
Gives the ith element.
Definition: argMaxSet.h:135