Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Manager.h
1 /*
2  * Data Manager classes
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2016, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_DATA_MANAGER_H_
22 #define ELM_DATA_MANAGER_H_
23 
24 #include <elm/alloc/DefaultAllocator.h>
25 #include <elm/compare.h>
26 #include <elm/hash.h>
27 #include <elm/equiv.h>
28 #include <elm/types.h>
29 
30 namespace elm {
31 
32 // EquivManager class
33 template <class T, class E = Equiv<T>, class A = DefaultAllocator>
34 class EquivManager {
35 public:
36  typedef E equiv_t;
37  typedef A alloc_t;
38 
39  inline EquivManager(E& e = single<E>(), A& a = A::DEFAULT): eq(e), alloc(a) { }
40 
41  inline bool equals(const T& v1, const T& v2) const { return eq.isEqual(v1, v2); }
42  inline void *allocate(t::size size) const { return alloc.allocate(size); }
43  inline void free(t::ptr p) const { alloc.free(p); }
44 
45  inline static EquivManager<T, E, A>& def() { return single<EquivManager<T, E, A> >(); }
46 
47  E& eq;
48  A& alloc;
49 };
50 
51 // CompareManager class
52 template <class T, class C = Comparator<T>, class E = Equiv<T>, class A = DefaultAllocator>
54 public:
55  typedef T t;
56  typedef E equiv_t;
57  typedef A alloc_t;
58 
59  inline CompareManager(const C& c = single<C>(), const E& e = single<E>(), A& a = DefaultAllocator::DEFAULT)
60  : cmp(c), eq(e), alloc(a) { }
61 
62  inline int compare(const T& v1, const T& v2) const { return cmp.doCompare(v1, v2); }
63  inline bool equals(const T& v1, const T& v2) const { return eq.isEqual(v1, v2); }
64  inline void *allocate(elm::t::size size) const { return alloc.allocate(size); }
65  inline void free(elm::t::ptr p) const { alloc.free(p); }
66 
67  inline static CompareManager<T, C, E, A>& def() { return single<CompareManager<T, C, E, A> >(); }
68 
69  const C& cmp;
70  const E& eq;
71  A& alloc;
72 };
73 
74 
75 // HashManager class
76 template <class K, class H = HashKey<K>, class A = DefaultAllocator>
77 class HashManager {
78 public:
79  typedef H hash_t;
80  typedef A alloc_t;
81 
82  inline HashManager(void): alloc(DefaultAllocator::DEFAULT) { }
83  inline HashManager(const H& h, A& a = DefaultAllocator::DEFAULT): hash(h), alloc(a) { }
84 
85  inline bool isEqual(const K& k1, const K& k2) const { return hash.isEqual(k1, k2); }
86  inline t::hash computeHash(const K& k) const { return hash.computeHash(k); }
87  template <class T> inline void *allocate() { return alloc.allocate(sizeof(T)); }
88  inline void free(void *p) { alloc.free(p); }
89 
90  inline static HashManager<K, H, A>& def() { return single<HashManager<K, H, A> >(); }
91 
92  H hash;
93  A& alloc;
94 };
95 
96 
97 // CompareManager class
98 #if 0
99 template <class T, class A = IdAdapter<T>, class C = Comparator<T>, class TC = Comparator<T>, class A = DefaultAllocator>
100 class KeyCompareManager {
101 public:
102  /*inline CompareMapManager(void): alloc(DefaultAllocator::DEFAULT) { }
103  inline CompareMapManager(const KC& kc, const TC& tc, A& a = DefaultAllocator::DEFAULT): kcmp(kc), tcmp(tc), alloc(a) { }
104  KC kcmp;
105  TC tcmp;
106  A& alloc;*/
107 };
108 #endif
109 
110 } // elm
111 
112 #endif /* ELM_DATA_MANAGER_H_ */
elm::EquivManager::def
static EquivManager< T, E, A > & def()
Definition: Manager.h:45
elm::io::p
Printable< T, M > p(const T &data, const M &man)
Definition: Output.h:302
elm::CompareManager::allocate
void * allocate(elm::t::size size) const
Definition: Manager.h:64
elm::HashManager::HashManager
HashManager(void)
Definition: Manager.h:82
elm::HashManager::hash
H hash
Definition: Manager.h:92
elm::HashManager::free
void free(void *p)
Definition: Manager.h:88
elm::HashManager::allocate
void * allocate()
Definition: Manager.h:87
elm::EquivManager::equals
bool equals(const T &v1, const T &v2) const
Definition: Manager.h:41
elm::EquivManager::equiv_t
E equiv_t
Definition: Manager.h:36
elm::CompareManager::CompareManager
CompareManager(const C &c=single< C >(), const E &e=single< E >(), A &a=DefaultAllocator::DEFAULT)
Definition: Manager.h:59
elm::EquivManager::eq
E & eq
Definition: Manager.h:47
void
elm::CompareManager::compare
int compare(const T &v1, const T &v2) const
Definition: Manager.h:62
elm::HashManager::HashManager
HashManager(const H &h, A &a=DefaultAllocator::DEFAULT)
Definition: Manager.h:83
elm::CompareManager::equals
bool equals(const T &v1, const T &v2) const
Definition: Manager.h:63
elm::EquivManager::free
void free(t::ptr p) const
Definition: Manager.h:43
elm::HashManager::def
static HashManager< K, H, A > & def()
Definition: Manager.h:90
elm
Definition: adapter.h:26
elm::HashManager
Definition: Manager.h:77
elm::HashManager::computeHash
t::hash computeHash(const K &k) const
Definition: Manager.h:86
elm::HashManager::alloc_t
A alloc_t
Definition: Manager.h:80
elm::t::size
uint64 size
Definition: arch.h:35
elm::DefaultAllocator
Definition: DefaultAllocator.h:39
elm::DefaultAllocator::DEFAULT
static DefaultAllocator DEFAULT
Definition: DefaultAllocator.h:41
elm::CompareManager::free
void free(elm::t::ptr p) const
Definition: Manager.h:65
elm::EquivManager::alloc
A & alloc
Definition: Manager.h:48
elm::CompareManager::alloc
A & alloc
Definition: Manager.h:71
elm::HashManager::isEqual
bool isEqual(const K &k1, const K &k2) const
Definition: Manager.h:85
elm::CompareManager::def
static CompareManager< T, C, E, A > & def()
Definition: Manager.h:67
elm::EquivManager::alloc_t
A alloc_t
Definition: Manager.h:37
elm::CompareManager::cmp
const C & cmp
Definition: Manager.h:69
elm::HashManager::hash_t
H hash_t
Definition: Manager.h:79
elm::CompareManager::t
T t
Definition: Manager.h:55
elm::CompareManager
Definition: Manager.h:53
elm::t::hash
t::intptr hash
Definition: hash.h:34
elm::CompareManager::eq
const E & eq
Definition: Manager.h:70
elm::HashManager::alloc
A & alloc
Definition: Manager.h:93
elm::CompareManager::alloc_t
A alloc_t
Definition: Manager.h:57
elm::EquivManager::EquivManager
EquivManager(E &e=single< E >(), A &a=A::DEFAULT)
Definition: Manager.h:39
elm::EquivManager
Definition: Manager.h:34
elm::CompareManager::equiv_t
E equiv_t
Definition: Manager.h:56
elm::EquivManager::allocate
void * allocate(t::size size) const
Definition: Manager.h:42