1 /**
2  *
3  *   Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(_at_LIP6) & Christophe GONZALES(_at_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 Template methods of MultiDimFunctionGraph.
25  *
26  * @author Pierre-Henri WUILLEMIN(_at_LIP6) and Jean-Christophe MAGNAN and Christophe
27  * GONZALES(_at_AMU)
28  * @author Christophe GONZALES(_at_AMU) and Pierre-Henri WUILLEMIN(_at_LIP6)
29  *
30  */
31 
32 #include <agrum/tools/multidim/utils/FunctionGraphUtilities/link.h>
33 
34 namespace gum {
35 
36   // Constructor
37   template < typename T >
Link(const T & elem)38   INLINE Link< T >::Link(const T& elem) : _element_(elem) {
39     GUM_CONSTRUCTOR(Link);
40   }
41 
42   // Constructor
43   template < typename T >
Link(const T & elem,Link<T> * nextLink)44   INLINE Link< T >::Link(const T& elem, Link< T >* nextLink) :
45       _element_(elem), _nextLink_(nextLink) {
46     GUM_CONSTRUCTOR(Link);
47   }
48 
49   // Destructor
50   template < typename T >
~Link()51   INLINE Link< T >::~Link() {
52     GUM_DESTRUCTOR(Link);
53   }
54 
55   template < typename T >
new(size_t s)56   INLINE void* Link< T >::operator new(size_t s) {
57     return SmallObjectAllocator::instance().allocate(Size(s));
58   }
59 
60   template < typename T >
delete(void * p)61   INLINE void Link< T >::operator delete(void* p) {
62     SmallObjectAllocator::instance().deallocate(p, sizeof(Link< T >));
63   }
64 
65   template < typename T >
element()66   INLINE const T& Link< T >::element() const {
67     return _element_;
68   }
69 
70   template < typename T >
element()71   INLINE T& Link< T >::element() {
72     return _element_;
73   }
74 
75   template < typename T >
nextLink()76   INLINE const Link< T >* Link< T >::nextLink() const {
77     return _nextLink_;
78   }
79 
80   template < typename T >
nextLink()81   INLINE Link< T >* Link< T >::nextLink() {
82     return _nextLink_;
83   }
84 
85   template < typename T >
setNextLink(Link<T> * newLink)86   INLINE void Link< T >::setNextLink(Link< T >* newLink) {
87     _nextLink_ = newLink;
88   }
89 
90   // Constructor
91   template < typename T >
LinkedList()92   INLINE LinkedList< T >::LinkedList() {
93     GUM_CONSTRUCTOR(LinkedList);
94     _firstLink_ = nullptr;
95   }
96 
97   // Destructor
98   template < typename T >
~LinkedList()99   INLINE LinkedList< T >::~LinkedList() {
100     clear();
101     GUM_DESTRUCTOR(LinkedList);
102   }
103 
104   template < typename T >
new(size_t s)105   INLINE void* LinkedList< T >::operator new(size_t s) {
106     return SmallObjectAllocator::instance().allocate(Size(s));
107   }
108 
109   template < typename T >
delete(void * p)110   INLINE void LinkedList< T >::operator delete(void* p) {
111     SmallObjectAllocator::instance().deallocate(p, sizeof(LinkedList< T >));
112   }
113 
114   template < typename T >
list()115   INLINE const Link< T >* LinkedList< T >::list() const {
116     return _firstLink_;
117   }
118 
119   template < typename T >
list()120   INLINE Link< T >* LinkedList< T >::list() {
121     return _firstLink_;
122   }
123 
124   template < typename T >
clear()125   void LinkedList< T >::clear() {
126     Link< T >* curLink = _firstLink_;
127     Link< T >* nl      = nullptr;
128     while (curLink) {
129       nl = curLink->nextLink();
130       delete curLink;
131       curLink = nl;
132     }
133   }
134 
135   template < typename T >
addLink(const T & elem)136   INLINE void LinkedList< T >::addLink(const T& elem) {
137     Link< T >* newLink = new Link< T >(elem, _firstLink_);
138     _firstLink_        = newLink;
139   }
140 
141   template < typename T >
searchAndRemoveLink(const T & elem)142   INLINE void LinkedList< T >::searchAndRemoveLink(const T& elem) {
143     Link< T >* curLink  = _firstLink_;
144     Link< T >* prevLink = nullptr;
145     while (curLink && curLink->element() != elem) {
146       prevLink = curLink;
147       curLink  = curLink->nextLink();
148     }
149     if (curLink) {
150       if (prevLink)
151         prevLink->setNextLink(curLink->nextLink());
152       else
153         _firstLink_ = curLink->nextLink();
154       delete curLink;
155     }
156   }
157 
158 }   // namespace gum
159