FlashGraph-ng
A new frontier in large-scale graph analysis and data mining
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
factor.h
1 #ifndef __FACTOR_H__
2 #define __FACTOR_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 "vector.h"
24 
25 /*
26  * This factor is the same as the one in R.
27  */
28 
29 namespace fm
30 {
31 
32 typedef int factor_value_t;
33 
34 class factor
35 {
36  size_t num_levels;
37 public:
38  factor(size_t num_levels) {
39  this->num_levels = num_levels;
40  }
41 
42  size_t get_num_levels() const {
43  return num_levels;
44  }
45 
46  factor_value_t get_max() const {
47  return num_levels - 1;
48  }
49 
50  factor_value_t get_min() const {
51  return 0;
52  }
53 
54  bool is_valid_level(factor_value_t v) const {
55  return v >= 0 && (size_t) v < num_levels;
56  }
57 };
58 
59 class factor_vector: public vector
60 {
61  factor f;
62 
63  factor_vector(const factor &_f,
64  detail::vec_store::const_ptr store): vector(store), f(_f) {
65  }
66 
67  factor_vector(const factor &_f, size_t len, int num_nodes, bool in_mem,
68  const set_vec_operate &op): vector(detail::vec_store::create(len,
69  get_scalar_type<factor_value_t>(), num_nodes, in_mem)), f(_f) {
70  const_cast<detail::vec_store &>(get_data()).set_data(op);
71  }
72 public:
73  typedef std::shared_ptr<factor_vector> ptr;
74  typedef std::shared_ptr<const factor_vector> const_ptr;
75 
76  static ptr create(const factor &f, detail::vec_store::const_ptr vec) {
77  return ptr(new factor_vector(f, vec));
78  }
79 
80  static ptr create(const factor &f, size_t length, int num_nodes,
81  bool in_mem, const set_vec_operate &op) {
82  return ptr(new factor_vector(f, length, num_nodes, in_mem, op));
83  }
84 
85  const factor &get_factor() const {
86  return f;
87  }
88 };
89 
90 }
91 
92 #endif