FlashGraph-ng
A new frontier in large-scale graph analysis and data mining
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
generic_type.h
1 #ifndef __GENERAL_TYPE_H__
2 #define __GENERAL_TYPE_H__
3 
4 /*
5  * Copyright 2014 Open Connectome Project (http://openconnecto.me)
6  * Written by Da Zheng (zhengda1936@gmail.com)
7  *
8  * This file is part of FlashMatrix.
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22 
23 #include <string.h>
24 #include <stdlib.h>
25 
26 #include <memory>
27 
28 #include "sorter.h"
29 #include "stl_algs.h"
30 
31 namespace fm
32 {
33 
38 {
39  P_CHAR,
40  P_SHORT,
41  P_INTEGER,
42  P_LONG,
43  P_FLOAT,
44  P_DOUBLE,
45  P_LDOUBLE,
46  P_BOOL,
47  P_USHORT,
48  P_UINT,
49  P_ULONG,
50  NUM_TYPES,
51 };
52 
53 template<class T>
54 prim_type get_type()
55 {
56  return prim_type::NUM_TYPES;
57 }
58 
59 template<>
60 inline prim_type get_type<char>()
61 {
62  return prim_type::P_CHAR;
63 }
64 
65 template<>
66 inline prim_type get_type<short>()
67 {
68  return prim_type::P_SHORT;
69 }
70 
71 template<>
72 inline prim_type get_type<int>()
73 {
74  return prim_type::P_INTEGER;
75 }
76 
77 template<>
78 inline prim_type get_type<long>()
79 {
80  return prim_type::P_LONG;
81 }
82 
83 template<>
84 inline prim_type get_type<float>()
85 {
86  return prim_type::P_FLOAT;
87 }
88 
89 template<>
90 inline prim_type get_type<double>()
91 {
92  return prim_type::P_DOUBLE;
93 }
94 
95 template<>
96 inline prim_type get_type<long double>()
97 {
98  return prim_type::P_LDOUBLE;
99 }
100 
101 template<>
102 inline prim_type get_type<bool>()
103 {
104  return prim_type::P_BOOL;
105 }
106 
107 template<>
108 inline prim_type get_type<unsigned short>()
109 {
110  return prim_type::P_USHORT;
111 }
112 
113 template<>
114 inline prim_type get_type<unsigned int>()
115 {
116  return prim_type::P_UINT;
117 }
118 
119 template<>
120 inline prim_type get_type<unsigned long>()
121 {
122  return prim_type::P_ULONG;
123 }
124 
125 class basic_uops;
126 class basic_ops;
127 class agg_ops;
128 class scatter_gather;
129 class scalar_variable;
130 class rand_gen;
131 class set_operate;
132 class generic_hashtable;
133 class bulk_uoperate;
134 
139 {
140 public:
144  virtual std::shared_ptr<generic_hashtable> create_hashtable(
145  const scalar_type &val_type) const = 0;
146  virtual const basic_uops &get_basic_uops() const = 0;
147  virtual const basic_ops &get_basic_ops() const = 0;
148  virtual const agg_ops &get_agg_ops() const = 0;
149  virtual prim_type get_type() const = 0;
150  virtual size_t get_size() const = 0;
151  virtual const sorter &get_sorter() const = 0;
152  virtual const scatter_gather &get_sg() const = 0;
153  virtual const stl_algs &get_stl_algs() const = 0;
154  virtual const set_operate &get_set_const(const scalar_variable &val) const = 0;
155  virtual std::shared_ptr<scalar_variable> create_scalar() const = 0;
156  // Create Random generator with the uniform distribution.
157  virtual std::shared_ptr<rand_gen> create_randu_gen(const scalar_variable &min,
158  const scalar_variable &max) const = 0;
159  virtual std::shared_ptr<rand_gen> create_randu_gen(const scalar_variable &min,
160  const scalar_variable &max, const scalar_variable &seed) const = 0;
161  // Create Random generator with the normal distribution.
162  virtual std::shared_ptr<rand_gen> create_randn_gen(const scalar_variable &mean,
163  const scalar_variable &var) const = 0;
164  virtual std::shared_ptr<rand_gen> create_randn_gen(const scalar_variable &mean,
165  const scalar_variable &var, const scalar_variable &seed) const = 0;
166  virtual const bulk_uoperate &get_type_cast(const scalar_type &type) const = 0;
167 
168  virtual bool operator==(const scalar_type &type) const {
169  return get_type() == type.get_type();
170  }
171 
172  virtual bool operator!=(const scalar_type &type) const {
173  return get_type() != type.get_type();
174  }
175 };
176 
177 /*
178  * Here we implement the scalar type.
179  */
180 template<class T>
181 class scalar_type_impl: public scalar_type
182 {
183 public:
184  virtual std::shared_ptr<generic_hashtable> create_hashtable(
185  const scalar_type &val_type) const;
186  virtual const basic_uops &get_basic_uops() const;
187  virtual const basic_ops &get_basic_ops() const;
188  virtual const agg_ops &get_agg_ops() const;
189 
190  virtual std::shared_ptr<scalar_variable> create_scalar() const;
191  virtual std::shared_ptr<rand_gen> create_randu_gen(const scalar_variable &min,
192  const scalar_variable &max) const;
193  virtual std::shared_ptr<rand_gen> create_randu_gen(const scalar_variable &min,
194  const scalar_variable &max, const scalar_variable &seed) const;
195  virtual std::shared_ptr<rand_gen> create_randn_gen(const scalar_variable &mean,
196  const scalar_variable &var) const;
197  virtual std::shared_ptr<rand_gen> create_randn_gen(const scalar_variable &mean,
198  const scalar_variable &var, const scalar_variable &seed) const;
199 
200  virtual const sorter &get_sorter() const {
201  static type_sorter<T> sort;
202  return sort;
203  }
204 
205  virtual const scatter_gather &get_sg() const;
206  virtual const stl_algs &get_stl_algs() const {
207  static stl_algs_impl<T> algs;
208  return algs;
209  }
210  virtual const set_operate &get_set_const(const scalar_variable &val) const;
211  virtual const bulk_uoperate &get_type_cast(const scalar_type &type) const;
212 
213  virtual prim_type get_type() const {
214  return fm::get_type<T>();
215  }
216 
217  virtual size_t get_size() const {
218  return sizeof(T);
219  }
220 };
221 
222 template<class T>
223 const scalar_type &get_scalar_type()
224 {
225  static scalar_type_impl<T> t;
226  return t;
227 }
228 
229 const scalar_type &get_scalar_type(prim_type type);
230 
236 {
237 public:
238  typedef std::shared_ptr<scalar_variable> ptr;
239  typedef std::shared_ptr<const scalar_variable> const_ptr;
243  virtual const char *get_raw() const = 0;
244  virtual char *get_raw() = 0;
248  virtual const scalar_type &get_type() const = 0;
252  virtual bool set_raw(const char *v, int size) = 0;
253  /*
254  * Test if the value is equal to the one stored in the given address.
255  */
256  virtual bool equals(const char *addr) const = 0;
257 
258  virtual size_t get_size() const {
259  return get_type().get_size();
260  }
261 };
262 
263 template<class T>
264 class scalar_variable_impl: public scalar_variable
265 {
266  T v;
267 public:
268  scalar_variable_impl() {
269  v = 0;
270  }
271 
272  scalar_variable_impl(T v) {
273  this->v = v;
274  }
275 
276  virtual const char *get_raw() const {
277  return (const char *) &v;
278  }
279  virtual char *get_raw() {
280  return (char *) &v;
281  }
282 
283  virtual const scalar_type &get_type() const {
284  return get_scalar_type<T>();
285  }
286 
287  virtual bool set_raw(const char *v, int size) {
288  if (sizeof(T) != size)
289  return false;
290 
291  memcpy(&this->v, v, size);
292  return true;
293  }
294 
295  virtual bool equals(const char *addr) const {
296  return v == *(const T *) addr;
297  }
298 
299  T get() const {
300  return v;
301  }
302 
303  void set(T v) {
304  this->v = v;
305  }
306 };
307 
308 bool require_cast(const scalar_type &t1, const scalar_type &t2);
309 
310 }
311 
312 #endif
virtual std::shared_ptr< generic_hashtable > create_hashtable(const scalar_type &val_type) const =0
Definition: bulk_operate.h:39
Definition: generic_type.h:235
virtual bool set_raw(const char *v, int size)=0
prim_type
Definition: generic_type.h:37
Definition: generic_type.h:138
virtual const char * get_raw() const =0
virtual const scalar_type & get_type() const =0