tlds
Transactional Operations for Linked Data Structures
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
point.hpp
Go to the documentation of this file.
1 /**
2  * Copyright (C) 2011
3  * University of Rochester Department of Computer Science
4  * and
5  * Lehigh University Department of Computer Science and Engineering
6  *
7  * License: Modified BSD
8  * Please see the file LICENSE.RSTM for licensing information
9  */
10 
11 #ifndef POINT_HPP__
12 #define POINT_HPP__
13 
14 #include <set>
15  using std::set;
16 #include "common.hpp"
17 
18 class edge;
19 
20 static const int xdim = 0;
21 static const int ydim = 1;
22 static const int ccw = 0; // counter-clockwise
23 static const int cw = 1; // clockwise
24 
25 static const int MAX_COORD_BITS = 24;
26  // coordinate values in the range -2^24..(2^24-1)
27  // permit bucket calculations without overflow, so long as
28  // there are no more than 32 threads
29 
30 class point;
31 extern point *all_points;
32 
33 class point
34 {
35  // Point coordinates are immutable, but their first_edge field can change.
36  // In order to prevent bugs, we disable copying and assignment.
37 
38 #ifdef FGL
39  d_lock l;
40 #endif
41 
42 public:
43  int coord[2]; // final; no accessors needed
45 
46  size_t hash() const
47  {
48  return coord[xdim] ^ coord[ydim];
49  }
50 
51  bool operator==(const point &o) const
52  {
53  return coord[xdim] == o.coord[xdim]
54  && coord[ydim] == o.coord[ydim];
55  }
56 
57  bool operator!=(const point &rhs) const
58  {
59  return !(*this == rhs);
60  }
61 
62  // For locking and printing in canonical (sorted) order.
63  //
64  TRANSACTION_SAFE bool operator<(const point& o) const {
65  return (coord[xdim] < o.coord[xdim]
66  || (coord[xdim] == o.coord[xdim]
67  && coord[ydim] < o.coord[ydim]));
68  }
69 
70 #ifdef FGL
71  void acquire() {
72  l.acquire();
73  }
74  void release() {
75  l.release();
76  }
77 #endif // FGL
78 
79  point() { } // no copying, assignment allowed
80 };
81 
82 struct eq_point {
83  bool operator()(const point* const p1, const point* const p2) const {
84  return *p1 == *p2;
85  }
86 };
87 
88 struct lt_point {
89  bool operator()(const point* const p1, const point* const p2) const {
90  return *p1 < *p2;
91  }
92 };
93 
94 struct hash_point {
95  size_t operator()(const point* const p) const {
96  return p->hash();
97  }
98 };
99 
100 #ifdef FGL
101 // lock all points in given set at beginning of scope, release at end
102 //
103 class with_locked_points {
104  set<point*, lt_point> *S;
105 public:
106  with_locked_points(set<point*, lt_point> &pts) {
107  S = &pts;
108  for (set<point*, lt_point>::iterator it = S->begin();
109  it != S->end(); ++it) {
110  (*it)->acquire();
111  }
112  }
113  ~with_locked_points() {
114  for (set<point*, lt_point>::reverse_iterator it = S->rbegin();
115  it != S->rend(); ++it) {
116  (*it)->release();
117  }
118  }
119 };
120 
121 // To facilitate constructions of the form
122 // point_set S;
123 // with_locked_points lp(S | p1 | p2 | ... | pN);
124 //
125 class point_set : public set<point*, lt_point> {
126 public:
127  point_set &operator|(point *e) {
128  insert(e);
129  return *this;
130  }
131 };
132 #endif // FGL
133 
134 // these are set by create_points().
135 //
136 extern int min_coord[];
137 extern int max_coord[];
138 
139 // The point space is divided into vertical "buckets" -- twice as many as
140 // there are worker threads. During initial Dwyer triangulation, thread
141 // zero works, privately, on buckets 0 and 1; thread 1 on buckets 2 and 3;
142 // etc. During private basting, each thread takes a _seam_ and works on
143 // the buckets closest to it: thread 0 on buckets 0, 1, and 2; thread 1
144 // on buckets 3 and 4; ...; thread n-1 on buckets 2n-3, 2n-2, and 2n-1.
145 //
146 // Routines bucket, stripe, and closest_seam manage this division of labor.
147 // To avoid integer overflow, they count on coordinate values spanning less
148 // than 2^26.
149 //
150 // Global set edges contains a separate hash table for each bucket, ensuring
151 // private access during each private phase of computation.
152 //
153 TRANSACTION_SAFE inline int bucket(const point* const p) {
154  return ((p->coord[xdim] - min_coord[xdim]) * num_workers * 2)
155  / (max_coord[xdim] - min_coord[xdim] + 1);
156 }
157 inline int stripe(const point* const p) {
158  return bucket(p) / 2;
159 }
160 TRANSACTION_SAFE inline int closest_seam(const point* const p) {
161  const int b = bucket(p);
162  if (b == 0) return 0;
163  if (b == (num_workers * 2 - 1)) return num_workers - 2;
164  return (b-1)/2;
165 }
166 
167 // Does circumcircle of A, B, C (ccw) contain D?
168 //
169 TRANSACTION_SAFE bool encircled(const point* A, const point* B,
170  const point* C, const point* D, const int dir);
171 
172 // Is angle from p1 to p2 to p3, in direction dir
173 // around p2, greater than or equal to 180 degrees?
174 //
175 TRANSACTION_SAFE bool extern_angle(const point* p1, const point* p2,
176  const point* p3, const int dir);
177 
178 // Create all points. Read from stdin if seed == 0.
179 //
180 extern void create_points(const int seed);
181 
182 #endif // POINT_HPP__
point * all_points
Definition: point.cpp:30
TRANSACTION_SAFE bool extern_angle(const point *p1, const point *p2, const point *p3, const int dir)
Definition: point.cpp:86
bool operator()(const point *const p1, const point *const p2) const
Definition: point.hpp:89
TRANSACTION_SAFE int bucket(const point *const p)
Definition: point.hpp:153
static const int cw
Definition: point.hpp:23
point()
Definition: point.hpp:79
int min_coord[]
Definition: point.cpp:32
Definition: defs.h:133
#define TRANSACTION_SAFE
Definition: common.hpp:87
TRANSACTION_SAFE int closest_seam(const point *const p)
Definition: point.hpp:160
TRANSACTION_SAFE bool operator<(const point &o) const
Definition: point.hpp:64
static int seed
Definition: mesh.cpp:40
int coord[2]
Definition: point.hpp:43
size_t operator()(const point *const p) const
Definition: point.hpp:95
static const int ccw
Definition: point.hpp:22
Definition: point.hpp:33
Definition: lock.hpp:24
edge * first_edge
Definition: point.hpp:44
static const int MAX_COORD_BITS
Definition: point.hpp:25
Definition: edge.hpp:36
bool operator()(const point *const p1, const point *const p2) const
Definition: point.hpp:83
static const int ydim
Definition: point.hpp:21
bool operator==(const point &o) const
Definition: point.hpp:51
Definition: point.hpp:94
bool operator!=(const point &rhs) const
Definition: point.hpp:57
size_t hash() const
Definition: point.hpp:46
Definition: point.hpp:82
TRANSACTION_SAFE bool encircled(const point *A, const point *B, const point *C, const point *D, const int dir)
Definition: point.cpp:67
void create_points(const int seed)
Definition: point.cpp:116
int stripe(const point *const p)
Definition: point.hpp:157
static node_t * insert(rbtree_t *s, void *k, void *v, node_t *n)
Definition: rbtree.c:617
struct @18 p
int max_coord[]
Definition: point.cpp:33
Definition: point.hpp:88
static const int xdim
Definition: point.hpp:20
int num_workers
Definition: mesh.cpp:39
bool set_op int op_size set_t * l
Definition: stmskip.cc:240