static void log_array_push_runtime_issue(const char *detail) {
    char buffer[512];
    snprintf(buffer, sizeof(buffer), "[Line %d] ARRAY(\"PUSH\") %s", current_line, detail != NULL ? detail : "runtime issue.");
    append_basic_error_log(buffer);
}

static int array_push_checked_capacity(int count, int capacity) {
    size_t slot_limit = SIZE_MAX / sizeof(B2026_Var);
    int limit = INT_MAX - option_base;
    int next_capacity;
    if (slot_limit <= (size_t)option_base)
        runtime_error("Array append size overflow");
    slot_limit -= (size_t)option_base;
    if (slot_limit < (size_t)limit) limit = (int)slot_limit;
    if (count < 0 || capacity < 0 || count > capacity || capacity > limit || count >= limit)
        runtime_error("Array append size overflow");
    if (count < capacity) return capacity;
    if (capacity == 0) next_capacity = limit < 16 ? limit : 16;
    else next_capacity = capacity > limit / 2 ? limit : capacity * 2;
    return next_capacity;
}

static void array_push_append_number_item(B2026_Var **items, int *count, int *capacity, double value, int is_null_item) {
    int checked_capacity = array_push_checked_capacity(*count, *capacity);
    if (*count >= *capacity) {
        int old_capacity = *capacity;
        int new_capacity = checked_capacity;
        B2026_Var *new_items = (B2026_Var *)realloc(*items, (size_t)new_capacity * sizeof(B2026_Var));
        if (new_items == NULL) runtime_error("ARRAY() PUSH out of memory / 【ARRAY】PUSH処理用メモリを確保できません。");
        *items = new_items;
        memset(&(*items)[old_capacity], 0, (size_t)(new_capacity - old_capacity) * sizeof(B2026_Var));
        *capacity = new_capacity;
    }
    if (is_null_item) set_b2026_number(&(*items)[*count], null_value());
    else set_b2026_number(&(*items)[*count], value);
    (*count)++;
}

static void array_push_append_var_copy(B2026_Var **items, int *count, int *capacity, const B2026_Var *slot) {
    int checked_capacity = array_push_checked_capacity(*count, *capacity);
    if (*count >= *capacity) {
        int old_capacity = *capacity;
        int new_capacity = checked_capacity;
        B2026_Var *new_items = (B2026_Var *)realloc(*items, (size_t)new_capacity * sizeof(B2026_Var));
        if (new_items == NULL) runtime_error("REPLACE() array out of memory / 【REPLACE関数エラー】配列置換用メモリを確保できません。");
        *items = new_items;
        memset(&(*items)[old_capacity], 0, (size_t)(new_capacity - old_capacity) * sizeof(B2026_Var));
        *capacity = new_capacity;
    }
    copy_b2026_var(&(*items)[*count], slot);
    (*count)++;
}

static void array_push_append_from_array(B2026_Var **items, int *count, int *capacity, const char *source_name) {
    ArrayVariable *source_var = get_array_variable(source_name);
    int logical_count;
    int logical_index;
    int skipped_strings = 0;
    char detail[256];
    if (source_var == NULL) return;
    logical_count = array_logical_count(source_var);
    for (logical_index = 0; logical_index < logical_count; ++logical_index) {
        B2026_Var *slot = &source_var->data[option_base + logical_index];
        if (b2026_var_is_string(slot)) {
            skipped_strings++;
            continue;
        }
        if (b2026_var_is_null(slot)) array_push_append_number_item(items, count, capacity, 0.0, 1);
        else array_push_append_number_item(items, count, capacity, b2026_var_as_number(slot), 0);
    }
    if (skipped_strings > 0) {
        snprintf(detail, sizeof(detail), "ignored %d string element(s) from array %s().", skipped_strings, source_name);
        log_array_push_runtime_issue(detail);
    }
}

static void array_push_append_array_range(B2026_Var **items, int *count, int *capacity, const char *source_name, int start_offset, int end_offset) {
    ArrayVariable *source_var = get_array_variable(source_name);
    int logical_count;
    int logical_index;
    int skipped_strings = 0;
    char detail[256];
    if (source_var == NULL) return;
    logical_count = array_logical_count(source_var);
    if (start_offset < 0) start_offset = 0;
    if (end_offset > logical_count) end_offset = logical_count;
    if (start_offset >= end_offset) return;
    for (logical_index = start_offset; logical_index < end_offset; ++logical_index) {
        B2026_Var *slot = &source_var->data[option_base + logical_index];
        if (b2026_var_is_string(slot)) {
            skipped_strings++;
            continue;
        }
        if (b2026_var_is_null(slot)) array_push_append_number_item(items, count, capacity, 0.0, 1);
        else array_push_append_number_item(items, count, capacity, b2026_var_as_number(slot), 0);
    }
    if (skipped_strings > 0) {
        snprintf(detail, sizeof(detail), "ignored %d string element(s) from array %s().", skipped_strings, source_name);
        log_array_push_runtime_issue(detail);
    }
}

static void collect_array_push_items(const char *base_name, int item_count, const char **item_kinds, const char **item_array_names, const double *item_numbers, B2026_Var **items_out, int *count_out) {
    B2026_Var *items = NULL;
    int count = 0;
    int capacity = 0;
    int index;
    array_push_append_from_array(&items, &count, &capacity, base_name);
    for (index = 0; index < item_count; ++index) {
        const char *kind = (item_kinds != NULL) ? item_kinds[index] : "number";
        if (strcmp(kind, "array") == 0) {
            array_push_append_from_array(&items, &count, &capacity, item_array_names[index]);
        } else if (strcmp(kind, "null") == 0) {
            array_push_append_number_item(&items, &count, &capacity, 0.0, 1);
        } else {
            double value = item_numbers[index];
            if (is_null_number(value)) array_push_append_number_item(&items, &count, &capacity, 0.0, 1);
            else array_push_append_number_item(&items, &count, &capacity, value, 0);
        }
    }
    *items_out = items;
    *count_out = count;
}

static void collect_array_push_new_items(int item_count, const char **item_kinds, const char **item_array_names, const double *item_numbers, B2026_Var **items_out, int *count_out) {
    B2026_Var *items = NULL;
    int count = 0;
    int capacity = 0;
    int index;
    for (index = 0; index < item_count; ++index) {
        const char *kind = (item_kinds != NULL) ? item_kinds[index] : "number";
        if (strcmp(kind, "array") == 0) {
            array_push_append_from_array(&items, &count, &capacity, item_array_names[index]);
        } else if (strcmp(kind, "null") == 0) {
            array_push_append_number_item(&items, &count, &capacity, 0.0, 1);
        } else {
            double value = item_numbers[index];
            if (is_null_number(value)) array_push_append_number_item(&items, &count, &capacity, 0.0, 1);
            else array_push_append_number_item(&items, &count, &capacity, value, 0);
        }
    }
    *items_out = items;
    *count_out = count;
}

static void free_array_push_items(B2026_Var *items, int count) {
    int index;
    if (items == NULL) return;
    for (index = 0; index < count; ++index) clear_b2026_var(&items[index]);
    free(items);
}

void print_array_push(const char *base_name, int item_count, const char **item_kinds, const char **item_array_names, const double *item_numbers) {
    B2026_Var *items = NULL;
    int count = 0;
    int index;
    collect_array_push_items(base_name, item_count, item_kinds, item_array_names, item_numbers, &items, &count);
    for (index = 0; index < count; ++index) {
        if (index > 0) printf("%s", g_print_delimiter);
        if (b2026_var_is_null(&items[index])) printf("%s", g_print_null);
        else print_numeric_value(b2026_var_as_number(&items[index]));
    }
    free_array_push_items(items, count);
}

static void log_array_pop_runtime_issue(const char *detail) {
    char buffer[512];
    snprintf(buffer, sizeof(buffer), "[Line %d] ARRAY(\"POP\") %s", current_line, detail != NULL ? detail : "runtime issue.");
    append_basic_error_log(buffer);
}

static void clear_array_runtime_metadata(ArrayVariable *array_var) {
    if (array_var == NULL) return;
    array_var->screen_width = 0;
    array_var->screen_height = 0;
    array_var->draw_origin_x = 0;
    array_var->draw_origin_y = 0;
    array_var->draw_mode = 0;
    array_var->draw_scale_x = 1.0;
    array_var->draw_scale_y = 1.0;
    array_var->draw_rotate_deg = 0.0;
    array_var->draw_shear_x_deg = 0.0;
    array_var->draw_shear_y_deg = 0.0;
    array_var->draw_line_width = 1;
    free(array_var->draw_dash_pattern);
    array_var->draw_dash_pattern = NULL;
    array_var->draw_dash_count = 0;
    array_var->draw_dash_cap = 0;
    array_var->draw_line_cap = 0;
    array_var->draw_line_join = 0;
    array_var->draw_miter_limit = 10.0;
    free(array_var->draw_path_segments);
    array_var->draw_path_segments = NULL;
    array_var->draw_path_count = 0;
    array_var->draw_path_capacity = 0;
    array_var->draw_path_has_current = 0;
    array_var->draw_path_current_x = 0;
    array_var->draw_path_current_y = 0;
    array_var->draw_path_has_subpath = 0;
    array_var->draw_path_subpath_start_x = 0;
    array_var->draw_path_subpath_start_y = 0;
    array_var->image_phys_x = 0;
    array_var->image_phys_y = 0;
    array_var->image_phys_unit = 0;
    array_var->image_pixel_mode = 0;
    array_var->image_value_max = 0.0;
    array_var->fft_width = 0;
    array_var->fft_height = 0;
    array_var->fft_original_width = 0;
    array_var->fft_original_height = 0;
    array_var->fft_is_complex = 0;
    array_var->fft_channels = 0;
    array_var->fft_restore_255 = 0;
}

