am eff9be9c: Merge "Implement IntArrayView.copyToBuffer()."

* commit 'eff9be9cd63ed209c2bee833e6caf10d8f127aed':
  Implement IntArrayView.copyToBuffer().
main
Keisuke Kuroyanagi 2014-09-11 07:17:00 +00:00 committed by Android Git Automerger
commit 00fd1d6a30
2 changed files with 30 additions and 1 deletions

View File

@ -17,8 +17,9 @@
#ifndef LATINIME_INT_ARRAY_VIEW_H #ifndef LATINIME_INT_ARRAY_VIEW_H
#define LATINIME_INT_ARRAY_VIEW_H #define LATINIME_INT_ARRAY_VIEW_H
#include <array>
#include <cstdint> #include <cstdint>
#include <cstdlib> #include <cstring>
#include <vector> #include <vector>
#include "defines.h" #include "defines.h"
@ -103,6 +104,12 @@ class IntArrayView {
return IntArrayView(mPtr + n, mSize - n); return IntArrayView(mPtr + n, mSize - n);
} }
template <size_t N>
void copyToArray(std::array<int, N> *const buffer, const size_t offset) const {
ASSERT(mSize + offset <= N);
memmove(buffer->data() + offset, mPtr, sizeof(int) * mSize);
}
private: private:
DISALLOW_ASSIGNMENT_OPERATOR(IntArrayView); DISALLOW_ASSIGNMENT_OPERATOR(IntArrayView);

View File

@ -18,6 +18,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <array>
#include <vector> #include <vector>
namespace latinime { namespace latinime {
@ -89,5 +90,26 @@ TEST(IntArrayViewTest, TestSkip) {
} }
} }
TEST(IntArrayViewTest, TestCopyToArray) {
// "{{" to suppress warning.
std::array<int, 7> buffer = {{10, 20, 30, 40, 50, 60, 70}};
const std::vector<int> intVector = {3, 2, 1, 0, -1, -2};
IntArrayView intArrayView(intVector);
intArrayView.limit(0).copyToArray(&buffer, 0);
EXPECT_EQ(10, buffer[0]);
EXPECT_EQ(20, buffer[1]);
intArrayView.limit(1).copyToArray(&buffer, 0);
EXPECT_EQ(intVector[0], buffer[0]);
EXPECT_EQ(20, buffer[1]);
intArrayView.limit(1).copyToArray(&buffer, 1);
EXPECT_EQ(intVector[0], buffer[0]);
EXPECT_EQ(intVector[0], buffer[1]);
intArrayView.copyToArray(&buffer, 0);
for (size_t i = 0; i < intArrayView.size(); ++i) {
EXPECT_EQ(intVector[i], buffer[i]);
}
EXPECT_EQ(70, buffer[6]);
}
} // namespace } // namespace
} // namespace latinime } // namespace latinime