Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Side by Side Diff: third_party/prediction/utils/int_array_view.h

Issue 1247903003: Add spellcheck and word suggestion to the prediction service (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef LATINIME_INT_ARRAY_VIEW_H
18 #define LATINIME_INT_ARRAY_VIEW_H
19
20 #include <cstdint>
21 #include <cstdlib>
22 #include <vector>
23
24 #include "third_party/prediction/defines.h"
25
26 namespace latinime {
27
28 /**
29 * Helper class used to provide a read-only view of a given range of integer
30 *array. This class
31 * does not take ownership of the underlying integer array but is designed to be
32 *a lightweight
33 * object that obeys value semantics.
34 *
35 * Example:
36 * <code>
37 * bool constinsX(IntArrayView view) {
38 * for (size_t i = 0; i < view.size(); ++i) {
39 * if (view[i] == 'X') {
40 * return true;
41 * }
42 * }
43 * return false;
44 * }
45 *
46 * const int codePointArray[] = { 'A', 'B', 'X', 'Z' };
47 * auto view = IntArrayView(codePointArray, NELEMS(codePointArray));
48 * const bool hasX = constinsX(view);
49 * </code>
50 */
51 class IntArrayView {
52 public:
53 IntArrayView() : mPtr(nullptr), mSize(0) {}
54
55 IntArrayView(const int* const ptr, const size_t size)
56 : mPtr(ptr), mSize(size) {}
57
58 explicit IntArrayView(const std::vector<int>& vector)
59 : mPtr(vector.data()), mSize(vector.size()) {}
60
61 template <int N>
62 AK_FORCE_INLINE static IntArrayView fromFixedSizeArray(const int(&array)[N]) {
63 return IntArrayView(array, N);
64 }
65
66 // Returns a view that points one int object. Does not take ownership of the
67 // given object.
68 AK_FORCE_INLINE static IntArrayView fromObject(const int* const object) {
69 return IntArrayView(object, 1);
70 }
71
72 AK_FORCE_INLINE int operator[](const size_t index) const {
73 ASSERT(index < mSize);
74 return mPtr[index];
75 }
76
77 AK_FORCE_INLINE bool empty() const { return size() == 0; }
78
79 AK_FORCE_INLINE size_t size() const { return mSize; }
80
81 AK_FORCE_INLINE const int* data() const { return mPtr; }
82
83 AK_FORCE_INLINE const int* begin() const { return mPtr; }
84
85 AK_FORCE_INLINE const int* end() const { return mPtr + mSize; }
86
87 private:
88 DISALLOW_ASSIGNMENT_OPERATOR(IntArrayView);
89
90 const int* const mPtr;
91 const size_t mSize;
92 };
93
94 using WordIdArrayView = IntArrayView;
95 using PtNodePosArrayView = IntArrayView;
96
97 } // namespace latinime
98 #endif // LATINIME_MEMORY_VIEW_H
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698