static int array_pop_prepare(const char *source_name, double count_value, int has_count, ArrayVariable **source_out, int *logical_count_out, int *pop_count_out) {
    ArrayVariable *source_var = get_array_variable(source_name);
    int logical_count = 0;
    int pop_count = 0;
    if (source_var != NULL) logical_count = array_logical_count(source_var);
    if (has_count) {
        if (!is_null_number(count_value) && count_value > 0.0) pop_count = (int)floor(count_value);
    } else {
        pop_count = 1;
    }
    if (pop_count < 0) pop_count = 0;
    if (pop_count > logical_count) pop_count = logical_count;
    if (source_out != NULL) *source_out = source_var;
    if (logical_count_out != NULL) *logical_count_out = logical_count;
    if (pop_count_out != NULL) *pop_count_out = pop_count;
    return source_var != NULL;
}

static void array_pop_trim_source(ArrayVariable *source_var, int logical_count, int pop_count) {
    int remaining_count;
    int new_size;
    int logical_index;
    B2026_Var *new_data;
    if (source_var == NULL) return;
    ensure_array_unique(source_var);
    remaining_count = logical_count - pop_count;
    for (logical_index = remaining_count; logical_index < logical_count; ++logical_index) {
        clear_b2026_var(&source_var->data[option_base + logical_index]);
    }
    new_size = option_base + remaining_count - 1;
    if (new_size < option_base - 1) new_size = option_base - 1;
    if (new_size != source_var->size) {
        new_data = (B2026_Var *)realloc(source_var->data, (size_t)(new_size + 1) * sizeof(B2026_Var));
        if (new_data == NULL && new_size >= option_base) runtime_error("ARRAY() POP out of memory / 【ARRAY】POP処理用メモリを確保できません。");
        if (new_data != NULL || new_size < option_base) source_var->data = new_data;
        source_var->size = new_size;
    }
    source_var->read_count = remaining_count;
    clear_array_runtime_metadata(source_var);
}

double array_pop_to_number(const char *source_name, double count_value, int has_count) {
    ArrayVariable *source_var = NULL;
    int logical_count = 0;
    int pop_count = 0;
    double result;
    array_pop_prepare(source_name, count_value, has_count, &source_var, &logical_count, &pop_count);
    if (pop_count <= 0 || source_var == NULL) return null_value();
    if (pop_count != 1) {
        log_array_pop_runtime_issue("scalar target requires exactly one element; operation ignored.");
        return null_value();
    }
    result = b2026_var_as_number(&source_var->data[option_base + logical_count - 1]);
    array_pop_trim_source(source_var, logical_count, pop_count);
    return result;
}

void apply_array_pop_to_array(const char *target_name, const char *source_name, double count_value, int has_count) {
    ArrayVariable *source_var = NULL;
    ArrayVariable *target_var;
    int logical_count = 0;
    int pop_count = 0;
    int start_index;
    int index;
    array_pop_prepare(source_name, count_value, has_count, &source_var, &logical_count, &pop_count);
    dim_array(target_name, (double)(option_base + pop_count - 1));
    target_var = require_array_variable(target_name);
    if (pop_count <= 0 || source_var == NULL) {
        target_var->read_count = 0;
        clear_array_runtime_metadata(target_var);
        return;
    }
    start_index = logical_count - pop_count;
    for (index = 0; index < pop_count; ++index) {
        copy_b2026_var(&target_var->data[option_base + index], &source_var->data[option_base + start_index + index]);
    }
    target_var->read_count = pop_count;
    clear_array_runtime_metadata(target_var);
    array_pop_trim_source(source_var, logical_count, pop_count);
}

void array_pop_discard(const char *source_name, double count_value, int has_count) {
    ArrayVariable *source_var = NULL;
    int logical_count = 0;
    int pop_count = 0;
    array_pop_prepare(source_name, count_value, has_count, &source_var, &logical_count, &pop_count);
    if (pop_count <= 0 || source_var == NULL) return;
    array_pop_trim_source(source_var, logical_count, pop_count);
}

void print_array_pop(const char *source_name, double count_value, int has_count) {
    ArrayVariable *source_var = NULL;
    int logical_count = 0;
    int pop_count = 0;
    int start_index;
    int index;
    array_pop_prepare(source_name, count_value, has_count, &source_var, &logical_count, &pop_count);
    if (pop_count <= 0 || source_var == NULL) {
        printf("%s", g_print_null);
        return;
    }
    start_index = logical_count - pop_count;
    for (index = 0; index < pop_count; ++index) {
        if (index > 0) printf("%s", g_print_delimiter);
        if (b2026_var_is_null(&source_var->data[option_base + start_index + index])) printf("%s", g_print_null);
        else print_numeric_value(b2026_var_as_number(&source_var->data[option_base + start_index + index]));
    }
    array_pop_trim_source(source_var, logical_count, pop_count);
}

static void log_array_shift_runtime_issue(const char *detail) {
    char buffer[512];
    snprintf(buffer, sizeof(buffer), "[Line %d] ARRAY(\"SHIFT\") %s", current_line, detail != NULL ? detail : "runtime issue.");
    append_basic_error_log(buffer);
}

static void log_array_insert_runtime_issue(const char *detail) {
    char buffer[512];
    snprintf(buffer, sizeof(buffer), "[Line %d] ARRAY(\"INSERT\") %s", current_line, detail != NULL ? detail : "runtime issue.");
    append_basic_error_log(buffer);
}

static void log_array_remove_runtime_issue(const char *detail) {
    char buffer[512];
    snprintf(buffer, sizeof(buffer), "[Line %d] ARRAY(\"REMOVE\") %s", current_line, detail != NULL ? detail : "runtime issue.");
    append_basic_error_log(buffer);
}

static int normalize_array_insert_offset(int logical_count, double position_value) {
    long long raw_index;
    int offset;
    if (is_null_number(position_value)) return logical_count;
    raw_index = (long long)floor(position_value);
    if (raw_index < 0) offset = logical_count + (int)raw_index;
    else offset = (int)raw_index - option_base;
    if (offset < 0) offset = 0;
    if (offset > logical_count) offset = logical_count;
    return offset;
}

static int normalize_array_remove_offset(int logical_count, double position_value) {
    long long raw_index;
    int offset;
    if (is_null_number(position_value)) return logical_count;
    raw_index = (long long)floor(position_value);
    if (raw_index < 0) offset = logical_count + (int)raw_index;
    else offset = (int)raw_index - option_base;
    if (offset < 0) offset = 0;
    if (offset > logical_count) offset = logical_count;
    return offset;
}

static void collect_array_unshift_items(const char *base_name, int item_count, const char **item_kinds, const char **item_array_names, const double *item_numbers, B2026_Var **items_out, int *count_out) {
    B2026_Var *items = NULL;
    int count = 0;
    int capacity = 0;
    int index;
    for (index = 0; index < item_count; ++index) {
        const char *kind = (item_kinds != NULL) ? item_kinds[index] : "number";
        if (strcmp(kind, "array") == 0) {
            array_push_append_from_array(&items, &count, &capacity, item_array_names[index]);
        } else if (strcmp(kind, "null") == 0) {
            array_push_append_number_item(&items, &count, &capacity, 0.0, 1);
        } else {
            double value = item_numbers[index];
            if (is_null_number(value)) array_push_append_number_item(&items, &count, &capacity, 0.0, 1);
            else array_push_append_number_item(&items, &count, &capacity, value, 0);
        }
    }
    array_push_append_from_array(&items, &count, &capacity, base_name);
    *items_out = items;
    *count_out = count;
}

void print_array_unshift(const char *base_name, int item_count, const char **item_kinds, const char **item_array_names, const double *item_numbers) {
    B2026_Var *items = NULL;
    int count = 0;
    int index;
    collect_array_unshift_items(base_name, item_count, item_kinds, item_array_names, item_numbers, &items, &count);
    for (index = 0; index < count; ++index) {
        if (index > 0) printf("%s", g_print_delimiter);
        if (b2026_var_is_null(&items[index])) printf("%s", g_print_null);
        else print_numeric_value(b2026_var_as_number(&items[index]));
    }
    free_array_push_items(items, count);
}

static void collect_array_insert_items(const char *base_name, double position_value, int item_count, const char **item_kinds, const char **item_array_names, const double *item_numbers, B2026_Var **items_out, int *count_out) {
    B2026_Var *items = NULL;
    int count = 0;
    int capacity = 0;
    int logical_count = 0;
    int offset = 0;
    int index;
    ArrayVariable *source_var = get_array_variable(base_name);
    if (source_var != NULL) logical_count = array_logical_count(source_var);
    offset = normalize_array_insert_offset(logical_count, position_value);
    array_push_append_array_range(&items, &count, &capacity, base_name, 0, offset);
    for (index = 0; index < item_count; ++index) {
        const char *kind = (item_kinds != NULL) ? item_kinds[index] : "number";
        if (strcmp(kind, "array") == 0) {
            array_push_append_from_array(&items, &count, &capacity, item_array_names[index]);
        } else if (strcmp(kind, "null") == 0) {
            array_push_append_number_item(&items, &count, &capacity, 0.0, 1);
        } else {
            double value = item_numbers[index];
            if (is_null_number(value)) array_push_append_number_item(&items, &count, &capacity, 0.0, 1);
            else array_push_append_number_item(&items, &count, &capacity, value, 0);
        }
    }
    array_push_append_array_range(&items, &count, &capacity, base_name, offset, logical_count);
    *items_out = items;
    *count_out = count;
}

