FlashGraph-ng
A new frontier in large-scale graph analysis and data mining
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
mem_vec_store.h
1 #ifndef __MEM_VEC_STORE_H__
2 #define __MEM_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 "vec_store.h"
26 #include "raw_data_array.h"
27 #include "bulk_operate.h"
28 
29 namespace fm
30 {
31 
32 class local_vec_store;
33 
34 namespace detail
35 {
36 
37 class matrix_store;
38 
39 class mem_vec_store: public vec_store
40 {
41 public:
42  mem_vec_store(size_t length, const scalar_type &type): vec_store(
43  length, type, true) {
44  }
45  mem_vec_store(size_t length, size_t entry_size,
46  const scalar_type &type): vec_store(length, entry_size, type, true) {
47  }
48 
49  typedef std::shared_ptr<mem_vec_store> ptr;
50  typedef std::shared_ptr<const mem_vec_store> const_ptr;
51 
52  static ptr cast(vec_store::ptr store) {
53  assert(store->is_in_mem());
54  return std::static_pointer_cast<mem_vec_store>(store);
55  }
56 
57  static const_ptr cast(vec_store::const_ptr store) {
58  assert(store->is_in_mem());
59  return std::static_pointer_cast<const mem_vec_store>(store);
60  }
61 
62  static ptr create(size_t length, int num_nodes, const scalar_type &type);
63 
64  virtual int get_num_nodes() const {
65  return -1;
66  }
67 
68  virtual char *get_raw_arr() = 0;
69  virtual const char *get_raw_arr() const = 0;
70  virtual const char *get_sub_arr(off_t start, off_t end) const = 0;
71  virtual char *get_sub_arr(off_t start, off_t end) = 0;
72 
73  virtual std::shared_ptr<local_vec_store> get_portion(off_t loc,
74  size_t size) = 0;
75  virtual std::shared_ptr<const local_vec_store> get_portion(off_t loc,
76  size_t size) const = 0;
77  virtual bool copy_from(const char *buf, size_t num_bytes);
78 };
79 
80 class smp_vec_store: public mem_vec_store
81 {
82  char *arr;
83  detail::raw_data_array data;
84 
85  smp_vec_store(const detail::raw_data_array &data, const scalar_type &type);
86  smp_vec_store(size_t length, const scalar_type &type);
87 public:
88  typedef std::shared_ptr<smp_vec_store> ptr;
89  typedef std::shared_ptr<const smp_vec_store> const_ptr;
90 
91  static ptr create(size_t length, const scalar_type &type) {
92  return ptr(new smp_vec_store(length, type));
93  }
94  static ptr create(const detail::raw_data_array &data,
95  const scalar_type &type);
96 
97  static ptr cast(vec_store::ptr store) {
98  assert(store->is_in_mem() && std::static_pointer_cast<mem_vec_store>(
99  store)->get_num_nodes() < 0);
100  return std::static_pointer_cast<smp_vec_store>(store);
101  }
102 
103  static const_ptr cast(vec_store::const_ptr store) {
104  assert(store->is_in_mem());
105  return std::static_pointer_cast<const smp_vec_store>(store);
106  }
107 
108  std::shared_ptr<char> get_raw_data() {
109  return data.get_raw_data();
110  }
111 
112  std::shared_ptr<const char> get_raw_data() const {
113  return data.get_raw_data();
114  }
115 
116  virtual char *get_raw_arr() {
117  return arr;
118  }
119  virtual const char *get_raw_arr() const {
120  return arr;
121  }
122  virtual const char *get_sub_arr(off_t start, off_t end) const {
123  assert(start < end && (size_t) end <= get_length());
124  return arr + start * get_type().get_size();
125  }
126  virtual char *get_sub_arr(off_t start, off_t end) {
127  assert(start < end && (size_t) end <= get_length());
128  return arr + start * get_type().get_size();
129  }
130 
131  size_t get_sub_start() const {
132  return (arr - data.get_raw()) / get_entry_size();
133  }
134 
135  bool expose_sub_vec(off_t start, size_t length);
136 
137  virtual smp_vec_store::ptr get(const smp_vec_store &idxs) const;
138 
139  virtual bool append(std::vector<vec_store::const_ptr>::const_iterator vec_it,
140  std::vector<vec_store::const_ptr>::const_iterator vec_end);
141  virtual bool append(const vec_store &vec);
142  virtual bool resize(size_t new_length);
143  virtual vec_store::ptr deep_copy() const;
144  virtual vec_store::ptr shallow_copy() {
145  return vec_store::ptr(new smp_vec_store(*this));
146  }
147  virtual vec_store::const_ptr shallow_copy() const {
148  return vec_store::ptr(new smp_vec_store(*this));
149  }
150 
151  virtual bool set_portion(std::shared_ptr<const local_vec_store> store,
152  off_t loc);
153  virtual std::shared_ptr<local_vec_store> get_portion(off_t loc, size_t size);
154  virtual std::shared_ptr<const local_vec_store> get_portion(off_t loc,
155  size_t size) const;
156  virtual size_t get_portion_size() const;
157 
158  virtual void reset_data() {
159  data.reset_data();
160  }
161 
162  void set_data(const set_vec_operate &op);
163 
164  void set(const std::vector<const char *> &locs);
165 
166  virtual vec_store::ptr sort_with_index();
167  virtual void sort() {
168  get_type().get_sorter().sort(arr, get_length(), false);
169  }
170  virtual bool is_sorted() const {
171  return get_type().get_sorter().is_sorted(get_raw_arr(),
172  get_length(), false);
173  }
174 
175  virtual std::shared_ptr<matrix_store> conv2mat(size_t nrow,
176  size_t ncol, bool byrow);
177 
178  char *get(off_t idx) {
179  return arr + idx * get_entry_size();
180  }
181 
182  const char *get(off_t idx) const {
183  return arr + idx * get_entry_size();
184  }
185 
186  template<class T>
187  T get(off_t idx) const {
188  return *(const T *) get(idx);
189  }
190 
191  template<class T>
192  void set(off_t idx, T val) {
193  *(T *) get(idx) = val;
194  }
195 };
196 
197 }
198 
199 }
200 
201 #endif