FlashGraph-ng
A new frontier in large-scale graph analysis and data mining
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
vec_store.h
1 #ifndef __VEC_STORE_H__
2 #define __VEC_STORE_H__
3 
4 /*
5  * Copyright 2015 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 "log.h"
24 
25 #include "generic_type.h"
26 #include "bulk_operate.h"
27 
28 namespace fm
29 {
30 
31 class local_vec_store;
32 class set_vec_operate;
33 
34 namespace detail
35 {
36 
37 class matrix_store;
38 
39 class vec_store
40 {
41  size_t length;
42  const scalar_type &type;
43  bool in_mem;
44  // This is used to avoid virtual function call.
45  int entry_size;
46 public:
47  typedef std::shared_ptr<vec_store> ptr;
48  typedef std::shared_ptr<const vec_store> const_ptr;
49 
50  static ptr create(size_t length, const scalar_type &_type, int num_nodes,
51  bool in_mem);
52 
53  vec_store(size_t length, const scalar_type &_type, bool in_mem): type(_type) {
54  this->length = length;
55  this->in_mem = in_mem;
56  this->entry_size = type.get_size();
57  }
58  /*
59  * This is used for vector vector because its entry size isn't fixed.
60  */
61  vec_store(size_t length, size_t entry_size, const scalar_type &_type,
62  bool in_mem): type(_type) {
63  this->length = length;
64  this->entry_size = entry_size;
65  this->in_mem = in_mem;
66  }
67 
68  virtual ~vec_store() {
69  }
70 
71  bool is_in_mem() const {
72  return in_mem;
73  }
74 
75  size_t get_length() const {
76  return length;
77  }
78 
79  const scalar_type &get_type() const {
80  return type;
81  }
82 
83  template<class T>
84  bool is_type() const {
85  return get_type().get_type() == fm::get_type<T>();
86  }
87 
88  size_t get_entry_size() const {
89  return entry_size;
90  }
91 
92  virtual bool resize(size_t new_length) {
93  this->length = new_length;
94  return true;
95  }
96 
97  // Copy #eles to the data array and return #eles copied.
98  virtual size_t copy_to(char *data, size_t num_eles) const;
99 
100  virtual bool append(std::vector<vec_store::const_ptr>::const_iterator vec_it,
101  std::vector<vec_store::const_ptr>::const_iterator vec_end) = 0;
102  virtual bool append(const vec_store &vec) = 0;
103  virtual vec_store::ptr deep_copy() const = 0;
104  virtual vec_store::ptr shallow_copy() = 0;
105  virtual vec_store::const_ptr shallow_copy() const = 0;
106 
107  virtual bool set_portion(std::shared_ptr<const local_vec_store> store,
108  off_t loc) = 0;
109  virtual std::shared_ptr<local_vec_store> get_portion(off_t loc,
110  size_t size) = 0;
111  virtual std::shared_ptr<const local_vec_store> get_portion(off_t loc,
112  size_t size) const = 0;
113  virtual size_t get_portion_size() const = 0;
114  virtual size_t get_num_portions() const {
115  double len = get_length();
116  return ceil(len / get_portion_size());
117  }
118 
119  virtual void reset_data() = 0;
120  virtual void set_data(const set_vec_operate &op) = 0;
121 
122  virtual vec_store::ptr sort_with_index() = 0;
123  virtual void sort() = 0;
124  virtual bool is_sorted() const = 0;
125  virtual std::shared_ptr<const matrix_store> conv2mat(size_t nrow,
126  size_t ncol, bool byrow) const;
127  virtual std::shared_ptr<matrix_store> conv2mat(size_t nrow,
128  size_t ncol, bool byrow) = 0;
129 };
130 
131 template<class T>
132 class seq_set_vec_operate: public type_set_vec_operate<T>
133 {
134  long n;
135  T from;
136  T by;
137 public:
138  seq_set_vec_operate(long n, T from, T by) {
139  this->n = n;
140  this->from = from;
141  this->by = by;
142  }
143 
144  virtual void set(T *arr, size_t num_eles, off_t start_idx) const {
145  // We are initializing a single-column matrix.
146  T v = from + start_idx * by;
147  for (size_t i = 0; i < num_eles; i++) {
148  arr[i] = v;
149  v += by;
150  }
151  }
152 };
153 
154 /*
155  * Create a sequence of values in [start, end]. `end' is inclusive.
156  */
157 template<class EntryType>
158 vec_store::ptr create_seq_vec_store(EntryType start, EntryType end, EntryType stride,
159  int num_nodes = -1, bool in_mem = true)
160 {
161  if ((end < start && stride > 0) || (end > stride && stride < 0)) {
162  BOOST_LOG_TRIVIAL(error)
163  << "There are a negative number of elements in the sequence";
164  return vec_store::ptr();
165  }
166  long n = (end - start) / stride;
167  // We need to count the start element.
168  n++;
169  detail::vec_store::ptr v = detail::vec_store::create(n,
170  get_scalar_type<EntryType>(), num_nodes, in_mem);
171  v->set_data(seq_set_vec_operate<EntryType>(n, start, stride));
172  return v;
173 }
174 
175 /*
176  * Create a vector filled with a constant value.
177  */
178 template<class EntryType>
179 vec_store::ptr create_rep_vec_store(size_t length, EntryType initv,
180  int num_nodes = -1, bool in_mem = true)
181 {
182  detail::vec_store::ptr v = detail::vec_store::create(length,
183  get_scalar_type<EntryType>(), num_nodes, in_mem);
184  v->set_data(const_set_vec_operate<EntryType>(initv));
185  return v;
186 }
187 
188 template<>
189 vec_store::ptr create_seq_vec_store<double>(double start, double end,
190  double stride, int num_nodes, bool in_mem);
191 
192 }
193 
194 }
195 
196 #endif