static void collect_array_replace_items(const char *base_name, double position_value, int item_count, const char **item_kinds, const char **item_array_names, const double *item_numbers, B2026_Var **items_out, int *count_out) {
    B2026_Var *items = NULL;
    int count = 0;
    int capacity = 0;
    int logical_count = 0;
    int offset = 0;
    int remove_count = 0;
    int index;
    ArrayVariable *source_var = get_array_variable(base_name);
    if (source_var != NULL) logical_count = array_logical_count(source_var);
    offset = normalize_array_remove_offset(logical_count, position_value);
    if (offset < logical_count) remove_count = 1;
    array_push_append_array_range(&items, &count, &capacity, base_name, 0, offset);
    for (index = 0; index < item_count; ++index) {
        const char *kind = (item_kinds != NULL) ? item_kinds[index] : "number";
        if (strcmp(kind, "array") == 0) {
            array_push_append_from_array(&items, &count, &capacity, item_array_names[index]);
        } else if (strcmp(kind, "null") == 0) {
            array_push_append_number_item(&items, &count, &capacity, 0.0, 1);
        } else {
            double value = item_numbers[index];
            if (is_null_number(value)) array_push_append_number_item(&items, &count, &capacity, 0.0, 1);
            else array_push_append_number_item(&items, &count, &capacity, value, 0);
        }
    }
    array_push_append_array_range(&items, &count, &capacity, base_name, offset + remove_count, logical_count);
    *items_out = items;
    *count_out = count;
}

static int replace_array_pattern_match_at(ArrayVariable *source_var, int logical_count, int offset, int search_count, const unsigned char *search_modes, const double *search_values) {
    int pattern_index;
    if (search_count <= 0) return 0;
    if (offset < 0 || offset > logical_count || search_count > logical_count - offset) return 0;
    for (pattern_index = 0; pattern_index < search_count; ++pattern_index) {
        unsigned char mode = search_modes != NULL ? search_modes[pattern_index] : 1;
        B2026_Var *slot = &source_var->data[option_base + offset + pattern_index];
        if (mode == 0) continue;
        if (is_null_number(search_values[pattern_index])) {
            if (!b2026_var_is_null(slot)) return 0;
            continue;
        }
        if (b2026_var_is_null(slot) || b2026_var_is_string(slot)) return 0;
        if (b2026_var_as_number(slot) != search_values[pattern_index]) return 0;
    }
    return 1;
}

static void collect_replace_array_pattern_items(const char *source_name, int search_count, const unsigned char *search_modes, const double *search_values, int replace_count, const double *replace_values, B2026_Var **items_out, int *count_out) {
    B2026_Var *items = NULL;
    int count = 0;
    int capacity = 0;
    ArrayVariable *source_var = get_array_variable(source_name);
    int logical_count = source_var != NULL ? array_logical_count(source_var) : 0;
    int logical_index = 0;
    if (search_count <= 0 || source_var == NULL) {
        array_push_append_from_array(&items, &count, &capacity, source_name);
        *items_out = items;
        *count_out = count;
        return;
    }
    while (logical_index < logical_count) {
        if (replace_array_pattern_match_at(source_var, logical_count, logical_index, search_count, search_modes, search_values)) {
            int replace_index;
            for (replace_index = 0; replace_index < replace_count; ++replace_index) {
                if (is_null_number(replace_values[replace_index])) array_push_append_number_item(&items, &count, &capacity, 0.0, 1);
                else array_push_append_number_item(&items, &count, &capacity, replace_values[replace_index], 0);
            }
            logical_index += search_count;
            continue;
        }
        array_push_append_var_copy(&items, &count, &capacity, &source_var->data[option_base + logical_index]);
        logical_index++;
    }
    *items_out = items;
    *count_out = count;
}

static void array_shift_trim_source(ArrayVariable *source_var, int logical_count, int shift_count) {
    int remaining_count;
    int new_size;
    int logical_index;
    B2026_Var *new_data;
    if (source_var == NULL) return;
    ensure_array_unique(source_var);
    remaining_count = logical_count - shift_count;
    for (logical_index = 0; logical_index < remaining_count; ++logical_index) {
        B2026_Var *dest = &source_var->data[option_base + logical_index];
        B2026_Var *src = &source_var->data[option_base + shift_count + logical_index];
        clear_b2026_var(dest);
        *dest = *src;
        src->type = 0;
        src->data.number = 0.0;
    }
    for (logical_index = remaining_count; logical_index < logical_count; ++logical_index) {
        clear_b2026_var(&source_var->data[option_base + logical_index]);
    }
    new_size = option_base + remaining_count - 1;
    if (new_size < option_base - 1) new_size = option_base - 1;
    if (new_size != source_var->size) {
        new_data = (B2026_Var *)realloc(source_var->data, (size_t)(new_size + 1) * sizeof(B2026_Var));
        if (new_data == NULL && new_size >= option_base) runtime_error("ARRAY() SHIFT out of memory / 【ARRAY】SHIFT処理用メモリを確保できません。");
        if (new_data != NULL || new_size < option_base) source_var->data = new_data;
        source_var->size = new_size;
    }
    source_var->read_count = remaining_count;
    clear_array_runtime_metadata(source_var);
}

double array_shift_to_number(const char *source_name, double count_value, int has_count) {
    ArrayVariable *source_var = NULL;
    int logical_count = 0;
    int shift_count = 0;
    double result;
    array_pop_prepare(source_name, count_value, has_count, &source_var, &logical_count, &shift_count);
    if (shift_count <= 0 || source_var == NULL) return null_value();
    if (shift_count != 1) {
        log_array_shift_runtime_issue("scalar target requires exactly one element; operation ignored.");
        return null_value();
    }
    result = b2026_var_as_number(&source_var->data[option_base]);
    array_shift_trim_source(source_var, logical_count, shift_count);
    return result;
}

void apply_array_shift_to_array(const char *target_name, const char *source_name, double count_value, int has_count) {
    ArrayVariable *source_var = NULL;
    ArrayVariable *target_var;
    int logical_count = 0;
    int shift_count = 0;
    int index;
    array_pop_prepare(source_name, count_value, has_count, &source_var, &logical_count, &shift_count);
    dim_array(target_name, (double)(option_base + shift_count - 1));
    target_var = require_array_variable(target_name);
    if (shift_count <= 0 || source_var == NULL) {
        target_var->read_count = 0;
        clear_array_runtime_metadata(target_var);
        return;
    }
    for (index = 0; index < shift_count; ++index) {
        copy_b2026_var(&target_var->data[option_base + index], &source_var->data[option_base + index]);
    }
    target_var->read_count = shift_count;
    clear_array_runtime_metadata(target_var);
    array_shift_trim_source(source_var, logical_count, shift_count);
}

void array_shift_discard(const char *source_name, double count_value, int has_count) {
    ArrayVariable *source_var = NULL;
    int logical_count = 0;
    int shift_count = 0;
    array_pop_prepare(source_name, count_value, has_count, &source_var, &logical_count, &shift_count);
    if (shift_count <= 0 || source_var == NULL) return;
    array_shift_trim_source(source_var, logical_count, shift_count);
}

void print_array_shift(const char *source_name, double count_value, int has_count) {
    ArrayVariable *source_var = NULL;
    int logical_count = 0;
    int shift_count = 0;
    int index;
    array_pop_prepare(source_name, count_value, has_count, &source_var, &logical_count, &shift_count);
    if (shift_count <= 0 || source_var == NULL) {
        printf("%s", g_print_null);
        return;
    }
    for (index = 0; index < shift_count; ++index) {
        if (index > 0) printf("%s", g_print_delimiter);
        if (b2026_var_is_null(&source_var->data[option_base + index])) printf("%s", g_print_null);
        else print_numeric_value(b2026_var_as_number(&source_var->data[option_base + index]));
    }
    array_shift_trim_source(source_var, logical_count, shift_count);
}

void apply_array_unshift_to_array(const char *target_name, const char *base_name, int item_count, const char **item_kinds, const char **item_array_names, const double *item_numbers) {
    B2026_Var *items = NULL;
    int count = 0;
    int index;
    ArrayVariable *target_var;
    collect_array_unshift_items(base_name, item_count, item_kinds, item_array_names, item_numbers, &items, &count);
    if (count <= 0) {
        dim_array(target_name, (double)(option_base - 1));
        target_var = require_array_variable(target_name);
        target_var->read_count = 0;
        clear_array_runtime_metadata(target_var);
        free_array_push_items(items, count);
        return;
    }
    dim_array(target_name, (double)(option_base + count - 1));
    target_var = require_array_variable(target_name);
    for (index = 0; index < count; ++index) {
        copy_b2026_var(&target_var->data[option_base + index], &items[index]);
    }
    target_var->read_count = count;
    clear_array_runtime_metadata(target_var);
    free_array_push_items(items, count);
}

void apply_array_unshift_to_hash(const char *target_name, const char *base_name, int item_count, const char **item_kinds, const char **item_array_names, const double *item_numbers) {
    B2026_Var *items = NULL;
    int count = 0;
    int index;
    Frame *target_frame = get_hash_frame(target_name);
    HashVariable *target_var = ensure_local_hash(target_frame, target_name);
    char key_buffer[64];
    clear_hash_entries(target_var);
    collect_array_unshift_items(base_name, item_count, item_kinds, item_array_names, item_numbers, &items, &count);
    for (index = 0; index < count; ++index) {
        snprintf(key_buffer, sizeof(key_buffer), "%d", index + 1);
        if (b2026_var_is_null(&items[index])) hash_set_number(target_name, key_buffer, null_value());
        else hash_set_number(target_name, key_buffer, b2026_var_as_number(&items[index]));
    }
    free_array_push_items(items, count);
}

void print_array_insert(const char *base_name, double position_value, int item_count, const char **item_kinds, const char **item_array_names, const double *item_numbers) {
    B2026_Var *items = NULL;
    int count = 0;
    int index;
    collect_array_insert_items(base_name, position_value, item_count, item_kinds, item_array_names, item_numbers, &items, &count);
    for (index = 0; index < count; ++index) {
        if (index > 0) printf("%s", g_print_delimiter);
        if (b2026_var_is_null(&items[index])) printf("%s", g_print_null);
        else print_numeric_value(b2026_var_as_number(&items[index]));
    }
    free_array_push_items(items, count);
}

