A platform for high-performance distributed tool and library development written in C++. It can be deployed in two different cluster modes: standalone or distributed. API for v0.5.0, released on June 13, 2018.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
PDBVector.cc
Go to the documentation of this file.
1 /*****************************************************************************
2  * *
3  * Copyright 2018 Rice University *
4  * *
5  * Licensed under the Apache License, Version 2.0 (the "License"); *
6  * you may not use this file except in compliance with the License. *
7  * You may obtain a copy of the License at *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14  * See the License for the specific language governing permissions and *
15  * limitations under the License. *
16  * *
17  *****************************************************************************/
18 
19 #ifndef VECTOR_CC
20 #define VECTOR_CC
21 
22 #include <cstddef>
23 #include <iostream>
24 #include <vector>
25 #include <algorithm>
26 #include <iterator>
27 #include <type_traits>
28 #include <cstring>
29 
30 #include "Handle.h"
31 #include "Object.h"
32 #include "InterfaceFunctions.h"
33 
34 // Note: we need to write all operations in constructors, destructors, and assignment operators
35 // WITHOUT using
36 // the underlying type in any way (including assignment, initialization, destruction, size).
37 //
38 namespace pdb {
39 
40 template <class TypeContained>
41 Vector<TypeContained>::Vector(uint32_t initSize) {
42 
43  // this way, we'll allocate extra bytes on the end of the array
44  myArray = makeObjectWithExtraStorage<Array<TypeContained>>(sizeof(TypeContained) * initSize,
45  initSize);
46 }
47 
48 template <class TypeContained>
49 Vector<TypeContained>::Vector(uint32_t initSize, uint32_t usedSize) {
50 
51  // this way, we'll allocate extra bytes on the end of the array
52  // std :: cout << "sizeof(TypeContained)=" << sizeof(TypeContained) << std :: endl;
53  // std :: cout << "sizeof(Handle)=" << sizeof(Handle<Nothing>) << std :: endl;
54  // std :: cout << "sizeof(HandleBase)=" << sizeof(HandleBase) << std :: endl;
55  // std :: cout << "initSize=" << initSize << std :: endl;
56  myArray = makeObjectWithExtraStorage<Array<TypeContained>>(
57  sizeof(TypeContained) * initSize, initSize, usedSize);
58 }
59 
60 template <class TypeContained>
62 
63  myArray = makeObjectWithExtraStorage<Array<TypeContained>>(sizeof(TypeContained), 1);
64 }
65 
66 template <class TypeContained>
68  return myArray->numUsedSlots();
69 }
70 
71 template <class TypeContained>
72 TypeContained& Vector<TypeContained>::operator[](uint32_t which) {
73  return myArray->getObj(which);
74 }
75 
76 template <class TypeContained>
77 TypeContained& Vector<TypeContained>::operator[](uint32_t which) const {
78  return myArray->getObj(which);
79 }
80 
81 template <class TypeContained>
82 void Vector<TypeContained>::assign(uint32_t which, const TypeContained& val) {
83  myArray->assign(which, val);
84 }
85 
86 template <class TypeContained>
88  if (myArray->isFull()) {
89  myArray = myArray->doubleSize();
90  }
91  myArray->push_back();
92 }
93 
94 template <class TypeContained>
95 void Vector<TypeContained>::push_back(const TypeContained& val) {
96  if (myArray->isFull()) {
97  myArray = myArray->doubleSize();
98  }
99  myArray->push_back(val);
100 }
101 
102 template <class TypeContained>
104  myArray->pop_back();
105 }
106 
107 template <class TypeContained>
109  myArray = makeObjectWithExtraStorage<Array<TypeContained>>(sizeof(TypeContained), 1);
110 }
111 
112 template <class TypeContained>
113 void Vector<TypeContained>::resize(uint32_t toMe) {
114  myArray = myArray->resize(toMe);
115 }
116 
117 template <class TypeContained>
118 TypeContained* Vector<TypeContained>::c_ptr() const {
119  return myArray->c_ptr();
120 }
121 
122 // Add by Shangyu;
123 // Use std::cout to print out the elements in a Vector
124 template <class TypeContained>
126  for (uint32_t i = 0; i < this->size() - 1; i++) {
127  std::cout << (*this)[i] << ", ";
128  }
129  std::cout << (*this)[this->size() - 1] << std::endl;
130 }
131 
132 // Add by Shangyu;
133 // Fill the vector with the input val
134 template <class TypeContained>
135 void Vector<TypeContained>::fill(const TypeContained& val) {
136  // below code is inefficient due to vtable fixing overhead
137  /*
138  for (uint32_t i = 0; i < this->size(); i++) {
139  myArray->assign (i, val);
140  }
141  */
142  // optimized implementation:
143  size_t size = this->size();
144  TypeContained* myData = this->c_ptr();
145  for (uint32_t i = 0; i < size; i++) {
146  myData[i] = val;
147  }
148 }
149 }
150 
151 #endif
TypeContained * c_ptr() const
Definition: PDBVector.cc:118
void resize(uint32_t toMe)
Definition: PDBVector.cc:113
void print() const
Definition: PDBVector.cc:125
void push_back()
Definition: PDBVector.cc:87
void pop_back()
Definition: PDBVector.cc:103
void assign(uint32_t which, const TypeContained &val)
Definition: PDBVector.cc:82
TypeContained & operator[](uint32_t which)
Definition: PDBVector.cc:72
size_t size() const
Definition: PDBVector.cc:67
void clear()
Definition: PDBVector.cc:108
void fill(const TypeContained &val)
Definition: PDBVector.cc:135