"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.IndexedReadWriteXYCollection = exports.IndexedXYCollection = void 0;
/** @packageDocumentation
 * @module ArraysAndInterfaces
 */
const Point2dVector2d_1 = require("./Point2dVector2d");
/**
 * Abstract base class for access to XY data with indexed reference.
 * * This allows algorithms to work with Point2d[] or GrowableXY.
 *   * GrowableXYArray implements these for its data.
 *   * Point2dArrayCarrier carries a (reference to) a Point2d[] and implements the methods with calls on that array reference.
 * * In addition to "point by point" accessors, other abstract members compute commonly useful vector data "between points".
 * * Methods that create vectors among multiple indices allow callers to avoid creating temporaries.
 * @public
 */
class IndexedXYCollection {
    static _workPoint;
    /**
     * Access x of indexed point.
     * * Subclasses may wish to override with a more efficient implementation.
     */
    getXAtUncheckedPointIndex(pointIndex) {
        const pt = this.getPoint2dAtCheckedPointIndex(pointIndex, IndexedXYCollection._workPoint);
        if (undefined === IndexedXYCollection._workPoint)
            IndexedXYCollection._workPoint = pt; // allocate the cache
        return pt ? pt.x : 0.0;
    }
    /**
     * Access y of indexed point.
     * * Subclasses may wish to override with a more efficient implementation.
     */
    getYAtUncheckedPointIndex(pointIndex) {
        const pt = this.getPoint2dAtCheckedPointIndex(pointIndex, IndexedXYCollection._workPoint);
        if (undefined === IndexedXYCollection._workPoint)
            IndexedXYCollection._workPoint = pt; // allocate the cache
        return pt ? pt.y : 0.0;
    }
    /**
     * Compute the linear combination s of the indexed p_i and given scales s_i.
     * @param scales array of scales. For best results, scales should have same length as the instance.
     * @param result optional pre-allocated object to fill and return.
     * @return s = sum(p_i * s_i), where i ranges from 0 to min(this.length, scales.length).
     */
    linearCombination(scales, result) {
        const n = Math.min(this.length, scales.length);
        const sum = (result instanceof Point2dVector2d_1.Vector2d) ? Point2dVector2d_1.Vector2d.createZero(result) : Point2dVector2d_1.Point2d.createZero(result);
        for (let i = 0; i < n; ++i) {
            sum.x += scales[i] * this.getXAtUncheckedPointIndex(i);
            sum.y += scales[i] * this.getYAtUncheckedPointIndex(i);
        }
        return sum;
    }
}
exports.IndexedXYCollection = IndexedXYCollection;
/**
 * Abstract base class extends [[IndexedXYCollection]] with methods that modify the collection.
 * @public
 */
class IndexedReadWriteXYCollection extends IndexedXYCollection {
}
exports.IndexedReadWriteXYCollection = IndexedReadWriteXYCollection;
//# sourceMappingURL=IndexedXYCollection.js.map