void print_array_replace(const char *base_name, double position_value, int item_count, const char **item_kinds, const char **item_array_names, const double *item_numbers) {
    B2026_Var *items = NULL;
    int count = 0;
    int index;
    collect_array_replace_items(base_name, position_value, item_count, item_kinds, item_array_names, item_numbers, &items, &count);
    for (index = 0; index < count; ++index) {
        if (index > 0) printf("%s", g_print_delimiter);
        if (b2026_var_is_null(&items[index])) printf("%s", g_print_null);
        else print_numeric_value(b2026_var_as_number(&items[index]));
    }
    free_array_push_items(items, count);
}

void print_replace_array_pattern(const char *source_name, int search_count, const unsigned char *search_modes, const double *search_values, int replace_count, const double *replace_values) {
    B2026_Var *items = NULL;
    int count = 0;
    int index;
    collect_replace_array_pattern_items(source_name, search_count, search_modes, search_values, replace_count, replace_values, &items, &count);
    for (index = 0; index < count; ++index) {
        if (index > 0) printf("%s", g_print_delimiter);
        if (b2026_var_is_null(&items[index])) printf("%s", g_print_null);
        else print_numeric_value(b2026_var_as_number(&items[index]));
    }
    free_array_push_items(items, count);
}

void apply_array_insert_to_array(const char *target_name, const char *base_name, double position_value, int item_count, const char **item_kinds, const char **item_array_names, const double *item_numbers) {
    B2026_Var *items = NULL;
    int count = 0;
    int index;
    ArrayVariable *target_var;
    collect_array_insert_items(base_name, position_value, item_count, item_kinds, item_array_names, item_numbers, &items, &count);
    if (count <= 0) {
        dim_array(target_name, (double)(option_base - 1));
        target_var = require_array_variable(target_name);
        target_var->read_count = 0;
        clear_array_runtime_metadata(target_var);
        free_array_push_items(items, count);
        return;
    }
    dim_array(target_name, (double)(option_base + count - 1));
    target_var = require_array_variable(target_name);
    for (index = 0; index < count; ++index) {
        copy_b2026_var(&target_var->data[option_base + index], &items[index]);
    }
    target_var->read_count = count;
    clear_array_runtime_metadata(target_var);
    free_array_push_items(items, count);
}

void apply_array_replace_to_array(const char *target_name, const char *base_name, double position_value, int item_count, const char **item_kinds, const char **item_array_names, const double *item_numbers) {
    B2026_Var *items = NULL;
    int count = 0;
    int index;
    ArrayVariable *target_var;
    collect_array_replace_items(base_name, position_value, item_count, item_kinds, item_array_names, item_numbers, &items, &count);
    if (count <= 0) {
        dim_array(target_name, (double)(option_base - 1));
        target_var = require_array_variable(target_name);
        target_var->read_count = 0;
        clear_array_runtime_metadata(target_var);
        free_array_push_items(items, count);
        return;
    }
    dim_array(target_name, (double)(option_base + count - 1));
    target_var = require_array_variable(target_name);
    for (index = 0; index < count; ++index) {
        copy_b2026_var(&target_var->data[option_base + index], &items[index]);
    }
    target_var->read_count = count;
    clear_array_runtime_metadata(target_var);
    free_array_push_items(items, count);
}

void apply_replace_array_pattern_to_array(const char *target_name, const char *source_name, int search_count, const unsigned char *search_modes, const double *search_values, int replace_count, const double *replace_values) {
    B2026_Var *items = NULL;
    int count = 0;
    int index;
    ArrayVariable *target_var;
    collect_replace_array_pattern_items(source_name, search_count, search_modes, search_values, replace_count, replace_values, &items, &count);
    if (count <= 0) {
        dim_array(target_name, (double)(option_base - 1));
        target_var = require_array_variable(target_name);
        target_var->read_count = 0;
        clear_array_runtime_metadata(target_var);
        free_array_push_items(items, count);
        return;
    }
    dim_array(target_name, (double)(option_base + count - 1));
    target_var = require_array_variable(target_name);
    for (index = 0; index < count; ++index) {
        copy_b2026_var(&target_var->data[option_base + index], &items[index]);
    }
    target_var->read_count = count;
    clear_array_runtime_metadata(target_var);
    free_array_push_items(items, count);
}

void apply_array_insert_to_hash(const char *target_name, const char *base_name, double position_value, int item_count, const char **item_kinds, const char **item_array_names, const double *item_numbers) {
    B2026_Var *items = NULL;
    int count = 0;
    int index;
    Frame *target_frame = get_hash_frame(target_name);
    HashVariable *target_var = ensure_local_hash(target_frame, target_name);
    char key_buffer[64];
    clear_hash_entries(target_var);
    collect_array_insert_items(base_name, position_value, item_count, item_kinds, item_array_names, item_numbers, &items, &count);
    for (index = 0; index < count; ++index) {
        snprintf(key_buffer, sizeof(key_buffer), "%d", index + 1);
        if (b2026_var_is_null(&items[index])) hash_set_number(target_name, key_buffer, null_value());
        else hash_set_number(target_name, key_buffer, b2026_var_as_number(&items[index]));
    }
    free_array_push_items(items, count);
}

void apply_array_replace_to_hash(const char *target_name, const char *base_name, double position_value, int item_count, const char **item_kinds, const char **item_array_names, const double *item_numbers) {
    B2026_Var *items = NULL;
    int count = 0;
    int index;
    Frame *target_frame = get_hash_frame(target_name);
    HashVariable *target_var = ensure_local_hash(target_frame, target_name);
    char key_buffer[64];
    clear_hash_entries(target_var);
    collect_array_replace_items(base_name, position_value, item_count, item_kinds, item_array_names, item_numbers, &items, &count);
    for (index = 0; index < count; ++index) {
        snprintf(key_buffer, sizeof(key_buffer), "%d", index + 1);
        if (b2026_var_is_null(&items[index])) hash_set_number(target_name, key_buffer, null_value());
        else hash_set_number(target_name, key_buffer, b2026_var_as_number(&items[index]));
    }
    free_array_push_items(items, count);
}

static int array_remove_prepare(const char *source_name, double position_value, double count_value, int has_count, ArrayVariable **source_out, int *logical_count_out, int *offset_out, int *remove_count_out) {
    ArrayVariable *source_var = get_array_variable(source_name);
    int logical_count = 0;
    int offset = 0;
    int remove_count = 0;
    if (source_var != NULL) logical_count = array_logical_count(source_var);
    offset = normalize_array_remove_offset(logical_count, position_value);
    if (has_count) {
        if (!is_null_number(count_value) && count_value > 0.0) remove_count = (int)floor(count_value);
    } else {
        remove_count = 1;
    }
    if (remove_count < 0) remove_count = 0;
    if (offset > logical_count) offset = logical_count;
    if (offset + remove_count > logical_count) remove_count = logical_count - offset;
    if (remove_count < 0) remove_count = 0;
    if (source_out != NULL) *source_out = source_var;
    if (logical_count_out != NULL) *logical_count_out = logical_count;
    if (offset_out != NULL) *offset_out = offset;
    if (remove_count_out != NULL) *remove_count_out = remove_count;
    return source_var != NULL;
}

static void array_remove_trim_source(ArrayVariable *source_var, int logical_count, int offset, int remove_count) {
    int remaining_count;
    int new_size;
    int logical_index;
    B2026_Var *new_data;
    if (source_var == NULL) return;
    ensure_array_unique(source_var);
    remaining_count = logical_count - remove_count;
    for (logical_index = offset; logical_index < remaining_count; ++logical_index) {
        B2026_Var *dest = &source_var->data[option_base + logical_index];
        B2026_Var *src = &source_var->data[option_base + logical_index + remove_count];
        clear_b2026_var(dest);
        *dest = *src;
        src->type = 0;
        src->data.number = 0.0;
    }
    for (logical_index = remaining_count; logical_index < logical_count; ++logical_index) {
        clear_b2026_var(&source_var->data[option_base + logical_index]);
    }
    new_size = option_base + remaining_count - 1;
    if (new_size < option_base - 1) new_size = option_base - 1;
    if (new_size != source_var->size) {
        new_data = (B2026_Var *)realloc(source_var->data, (size_t)(new_size + 1) * sizeof(B2026_Var));
        if (new_data == NULL && new_size >= option_base) runtime_error("ARRAY() REMOVE out of memory / 【ARRAY】REMOVE処理用メモリを確保できません。");
        if (new_data != NULL || new_size < option_base) source_var->data = new_data;
        source_var->size = new_size;
    }
    source_var->read_count = remaining_count;
    clear_array_runtime_metadata(source_var);
}

double array_remove_to_number(const char *source_name, double position_value, double count_value, int has_count) {
    ArrayVariable *source_var = NULL;
    int logical_count = 0;
    int offset = 0;
    int remove_count = 0;
    double result;
    array_remove_prepare(source_name, position_value, count_value, has_count, &source_var, &logical_count, &offset, &remove_count);
    if (remove_count <= 0 || source_var == NULL) return null_value();
    if (remove_count != 1) {
        log_array_remove_runtime_issue("scalar target requires exactly one element; operation ignored.");
        return null_value();
    }
    result = b2026_var_as_number(&source_var->data[option_base + offset]);
    array_remove_trim_source(source_var, logical_count, offset, remove_count);
    return result;
}

