array_t.h
1 //File: $Id$
2 // Author: K. John Wu <John.Wu at acm.org>
3 // Lawrence Berkeley National Laboratory
4 // Copyright (c) 2000-2016 University of California
5 #ifndef IBIS_ARRAY_T_H
6 #define IBIS_ARRAY_T_H
7 #include "fileManager.h"
8 #include "horometer.h"
9 #include <cstddef> // ptrdiff_t
10 
21 // #ifdef __GNUC__
22 // #pragma interface
23 // #endif
24 template<class T> class ibis::array_t {
25 public:
26  typedef T* iterator;
27  typedef const T* const_iterator;
28  typedef T* pointer;
29  typedef const T* const_pointer;
30  typedef T& reference;
31  typedef const T& const_reference;
32  typedef T value_type;
33  typedef size_t size_type;
34  typedef std::ptrdiff_t difference_type;
35 
36  // constructor and destructor
37  ~array_t<T>() {freeMemory();}
38  array_t<T>();
39  explicit array_t<T>(size_t n); // donot convert integer to array_t
40  array_t<T>(size_t n, const T& val);
41  array_t<T>(const array_t<T>& rhs);
42  array_t<T>(const std::vector<T>& rhs);
43  array_t<T>(const array_t<T>& rhs, const size_t begin,
44  const size_t end=0);
47  const size_t start, const size_t end);
48  array_t<T>(const int fdes, const off_t begin, const off_t end);
49  array_t<T>(const char *fn, const off_t begin, const off_t end);
50  array_t<T>(const char *fn, const int fdes,
51  const off_t begin, const off_t end);
52  array_t<T>(T* addr, size_t nelm);
53 
54  array_t<T>& operator=(const array_t<T>& rhs);
55  void copy(const array_t<T>& rhs);
56  void deepCopy(const array_t<T>& rhs);
57 
58  // functions for the iterators
59  T* begin() {return m_begin;};
60  T* end() {return m_end;};
61  T& front() {return *m_begin;};
62  T& back() {return m_end[-1];};
63  const T* begin() const {return m_begin;};
64  const T* end() const {return m_end;};
65  const T& front() const {return *m_begin;};
66  const T& back() const {return m_end[-1];};
67 
68  bool empty() const {return (m_begin == 0 || m_begin >= m_end);};
69  size_t size() const {
70  return (m_begin > 0 && m_end > m_begin ? m_end - m_begin : 0);
71  };
72  inline void clear();
73 
75  void pop_back() {--m_end;}
76  void resize(size_t n);
77  void reserve(size_t n);
78  void truncate(size_t keep, size_t start);
79  size_t capacity() const;
80  inline void swap(array_t<T>& rhs);
81  inline void push_back(const T& elm);
82 
83  void deduplicate();
84  void sort(array_t<uint32_t> &ind) const;
85  void topk(uint32_t k, array_t<uint32_t> &ind) const;
86  void bottomk(uint32_t k, array_t<uint32_t> &ind) const;
87  size_t find(const T&) const;
88  size_t find_upper(const T&) const;
89  uint32_t find(const array_t<uint32_t>&, const T&) const;
90  void stableSort(array_t<T>& tmp);
91  void stableSort(array_t<uint32_t>& ind) const;
92  void stableSort(array_t<uint32_t>& ind, array_t<T>& sorted) const;
93  static void stableSort(array_t<T>& val, array_t<uint32_t>& ind,
94  array_t<T>& tmp, array_t<uint32_t>& itmp);
95 
96  bool isSorted() const;
97  bool isSorted(const array_t<uint32_t>&) const;
98  bool equal_to(const array_t<T>&) const;
99 
101  const T& operator[](size_t i) const {return m_begin[i];}
106  T& operator[](size_t i) {return m_begin[i];};
107  void nosharing();
109  bool incore() const {return(actual == 0 || actual->filename() == 0);}
110 
111  iterator insert(iterator pos, const T& val);
112  void insert(iterator p, size_t n, const T& val);
113  void insert(iterator p, const_iterator i, const_iterator j);
114 
115  // erase one value or a range of values
116  iterator erase(iterator p);
117  iterator erase(iterator i, iterator j);
118 
119  // the IO functions
120  void read(const char*);
121  off_t read(const char*, const off_t, const off_t);
122  off_t read(const int, const off_t, const off_t);
123  int write(const char*) const;
124  int write(FILE* fptr) const;
125 
126  // print internal pointer addresses
127  void printStatus(std::ostream& out) const;
128  void print(std::ostream& out) const;
129 
130  T* release();
131 
140 
141 private:
150  T* m_begin;
153  T* m_end;
154 
155  void freeMemory();
156 
158  uint32_t partition(array_t<uint32_t>& ind, uint32_t front,
159  uint32_t back) const;
161  void isort(array_t<uint32_t>& ind, uint32_t front, uint32_t back) const;
163  void hsort(array_t<uint32_t>& ind, uint32_t front, uint32_t back) const;
165  void qsort(array_t<uint32_t>& ind, uint32_t front, uint32_t back,
166  uint32_t lvl=0) const;
167 }; // ibis::array_t
168 
170 template<class T>
171 inline void ibis::array_t<T>::clear() {
172 #if defined(DEBUG) || defined(_DEBUG)
173  LOGGER(ibis::gVerbose > 10)
174  << "array_t::clear -- (" << static_cast<const void*>(this) << ", "
175  << static_cast<const void*>(m_begin) << ") resets m_end from "
176  << static_cast<const void*>(m_end) << " to "
177  << static_cast<const void*>(m_begin);
178 #endif
179  m_end = m_begin;
180 } // ibis::array_t<T>::clear
181 
183 template<class T>
185  ibis::fileManager::storage *a = rhs.actual;
186  rhs.actual = actual;
187  actual = a;
188  T* b = rhs.m_begin;
189  rhs.m_begin = m_begin;
190  m_begin = b;
191  T* e = rhs.m_end;
192  rhs.m_end = m_end;
193  m_end = e;
194 } // ibis::array_t<T>::swap
195 
203 template<class T>
204 inline void ibis::array_t<T>::push_back(const T& elm) {
205  if (actual != 0 && actual->filename() == 0 &&
206  (char*)(m_end+1) <= actual->end()) {
207  // simply add the value
208  *m_end = elm;
209  ++ m_end;
210  }
211  else { // need space
212  const difference_type nold = (m_end > m_begin ? m_end - m_begin : 0);
213  if (nold > 0x7FFFFFFE) {
214  throw "array_t must have less than 2^31 elements";
215  }
216  if (actual == 0 || actual->filename() != 0 ||
217  (const T*)actual->end() <= m_end) {
218  size_t nnew = (nold >= 7 ? nold : 7) + nold;
219  if (nnew > 0x7FFFFFFFU)
220  nnew = 0x7FFFFFFFU;
221  reserve(nnew);
222  }
223  if (actual != 0 && actual->filename() == 0 &&
224  (char*)(m_end+1) <= actual->end()) {
225  // simply add the value
226  *m_end = elm;
227  ++ m_end;
228  }
229  else { // the function reserve has failed
230  throw "array_t failed to acquire the necessary memory space";
231  }
232  }
233 } // ibis::array_t<T>::push_back
234 
236 template <class T>
237 inline size_t ibis::array_t<T>::capacity() const {
238  return (actual!=0 ? (const T*)actual->end()-m_begin :
239  m_end>=m_begin ? m_end-m_begin : 0);
240 } // ibis::array_t<T>::capacity
241 #endif // IBIS_ARRAY_T_H
std::ptrdiff_t difference_type
!< For array size.
Definition: array_t.h:34
Defines a simple file manager.
T * pointer
!< Const iterator type.
Definition: array_t.h:28
size_t size() const
Definition: array_t.h:69
const T & const_reference
!< Reference to a value.
Definition: array_t.h:31
bool equal_to(const array_t< T > &) const
Does this array have the same content as the other? Return true is yes, otherwise false...
Definition: array_t.cpp:820
void clear()
Reset the size to zero.
Definition: array_t.h:171
void sort(array_t< uint32_t > &ind) const
Produce index for ascending order.
Definition: array_t.cpp:944
The storage class treats all memory as char*.
Definition: fileManager.h:237
void nosharing()
Make a non-shared copy of the array.
Definition: array_t.cpp:423
void swap(array_t< T > &rhs)
Swap the content of two array_t objects.
Definition: array_t.h:184
void stableSort(array_t< T > &tmp)
A stable sort using the provided workspace.
Definition: array_t.cpp:564
void read(const char *)
Read an array from the name file.
Definition: array_t.cpp:1764
void printStatus(std::ostream &out) const
Print internal pointer addresses.
Definition: array_t.cpp:1870
void push_back(const T &elm)
Add one element from the back.
Definition: array_t.h:204
void truncate(size_t keep, size_t start)
Replace the current array with nnew rows.
Definition: array_t.cpp:1436
T & reference
!< Pointer to a constant value.
Definition: array_t.h:30
int write(const char *) const
Write the content of array to the named file.
Definition: array_t.cpp:1823
size_t find(const T &) const
Find the first position where the value is no less than val.
Definition: array_t.cpp:507
void copy(const array_t< T > &rhs)
The copy function. It performs a shallow copy.
Definition: array_t.cpp:365
size_t capacity() const
The maximum number of elements can be stored with the current memory.
Definition: array_t.h:237
bool isSorted() const
Verify the values are in ascending order.
Definition: array_t.cpp:831
Template array_t is a replacement of std::vector.
Definition: array_t.h:24
size_t find_upper(const T &) const
Find the first position where the value is greater than val.
Definition: array_t.cpp:537
void resize(size_t n)
Change the size of the array to have elements.
Definition: array_t.cpp:1469
void deduplicate()
Remove the duplicate values.
Definition: array_t.cpp:903
T & operator[](size_t i)
Modifiable reference to an element of the array.
Definition: array_t.h:106
const T * const_iterator
!< Iterator type.
Definition: array_t.h:27
bool incore() const
Is the content of the array solely in memory?
Definition: array_t.h:109
size_t size_type
!< Type of values.
Definition: array_t.h:33
T * release()
Release the memory under management to the caller as a raw pointer.
Definition: array_t.cpp:1560
void deepCopy(const array_t< T > &rhs)
The deep copy function.
Definition: array_t.cpp:382
void pop_back()
Remove the last element.
Definition: array_t.h:75
void topk(uint32_t k, array_t< uint32_t > &ind) const
Return the positions of the k largest elements.
Definition: array_t.cpp:985
void reserve(size_t n)
Increase the size of the array_t to have the capacity to store at least n elements.
Definition: array_t.cpp:1509
const char * filename() const
Pointer to the file name supporting this storage object.
Definition: fileManager.h:253
array_t< T > & operator=(const array_t< T > &rhs)
Assignment operator. It performs a shallow copy.
Definition: array_t.cpp:349
T value_type
!< Reference to a constant value.
Definition: array_t.h:32
void bottomk(uint32_t k, array_t< uint32_t > &ind) const
Return the positions of the k smallest elements.
Definition: array_t.cpp:1047
void print(std::ostream &out) const
Print out the content of the array to the given output stream.
Definition: array_t.cpp:1886
ibis::fileManager::storage * getStorage()
Export the actual storage object.
Definition: array_t.h:139
const T & operator[](size_t i) const
Non-modifiable reference to an element of the array.
Definition: array_t.h:101
const T * const_pointer
!< Pointer to a value.
Definition: array_t.h:29

Make It A Bit Faster
Contact us
Disclaimers
FastBit source code
FastBit mailing list archive