Flex  0.17.9
adj_list.h
Go to the documentation of this file.
1 
16 #ifndef STORAGES_RT_MUTABLE_GRAPH_CSR_ADJ_LIST_H_
17 #define STORAGES_RT_MUTABLE_GRAPH_CSR_ADJ_LIST_H_
18 
21 #include "flex/utils/allocators.h"
22 
23 namespace gs {
24 
25 template <typename T>
27  static void copy(T* new_buffer, T* old_buffer, size_t len) {
28  memcpy((void*) new_buffer, (void*) old_buffer, len * sizeof(T));
29  }
30 };
31 
32 template <typename EDATA_T>
34  public:
38  MutableAdjlist() : buffer_(NULL), size_(0), capacity_(0) {}
40  : buffer_(rhs.buffer_),
41  size_(rhs.size_.load(std::memory_order_acquire)),
42  capacity_(rhs.capacity_) {}
44 
45  void init(nbr_t* ptr, int cap, int size) {
46  buffer_ = ptr;
47  capacity_ = cap;
48  size_ = size;
49  }
50 
51  void batch_put_edge(vid_t neighbor, const EDATA_T& data, timestamp_t ts = 0) {
52  CHECK_LT(size_, capacity_);
53  auto& nbr = buffer_[size_++];
54  nbr.neighbor = neighbor;
55  nbr.data = data;
56  nbr.timestamp.store(ts);
57  }
58 
59  void put_edge(vid_t neighbor, const EDATA_T& data, timestamp_t ts,
60  Allocator& allocator) {
61  if (size_ == capacity_) {
62  capacity_ += ((capacity_) >> 1);
63  capacity_ = std::max(capacity_, 8);
64  nbr_t* new_buffer =
65  static_cast<nbr_t*>(allocator.allocate(capacity_ * sizeof(nbr_t)));
66  if (size_ > 0) {
68  }
69  buffer_ = new_buffer;
70  }
71  auto& nbr = buffer_[size_.fetch_add(1)];
72  nbr.neighbor = neighbor;
73  nbr.data = data;
74  nbr.timestamp.store(ts);
75  }
76 
77  inline slice_t get_edges() const {
78  slice_t ret;
79  ret.set_size(size_.load(std::memory_order_acquire));
80  ret.set_begin(buffer_);
81  return ret;
82  }
83 
85  mut_slice_t ret;
86  ret.set_size(size_.load());
87  ret.set_begin(buffer_);
88  return ret;
89  }
90 
91  int capacity() const { return capacity_; }
92  int size() const { return size_; }
93  const nbr_t* data() const { return buffer_; }
94  nbr_t* data() { return buffer_; }
95 
96  private:
98  std::atomic<int> size_;
99  int capacity_;
100 };
101 
102 } // namespace gs
103 
104 #endif // STORAGES_RT_MUTABLE_GRAPH_CSR_ADJ_LIST_H_
Definition: allocators.h:29
void * allocate(size_t size)
Definition: allocators.h:60
Definition: adj_list.h:33
void put_edge(vid_t neighbor, const EDATA_T &data, timestamp_t ts, Allocator &allocator)
Definition: adj_list.h:59
const nbr_t * data() const
Definition: adj_list.h:93
void batch_put_edge(vid_t neighbor, const EDATA_T &data, timestamp_t ts=0)
Definition: adj_list.h:51
mut_slice_t get_edges_mut()
Definition: adj_list.h:84
MutableAdjlist(const MutableAdjlist &rhs)
Definition: adj_list.h:39
nbr_t * data()
Definition: adj_list.h:94
~MutableAdjlist()
Definition: adj_list.h:43
void init(nbr_t *ptr, int cap, int size)
Definition: adj_list.h:45
slice_t get_edges() const
Definition: adj_list.h:77
nbr_t * buffer_
Definition: adj_list.h:97
std::atomic< int > size_
Definition: adj_list.h:98
MutableAdjlist()
Definition: adj_list.h:38
int capacity_
Definition: adj_list.h:99
int size() const
Definition: adj_list.h:92
int capacity() const
Definition: adj_list.h:91
Definition: nbr.h:496
void set_begin(nbr_t *ptr)
Definition: nbr.h:506
void set_size(int size)
Definition: nbr.h:503
Definition: nbr.h:318
void set_size(int size)
Definition: nbr.h:327
void set_begin(const_nbr_ptr_t ptr)
Definition: nbr.h:330
Definition: adj_list.h:23
uint32_t timestamp_t
Definition: types.h:30
uint32_t vid_t
Definition: types.h:31
Definition: loading_config.h:232
Definition: nbr.h:258
vid_t neighbor
Definition: nbr.h:284
std::atomic< timestamp_t > timestamp
Definition: nbr.h:285
Definition: adj_list.h:26
static void copy(T *new_buffer, T *old_buffer, size_t len)
Definition: adj_list.h:27