FlashGraph-ng
A new frontier in large-scale graph analysis and data mining
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
scan_pointer.h
1 #ifndef __SCAN_POINTER_H__
2 #define __SCAN_POINTER_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 FlashGraph.
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 <stdlib.h>
24 
25 namespace fg
26 {
27 
28 /*
29  * This class helps to scan a list from the beginning to the end
30  * or from the end to the beginning.
31  */
32 struct scan_pointer
33 {
34  size_t range_start;
35  size_t size;
36  size_t idx;
37  bool forward;
38 public:
39  scan_pointer(size_t size, bool forward) {
40  this->forward = forward;
41  this->range_start = 0;
42  this->size = size;
43  if (forward)
44  idx = range_start;
45  else
46  idx = size + range_start;
47  }
48 
49  size_t get_num_remaining() const {
50  if (forward)
51  return size + range_start - idx;
52  else
53  return idx - range_start;
54  }
55 
56  size_t get_curr_loc() const {
57  return idx;
58  }
59 
60  size_t move(size_t dist) {
61  if (forward) {
62  idx += dist;
63  idx = min(idx, size);
64  }
65  else if (idx >= dist)
66  idx -= dist;
67  else
68  idx = 0;
69  return idx;
70  }
71 
72  size_t get_range_start() const {
73  return range_start;
74  }
75 
76  size_t get_range_end() const {
77  return range_start + size;
78  }
79 
80  size_t get_range_size() const {
81  return size;
82  }
83 
84  void set_scan_dir(bool forward) {
85  if (this->forward == forward)
86  return;
87 
88  size_t new_range_start;
89  size_t new_range_size;
90  if (forward) {
91  // So the original direction is backward.
92  new_range_start = get_range_start();
93  new_range_size
94  = get_curr_loc() - get_range_start();
95  idx = new_range_start;
96  }
97  else {
98  // So the original direction is forward.
99  new_range_start = get_curr_loc();
100  new_range_size
101  = get_range_end() - get_curr_loc();
102  idx = new_range_start + new_range_size;
103 
104  }
105  range_start = new_range_start;
106  size = new_range_size;
107  this->forward = forward;
108  }
109 };
110 
111 }
112 
113 #endif