void apply_array_remove_to_array(const char *target_name, const char *source_name, double position_value, double count_value, int has_count) {
    ArrayVariable *source_var = NULL;
    ArrayVariable *target_var;
    int logical_count = 0;
    int offset = 0;
    int remove_count = 0;
    int index;
    array_remove_prepare(source_name, position_value, count_value, has_count, &source_var, &logical_count, &offset, &remove_count);
    dim_array(target_name, (double)(option_base + remove_count - 1));
    target_var = require_array_variable(target_name);
    if (remove_count <= 0 || source_var == NULL) {
        target_var->read_count = 0;
        clear_array_runtime_metadata(target_var);
        return;
    }
    for (index = 0; index < remove_count; ++index) {
        copy_b2026_var(&target_var->data[option_base + index], &source_var->data[option_base + offset + index]);
    }
    target_var->read_count = remove_count;
    clear_array_runtime_metadata(target_var);
    array_remove_trim_source(source_var, logical_count, offset, remove_count);
}

void array_remove_discard(const char *source_name, double position_value, double count_value, int has_count) {
    ArrayVariable *source_var = NULL;
    int logical_count = 0;
    int offset = 0;
    int remove_count = 0;
    array_remove_prepare(source_name, position_value, count_value, has_count, &source_var, &logical_count, &offset, &remove_count);
    if (remove_count <= 0 || source_var == NULL) return;
    array_remove_trim_source(source_var, logical_count, offset, remove_count);
}

void apply_array_slice_remove_to_array(const char *target_name, const char *source_name, double position_value, double count_value, int has_count) {
    ArrayVariable *source_var = NULL;
    ArrayVariable *target_var;
    int logical_count = 0;
    int offset = 0;
    int remove_count = 0;
    int count = 0;
    int index;
    B2026_Var *items = NULL;
    int capacity = 0;
    array_remove_prepare(source_name, position_value, count_value, has_count, &source_var, &logical_count, &offset, &remove_count);
    array_push_append_array_range(&items, &count, &capacity, source_name, 0, offset);
    array_push_append_array_range(&items, &count, &capacity, source_name, offset + remove_count, logical_count);
    if (count <= 0) {
        dim_array(target_name, (double)(option_base - 1));
        target_var = require_array_variable(target_name);
        target_var->read_count = 0;
        clear_array_runtime_metadata(target_var);
        free_array_push_items(items, count);
        return;
    }
    dim_array(target_name, (double)(option_base + count - 1));
    target_var = require_array_variable(target_name);
    for (index = 0; index < count; ++index) {
        copy_b2026_var(&target_var->data[option_base + index], &items[index]);
    }
    target_var->read_count = count;
    clear_array_runtime_metadata(target_var);
    free_array_push_items(items, count);
}

void apply_array_slice_remove_to_hash(const char *target_name, const char *source_name, double position_value, double count_value, int has_count) {
    ArrayVariable *source_var = NULL;
    int logical_count = 0;
    int offset = 0;
    int remove_count = 0;
    int count = 0;
    int index;
    B2026_Var *items = NULL;
    int capacity = 0;
    Frame *target_frame = get_hash_frame(target_name);
    HashVariable *target_var = ensure_local_hash(target_frame, target_name);
    char key_buffer[64];
    clear_hash_entries(target_var);
    array_remove_prepare(source_name, position_value, count_value, has_count, &source_var, &logical_count, &offset, &remove_count);
    array_push_append_array_range(&items, &count, &capacity, source_name, 0, offset);
    array_push_append_array_range(&items, &count, &capacity, source_name, offset + remove_count, logical_count);
    for (index = 0; index < count; ++index) {
        snprintf(key_buffer, sizeof(key_buffer), "%d", index + 1);
        if (b2026_var_is_null(&items[index])) hash_set_number(target_name, key_buffer, null_value());
        else hash_set_number(target_name, key_buffer, b2026_var_as_number(&items[index]));
    }
    free_array_push_items(items, count);
}

void print_array_remove(const char *source_name, double position_value, double count_value, int has_count) {
    ArrayVariable *source_var = NULL;
    int logical_count = 0;
    int offset = 0;
    int remove_count = 0;
    int index;
    array_remove_prepare(source_name, position_value, count_value, has_count, &source_var, &logical_count, &offset, &remove_count);
    if (remove_count <= 0 || source_var == NULL) {
        printf("%s", g_print_null);
        return;
    }
    for (index = 0; index < remove_count; ++index) {
        if (index > 0) printf("%s", g_print_delimiter);
        if (b2026_var_is_null(&source_var->data[option_base + offset + index])) printf("%s", g_print_null);
        else print_numeric_value(b2026_var_as_number(&source_var->data[option_base + offset + index]));
    }
    array_remove_trim_source(source_var, logical_count, offset, remove_count);
}

void print_array_slice_remove(const char *source_name, double position_value, double count_value, int has_count) {
    ArrayVariable *source_var = NULL;
    int logical_count = 0;
    int offset = 0;
    int remove_count = 0;
    int count = 0;
    int index;
    B2026_Var *items = NULL;
    int capacity = 0;
    array_remove_prepare(source_name, position_value, count_value, has_count, &source_var, &logical_count, &offset, &remove_count);
    array_push_append_array_range(&items, &count, &capacity, source_name, 0, offset);
    array_push_append_array_range(&items, &count, &capacity, source_name, offset + remove_count, logical_count);
    if (count <= 0) {
        free_array_push_items(items, count);
        return;
    }
    for (index = 0; index < count; ++index) {
        if (index > 0) printf("%s", g_print_delimiter);
        if (b2026_var_is_null(&items[index])) printf("%s", g_print_null);
        else print_numeric_value(b2026_var_as_number(&items[index]));
    }
    free_array_push_items(items, count);
}

void apply_array_reverse_to_array(const char *target_name, const char *source_name) {
    ArrayVariable *source_var = get_array_variable(source_name);
    ArrayVariable *target_var;
    int logical_count = source_var != NULL ? array_logical_count(source_var) : 0;
    int index;
    B2026_Var *items = NULL;
    if (logical_count <= 0) {
        dim_array(target_name, (double)(option_base - 1));
        target_var = require_array_variable(target_name);
        target_var->read_count = 0;
        clear_array_runtime_metadata(target_var);
        return;
    }
    items = (B2026_Var *)calloc((size_t)logical_count, sizeof(B2026_Var));
    if (items == NULL) runtime_error("ARRAY() REVERSE out of memory / 【ARRAY】REVERSE処理用メモリを確保できません。");
    for (index = 0; index < logical_count; ++index) {
        copy_b2026_var(&items[index], &source_var->data[option_base + (logical_count - 1 - index)]);
    }
    dim_array(target_name, (double)(option_base + logical_count - 1));
    target_var = require_array_variable(target_name);
    for (index = 0; index < logical_count; ++index) {
        copy_b2026_var(&target_var->data[option_base + index], &items[index]);
    }
    target_var->read_count = logical_count;
    clear_array_runtime_metadata(target_var);
    free_array_push_items(items, logical_count);
}

