<commit_before>#pragma once

#include "collection_functions.h"

namespace tes_api_3 {

    using namespace collections;

#define NEGATIVE_IDX_COMMENT "negative index accesses items from the end of container counting backwards."

    class tes_array : public class_meta< tes_array >, public collections::array_functions {
    public:

        typedef array::ref& ref;

        REGISTER_TES_NAME("JArray");

        void additionalSetup() {
            metaInfo.comment = "Ordered collection of values (value is float, integer, string, form or another container).\n"
                "Inherits JValue functionality";
        }

        static bool validateReadIndex(const array *obj, UInt32 index) {
            return obj && index < obj->_array.size();
        }

        static bool validateReadIndexRange(const array *obj, UInt32 begin, UInt32 end) {
            return obj && begin < end && end <= obj->_array.size();
        }

        static bool validateWriteIndex(const array *obj, UInt32 index) {
            return obj && index <= obj->_array.size();
        }

        typedef array::Index Index;

        REGISTERF(tes_object::object<array>, "object", "", kCommentObject);

        static object_base* objectWithSize(SInt32 size) {
            if (size < 0) {
                return nullptr;
            }

            auto& obj = array::objectWithInitializer([&](array &me) {
                me._array.resize(size);
            },
                tes_context::instance());

            return &obj;
        }
        REGISTERF2(objectWithSize, "size", "creates array of given size, filled with empty items");

        template<class T>
        static object_base* fromArray(VMArray<T> arr) {
            auto obj = &array::objectWithInitializer([&](array &me) {
                for (UInt32 i = 0; i < arr.Length(); ++i) {
                    T val;
                    arr.Get(&val, i);
                    me._array.push_back(Item(val));
                }
            },
                tes_context::instance());

            return obj;
        }
        REGISTERF(fromArray<SInt32>, "objectWithInts", "values",
"creates new array that contains given values\n\
objectWithBooleans converts booleans into integers");
        REGISTERF(fromArray<BSFixedString>, "objectWithStrings",  "values", nullptr);
        REGISTERF(fromArray<Float32>, "objectWithFloats",  "values", nullptr);
        REGISTERF(fromArray<bool>, "objectWithBooleans",  "values", nullptr);

        static object_base* subArray(ref source, SInt32 startIndex, SInt32 endIndex) {
            if (!source) {
                return nullptr;
            }

            object_lock g(source);

            if (!validateReadIndexRange(source.get(), startIndex, endIndex)) {
                return nullptr;
            }

            auto obj = &array::objectWithInitializer([&](array &me) {
                me._array.insert(me.begin(), source->begin() + startIndex, source->begin() + endIndex);
            },
                tes_context::instance());

            return obj;
        }
        REGISTERF2(subArray, "* startIndex endIndex", "creates new array containing all values from source array in range [startIndex, endIndex)");

        static void addFromArray(ref obj, ref another, SInt32 insertAtIndex = -1) {
            if (!obj || !another || obj == another) {
                return ;
            }

            object_lock g2(another);

            doWriteOp(obj, insertAtIndex, [&obj, &another](uint32_t whereTo) {
                obj->_array.insert(obj->begin() + whereTo, another->begin(), another->end());
            });
        }
        REGISTERF2(addFromArray, "* source insertAtIndex=-1",
"adds values from source array into this array. if insertAtIndex is -1 (default behaviour) it adds to the end.\n"
NEGATIVE_IDX_COMMENT);

        static void addFromFormList(ref obj, BGSListForm *formList, SInt32 insertAtIndex = -1) {
            if (!obj || !formList) {
                return;
            }

            struct inserter : BGSListForm::Visitor {

                virtual bool Accept(TESForm * form) override {
                    arr->u_push(Item(form));
                    return false;
                }

                array *arr;

                inserter(array *obj) : arr(obj) {}
            };

            doWriteOp(obj, insertAtIndex, [formList, &obj](uint32_t idx) {
                formList->Visit(inserter(obj.get()));
            });
        }
        REGISTERF2(addFromFormList, "* source insertAtIndex=-1", nullptr);

        template<class T><commit_msg>JArray.addFromFormList: non-default insertAtIndex arg. really changes insert location<commit_after>#pragma once

#include "collection_functions.h"

namespace tes_api_3 {

    using namespace collections;

#define NEGATIVE_IDX_COMMENT "negative index accesses items from the end of container counting backwards."

