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 "defines.h"
25 
26 namespace latinime {
27 
28 /**
29  * Helper class used to provide a read-only view of a given range of integer array. This class
30  * does not take ownership of the underlying integer array but is designed to be a lightweight
31  * object that obeys value semantics.
32  *
33  * Example:
34  * <code>
35  * bool constinsX(IntArrayView view) {
36  *     for (size_t i = 0; i < view.size(); ++i) {
37  *         if (view[i] == 'X') {
38  *             return true;
39  *         }
40  *     }
41  *     return false;
42  * }
43  *
44  * const int codePointArray[] = { 'A', 'B', 'X', 'Z' };
45  * auto view = IntArrayView(codePointArray, NELEMS(codePointArray));
46  * const bool hasX = constinsX(view);
47  * </code>
48  */
49 class IntArrayView {
50  public:
IntArrayView()51     IntArrayView() : mPtr(nullptr), mSize(0) {}
52 
IntArrayView(const int * const ptr,const size_t size)53     IntArrayView(const int *const ptr, const size_t size)
54             : mPtr(ptr), mSize(size) {}
55 
IntArrayView(const std::vector<int> & vector)56     explicit IntArrayView(const std::vector<int> &vector)
57             : mPtr(vector.data()), mSize(vector.size()) {}
58 
59     template <int N>
fromFixedSizeArray(const int (& array)[N])60     AK_FORCE_INLINE static IntArrayView fromFixedSizeArray(const int (&array)[N]) {
61         return IntArrayView(array, N);
62     }
63 
64     // Returns a view that points one int object. Does not take ownership of the given object.
fromObject(const int * const object)65     AK_FORCE_INLINE static IntArrayView fromObject(const int *const object) {
66         return IntArrayView(object, 1);
67     }
68 
69     AK_FORCE_INLINE int operator[](const size_t index) const {
70         ASSERT(index < mSize);
71         return mPtr[index];
72     }
73 
empty()74     AK_FORCE_INLINE bool empty() const {
75         return size() == 0;
76     }
77 
size()78     AK_FORCE_INLINE size_t size() const {
79         return mSize;
80     }
81 
data()82     AK_FORCE_INLINE const int *data() const {
83         return mPtr;
84     }
85 
begin()86     AK_FORCE_INLINE const int *begin() const {
87         return mPtr;
88     }
89 
end()90     AK_FORCE_INLINE const int *end() const {
91         return mPtr + mSize;
92     }
93 
94  private:
95     DISALLOW_ASSIGNMENT_OPERATOR(IntArrayView);
96 
97     const int *const mPtr;
98     const size_t mSize;
99 };
100 
101 using WordIdArrayView = IntArrayView;
102 using PtNodePosArrayView = IntArrayView;
103 
104 } // namespace latinime
105 #endif // LATINIME_MEMORY_VIEW_H
106