tlds
Transactional Operations for Linked Data Structures
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
rstmlist.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 LIST_HPP__
12 #define LIST_HPP__
13 
14 #include <api/api.hpp>
15 
16 // We construct other data structures from the List. In order to do their
17 // sanity checks correctly, we might need to pass in a validation function of
18 // this type
19 typedef bool (*verifier)(uint32_t, uint32_t);
20 
21 // Set of LLNodes represented as a linked list in sorted order
22 class RSTMList
23 {
24 
25  // Node in a List
26  struct Node
27  {
28  int m_val;
30 
31  // ctors
32  Node(int val = -1) : m_val(val), m_next() { }
33 
34  Node(int val, Node* next) : m_val(val), m_next(next) { }
35  };
36 
37  public:
38 
40 
41  RSTMList();
42 
43  // true iff val is in the data structure
45  bool lookup(int val TM_ARG) const;
46 
47  // standard IntSet methods
49  bool insert(int val TM_ARG);
50 
51  // remove a node if its value = val
53  bool remove(int val TM_ARG);
54 
55  // make sure the list is in sorted order
56  bool isSane() const;
57 
58  // make sure the list is in sorted order and for each node x,
59  // v(x, verifier_param) is true
60  bool extendedSanityCheck(verifier v, uint32_t param) const;
61 
62  // find max and min
64  int findmax(TM_ARG_ALONE) const;
65 
67  int findmin(TM_ARG_ALONE) const;
68 
69  // overwrite all elements up to val
71  void overwrite(int val TM_ARG);
72 };
73 
74 
75 // constructor just makes a sentinel for the data structure
76 RSTMList::RSTMList() : sentinel(new Node()) { }
77 
78 // simple sanity check: make sure all elements of the list are in sorted order
79 bool RSTMList::isSane(void) const
80 {
81  const Node* prev(sentinel);
82  const Node* curr((prev->m_next));
83 
84  while (curr != NULL) {
85  if ((prev->m_val) >= (curr->m_val))
86  return false;
87  prev = curr;
88  curr = curr->m_next;
89  }
90  return true;
91 }
92 
93 // extended sanity check, does the same as the above method, but also calls v()
94 // on every item in the list
95 bool RSTMList::extendedSanityCheck(verifier v, uint32_t v_param) const
96 {
97  const Node* prev(sentinel);
98  const Node* curr((prev->m_next));
99  while (curr != NULL) {
100  if (!v((curr->m_val), v_param) || ((prev->m_val) >= (curr->m_val)))
101  return false;
102  prev = curr;
103  curr = prev->m_next;
104  }
105  return true;
106 }
107 
108 // insert method; find the right place in the list, add val so that it is in
109 // sorted order; if val is already in the list, exit without inserting
112 {
113  // traverse the list to find the insertion point
114  const Node* prev(sentinel);
115  const Node* curr(TM_READ(prev->m_next));
116 
117  while (curr != NULL) {
118  if (TM_READ(curr->m_val) >= val)
119  break;
120  prev = curr;
121  curr = TM_READ(prev->m_next);
122  }
123 
124  // now insert new_node between prev and curr
125  if (!curr || (TM_READ(curr->m_val) > val)) {
126  Node* insert_point = const_cast<Node*>(prev);
127 
128  // create the new node
129  Node* i = (Node*)TM_ALLOC(sizeof(Node));
130  i->m_val = val;
131  i->m_next = const_cast<Node*>(curr);
132  TM_WRITE(insert_point->m_next, i);
133 
134  return true;
135  }
136 
137  return false;
138 }
139 
140 // search function
142 bool RSTMList::lookup(int val TM_ARG) const
143 {
144  bool found = false;
145  const Node* curr(sentinel);
146  curr = TM_READ(curr->m_next);
147 
148  while (curr != NULL) {
149  if (TM_READ(curr->m_val) >= val)
150  break;
151  curr = TM_READ(curr->m_next);
152  }
153 
154  found = ((curr != NULL) && (TM_READ(curr->m_val) == val));
155  return found;
156 }
157 
158 // findmax function
161 {
162  int max = -1;
163  const Node* curr(sentinel);
164  while (curr != NULL) {
165  max = TM_READ(curr->m_val);
166  curr = TM_READ(curr->m_next);
167  }
168  return max;
169 }
170 
171 // findmin function
174 {
175  int min = -1;
176  const Node* curr(sentinel);
177  curr = TM_READ(curr->m_next);
178  if (curr != NULL)
179  min = TM_READ(curr->m_val);
180  return min;
181 }
182 
183 // remove a node if its value == val
186 {
187  bool ret = false;
188  // find the node whose val matches the request
189  const Node* prev(sentinel);
190  const Node* curr(TM_READ(prev->m_next));
191  while (curr != NULL) {
192  // if we find the node, disconnect it and end the search
193  if (TM_READ(curr->m_val) == val) {
194  Node* mod_point = const_cast<Node*>(prev);
195  TM_WRITE(mod_point->m_next, TM_READ(curr->m_next));
196 
197  // delete curr...
198  TM_FREE(const_cast<Node*>(curr));
199  ret = true;
200  break;
201  }
202  else if (TM_READ(curr->m_val) > val) {
203  // this means the search failed
204  ret = false;
205  break;
206  }
207  prev = curr;
208  curr = TM_READ(prev->m_next);
209  }
210 
211  return ret;
212 }
213 
214 // search function
217 {
218  const Node* curr(sentinel);
219  curr = TM_READ(curr->m_next);
220 
221  while (curr != NULL) {
222  if (TM_READ(curr->m_val) >= val)
223  break;
224  Node* wcurr = const_cast<Node*>(curr);
225  TM_WRITE(wcurr->m_val, TM_READ(wcurr->m_val));
226  curr = TM_READ(wcurr->m_next);
227  }
228 }
229 
230 #endif // LIST_HPP__
Node * sentinel
Definition: rstmlist.hpp:39
#define TM_CALLABLE
Definition: cxxtm.hpp:32
#define TM_ARG_ALONE
Definition: cxxtm.hpp:41
#define TM_WRITE(x, y)
Definition: cxxtm.hpp:46
Node * m_next
Definition: rstmlist.hpp:29
TM_CALLABLE bool remove(int val TM_ARG)
Definition: rstmlist.hpp:185
bool isSane() const
Definition: rstmlist.hpp:79
TM_CALLABLE int findmin(TM_ARG_ALONE) const
Definition: rstmlist.hpp:173
#define TM_ARG
Definition: cxxtm.hpp:40
Definition: rstmlist.hpp:22
#define TM_READ(x)
Definition: cxxtm.hpp:45
TM_CALLABLE bool lookup(int val TM_ARG) const
Definition: rstmlist.hpp:142
TM_CALLABLE int findmax(TM_ARG_ALONE) const
Definition: rstmlist.hpp:160
Node(int val=-1)
Definition: rstmlist.hpp:32
bool(* verifier)(uint32_t, uint32_t)
Definition: rstmlist.hpp:19
bool ret
Definition: stmskip.cc:242
#define TM_ALLOC
Definition: library.hpp:294
int m_val
Definition: rstmlist.hpp:28
bool extendedSanityCheck(verifier v, uint32_t param) const
Definition: rstmlist.hpp:95
Definition: rstmlist.hpp:26
TM_CALLABLE bool insert(int val TM_ARG)
Definition: rstmlist.hpp:111
RSTMList()
Definition: rstmlist.hpp:76
TM_CALLABLE void overwrite(int val TM_ARG)
Definition: rstmlist.hpp:216
Node(int val, Node *next)
Definition: rstmlist.hpp:34
#define TM_FREE
Definition: library.hpp:295