    class tes_array : public class_meta< tes_array >, public collections::array_functions {
    public:

        typedef array::ref& ref;

        REGISTER_TES_NAME("JArray");

        void additionalSetup() {
            metaInfo.comment = "Ordered collection of values (value is float, integer, string, form or another container).\n"
                "Inherits JValue functionality";
        }

        static bool validateReadIndex(const array *obj, UInt32 index) {
            return obj && index < obj->_array.size();
        }

        static bool validateReadIndexRange(const array *obj, UInt32 begin, UInt32 end) {
            return obj && begin < end && end <= obj->_array.size();
        }

        static bool validateWriteIndex(const array *obj, UInt32 index) {
            return obj && index <= obj->_array.size();
        }

        typedef array::Index Index;

        REGISTERF(tes_object::object<array>, "object", "", kCommentObject);

        static object_base* objectWithSize(SInt32 size) {
            if (size < 0) {
                return nullptr;
            }

            auto& obj = array::objectWithInitializer([&](array &me) {
                me._array.resize(size);
            },
                tes_context::instance());

            return &obj;
        }
        REGISTERF2(objectWithSize, "size", "creates array of given size, filled with empty items");

        template<class T>
        static object_base* fromArray(VMArray<T> arr) {
            auto obj = &array::objectWithInitializer([&](array &me) {
                for (UInt32 i = 0; i < arr.Length(); ++i) {
                    T val;
                    arr.Get(&val, i);
                    me._array.push_back(Item(val));
                }
            },
                tes_context::instance());

            return obj;
        }
        REGISTERF(fromArray<SInt32>, "objectWithInts", "values",
"creates new array that contains given values\n\
objectWithBooleans converts booleans into integers");
        REGISTERF(fromArray<BSFixedString>, "objectWithStrings",  "values", nullptr);
        REGISTERF(fromArray<Float32>, "objectWithFloats",  "values", nullptr);
        REGISTERF(fromArray<bool>, "objectWithBooleans",  "values", nullptr);

        static object_base* subArray(ref source, SInt32 startIndex, SInt32 endIndex) {
            if (!source) {
                return nullptr;
            }

            object_lock g(source);

            if (!validateReadIndexRange(source.get(), startIndex, endIndex)) {
                return nullptr;
            }

            auto obj = &array::objectWithInitializer([&](array &me) {
                me._array.insert(me.begin(), source->begin() + startIndex, source->begin() + endIndex);
            },
                tes_context::instance());

            return obj;
        }
        REGISTERF2(subArray, "* startIndex endIndex", "creates new array containing all values from source array in range [startIndex, endIndex)");

        static void addFromArray(ref obj, ref another, SInt32 insertAtIndex = -1) {
            if (!obj || !another || obj == another) {
                return ;
            }

            object_lock g2(another);

            doWriteOp(obj, insertAtIndex, [&obj, &another](uint32_t whereTo) {
                obj->_array.insert(obj->begin() + whereTo, another->begin(), another->end());
            });
        }
        REGISTERF2(addFromArray, "* source insertAtIndex=-1",
"adds values from source array into this array. if insertAtIndex is -1 (default behaviour) it adds to the end.\n"
NEGATIVE_IDX_COMMENT);

        static void addFromFormList(ref obj, BGSListForm *formList, SInt32 insertAtIndex = -1) {
            if (!obj || !formList) {
                return;
            }

            struct inserter : BGSListForm::Visitor {

                virtual bool Accept(TESForm * form) override {
                    arr->u_container().insert(arr->u_container().begin() + insertIdx, Item(form));
                    return false;
                }

                array *arr;
                uint32_t insertIdx;

                inserter(array *obj, uint32_t insertAt) : arr(obj), insertIdx(insertAt) {}
            };

            doWriteOp(obj, insertAtIndex, [formList, &obj](uint32_t idx) {
                formList->Visit(inserter(obj.get(), idx));
            });
        }
        REGISTERF2(addFromFormList, "* source insertAtIndex=-1", nullptr);

        template<class T>
{'fraction_non_alphanumeric': 0.11261886953734374, 'fraction_numerical': 0.009189015920504327, 'mean_word_length': 2.0498533724340176, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5695315', 'n_tokens_mistral': 2644, 'n_tokens_neox': 2463, 'n_words': 642}