Skip to content

Commit a798218

Browse files
committed
genericvector: Pass parameters by reference
This fixes warnings like the following one from LGTM: This parameter of type ParamsTrainingHypothesis is 112 bytes - consider passing a pointer/reference instead. Most parameters can also get the const attribute. Signed-off-by: Stefan Weil <[email protected]>
1 parent 819c43d commit a798218

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

src/ccutil/genericvector.h

+17-17
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class GenericVector {
3939
GenericVector() {
4040
init(kDefaultVectorSize);
4141
}
42-
GenericVector(int size, T init_val) {
42+
GenericVector(int size, const T& init_val) {
4343
init(size);
4444
init_to_size(size, init_val);
4545
}
@@ -60,7 +60,7 @@ class GenericVector {
6060
void double_the_size();
6161

6262
// Resizes to size and sets all values to t.
63-
void init_to_size(int size, T t);
63+
void init_to_size(int size, const T& t);
6464
// Resizes to size without any initialization.
6565
void resize_no_init(int size) {
6666
reserve(size);
@@ -101,31 +101,31 @@ class GenericVector {
101101
// Return the index of the T object.
102102
// This method NEEDS a compare_callback to be passed to
103103
// set_compare_callback.
104-
int get_index(T object) const;
104+
int get_index(const T& object) const;
105105

106106
// Return true if T is in the array
107-
bool contains(T object) const;
107+
bool contains(const T& object) const;
108108

109109
// Return true if the index is valid
110110
T contains_index(int index) const;
111111

112112
// Push an element in the end of the array
113113
int push_back(T object);
114-
void operator+=(T t);
114+
void operator+=(const T& t);
115115

116116
// Push an element in the end of the array if the same
117117
// element is not already contained in the array.
118-
int push_back_new(T object);
118+
int push_back_new(const T& object);
119119

120120
// Push an element in the front of the array
121121
// Note: This function is O(n)
122-
int push_front(T object);
122+
int push_front(const T& object);
123123

124124
// Set the value at the given index
125-
void set(T t, int index);
125+
void set(const T& t, int index);
126126

127127
// Insert t at the given index, push other elements to the right.
128-
void insert(T t, int index);
128+
void insert(const T& t, int index);
129129

130130
// Removes an element at the given index and
131131
// shifts the remaining elements to the left.
@@ -705,7 +705,7 @@ void GenericVector<T>::double_the_size() {
705705

706706
// Resizes to size and sets all values to t.
707707
template <typename T>
708-
void GenericVector<T>::init_to_size(int size, T t) {
708+
void GenericVector<T>::init_to_size(int size, const T& t) {
709709
reserve(size);
710710
size_used_ = size;
711711
for (int i = 0; i < size; ++i)
@@ -740,7 +740,7 @@ T GenericVector<T>::pop_back() {
740740

741741
// Return the object from an index.
742742
template <typename T>
743-
void GenericVector<T>::set(T t, int index) {
743+
void GenericVector<T>::set(const T& t, int index) {
744744
assert(index >= 0 && index < size_used_);
745745
data_[index] = t;
746746
}
@@ -749,7 +749,7 @@ void GenericVector<T>::set(T t, int index) {
749749
// space for the new elements and inserts the given element
750750
// at the specified index.
751751
template <typename T>
752-
void GenericVector<T>::insert(T t, int index) {
752+
void GenericVector<T>::insert(const T& t, int index) {
753753
assert(index >= 0 && index <= size_used_);
754754
if (size_reserved_ == size_used_)
755755
double_the_size();
@@ -779,7 +779,7 @@ T GenericVector<T>::contains_index(int index) const {
779779

780780
// Return the index of the T object.
781781
template <typename T>
782-
int GenericVector<T>::get_index(T object) const {
782+
int GenericVector<T>::get_index(const T& object) const {
783783
for (int i = 0; i < size_used_; ++i) {
784784
assert(compare_cb_ != nullptr);
785785
if (compare_cb_->Run(object, data_[i]))
@@ -790,7 +790,7 @@ int GenericVector<T>::get_index(T object) const {
790790

791791
// Return true if T is in the array
792792
template <typename T>
793-
bool GenericVector<T>::contains(T object) const {
793+
bool GenericVector<T>::contains(const T& object) const {
794794
return get_index(object) != -1;
795795
}
796796

@@ -806,7 +806,7 @@ int GenericVector<T>::push_back(T object) {
806806
}
807807

808808
template <typename T>
809-
int GenericVector<T>::push_back_new(T object) {
809+
int GenericVector<T>::push_back_new(const T& object) {
810810
int index = get_index(object);
811811
if (index >= 0)
812812
return index;
@@ -815,7 +815,7 @@ int GenericVector<T>::push_back_new(T object) {
815815

816816
// Add an element in the array (front)
817817
template <typename T>
818-
int GenericVector<T>::push_front(T object) {
818+
int GenericVector<T>::push_front(const T& object) {
819819
if (size_used_ == size_reserved_)
820820
double_the_size();
821821
for (int i = size_used_; i > 0; --i)
@@ -826,7 +826,7 @@ int GenericVector<T>::push_front(T object) {
826826
}
827827

828828
template <typename T>
829-
void GenericVector<T>::operator+=(T t) {
829+
void GenericVector<T>::operator+=(const T& t) {
830830
push_back(t);
831831
}
832832

0 commit comments

Comments
 (0)