static void shuffle_copy_runtime_metadata_snapshot(ArrayVariable *snapshot, const ArrayVariable *source_var) {
    memset(snapshot, 0, sizeof(*snapshot));
    if (source_var == NULL) return;
    snapshot->screen_width = source_var->screen_width;
    snapshot->screen_height = source_var->screen_height;
    snapshot->draw_pen = source_var->draw_pen;
    snapshot->stroke_pen = source_var->stroke_pen;
    snapshot->stroke_follows_pen = source_var->stroke_follows_pen;
    snapshot->stroke_tile_mode = source_var->stroke_tile_mode;
    snapshot->fill_pen = source_var->fill_pen;
    snapshot->fill_follows_pen = source_var->fill_follows_pen;
    snapshot->fill_tile_mode = source_var->fill_tile_mode;
    snapshot->draw_origin_x = source_var->draw_origin_x;
    snapshot->draw_origin_y = source_var->draw_origin_y;
    snapshot->draw_mode = source_var->draw_mode;
    snapshot->draw_scale_x = source_var->draw_scale_x;
    snapshot->draw_scale_y = source_var->draw_scale_y;
    snapshot->draw_rotate_deg = source_var->draw_rotate_deg;
    snapshot->draw_shear_x_deg = source_var->draw_shear_x_deg;
    snapshot->draw_shear_y_deg = source_var->draw_shear_y_deg;
    snapshot->draw_translate_x = source_var->draw_translate_x;
    snapshot->draw_translate_y = source_var->draw_translate_y;
    snapshot->draw_opacity = source_var->draw_opacity;
    snapshot->draw_blend_mode = source_var->draw_blend_mode;
    snapshot->draw_antialias = source_var->draw_antialias;
    snapshot->draw_antialias_pass = 0;
    snapshot->draw_text_align_h = source_var->draw_text_align_h;
    snapshot->draw_text_align_v = source_var->draw_text_align_v;
    snapshot->draw_line_width = source_var->draw_line_width;
    snapshot->draw_dash_count = source_var->draw_dash_count;
    snapshot->draw_dash_cap = source_var->draw_dash_cap;
    snapshot->draw_line_cap = source_var->draw_line_cap;
    snapshot->draw_line_join = source_var->draw_line_join;
    snapshot->draw_miter_limit = source_var->draw_miter_limit;
    snapshot->draw_fill_rule = source_var->draw_fill_rule;
    snapshot->draw_output_mode = source_var->draw_output_mode;
    snapshot->draw_clip_enabled = source_var->draw_clip_enabled;
    snapshot->draw_clip_rule = source_var->draw_clip_rule;
    snapshot->draw_clip_serial = source_var->draw_clip_serial;
    snapshot->draw_clip_mask_width = source_var->draw_clip_mask_width;
    snapshot->draw_clip_mask_height = source_var->draw_clip_mask_height;
    snapshot->draw_path_has_current = source_var->draw_path_has_current;
    snapshot->draw_path_current_x = source_var->draw_path_current_x;
    snapshot->draw_path_current_y = source_var->draw_path_current_y;
    snapshot->draw_path_has_subpath = source_var->draw_path_has_subpath;
    snapshot->draw_path_subpath_start_x = source_var->draw_path_subpath_start_x;
    snapshot->draw_path_subpath_start_y = source_var->draw_path_subpath_start_y;
    snapshot->image_phys_x = source_var->image_phys_x;
    snapshot->image_phys_y = source_var->image_phys_y;
    snapshot->image_phys_unit = source_var->image_phys_unit;
    snapshot->image_pixel_mode = source_var->image_pixel_mode;
    snapshot->image_value_max = source_var->image_value_max;
    snapshot->qr_version = source_var->qr_version;
    snapshot->qr_requested_version = source_var->qr_requested_version;
    snapshot->qr_requested_mask = source_var->qr_requested_mask;
    snapshot->qr_ecl = source_var->qr_ecl;
    snapshot->qr_encoding_kind = source_var->qr_encoding_kind;
    snapshot->qr_payload_kind = source_var->qr_payload_kind;
    snapshot->qr_mode_kind = source_var->qr_mode_kind;
    snapshot->qr_mask = source_var->qr_mask;
    snapshot->qr_margin_modules = source_var->qr_margin_modules;
    snapshot->qr_logo_margin_modules = source_var->qr_logo_margin_modules;
    snapshot->qr_module_scale = source_var->qr_module_scale;
    snapshot->qr_data_bytes = source_var->qr_data_bytes;
    if (source_var->qr_text != NULL) {
        snapshot->qr_text = strdup(source_var->qr_text);
        if (snapshot->qr_text == NULL) runtime_error("SHUFFLE() metadata copy out of memory / 【SHUFFLE関数エラー】QRテキスト用メモリを確保できません。");
    }
    snapshot->fft_width = source_var->fft_width;
    snapshot->fft_height = source_var->fft_height;
    snapshot->fft_original_width = source_var->fft_original_width;
    snapshot->fft_original_height = source_var->fft_original_height;
    snapshot->fft_is_complex = source_var->fft_is_complex;
    snapshot->fft_channels = source_var->fft_channels;
    snapshot->fft_restore_255 = source_var->fft_restore_255;
    if (source_var->draw_dash_count > 0 && source_var->draw_dash_pattern != NULL) {
        snapshot->draw_dash_pattern = (double *)malloc(sizeof(double) * (size_t)source_var->draw_dash_count);
        if (snapshot->draw_dash_pattern == NULL) runtime_error("SHUFFLE() metadata copy out of memory / 【SHUFFLE関数エラー】メタ情報コピー用メモリを確保できません。");
        memcpy(snapshot->draw_dash_pattern, source_var->draw_dash_pattern, sizeof(double) * (size_t)source_var->draw_dash_count);
    }
    if (source_var->draw_path_count > 0 && source_var->draw_path_segments != NULL) {
        snapshot->draw_path_segments = (DrawPathSegment *)malloc(sizeof(DrawPathSegment) * (size_t)source_var->draw_path_count);
        if (snapshot->draw_path_segments == NULL) runtime_error("SHUFFLE() metadata copy out of memory / 【SHUFFLE関数エラー】メタ情報コピー用メモリを確保できません。");
        memcpy(snapshot->draw_path_segments, source_var->draw_path_segments, sizeof(DrawPathSegment) * (size_t)source_var->draw_path_count);
        snapshot->draw_path_count = source_var->draw_path_count;
        snapshot->draw_path_capacity = source_var->draw_path_count;
    }
    if (source_var->draw_clip_count > 0 && source_var->draw_clip_segments != NULL) {
        snapshot->draw_clip_segments = (DrawPathSegment *)malloc(sizeof(DrawPathSegment) * (size_t)source_var->draw_clip_count);
        if (snapshot->draw_clip_segments == NULL) runtime_error("SHUFFLE() metadata copy out of memory / 【SHUFFLE関数エラー】CLIP メタ情報コピー用メモリを確保できません。");
        memcpy(snapshot->draw_clip_segments, source_var->draw_clip_segments, sizeof(DrawPathSegment) * (size_t)source_var->draw_clip_count);
        snapshot->draw_clip_count = source_var->draw_clip_count;
        snapshot->draw_clip_capacity = source_var->draw_clip_count;
    }
    if (source_var->draw_clip_enabled && source_var->draw_clip_mask != NULL && source_var->draw_clip_mask_width > 0 && source_var->draw_clip_mask_height > 0) {
        size_t clip_bytes = (size_t)source_var->draw_clip_mask_width * (size_t)source_var->draw_clip_mask_height;
        snapshot->draw_clip_mask = (unsigned char *)malloc(clip_bytes);
        if (snapshot->draw_clip_mask == NULL) runtime_error("SHUFFLE() metadata copy out of memory / 【SHUFFLE関数エラー】CLIP マスクコピー用メモリを確保できません。");
        memcpy(snapshot->draw_clip_mask, source_var->draw_clip_mask, clip_bytes);
    }
}

static void shuffle_free_runtime_metadata_snapshot(ArrayVariable *snapshot) {
    if (snapshot == NULL) return;
    free(snapshot->draw_dash_pattern);
    snapshot->draw_dash_pattern = NULL;
    free(snapshot->draw_path_segments);
    snapshot->draw_path_segments = NULL;
    free(snapshot->draw_clip_segments);
    snapshot->draw_clip_segments = NULL;
    free(snapshot->draw_clip_mask);
    snapshot->draw_clip_mask = NULL;
    free(snapshot->qr_text);
    snapshot->qr_text = NULL;
    snapshot->draw_dash_count = 0;
    snapshot->draw_path_count = 0;
    snapshot->draw_path_capacity = 0;
    snapshot->draw_clip_count = 0;
    snapshot->draw_clip_capacity = 0;
}

static void shuffle_restore_runtime_metadata(ArrayVariable *target_var, const ArrayVariable *snapshot) {
    if (target_var == NULL || snapshot == NULL) return;
    target_var->screen_width = snapshot->screen_width;
    target_var->screen_height = snapshot->screen_height;
    draw_copy_canvas_state(target_var, snapshot);
    target_var->image_phys_x = snapshot->image_phys_x;
    target_var->image_phys_y = snapshot->image_phys_y;
    target_var->image_phys_unit = snapshot->image_phys_unit;
    target_var->image_pixel_mode = snapshot->image_pixel_mode;
    target_var->image_value_max = snapshot->image_value_max;
    target_var->qr_version = snapshot->qr_version;
    target_var->qr_requested_version = snapshot->qr_requested_version;
    target_var->qr_requested_mask = snapshot->qr_requested_mask;
    target_var->qr_ecl = snapshot->qr_ecl;
    target_var->qr_encoding_kind = snapshot->qr_encoding_kind;
    target_var->qr_payload_kind = snapshot->qr_payload_kind;
    target_var->qr_mode_kind = snapshot->qr_mode_kind;
    target_var->qr_mask = snapshot->qr_mask;
    target_var->qr_margin_modules = snapshot->qr_margin_modules;
    target_var->qr_logo_margin_modules = snapshot->qr_logo_margin_modules;
    target_var->qr_module_scale = snapshot->qr_module_scale;
    target_var->qr_data_bytes = snapshot->qr_data_bytes;
    free(target_var->qr_text);
    target_var->qr_text = snapshot->qr_text != NULL ? strdup(snapshot->qr_text) : NULL;
    if (snapshot->qr_text != NULL && target_var->qr_text == NULL) runtime_error("SHUFFLE() metadata restore out of memory / 【SHUFFLE関数エラー】QRテキスト復元用メモリを確保できません。");
    target_var->fft_width = snapshot->fft_width;
    target_var->fft_height = snapshot->fft_height;
    target_var->fft_original_width = snapshot->fft_original_width;
    target_var->fft_original_height = snapshot->fft_original_height;
    target_var->fft_is_complex = snapshot->fft_is_complex;
    target_var->fft_channels = snapshot->fft_channels;
    target_var->fft_restore_255 = snapshot->fft_restore_255;
}

void apply_shuffle_array_to_array(const char *target_name, const char *source_name) {
    ArrayVariable *source_var = get_array_variable(source_name);
    ArrayVariable *target_var;
    ArrayVariable metadata_snapshot;
    int logical_count = source_var != NULL ? array_logical_count(source_var) : 0;
    int is_pixel = 0;
    int unit_size = 1;
    int unit_count = 0;
    int index;
    B2026_Var *items = NULL;
    int *order = NULL;
    if (source_var != NULL && source_var->screen_width > 0 && source_var->screen_height > 0 && logical_count >= source_var->screen_width * source_var->screen_height * 4) {
        is_pixel = 1;
        unit_size = 4;
    }
    if (logical_count <= 0 || source_var == NULL) {
        dim_array(target_name, (double)(option_base - 1));
        target_var = require_array_variable(target_name);
        target_var->read_count = 0;
        clear_array_runtime_metadata(target_var);
        return;
    }
    if (is_pixel && (logical_count % 4) != 0) runtime_error("SHUFFLE() pixel array size is invalid / 【SHUFFLE関数エラー】ピクセル配列の要素数が不正です。");
    unit_count = logical_count / unit_size;
    items = (B2026_Var *)calloc((size_t)logical_count, sizeof(B2026_Var));
    order = (int *)malloc(sizeof(int) * (size_t)unit_count);
    if (items == NULL || order == NULL) runtime_error("SHUFFLE() array out of memory / 【SHUFFLE関数エラー】配列シャッフル用メモリを確保できません。");
    for (index = 0; index < unit_count; ++index) order[index] = index;
    for (index = unit_count - 1; index > 0; --index) {
        int swap_index = b2026_random_index(index + 1);
        int temp = order[index];
        order[index] = order[swap_index];
        order[swap_index] = temp;
    }
    for (index = 0; index < unit_count; ++index) {
        int src_unit = order[index];
        int sub_index;
        for (sub_index = 0; sub_index < unit_size; ++sub_index) {
            copy_b2026_var(&items[index * unit_size + sub_index], &source_var->data[option_base + src_unit * unit_size + sub_index]);
        }
    }
    memset(&metadata_snapshot, 0, sizeof(metadata_snapshot));
    if (is_pixel) shuffle_copy_runtime_metadata_snapshot(&metadata_snapshot, source_var);
    dim_array(target_name, (double)(option_base + logical_count - 1));
    target_var = require_array_variable(target_name);
    for (index = 0; index < logical_count; ++index) {
        copy_b2026_var(&target_var->data[option_base + index], &items[index]);
    }
    target_var->read_count = logical_count;
    if (is_pixel) shuffle_restore_runtime_metadata(target_var, &metadata_snapshot);
    else clear_array_runtime_metadata(target_var);
    shuffle_free_runtime_metadata_snapshot(&metadata_snapshot);
    free_array_push_items(items, logical_count);
    free(order);
}

