|
| 1 | +/** |
| 2 | + * Copyright (C) 2015 Dato, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This software may be modified and distributed under the terms |
| 6 | + * of the BSD license. See the LICENSE file for details. |
| 7 | + */ |
| 8 | +#ifndef GRAPHLAB_UNITY_SARRAY_BUILDER_HPP |
| 9 | +#define GRAPHLAB_UNITY_SARRAY_BUILDER_HPP |
| 10 | + |
| 11 | +#include <vector> |
| 12 | +#include <sframe/sarray.hpp> |
| 13 | +#include <boost/circular_buffer.hpp> |
| 14 | +#include <unity/lib/api/unity_sarray_builder_interface.hpp> |
| 15 | + |
| 16 | +namespace graphlab { |
| 17 | + |
| 18 | +// forward declarations |
| 19 | +template <typename T> |
| 20 | +class sarray; |
| 21 | + |
| 22 | +/** |
| 23 | + * Provides a Python interface to incrementally build an SArray. |
| 24 | + * |
| 25 | + * Unlike most other unity objects, this is not a wrapper of another |
| 26 | + * "sarray_builder" class, but provides the implementation. This is because it |
| 27 | + * is a slightly embellished wrapper around the SArray's output iterator, so |
| 28 | + * there is no further functionality that needs to be available for the C++ |
| 29 | + * side. |
| 30 | + * |
| 31 | + * The unity_sarray_builder is designed to append values until \ref close is |
| 32 | + * called, which returns the SArray. No "reopening" is allowed, and no |
| 33 | + * operations in that instance of unity_sarray_builder will work after close is |
| 34 | + * called. |
| 35 | + */ |
| 36 | +class unity_sarray_builder: public unity_sarray_builder_base { |
| 37 | + public: |
| 38 | + /** |
| 39 | + * Default constructor. Does nothing |
| 40 | + */ |
| 41 | + unity_sarray_builder() {} |
| 42 | + |
| 43 | + /** |
| 44 | + * Initialize the unity_sarray_buidler. |
| 45 | + * |
| 46 | + * This essentially opens the output iterator for writing. |
| 47 | + * |
| 48 | + */ |
| 49 | + void init(size_t num_segments, size_t history_size, flex_type_enum dtype); |
| 50 | + |
| 51 | + /** |
| 52 | + * Add a single flexible_type value to the SArray. |
| 53 | + * |
| 54 | + * The segment number allows the user to use the parallel interface provided |
| 55 | + * by the underlying output_iterator. |
| 56 | + * |
| 57 | + * Throws if: |
| 58 | + * - init hasn't been called or close has been called |
| 59 | + * - segment number is invalid |
| 60 | + * - the type of \p val differs from the type given in \ref init |
| 61 | + * |
| 62 | + */ |
| 63 | + void append(const flexible_type &val, size_t segment); |
| 64 | + |
| 65 | + /** |
| 66 | + * A wrapper around \ref append which adds multiple flexible_types to SArray. |
| 67 | + * |
| 68 | + * Throws if: |
| 69 | + * - init hasn't been called or close has been called |
| 70 | + * - segment number is invalid |
| 71 | + * - the type of any values in \p vals differs from |
| 72 | + * the type given in \ref init |
| 73 | + */ |
| 74 | + void append_multiple(const std::vector<flexible_type> &vals, size_t segment); |
| 75 | + |
| 76 | + /** |
| 77 | + * Return the current type of the SArray. |
| 78 | + */ |
| 79 | + flex_type_enum get_type(); |
| 80 | + |
| 81 | + /** |
| 82 | + * Return the last \p num_elems elements appended. |
| 83 | + */ |
| 84 | + std::vector<flexible_type> read_history(size_t num_elems, size_t segment); |
| 85 | + |
| 86 | + /** |
| 87 | + * Finalize SArray and return it. |
| 88 | + */ |
| 89 | + std::shared_ptr<unity_sarray_base> close(); |
| 90 | + |
| 91 | + unity_sarray_builder(const unity_sarray_builder&) = delete; |
| 92 | + unity_sarray_builder& operator=(const unity_sarray_builder&) = delete; |
| 93 | + private: |
| 94 | + /// Methods |
| 95 | + |
| 96 | + /// Variables |
| 97 | + bool m_inited = false; |
| 98 | + bool m_closed = false; |
| 99 | + std::shared_ptr<sarray<flexible_type>> m_sarray; |
| 100 | + std::vector<sarray<flexible_type>::iterator> m_out_iters; |
| 101 | + flex_type_enum m_given_dtype = flex_type_enum::UNDEFINED; |
| 102 | + std::set<flex_type_enum> m_types_inserted; |
| 103 | + |
| 104 | + std::vector<std::shared_ptr<boost::circular_buffer<flexible_type>>> m_history; |
| 105 | +}; |
| 106 | + |
| 107 | +} // namespace graphlab |
| 108 | +#endif // GRAPHLAB_UNITY_SARRAY_BUILDER_HPP |
0 commit comments