tlds
Transactional Operations for Linked Data Structures
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
tm_list_set.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 /* tm_list_set.hpp
12  *
13  * Simple list-based set.
14  * Element type must be the same size as unsigned long.
15  */
16 
17 #ifndef TM_LIST_SET_HPP__
18 #define TM_LIST_SET_HPP__
19 
20 #include <cassert>
21 #include "common.hpp"
22 #include "tm_set.hpp"
23 
24 template<typename T>
26 
27 // LLNode is a single node in a sorted linked list
28 //
29 template<class T>
30 class LLNode
31 {
32  friend class tm_list_set<T>; // all fields are nominally private
33 
34  const T val;
36 
38  LLNode(const T val, LLNode<T>* next) : val(val), next_node(next) { }
39 };
40 
41 template<typename T>
42 class tm_list_set : public tm_set<T>
43 {
45 
46  // no implementation; forbids passing by value
47  tm_list_set(const tm_list_set&);
48  // no implementation; forbids copying
50 
51  public:
52  // note that assert in constructor guarantees that items and
53  // unsigned ints are the same size
54 
55  TRANSACTION_SAFE void insert(const T item)
56  {
57  // traverse the list to find the insertion point
58  LLNode<T>* prev = head_node;
59  LLNode<T>* curr = prev->next_node;
60  while (curr != 0) {
61  if (curr->val == item) return;
62  prev = curr;
63  curr = prev->next_node;
64  }
65  prev->next_node = new LLNode<T>(item, curr);
66  }
67 
68  TRANSACTION_SAFE void remove(const T item)
69  {
70  // find the node whose val matches the request
71  LLNode<T>* prev = head_node;
72  LLNode<T>* curr = prev->next_node;
73  while (curr != 0) {
74  // if we find the node, disconnect it and end the search
75  if (curr->val == item) {
76  prev->next_node = curr->next_node;
77  delete curr;
78  break;
79  }
80  prev = curr;
81  curr = prev->next_node;
82  }
83  }
84 
85  // find out whether item is in list
86  bool lookup(const T item)
87  {
88  LLNode<T>* curr = head_node;
89  curr = curr->next_node;
90  while (curr != 0) {
91  if (curr->val == item) return true;
92  curr = curr->next_node;
93  }
94  return false;
95  }
96 
97  // apply a function to every element of the list
98  void apply_to_all(void (*f)(T item))
99  {
100  LLNode<T>* curr = head_node;
101  curr = curr->next_node;
102  while (curr != 0) {
103  f(curr->val);
104  curr = curr->next_node;
105  }
106  }
107 
108  tm_list_set() : head_node(new LLNode<T>(0, 0))
109  {
110  assert(sizeof(T) == sizeof(unsigned long));
111  }
112 
113  // Destruction not currently supported.
114  virtual ~tm_list_set() { assert(false); }
115 
116  // count elements in the list
117  int size() const
118  {
119  int rtn = 0;
120  LLNode<T>* curr = head_node->next_node;
121  while (curr != 0) {
122  rtn++;
123  curr = curr->next_node;
124  }
125  return rtn;
126  }
127 };
128 
129 #endif // TM_LIST_SET_HPP__
bool lookup(const T item)
Definition: tm_list_set.hpp:86
TRANSACTION_SAFE void insert(const T item)
Definition: tm_list_set.hpp:55
TRANSACTION_SAFE LLNode(const T val, LLNode< T > *next)
Definition: tm_list_set.hpp:38
Definition: tm_set.hpp:20
const T val
Definition: tm_list_set.hpp:34
#define TRANSACTION_SAFE
Definition: common.hpp:87
tm_list_set & operator=(const tm_list_set &)
Definition: tm_list_set.hpp:30
virtual ~tm_list_set()
Definition: tm_list_set.hpp:114
int size() const
Definition: tm_list_set.hpp:117
tm_list_set()
Definition: tm_list_set.hpp:108
void apply_to_all(void(*f)(T item))
Definition: tm_list_set.hpp:98
LLNode< T > * head_node
Definition: tm_list_set.hpp:44
Definition: tm_list_set.hpp:25
LLNode< T > * next_node
Definition: tm_list_set.hpp:35