void apply_unshuffle_array_to_array(const char *target_name, const char *source_name) {
    ArrayVariable *source_var = get_array_variable(source_name);
    ArrayVariable *target_var;
    ArrayVariable metadata_snapshot;
    int logical_count = source_var != NULL ? array_logical_count(source_var) : 0;
    int is_pixel = 0;
    int unit_size = 1;
    int unit_count = 0;
    int index;
    B2026_Var *items = NULL;
    int *order = NULL;
    if (source_var != NULL && source_var->screen_width > 0 && source_var->screen_height > 0 && logical_count >= source_var->screen_width * source_var->screen_height * 4) {
        is_pixel = 1;
        unit_size = 4;
    }
    if (logical_count <= 0 || source_var == NULL) {
        dim_array(target_name, (double)(option_base - 1));
        target_var = require_array_variable(target_name);
        target_var->read_count = 0;
        clear_array_runtime_metadata(target_var);
        return;
    }
    if (is_pixel && (logical_count % 4) != 0) runtime_error("UNSHUFFLE() pixel array size is invalid / 【UNSHUFFLE関数エラー】ピクセル配列の要素数が不正です。");
    unit_count = logical_count / unit_size;
    items = (B2026_Var *)calloc((size_t)logical_count, sizeof(B2026_Var));
    order = (int *)malloc(sizeof(int) * (size_t)unit_count);
    if (items == NULL || order == NULL) runtime_error("UNSHUFFLE() array out of memory / 【UNSHUFFLE関数エラー】配列復元用メモリを確保できません。");
    for (index = 0; index < unit_count; ++index) order[index] = index;
    for (index = unit_count - 1; index > 0; --index) {
        int swap_index = b2026_random_index(index + 1);
        int temp = order[index];
        order[index] = order[swap_index];
        order[swap_index] = temp;
    }
    for (index = 0; index < unit_count; ++index) {
        int dest_unit = order[index];
        int sub_index;
        for (sub_index = 0; sub_index < unit_size; ++sub_index) {
            copy_b2026_var(&items[dest_unit * unit_size + sub_index], &source_var->data[option_base + index * unit_size + sub_index]);
        }
    }
    memset(&metadata_snapshot, 0, sizeof(metadata_snapshot));
    if (is_pixel) shuffle_copy_runtime_metadata_snapshot(&metadata_snapshot, source_var);
    dim_array(target_name, (double)(option_base + logical_count - 1));
    target_var = require_array_variable(target_name);
    for (index = 0; index < logical_count; ++index) {
        copy_b2026_var(&target_var->data[option_base + index], &items[index]);
    }
    target_var->read_count = logical_count;
    if (is_pixel) shuffle_restore_runtime_metadata(target_var, &metadata_snapshot);
    else clear_array_runtime_metadata(target_var);
    shuffle_free_runtime_metadata_snapshot(&metadata_snapshot);
    free_array_push_items(items, logical_count);
    free(order);
}

void apply_shuffle_string_array_to_string_array(const char *target_name, const char *source_name) {
    ArrayVariable *source_var = get_array_variable(source_name);
    ArrayVariable *target_var;
    int logical_count = source_var != NULL ? array_logical_count(source_var) : 0;
    int index;
    B2026_Var *items = NULL;
    int *order = NULL;
    if (logical_count <= 0 || source_var == NULL) {
        dim_array(target_name, (double)(option_base - 1));
        target_var = require_array_variable(target_name);
        target_var->read_count = 0;
        clear_array_runtime_metadata(target_var);
        return;
    }
    items = (B2026_Var *)calloc((size_t)logical_count, sizeof(B2026_Var));
    order = (int *)malloc(sizeof(int) * (size_t)logical_count);
    if (items == NULL || order == NULL) runtime_error("SHUFFLE() string array out of memory / 【SHUFFLE関数エラー】文字列配列シャッフル用メモリを確保できません。");
    for (index = 0; index < logical_count; ++index) order[index] = index;
    for (index = logical_count - 1; index > 0; --index) {
        int swap_index = b2026_random_index(index + 1);
        int temp = order[index];
        order[index] = order[swap_index];
        order[swap_index] = temp;
    }
    for (index = 0; index < logical_count; ++index) {
        copy_b2026_var(&items[index], &source_var->data[option_base + order[index]]);
    }
    dim_array(target_name, (double)(option_base + logical_count - 1));
    target_var = require_array_variable(target_name);
    for (index = 0; index < logical_count; ++index) {
        copy_b2026_var(&target_var->data[option_base + index], &items[index]);
    }
    target_var->read_count = logical_count;
    clear_array_runtime_metadata(target_var);
    free_array_push_items(items, logical_count);
    free(order);
}

void apply_unshuffle_string_array_to_string_array(const char *target_name, const char *source_name) {
    ArrayVariable *source_var = get_array_variable(source_name);
    ArrayVariable *target_var;
    int logical_count = source_var != NULL ? array_logical_count(source_var) : 0;
    int index;
    B2026_Var *items = NULL;
    int *order = NULL;
    if (logical_count <= 0 || source_var == NULL) {
        dim_array(target_name, (double)(option_base - 1));
        target_var = require_array_variable(target_name);
        target_var->read_count = 0;
        clear_array_runtime_metadata(target_var);
        return;
    }
    items = (B2026_Var *)calloc((size_t)logical_count, sizeof(B2026_Var));
    order = (int *)malloc(sizeof(int) * (size_t)logical_count);
    if (items == NULL || order == NULL) runtime_error("UNSHUFFLE() string array out of memory / 【UNSHUFFLE関数エラー】文字列配列復元用メモリを確保できません。");
    for (index = 0; index < logical_count; ++index) order[index] = index;
    for (index = logical_count - 1; index > 0; --index) {
        int swap_index = b2026_random_index(index + 1);
        int temp = order[index];
        order[index] = order[swap_index];
        order[swap_index] = temp;
    }
    for (index = 0; index < logical_count; ++index) {
        copy_b2026_var(&items[order[index]], &source_var->data[option_base + index]);
    }
    dim_array(target_name, (double)(option_base + logical_count - 1));
    target_var = require_array_variable(target_name);
    for (index = 0; index < logical_count; ++index) {
        copy_b2026_var(&target_var->data[option_base + index], &items[index]);
    }
    target_var->read_count = logical_count;
    clear_array_runtime_metadata(target_var);
    free_array_push_items(items, logical_count);
    free(order);
}

void apply_array_reverse_to_hash(const char *target_name, const char *source_name) {
    ArrayVariable *source_var = get_array_variable(source_name);
    int logical_count = source_var != NULL ? array_logical_count(source_var) : 0;
    int index;
    Frame *target_frame = get_hash_frame(target_name);
    HashVariable *target_var = ensure_local_hash(target_frame, target_name);
    char key_buffer[64];
    clear_hash_entries(target_var);
    for (index = 0; index < logical_count; ++index) {
        snprintf(key_buffer, sizeof(key_buffer), "%d", index + 1);
        if (b2026_var_is_null(&source_var->data[option_base + (logical_count - 1 - index)])) hash_set_number(target_name, key_buffer, null_value());
        else hash_set_number(target_name, key_buffer, b2026_var_as_number(&source_var->data[option_base + (logical_count - 1 - index)]));
    }
}

void print_array_reverse(const char *source_name) {
    ArrayVariable *source_var = get_array_variable(source_name);
    int logical_count = source_var != NULL ? array_logical_count(source_var) : 0;
    int index;
    for (index = 0; index < logical_count; ++index) {
        if (index > 0) printf("%s", g_print_delimiter);
        if (b2026_var_is_null(&source_var->data[option_base + (logical_count - 1 - index)])) printf("%s", g_print_null);
        else print_numeric_value(b2026_var_as_number(&source_var->data[option_base + (logical_count - 1 - index)]));
    }
}

static void collect_array_unique_items(const char *source_name, B2026_Var **items_out, int *count_out) {
    ArrayVariable *source_var = get_array_variable(source_name);
    int logical_count = source_var != NULL ? array_logical_count(source_var) : 0;
    B2026_Var *items = NULL;
    int count = 0;
    int capacity = 0;
    int index;
    if (items_out != NULL) *items_out = NULL;
    if (count_out != NULL) *count_out = 0;
    for (index = 0; index < logical_count; ++index) {
        B2026_Var *slot = &source_var->data[option_base + index];
        int existing;
        int duplicate = 0;
        for (existing = 0; existing < count; ++existing) {
            if (b2026_var_equals(&items[existing], slot)) {
                duplicate = 1;
                break;
            }
        }
        if (duplicate) continue;
        if (count >= capacity) {
            int new_capacity = capacity <= 0 ? 8 : capacity * 2;
            B2026_Var *new_items = (B2026_Var *)realloc(items, sizeof(B2026_Var) * (size_t)new_capacity);
            if (new_items == NULL) runtime_error("ARRAY() UNIQUE out of memory / 【ARRAY】UNIQUE処理用メモリを確保できません。");
            items = new_items;
            for (existing = capacity; existing < new_capacity; ++existing) {
                items[existing].type = 0;
                items[existing].data.number = 0.0;
            }
            capacity = new_capacity;
        }
        copy_b2026_var(&items[count], slot);
        ++count;
    }
    if (items_out != NULL) *items_out = items;
    if (count_out != NULL) *count_out = count;
}

