
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/


/**
 * AUTO-GENERATED FILE. DO NOT MODIFY.
 */

/**
 * 高性能可扩容 TypedArray 封装
 * @template T 具体 TypedArray 类型，如 Float64Array、Uint32Array 等
 */
var DynamicTypedArray = /** @class */function () {
  /**
   * @param ctor 类型化数组构造器，例如 Float64Array
   * @param initialCapacity 初始容量（默认 1024，避免小数据频繁扩容）
   */
  function DynamicTypedArray(initialCapacity) {
    if (initialCapacity === void 0) {
      initialCapacity = 1024;
    }
    this._len = 0; // 当前有效元素个数
    this._CTOR = Float64Array;
    this._buf = new this._CTOR(initialCapacity);
  }
  // ========== 基础属性 ==========
  DynamicTypedArray.prototype.length = function () {
    return this._len;
  };
  // get capacity(): number { return this._buf.length; }
  DynamicTypedArray.prototype.buffer = function () {
    return this._buf;
  }; // 直接暴露，极致性能
  // get typedArray(): T { return this._buf.subarray(0, this._len) as T; } // 当前有效部分视图（共享内存）
  // ========== 扩容控制 ==========
  /** 确保至少能容纳 extra 个额外元素 */
  DynamicTypedArray.prototype.reserve = function (extra) {
    var required = this._len + extra;
    if (required > this._buf.length) {
      this._grow(required);
    }
  };
  /** 内部扩容，翻倍直到满足 required */
  DynamicTypedArray.prototype._grow = function (required) {
    var newCap = this._buf.length;
    while (newCap < required) newCap *= 2;
    var newBuf = new this._CTOR(newCap);
    newBuf.set(this._buf.subarray(0, this._len));
    this._buf = newBuf;
  };
  // ========== 单元素操作 ==========
  /** 末尾追加一个值 */
  DynamicTypedArray.prototype.push = function (value) {
    if (this._len >= this._buf.length) this._grow(this._len + 1);
    this._buf[this._len++] = value;
    return this._len;
  };
  /** 弹出最后一个值 */
  DynamicTypedArray.prototype.pop = function () {
    if (this._len === 0) return undefined;
    this._len--;
    return this._buf[this._len];
  };
  /** 读取索引（支持负数） */
  DynamicTypedArray.prototype.at = function (index) {
    if (index < 0) index += this._len;
    return index >= 0 && index < this._len ? this._buf[index] : undefined;
  };
  /** 写入索引 */
  DynamicTypedArray.prototype.setAt = function (index, value) {
    if (index < 0) index += this._len;
    if (index < 0 || index >= this._len) throw new RangeError('Index out of bounds');
    this._buf[index] = value;
  };
  // ========== 批量追加（高性能核心） ==========
  /**
   * 批量追加，利用原生 set 实现内存级拷贝。
   * @param source 数值数组或另一 TypedArray
   */
  DynamicTypedArray.prototype.pushMany = function (source) {
    var addLen = source.length;
    if (addLen === 0) return this._len;
    this.reserve(addLen);
    // TypedArray 的 set 方法兼容普通数组（会做数值转换）
    this._buf.set(source, this._len);
    this._len += addLen;
    return this._len;
  };
  // ========== 功能性遍历方法（均直接操作缓冲区） ==========
  DynamicTypedArray.prototype.forEach = function (callback) {
    var buf = this._buf;
    for (var i = 0; i < this._len; i++) {
      callback(buf[i], i, this);
    }
  };
  DynamicTypedArray.prototype[Symbol.iterator] = function () {
    var _a;
    var i = 0;
    var buf = this._buf;
    var len = this._len;
    return _a = {
      next: function () {
        if (i < len) {
          return {
            value: buf[i++],
            done: false
          };
        }
        return {
          value: undefined,
          done: true
        };
      }
    },
    // 可选，兼容 for...of
    _a[Symbol.iterator] = function () {
      return this;
    }, _a;
  };
  // ========== 导出原生 TypedArray ==========
  /** 返回一份新的 TypedArray（拷贝） */
  DynamicTypedArray.prototype.toTypedArray = function () {
    var copy = new this._CTOR(this._len);
    copy.set(this._buf.subarray(0, this._len));
    return copy;
  };
  /** 清空 */
  DynamicTypedArray.prototype.clear = function () {
    this._len = 0;
  };
  return DynamicTypedArray;
}();
export { DynamicTypedArray };