FlashGraph-ng
A new frontier in large-scale graph analysis and data mining
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
local_vv_store.h
1 #ifndef __LOCAL_VV_STORE_H__
2 #define __LOCAL_VV_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 "vv_store.h"
24 #include "local_vec_store.h"
25 
26 namespace fm
27 {
28 
29 class local_vv_store: public local_vec_store
30 {
31  std::vector<off_t> offs;
32  local_vec_store::ptr vec;
33 
34  const char *get_orig_const_data() const {
35  return static_cast<const local_vec_store &>(*vec).get_raw_arr();
36  }
37 
38  char *get_orig_data() {
39  return vec->get_raw_arr();
40  }
41 
42  std::vector<off_t> get_rel_offs(off_t start, size_t len) const;
43 public:
44  typedef std::shared_ptr<local_vv_store> ptr;
45  typedef std::shared_ptr<const local_vv_store> const_ptr;
46 
47  static ptr cast(local_vec_store::ptr vec) {
48  assert(vec->get_entry_size() == 0);
49  return std::static_pointer_cast<local_vv_store>(vec);
50  }
51 
52  static const_ptr cast(local_vec_store::const_ptr vec) {
53  assert(vec->get_entry_size() == 0);
54  return std::static_pointer_cast<const local_vv_store>(vec);
55  }
56 
57  local_vv_store(off_t global_start, const std::vector<off_t> &offs,
58  local_vec_store::ptr vec): local_vec_store(
59  static_cast<const local_vec_store &>(*vec).get_raw_arr(),
60  vec->get_raw_arr(), global_start, offs.size() - 1, 0,
61  vec->get_type(), -1) {
62  assert(!detail::vv_store::is_vector_vector(*vec));
63  this->offs = offs;
64  this->vec = vec;
65  }
66 
67  bool is_whole() const {
68  return offs.front() == 0
69  && offs.size() - 1 == local_vec_store::get_length();
70  }
71 
72  virtual bool expose_sub_vec(off_t start, size_t length) {
73  assert(start + length < offs.size());
74  char *new_data = this->get_orig_data() + offs[start];
75  const char *const_new_data = this->get_orig_const_data() + offs[start];
76  local_vec_store::set_data(const_new_data, new_data);
77  return local_vec_store::expose_sub_vec(start, length);
78  }
79  virtual void reset_expose() {
80  local_vec_store::set_data(this->get_orig_const_data(),
81  this->get_orig_data());
82  local_vec_store::reset_expose();
83  }
84 
85  virtual const char *get_sub_arr(off_t start, off_t end) const {
86  start += get_local_start();
87  assert((size_t) start < offs.size() && (size_t) end < offs.size());
88  return this->get_orig_const_data() + offs[start];
89  }
90  virtual char *get_sub_arr(off_t start, off_t end) {
91  start += get_local_start();
92  assert((size_t) start < offs.size() && (size_t) end < offs.size());
93  return this->get_orig_data() + offs[start];
94  }
95 
96  virtual void reset_data() {
97  assert(is_whole());
98  memset(this->get_orig_data(), 0, get_num_bytes());
99  }
100 
101  size_t get_num_bytes(off_t idx) const {
102  idx += get_local_start();
103  return offs[idx + 1] - offs[idx];
104  }
105 
106  using local_vec_store::get_length;
107  size_t get_length(off_t idx) const {
108  return get_num_bytes(idx) / get_type().get_size();
109  }
110 
111  size_t get_num_vecs() const {
112  return local_vec_store::get_length();
113  }
114 
115  size_t get_num_bytes() const {
116  off_t start = get_local_start();
117  off_t end = start + local_vec_store::get_length();
118  return offs[end] - offs[start];
119  }
120 
121  using local_vec_store::get_raw_arr;
122  const char*get_raw_arr(off_t idx) const {
123  idx += get_local_start();
124  return this->get_orig_const_data() + offs[idx];
125  }
126 
127  virtual bool resize(size_t new_length) {
128  assert(0);
129  return false;
130  }
131 
132  virtual detail::vec_store::ptr shallow_copy() {
133  return ptr(new local_vv_store(*this));
134  }
135  virtual detail::vec_store::const_ptr shallow_copy() const {
136  return ptr(new local_vv_store(*this));
137  }
138 
139  virtual local_vec_store::ptr get_portion(off_t loc, size_t size);
140  virtual local_vec_store::const_ptr get_portion(off_t loc,
141  size_t size) const;
142 
143  /*
144  * The methods below aren't used.
145  */
146 
147  virtual void set_data(const set_vec_operate &op) {
148  assert(0);
149  }
150 
151  virtual detail::vec_store::ptr sort_with_index() {
152  assert(0);
153  }
154  virtual void sort() {
155  assert(0);
156  }
157  virtual bool is_sorted() const {
158  return false;
159  }
160  virtual std::shared_ptr<const detail::matrix_store> conv2mat(
161  size_t nrow, size_t ncol, bool byrow) const {
162  assert(0);
163  }
164 
165  local_vec_store::ptr get(std::vector<off_t> &idxs) const;
166 };
167 
168 namespace detail
169 {
170 class mem_vv_store;
171 
172 std::shared_ptr<detail::mem_vv_store> apply(const local_vv_store &store,
173  const arr_apply_operate &op);
174 }
175 
176 }
177 
178 #endif