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