File tree 2 files changed +19
-0
lines changed
2 files changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -101,6 +101,19 @@ template <typename T> class Array
101
101
return index < data_.size () ? index : -1 ;
102
102
}
103
103
104
+ /* *
105
+ * Get the last index at which a given element can be found in the array
106
+ * @see https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/lastIndexOf
107
+ *
108
+ * @param item Item last index of which we want to find out.
109
+ * @returns Item's last index or -1 if item does not exist in the array.
110
+ */
111
+ ssize_t lastIndexOf (const T & item) const {
112
+ size_t lastIndex = std::find (data_.rbegin (), data_.rend (), item) - data_.rbegin ();
113
+
114
+ return data_.size () - lastIndex - 1 ;
115
+ }
116
+
104
117
/* *
105
118
* Push value to the end of array.
106
119
* @see https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/push
Original file line number Diff line number Diff line change @@ -33,6 +33,11 @@ class ArrayTest : public CppUnit::TestCase
33
33
CPPUNIT_ASSERT ( arr->indexOf (5 ) == 5 );
34
34
}
35
35
36
+ void testLastIndexOf () {
37
+ arr->push (1 );
38
+ CPPUNIT_ASSERT ( arr->lastIndexOf (1 ) == 10 );
39
+ }
40
+
36
41
void testFilter () {
37
42
using std::tr1::bind;
38
43
using std::tr1::placeholders::_1;
@@ -120,6 +125,7 @@ class ArrayTest : public CppUnit::TestCase
120
125
121
126
CPPUNIT_TEST ( testLength );
122
127
CPPUNIT_TEST ( testIndexOf );
128
+ CPPUNIT_TEST ( testLastIndexOf );
123
129
CPPUNIT_TEST ( testFilter );
124
130
CPPUNIT_TEST ( testReduce );
125
131
CPPUNIT_TEST ( testEvery );
You can’t perform that action at this time.
0 commit comments