123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- /* Declarations for the array type.
- This file is part of khipu.
- khipu is free software: you can redistribute it and/or modify
- it under the terms of the GNU Lesser General Public License as published by
- the Free Software Foundation; either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Lesser General Public License for more details.
- You should have received a copy of the GNU Lesser General Public License
- along with this program. If not, see <https://www.gnu.org/licenses/>. */
- #ifndef __KP_ARRAY__
- #define __KP_ARRAY__ 1
- #include "interp.hpp"
- #include "initop.hpp"
- KP_DECLS_BEGIN
- struct alignas (object) array : public varobj
- {
- static const uint32_t nonref_flag = 1u << 15;
- static const int code = typecode::ARRAY;
- uint32_t len;
- object *data;
- static array* alloc_raw (uint32_t nelems);
- // Needed by the raw_acc interface.
- uint32_t& len_ref ()
- {
- return (this->len);
- }
- object* data_ptr ()
- {
- return (this->data);
- }
- void local_init (object *data = nullptr, uint32_t len = 0)
- {
- this->data = data;
- this->len = len;
- }
- struct iterator
- {
- valref value;
- object *curr;
- object *last;
- inline iterator (interpreter *interp, object ax, uint32_t i = 0);
- iterator (interpreter *interp, const iterator& right) :
- value (interp, *right.value), curr (right.curr), last (right.curr)
- {
- }
- bool valid () const
- {
- return (this->curr < this->last);
- }
- object operator* () const
- {
- return (*this->value);
- }
- iterator& operator++ ()
- {
- ++this->curr;
- if (this->valid ())
- *this->value = *this->curr;
- return (*this);
- }
- iterator operator++ (int)
- {
- iterator ret { interpreter::self (), *this };
- ++*this;
- return (ret);
- }
- };
- };
- inline array* as_array (object obj)
- {
- return ((array *)unmask (obj));
- }
- #ifdef KP_ARCH_WIDE
- inline constexpr bool array_p (object obj)
- {
- return (itype (obj) == typecode::ARRAY);
- }
- #else
- inline bool array_p (object obj)
- {
- return (varobj_p (obj) && as_varobj(obj)->vo_type == typecode::ARRAY);
- }
- #endif
- template <typename T>
- inline object& xaref (object obj, T idx)
- {
- return (as_array(obj)->data[idx]);
- }
- inline uint32_t
- len_a (object obj)
- {
- return (as_array(obj)->len);
- }
- array::iterator::iterator (interpreter *interp, object ax, uint32_t i) :
- value (interp, UNBOUND), curr (&xaref(ax, i)), last (&xaref(ax, len_a (ax)))
- {
- if (len_a (ax))
- *this->value = *this->curr;
- }
- // Allocate an array of length NELEM, filling it with FILL.
- KP_EXPORT result<object> alloc_array (interpreter *interp, uint32_t nelem,
- object fill = UNBOUND);
- // Add 2 arrays.
- KP_EXPORT result<object> add_aa (interpreter *interp, object a1, object a2);
- // Concatenate ARGC arrays from ARGV.
- KP_EXPORT result<object> concat_a (interpreter *interp,
- object *argv, int argc);
- // Multiply an array by an integer.
- KP_EXPORT result<object> mul_ia (interpreter *interp,
- object ival, object array);
- // Index an array.
- KP_EXPORT result<object> get_a (interpreter *interp,
- object array, object idx, object dfl);
- // Destructively set an object inside an array.
- KP_EXPORT result<object> nput_a (interpreter *interp,
- object array, object idx, object val);
- // Compute the hashcode of an array.
- KP_EXPORT result<uint32_t> hash_a (interpreter *interp, object obj);
- // Get the subsequence of an array.
- KP_EXPORT result<object> subseq_a (interpreter *interp,
- object array, object i1, object i2);
- // Copy an array.
- KP_EXPORT result<object> copy_a (interpreter *interp, object obj, bool deep);
- // Reverse an array.
- KP_EXPORT result<object> reverse_a (interpreter *interp, object obj);
- // Destructively reverse an array.
- KP_EXPORT result<object> nreverse_a (interpreter *interp, object obj);
- // Destructively sort an array.
- KP_EXPORT result<object> nsort_a (interpreter *interp,
- object obj, comparator& c);
- // Test for array equality.
- KP_EXPORT result<bool> eq_aa (interpreter *interp, object a1, object a2);
- // Compare 2 arrays.
- KP_EXPORT result<int> cmp_aa (interpreter *interp, object a1, object a2);
- // Iterator interface for an array.
- KP_EXPORT result<object> iter_a (interpreter *interp,
- object obj, object token, bool adv);
- // Mutate an object inside the array
- KP_EXPORT result<object> nzap_a (interpreter *interp, object obj, object key,
- uint32_t flags, object fn,
- object *argv, int argc);
- // Return the last element of an array.
- KP_EXPORT result<object> last_a (interpreter *interp, object obj);
- // Find an element in an array.
- KP_EXPORT result<object> find_a (interpreter *interp, object obj,
- object key, object start,
- object end, object test);
- // Write an array to a stream.
- KP_EXPORT result<int64_t> write_a (interpreter *interp,
- stream *strm, object obj, io_info& info);
- // Serialize an array in a stream.
- KP_EXPORT result<int64_t> pack_a (interpreter *interp,
- stream *strm, object obj, pack_info& info);
- // Deserialize an array from a stream.
- KP_EXPORT result<object> unpack_a (interpreter *interp,
- stream *strm, pack_info& info, bool save);
- // Init OP for arrays.
- KP_EXPORT init_op init_array;
- KP_DECLS_END
- #endif
|