void apply_array_unique_to_array(const char *target_name, const char *source_name) {
    B2026_Var *items = NULL;
    int count = 0;
    int index;
    ArrayVariable *target_var;
    collect_array_unique_items(source_name, &items, &count);
    if (count <= 0) {
        dim_array(target_name, (double)(option_base - 1));
        target_var = require_array_variable(target_name);
        target_var->read_count = 0;
        clear_array_runtime_metadata(target_var);
        free_array_push_items(items, count);
        return;
    }
    dim_array(target_name, (double)(option_base + count - 1));
    target_var = require_array_variable(target_name);
    for (index = 0; index < count; ++index) {
        copy_b2026_var(&target_var->data[option_base + index], &items[index]);
    }
    target_var->read_count = count;
    clear_array_runtime_metadata(target_var);
    free_array_push_items(items, count);
}

void apply_array_unique_to_hash(const char *target_name, const char *source_name) {
    B2026_Var *items = NULL;
    int count = 0;
    int index;
    Frame *target_frame = get_hash_frame(target_name);
    HashVariable *target_var = ensure_local_hash(target_frame, target_name);
    char key_buffer[64];
    clear_hash_entries(target_var);
    collect_array_unique_items(source_name, &items, &count);
    for (index = 0; index < count; ++index) {
        snprintf(key_buffer, sizeof(key_buffer), "%d", index + 1);
        if (b2026_var_is_null(&items[index])) hash_set_number(target_name, key_buffer, null_value());
        else hash_set_number(target_name, key_buffer, b2026_var_as_number(&items[index]));
    }
    free_array_push_items(items, count);
}

void print_array_unique(const char *source_name) {
    B2026_Var *items = NULL;
    int count = 0;
    int index;
    collect_array_unique_items(source_name, &items, &count);
    for (index = 0; index < count; ++index) {
        if (index > 0) printf("%s", g_print_delimiter);
        if (b2026_var_is_null(&items[index])) printf("%s", g_print_null);
        else print_numeric_value(b2026_var_as_number(&items[index]));
    }
    free_array_push_items(items, count);
}

static int array_pixel_unique_mode_rgb_only(const char *mode_text) {
    if (mode_text == NULL || *mode_text == '\0') return 0;
    return b2026_strcasecmp(mode_text, "RGB") == 0;
}

static unsigned long long array_pixel_unique_double_key(double value) {
    unsigned long long bits = 0ULL;
    if (is_null_number(value)) return 0x7ff8000000000001ULL;
    if (value == 0.0) return 0ULL;
    memcpy(&bits, &value, sizeof(bits));
    return bits;
}

static unsigned long long array_pixel_unique_color_hash(double r, double g, double b, double a, int rgb_only) {
    unsigned long long hash = 1469598103934665603ULL;
    unsigned long long key;
    key = array_pixel_unique_double_key(r); hash ^= key; hash *= 1099511628211ULL;
    key = array_pixel_unique_double_key(g); hash ^= key; hash *= 1099511628211ULL;
    key = array_pixel_unique_double_key(b); hash ^= key; hash *= 1099511628211ULL;
    if (!rgb_only) {
        key = array_pixel_unique_double_key(a); hash ^= key; hash *= 1099511628211ULL;
    }
    return hash;
}

static void collect_array_pixel_unique_items(const char *source_name, const char *mode_text, B2026_Var **items_out, int *count_out, ArrayVariable **source_var_out) {
    ArrayVariable *source_var = get_array_variable(source_name);
    int logical_count = source_var != NULL ? array_logical_count(source_var) : 0;
    int rgb_only = array_pixel_unique_mode_rgb_only(mode_text);
    int pixel_total = logical_count / 4;
    B2026_Var *items = NULL;
    int *heads = NULL;
    int *nexts = NULL;
    int bucket_count = 0;
    int count = 0;
    int capacity = 0;
    int pixel_index;
    if (items_out != NULL) *items_out = NULL;
    if (count_out != NULL) *count_out = 0;
    if (source_var_out != NULL) *source_var_out = source_var;
    if (source_var == NULL || source_var->screen_width <= 0 || source_var->screen_height <= 0) {
        runtime_error("ARRAY(\"ピクセル配列の重複除去\") requires a pixel array / 【ARRAY】ピクセル配列の重複除去には SCREEN または LOADIMAGE で初期化されたピクセル配列が必要です。");
    }
    if ((logical_count % 4) != 0) {
        runtime_error("ARRAY(\"ピクセル配列の重複除去\") pixel array size is invalid / 【ARRAY】ピクセル配列の重複除去で配列要素数が4の倍数ではありません。");
    }
    bucket_count = pixel_total > 0 ? (pixel_total * 2 + 1) : 1;
    heads = (int *)malloc(sizeof(int) * (size_t)bucket_count);
    nexts = pixel_total > 0 ? (int *)malloc(sizeof(int) * (size_t)pixel_total) : NULL;
    if (heads == NULL || (pixel_total > 0 && nexts == NULL)) runtime_error("ARRAY() pixel unique out of memory / 【ARRAY】ピクセル配列の重複除去用メモリを確保できません。");
    for (pixel_index = 0; pixel_index < bucket_count; ++pixel_index) heads[pixel_index] = -1;
    for (pixel_index = 0; pixel_index < pixel_total; ++pixel_index) {
        int existing;
        int duplicate = 0;
        int src = option_base + pixel_index * 4;
        int bucket;
        unsigned long long hash_value;
        double r = b2026_var_as_number(&source_var->data[src + 0]);
        double g = b2026_var_as_number(&source_var->data[src + 1]);
        double b = b2026_var_as_number(&source_var->data[src + 2]);
        double a = b2026_var_as_number(&source_var->data[src + 3]);
        hash_value = array_pixel_unique_color_hash(r, g, b, a, rgb_only);
        bucket = (int)(hash_value % (unsigned long long)bucket_count);
        existing = heads[bucket];
        while (existing >= 0) {
            int dst = existing * 4;
            if (value_eq(b2026_var_as_number(&items[dst + 0]), r) != 0.0 &&
                value_eq(b2026_var_as_number(&items[dst + 1]), g) != 0.0 &&
                value_eq(b2026_var_as_number(&items[dst + 2]), b) != 0.0 &&
                (rgb_only || value_eq(b2026_var_as_number(&items[dst + 3]), a) != 0.0)) {
                duplicate = 1;
                break;
            }
            existing = nexts[existing];
        }
        if (duplicate) continue;
        if (count >= capacity) {
            int new_capacity = capacity <= 0 ? 8 : capacity * 2;
            B2026_Var *new_items = (B2026_Var *)realloc(items, sizeof(B2026_Var) * (size_t)(new_capacity * 4));
            if (new_items == NULL) runtime_error("ARRAY() pixel unique out of memory / 【ARRAY】ピクセル配列の重複除去用メモリを確保できません。");
            items = new_items;
            for (existing = capacity * 4; existing < new_capacity * 4; ++existing) {
                items[existing].type = 0;
                items[existing].data.number = 0.0;
            }
            capacity = new_capacity;
        }
        set_b2026_number(&items[count * 4 + 0], r);
        set_b2026_number(&items[count * 4 + 1], g);
        set_b2026_number(&items[count * 4 + 2], b);
        set_b2026_number(&items[count * 4 + 3], a);
        nexts[count] = heads[bucket];
        heads[bucket] = count;
        ++count;
    }
    free(heads);
    free(nexts);
    if (items_out != NULL) *items_out = items;
    if (count_out != NULL) *count_out = count;
}

void apply_array_pixel_unique_to_array(const char *target_name, const char *source_name, const char *mode_text) {
    B2026_Var *items = NULL;
    int count = 0;
    int index;
    ArrayVariable *target_var;
    ArrayVariable *source_var = NULL;
    int source_pixel_mode = 0;
    double source_value_max = 0.0;
    if (mode_text != NULL && *mode_text != '\0' &&
        b2026_strcasecmp(mode_text, "RGB") != 0 &&
        b2026_strcasecmp(mode_text, "RGBA") != 0) {
        runtime_error("ARRAY(\"ピクセル配列の重複除去\") mode must be RGB or RGBA / 【ARRAY】ピクセル配列の重複除去モードは RGB または RGBA を指定してください。");
    }
    collect_array_pixel_unique_items(source_name, mode_text, &items, &count, &source_var);
    if (source_var != NULL) {
        source_pixel_mode = source_var->image_pixel_mode;
        source_value_max = source_var->image_value_max;
    }
    if (count <= 0) {
        dim_array(target_name, (double)(option_base - 1));
        target_var = require_array_variable(target_name);
        target_var->read_count = 0;
        clear_array_runtime_metadata(target_var);
        if (source_var != NULL) {
            target_var->image_pixel_mode = source_pixel_mode;
            target_var->image_value_max = source_value_max;
        }
        free(items);
        return;
    }
    dim_array(target_name, (double)(option_base + count * 4 - 1));
    target_var = require_array_variable(target_name);
    for (index = 0; index < count * 4; ++index) {
        copy_b2026_var(&target_var->data[option_base + index], &items[index]);
    }
    target_var->read_count = count * 4;
    clear_array_runtime_metadata(target_var);
    target_var->screen_width = count;
    target_var->screen_height = 1;
    if (source_var != NULL) {
        target_var->image_pixel_mode = source_pixel_mode;
        target_var->image_value_max = source_value_max;
    }
    free(items);
}

