MCPcopy Create free account
hub / github.com/apache/madlib / AvgVectorState

Class AvgVectorState

src/modules/linalg/average.cpp:31–95  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

29 */
30template <class Handle>
31class AvgVectorState {
32 template <class OtherHandle>
33 friend class AvgVectorState;
34
35public:
36 AvgVectorState(const AnyType &inArray)
37 : mStorage(inArray.getAs<Handle>()) {
38
39 rebind(static_cast<uint32_t>(mStorage[1]));
40 }
41
42 inline operator AnyType() const {
43 return mStorage;
44 }
45
46 inline void initialize(const Allocator &inAllocator,
47 uint32_t inNumDimensions) {
48
49 mStorage = inAllocator.allocateArray<double, dbal::AggregateContext,
50 dbal::DoZero, dbal::ThrowBadAlloc>(arraySize(inNumDimensions));
51 rebind(inNumDimensions);
52 numDimensions = inNumDimensions;
53 }
54
55 template <class OtherHandle>
56 AvgVectorState &operator+=(
57 const AvgVectorState<OtherHandle> &inOtherState) {
58
59 numRows += inOtherState.numRows;
60 sumOfVectors += inOtherState.sumOfVectors;
61 return *this;
62 }
63
64private:
65 static inline size_t arraySize(uint32_t inNumDimensions) {
66 return static_cast<size_t>(2 + inNumDimensions);
67 }
68
69 /**
70 * @brief Rebind to a new storage array
71 *
72 * @param inNumDimensions The number of dimensions
73 *
74 * Array layout (iteration refers to one aggregate-function call):
75 * Inter-iteration components (updated in final function):
76 * - 0: numRows (number of rows already processed in this iteration)
77 * - 1: numDimensions (dimension of space that points are from)
78 * - 2: sumOfPoints (vector with \c numDimensions rows)
79 */
80 void rebind(uint32_t inNumDimensions) {
81 numRows.rebind(&mStorage[0]);
82 numDimensions.rebind(&mStorage[1]);
83 sumOfVectors.rebind(&mStorage[2], inNumDimensions);
84 madlib_assert(mStorage.size() >= arraySize(inNumDimensions),
85 std::runtime_error("Out-of-bounds array access detected."));
86 }
87
88 Handle mStorage;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected