Repository navigation
Expand file tree
/
Copy patharray-ref.h
More file actions
107 lines (74 loc) · 3.08 KB
/
Copy patharray-ref.h
File metadata and controls
107 lines (74 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// tensor/array-ref.h
// Copyright 2019 Johns Hopkins University (author: Daniel Povey)
// See ../../COPYING for clarification regarding multiple authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.
#include <base/kaldi-error.h>
#include <tensor/tensor-common.h>
namespace kaldi {
namespace tensor {
/**
Similar to llvm/PyTorch's ArrayRef, this is a lightweight way to store a const
array. The data in array is not owned here; it will generally be unsafe to use
an ArrayRef as other than a local variable.
ArrayRef has only two members and it will probably make sense to pass it by
value most of the time. Its constructors via std::vector<T> and
std::initializer_list<T> will be the usual way of creating it;
*/
template <typename T>
struct ArrayRef final {
const T *data;
size_t size;
inline T& operator [] (uint64_t i) const {
KALDI_ASSERT(i < size);
return data[i];
}
constexpr ArrayRef() : size(0), data(nullptr) { }
// Construct from one element.
// Caution: this constructor allows you to evade 'const'.
constexpr ArrayRef(const T &element) : size(1), data(&element) { }
// Construct from data and size
constexpr ArrayRef(const T* data, size_t size): data(data), size(size) { }
/// Construct from a range. Caution: this constructor allows
/// you to evade 'const'.
constexpr ArrayRef(const T* begin, const T* end): data(begin), size(end - begin) { }
/// Construct from a std::vector.
ArrayRef(const std::vector<T> &vec): data(vec.data()), size(vec.size()) { }
/// Construct from a C array.
template <size_t N>
constexpr ArrayRef(const T (&data)[N]): data(data), size(N) { }
/// Construct from a std::initializer_list
constexpr ArrayRef(const std::initializer_list<T> &vec):
data(vec.data()), size(vec.size()) { }
T *begin() { return data; }
T *end() { return data + size; }
// We will add iterators later if they are needed.
};
// Converts an ArrayRef<int32> to a string, e.g. "[1 4 300]"; can implicitly
// print std::vector as well.
std::string ArrayAsString(const ArrayRef<int32> a);
/**
This template is a mechanism of keeping a collection of shared pointers, in
no particular order, with a mechanism to iterate over the list (typically
for purposes of dependency tracking, from Ops to Nodes and vice versa).
*/
template <class T, int BlockSize>
class SharedPointerCollection {
public:
private:
std::shared_ptr<T> data[BlockSize];
std::unique_ptr<SmallSharedPointerCollection<T,BlockSize> > next;
};
} // namespace tensor
} // namespace kaldi