tlds
Transactional Operations for Linked Data Structures
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
MiniVector.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 /**
12  * A simple vector-like templated collection object. The main difference
13  * from the STL vector is that we can force uncommon code (such as resize)
14  * to be a function call by putting instantiations of the expand() method
15  * into their own .o file.
16  */
17 
18 #ifndef MINIVECTOR_HPP__
19 #define MINIVECTOR_HPP__
20 
21 #include <cassert>
22 #include <cstdlib>
23 #include <string.h>
24 #include <common/platform.hpp>
25 
26 namespace stm
27 {
28  /*** Self-growing array */
29  template <class T>
30  class MiniVector
31  {
32  unsigned long m_cap; // current vector capacity
33  unsigned long m_size; // current number of used elements
34  T* m_elements; // the actual elements in the vector
35 
36  /*** double the size of the minivector */
37  void expand();
38 
39  public:
40 
41  /*** Construct a minivector with a default size */
42  MiniVector(const unsigned long capacity)
43  : m_cap(capacity), m_size(0),
44  m_elements(static_cast<T*>(malloc(sizeof(T)*m_cap)))
45  {
46  assert(m_elements);
47  }
48 
49  ~MiniVector() { free(m_elements); }
50 
51  /*** Reset the vector without destroying the elements it holds */
52  TM_INLINE void reset() { m_size = 0; }
53 
54  /*** Insert an element into the minivector */
56  {
57  // NB: There is a tradeoff here. If we put the element into the list
58  // first, we are going to have to copy one more object any time we
59  // double the list. However, by doing things in this order we avoid
60  // constructing /data/ on the stack if (1) it has a simple
61  // constructor and (2) /data/ isn't that big relative to the number
62  // of available registers.
63 
64  // Push data onto the end of the array and increment the size
65  m_elements[m_size++] = data;
66 
67  // If the list is full, double the list size, allocate a new array
68  // of elements, bitcopy the old array into the new array, and free
69  // the old array. No destructors are called.
70  if (m_size != m_cap)
71  return;
72  expand();
73  }
74 
75  /*** Simple getter to determine the array size */
76  TM_INLINE unsigned long size() const { return m_size; }
77 
78  /*** iterator interface, just use a basic pointer */
79  typedef T* iterator;
80 
81  /*** iterator to the start of the array */
82  TM_INLINE iterator begin() const { return m_elements; }
83 
84  /*** iterator to the end of the array */
85  TM_INLINE iterator end() const { return m_elements + m_size; }
86 
87  }; // class MiniVector
88 
89 } // stm
90 
91 #endif // MINIVECTOR_HPP__
MiniVector(const unsigned long capacity)
Definition: MiniVector.hpp:42
~MiniVector()
Definition: MiniVector.hpp:49
Definition: stm_fraser.c:61
void expand()
Definition: types.cpp:42
TM_INLINE void insert(T data)
Definition: MiniVector.hpp:55
TM_INLINE void reset()
Definition: MiniVector.hpp:52
TM_INLINE iterator begin() const
Definition: MiniVector.hpp:82
#define TM_INLINE
Definition: platform.hpp:77
Definition: MiniVector.hpp:30
T * iterator
Definition: MiniVector.hpp:79
unsigned long m_cap
Definition: MiniVector.hpp:32
TM_INLINE unsigned long size() const
Definition: MiniVector.hpp:76
unsigned long m_size
Definition: MiniVector.hpp:33
T * m_elements
Definition: MiniVector.hpp:34
TM_INLINE iterator end() const
Definition: MiniVector.hpp:85
Definition: data.h:80