/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module RangeSearch
 */
import { Range2d } from "../../geometry3d/Range";
/**
 * An array of decorated Range2d.
 * * User data is attached to each range via `(myRange as any).tag = myTag`.
 * * Search operations are simple linear.
 * * This class can be used directly for "smallish" range sets, or as the leaf level of hierarchical structures for larger range sets.
 * @internal
 */
export class LinearSearchRange2dArray {
    _rangeArray;
    _isDirty;
    _compositeRange;
    constructor() {
        this._rangeArray = [];
        this._isDirty = false;
        this._compositeRange = Range2d.createNull();
    }
    // TODO: build search structure
    updateForSearch() {
        this._isDirty = false;
    }
    /** Return the overall range of all members. */
    totalRange(result) {
        return this._compositeRange.clone(result);
    }
    /** Add a range to the search set. */
    addRange(range, tag) {
        this._isDirty = true;
        const myRange = Range2d.createNull();
        myRange.tag = tag;
        myRange.extendXY(range.low.x, range.low.y);
        myRange.extendXY(range.high.x, range.high.y);
        this._compositeRange.extendRange(myRange);
        this._rangeArray.push(myRange);
    }
    /**
     * * Search for ranges containing testRange
     * * Pass each range and tag to handler
     * * terminate search if handler returns false.
     * @param testRange search range.
     * @param handler function to receive range and tag hits.
     * @return false if search terminated by handler.  Return true if no handler returned false.
     */
    searchXY(x, y, handler) {
        if (this._isDirty)
            this.updateForSearch();
        // NEEDS WORK: Linear search here -- do better!
        for (const candidate of this._rangeArray) {
            if (candidate.containsXY(x, y))
                if (!handler(candidate, candidate.tag))
                    return false;
        }
        return true;
    }
    /**
     * * Search for ranges overlapping testRange
     * * Pass each range and tag to handler
     * * terminate search if handler returns false.
     * @param testRange search range.
     * @param handler function to receive range and tag hits.
     * @return false if search terminated by handler.  Return true if no handler returned false.
     */
    searchRange2d(testRange, handler) {
        if (this._isDirty)
            this.updateForSearch();
        for (const candidate of this._rangeArray) {
            if (candidate.intersectsRange(testRange))
                if (!handler(candidate, candidate.tag))
                    return false;
        }
        return true;
    }
}
//# sourceMappingURL=LinearSearchRange2dArray.js.map