1 /*************************************************************************
3 * Copyright 2016 Realm Inc.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 **************************************************************************/
19 #ifndef REALM_ARRAY_BASIC_HPP
20 #define REALM_ARRAY_BASIC_HPP
22 #include <realm/array.hpp>
26 /// A BasicArray can currently only be used for simple unstructured
27 /// types like float, double.
29 class BasicArray : public Array {
31 explicit BasicArray(Allocator&) noexcept;
32 ~BasicArray() noexcept override
36 // Disable copying, this is not allowed.
37 BasicArray& operator=(const BasicArray&) = delete;
38 BasicArray(const BasicArray&) = delete;
40 T get(size_t ndx) const noexcept;
41 bool is_null(size_t ndx) const noexcept;
43 void set(size_t ndx, T value);
44 void set_null(size_t ndx);
45 void insert(size_t ndx, T value);
46 void erase(size_t ndx);
47 void truncate(size_t size);
50 size_t find_first(T value, size_t begin = 0, size_t end = npos) const;
51 void find_all(IntegerColumn* result, T value, size_t add_offset = 0, size_t begin = 0, size_t end = npos) const;
53 size_t count(T value, size_t begin = 0, size_t end = npos) const;
54 bool maximum(T& result, size_t begin = 0, size_t end = npos) const;
55 bool minimum(T& result, size_t begin = 0, size_t end = npos) const;
57 /// Compare two arrays for equality.
58 bool compare(const BasicArray<T>&) const;
60 /// Get the specified element without the cost of constructing an
61 /// array instance. If an array instance is already available, or
62 /// you need to get multiple values, then this method will be
64 static T get(const char* header, size_t ndx) noexcept;
66 ref_type bptree_leaf_insert(size_t ndx, T, TreeInsertBase& state);
68 size_t lower_bound(T value) const noexcept;
69 size_t upper_bound(T value) const noexcept;
71 /// Construct a basic array of the specified size and return just
72 /// the reference to the underlying memory. All elements will be
73 /// initialized to `T()`.
74 static MemRef create_array(size_t size, Allocator&);
76 static MemRef create_array(Array::Type leaf_type, bool context_flag, size_t size, T value, Allocator&);
78 /// Create a new empty array and attach this accessor to it. This
79 /// does not modify the parent reference information of this
82 /// Note that the caller assumes ownership of the allocated
83 /// underlying node. It is not owned by the accessor.
84 void create(Array::Type = type_Normal, bool context_flag = false);
86 /// Construct a copy of the specified slice of this basic array
87 /// using the specified target allocator.
88 MemRef slice(size_t offset, size_t size, Allocator& target_alloc) const;
89 MemRef slice_and_clone_children(size_t offset, size_t size, Allocator& target_alloc) const;
92 void to_dot(std::ostream&, StringData title = StringData()) const;
96 size_t find(T target, size_t begin, size_t end) const;
98 size_t calc_byte_len(size_t count, size_t width) const override;
99 virtual size_t calc_item_count(size_t bytes, size_t width) const noexcept override;
101 template <bool find_max>
102 bool minmax(T& result, size_t begin, size_t end) const;
104 /// Calculate the total number of bytes needed for a basic array
105 /// with the specified number of elements. This includes the size
106 /// of the header. The result will be upwards aligned to the
107 /// closest 8-byte boundary.
108 static size_t calc_aligned_byte_size(size_t size);
112 // Class typedefs for BasicArray's: ArrayFloat and ArrayDouble
113 typedef BasicArray<float> ArrayFloat;
114 typedef BasicArray<double> ArrayDouble;
118 #include <realm/array_basic_tpl.hpp>
120 #endif // REALM_ARRAY_BASIC_HPP