@@ -39,7 +39,7 @@ class GenericVector {
39
39
GenericVector () {
40
40
init (kDefaultVectorSize );
41
41
}
42
- GenericVector (int size, T init_val) {
42
+ GenericVector (int size, const T& init_val) {
43
43
init (size);
44
44
init_to_size (size, init_val);
45
45
}
@@ -60,7 +60,7 @@ class GenericVector {
60
60
void double_the_size ();
61
61
62
62
// 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);
64
64
// Resizes to size without any initialization.
65
65
void resize_no_init (int size) {
66
66
reserve (size);
@@ -101,31 +101,31 @@ class GenericVector {
101
101
// Return the index of the T object.
102
102
// This method NEEDS a compare_callback to be passed to
103
103
// set_compare_callback.
104
- int get_index (T object) const ;
104
+ int get_index (const T& object) const ;
105
105
106
106
// Return true if T is in the array
107
- bool contains (T object) const ;
107
+ bool contains (const T& object) const ;
108
108
109
109
// Return true if the index is valid
110
110
T contains_index (int index) const ;
111
111
112
112
// Push an element in the end of the array
113
113
int push_back (T object);
114
- void operator +=(T t);
114
+ void operator +=(const T& t);
115
115
116
116
// Push an element in the end of the array if the same
117
117
// element is not already contained in the array.
118
- int push_back_new (T object);
118
+ int push_back_new (const T& object);
119
119
120
120
// Push an element in the front of the array
121
121
// Note: This function is O(n)
122
- int push_front (T object);
122
+ int push_front (const T& object);
123
123
124
124
// Set the value at the given index
125
- void set (T t, int index);
125
+ void set (const T& t, int index);
126
126
127
127
// 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);
129
129
130
130
// Removes an element at the given index and
131
131
// shifts the remaining elements to the left.
@@ -705,7 +705,7 @@ void GenericVector<T>::double_the_size() {
705
705
706
706
// Resizes to size and sets all values to t.
707
707
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) {
709
709
reserve (size);
710
710
size_used_ = size;
711
711
for (int i = 0 ; i < size; ++i)
@@ -740,7 +740,7 @@ T GenericVector<T>::pop_back() {
740
740
741
741
// Return the object from an index.
742
742
template <typename T>
743
- void GenericVector<T>::set(T t, int index) {
743
+ void GenericVector<T>::set(const T& t, int index) {
744
744
assert (index >= 0 && index < size_used_);
745
745
data_[index ] = t;
746
746
}
@@ -749,7 +749,7 @@ void GenericVector<T>::set(T t, int index) {
749
749
// space for the new elements and inserts the given element
750
750
// at the specified index.
751
751
template <typename T>
752
- void GenericVector<T>::insert(T t, int index) {
752
+ void GenericVector<T>::insert(const T& t, int index) {
753
753
assert (index >= 0 && index <= size_used_);
754
754
if (size_reserved_ == size_used_)
755
755
double_the_size ();
@@ -779,7 +779,7 @@ T GenericVector<T>::contains_index(int index) const {
779
779
780
780
// Return the index of the T object.
781
781
template <typename T>
782
- int GenericVector<T>::get_index(T object) const {
782
+ int GenericVector<T>::get_index(const T& object) const {
783
783
for (int i = 0 ; i < size_used_; ++i) {
784
784
assert (compare_cb_ != nullptr );
785
785
if (compare_cb_->Run (object, data_[i]))
@@ -790,7 +790,7 @@ int GenericVector<T>::get_index(T object) const {
790
790
791
791
// Return true if T is in the array
792
792
template <typename T>
793
- bool GenericVector<T>::contains(T object) const {
793
+ bool GenericVector<T>::contains(const T& object) const {
794
794
return get_index (object) != -1 ;
795
795
}
796
796
@@ -806,7 +806,7 @@ int GenericVector<T>::push_back(T object) {
806
806
}
807
807
808
808
template <typename T>
809
- int GenericVector<T>::push_back_new(T object) {
809
+ int GenericVector<T>::push_back_new(const T& object) {
810
810
int index = get_index (object);
811
811
if (index >= 0 )
812
812
return index ;
@@ -815,7 +815,7 @@ int GenericVector<T>::push_back_new(T object) {
815
815
816
816
// Add an element in the array (front)
817
817
template <typename T>
818
- int GenericVector<T>::push_front(T object) {
818
+ int GenericVector<T>::push_front(const T& object) {
819
819
if (size_used_ == size_reserved_)
820
820
double_the_size ();
821
821
for (int i = size_used_; i > 0 ; --i)
@@ -826,7 +826,7 @@ int GenericVector<T>::push_front(T object) {
826
826
}
827
827
828
828
template <typename T>
829
- void GenericVector<T>::operator +=(T t) {
829
+ void GenericVector<T>::operator +=(const T& t) {
830
830
push_back (t);
831
831
}
832
832
0 commit comments