{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "arrays-0",
      "metadata": {},
      "source": "# Aner typed arrays\n\nChoose **Run All** with the compatible native interpreter. Enable **Dev** and open **Inspector** to see dtype, length, capacity and sorted search status. Arrays need no imports. Read `docs/ARRAYS.md` for time/space complexity and numerical limits.\n"
    },
    {
      "cell_type": "code",
      "id": "arrays-1",
      "metadata": {},
      "source": [
        "let values = DynamicArray<Int64>(capacity: 2)\n",
        "values.append(30)\n",
        "values.append(10)\n",
        "values.append(20)\n",
        "print(values.len())\n",
        "print(values.capacity())\n",
        "print(values.find(20))\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "id": "arrays-2",
      "metadata": {},
      "source": "## Sorted search and mutation\n\nBefore sorting, `find` uses a linear scan and returns index 2. After sorting it uses binary search and returns index 1. Setting index zero to 99 breaks ascending order, so searching switches back to a linear scan.\n"
    },
    {
      "cell_type": "code",
      "id": "arrays-3",
      "metadata": {},
      "source": [
        "values.sort()\n",
        "print(values.is_sorted())\n",
        "print(values.find(20))\n",
        "values.set(0, 99)\n",
        "print(values.is_sorted())\n",
        "print(values.find(20))\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "id": "arrays-4",
      "metadata": {},
      "source": "## Fixed storage and independent copies\n\nFixed arrays allow element changes but cannot grow. `copy()` retains capacity and creates independent storage.\n"
    },
    {
      "cell_type": "code",
      "id": "arrays-5",
      "metadata": {},
      "source": [
        "let fixed = Array<Float64>(size: 3, fill: 0.0)\n",
        "fixed.set(0, 1.0)\n",
        "fixed.set(1, 2.0)\n",
        "fixed.set(2, 3.0)\n",
        "let copy = fixed.copy()\n",
        "copy.set(0, 50.0)\n",
        "print(fixed.to_list())\n",
        "print(copy.to_list())\n",
        "print(fixed.mean())\n",
        "print(fixed.dot(fixed))\n",
        "print(fixed.cumsum().to_list())\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "id": "arrays-6",
      "metadata": {},
      "source": "## Growth without changing length\n\n`reserve` grows capacity directly. `resize` initializes newly visible elements. Clearing retains capacity. String elements have their own text copy and destruction costs.\n"
    },
    {
      "cell_type": "code",
      "id": "arrays-7",
      "metadata": {},
      "source": [
        "values.reserve(capacity: 16)\n",
        "values.resize(size: 5, fill: 0)\n",
        "print(values.to_list())\n",
        "values.clear()\n",
        "print(values.len())\n",
        "print(values.capacity())\n",
        "let words = DynamicArray<String>(capacity: 2)\n",
        "words.append(\"pear\")\n",
        "words.append(\"apple\")\n",
        "words.sort()\n",
        "print(words.to_list())\n"
      ],
      "execution_count": null,
      "outputs": []
    }
  ],
  "metadata": {
    "aner": {
      "execution_mode": "persistent-session",
      "runtime_mode": "development"
    },
    "kernelspec": {
      "display_name": "Aner",
      "language": "aner",
      "name": "aner"
    },
    "language_info": {
      "name": "aner",
      "file_extension": ".aner"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
