import { v as b } from "./index-DSGwv2Yx.js";
const y = -2, N = -1;
function F(r, e, n) {
  const t = r.shape.length;
  b(t === e.length, () => `Error in slice${t}D: Length of begin ${e} must match the rank of the array (${t}).`), b(t === n.length, () => `Error in slice${t}D: Length of size ${n} must match the rank of the array (${t}).`);
  for (let l = 0; l < t; ++l)
    b(e[l] + n[l] <= r.shape[l], () => `Error in slice${t}D: begin[${l}] + size[${l}] (${e[l] + n[l]}) would overflow input.shape[${l}] (${r.shape[l]})`);
}
function L(r, e, n) {
  const t = [];
  for (let l = 0; l < r.length; l++)
    t[l] = Math.ceil((e[l] - r[l]) / n[l]);
  return t;
}
function P(r, e, n) {
  let t = n.length;
  for (let l = 0; l < n.length; l++)
    if (n[l] > 1) {
      t = l;
      break;
    }
  for (let l = t + 1; l < n.length; l++)
    if (e[l] > 0 || n[l] !== r[l])
      return !1;
  return !0;
}
function X(r, e) {
  let n = r.length > 0 ? r[r.length - 1] : 1;
  for (let t = 0; t < r.length - 1; t++)
    n += r[t] * e[t];
  return n;
}
function C(r, e, n) {
  let t;
  const l = r.shape.length;
  typeof e == "number" ? t = [e, ...new Array(l - 1).fill(0)] : e.length < l ? t = e.concat(new Array(l - e.length).fill(0)) : t = e.slice(), t.forEach((f) => {
    b(f !== -1, () => "slice() does not support negative begin indexing.");
  });
  let a;
  return n == null ? a = new Array(l).fill(-1) : typeof n == "number" ? a = [n, ...new Array(l - 1).fill(-1)] : n.length < l ? a = n.concat(new Array(l - n.length).fill(-1)) : a = n, a = a.map((f, u) => f >= 0 ? f : (b(f === -1, () => `Negative size values should be exactly -1 but got ${f} for the slice() size at index ${u}.`), r.shape[u] - t[u])), [t, a];
}
function H(r, e, n, t, l, a, f, u, D) {
  let g;
  if (t == null ? (g = new Array(e.length), g.fill(1)) : g = t, f != null && (f & f - 1) !== 0)
    throw new Error("Multiple ellipses in slice is not allowed.");
  let w = !1;
  const d = {
    dims: g.length,
    numAddAxisAfterEllipsis: 0,
    begin: e.slice(),
    end: n.slice(),
    strides: g.slice(),
    beginMask: l,
    endMask: a,
    ellipsisMask: f,
    newAxisMask: u,
    shrinkAxisMask: D
  };
  for (let i = 0; i < d.dims; i++)
    w && (1 << i & u) !== 0 && d.numAddAxisAfterEllipsis++, 1 << i & f && (w = !0);
  w || (d.ellipsisMask |= 1 << d.dims, d.dims++);
  const s = {
    dims: r.length,
    beginMask: 0,
    endMask: 0,
    beginValid: !1,
    endValid: !1
  };
  O(d, s);
  let m = !0, S = !0, M = !0;
  const k = [], I = [];
  for (let i = 0; i < r.length; ++i) {
    if (s.strides[i] === 0)
      throw Error(`strides[${i}] must be non-zero`);
    const o = !!(s.shrinkAxisMask & 1 << i), h = r[i];
    if (h === -1) {
      k.push(o ? 1 : -1);
      continue;
    }
    const G = [s.beginMask & 1 << i, s.endMask & 1 << i], E = [
      s.strides[i] > 0 ? 0 : -1,
      s.strides[i] > 0 ? h : h - 1
    ];
    if (o && s.strides[i] <= 0)
      throw Error("only stride 1 allowed on non-range indexing.");
    M = M && s.strides[i] === 1;
    const $ = !!(s.beginMask & 1 << i && s.endMask & 1 << i);
    if (s.beginValid && s.endValid) {
      if (o) {
        const x = s.begin[i] < 0 ? h + s.begin[i] : s.begin[i];
        if (s.begin[i] = x, s.end[i] = s.begin[i] + 1, x < 0 || x >= h)
          throw Error(`slice index ${s.begin[i]} of dimension ${i} out of bounds.`);
      } else
        s.begin[i] = V(s.begin[i], 0, s.strides[i], h, G, E), s.end[i] = V(s.end[i], 1, s.strides[i], h, G, E);
      const p = s.strides[i] === 1 && s.begin[i] === 0 && s.end[i] === h;
      m = m && p, S = S && (i === 0 && s.strides[i] === 1 || p);
    } else
      m = m && s.strides[i] === 1 && $, S = S && (i === 0 && s.strides[i] === 1 || $);
    let c, A = !1;
    if (s.beginValid && s.endValid ? (c = s.end[i] - s.begin[i], A = !0) : o ? (c = 1, A = !0) : $ && h >= 0 && (s.strides[i] < 0 ? c = -h : c = h, A = !0), A) {
      let p;
      c === 0 || c < 0 != s.strides[i] < 0 ? p = 0 : p = Math.trunc(c / s.strides[i]) + (c % s.strides[i] !== 0 ? 1 : 0), k.push(p);
    } else
      k.push(-1);
  }
  for (let i = 0; i < s.finalShapeGatherIndices.length; ++i) {
    const o = s.finalShapeGatherIndices[i];
    o >= 0 ? I.push(k[o]) : o === y && I.push(1);
  }
  return {
    finalShapeSparse: I.filter((i, o) => s.finalShapeGatherIndices[o] !== y),
    finalShape: I,
    isIdentity: m,
    sliceDim0: S,
    isSimpleSlice: M,
    begin: s.begin,
    end: s.end,
    strides: s.strides
  };
}
function O(r, e) {
  e.beginMask = 0, e.endMask = 0, e.shrinkAxisMask = 0;
  let n = 0;
  e.beginValid = r.begin != null, e.endValid = r.end != null, e.begin = new Array(e.dims), e.end = new Array(e.dims), e.strides = new Array(e.dims), e.finalShapeGatherIndices = [], e.finalShapeGatherIndicesSparse = [], e.inputShapeGatherIndicesSparse = new Array(e.dims);
  for (let t = 0; t < r.dims; t++)
    if (1 << t & r.ellipsisMask) {
      const l = Math.min(e.dims - (r.dims - t) + 1 + r.numAddAxisAfterEllipsis, e.dims);
      for (; n < l; n++)
        e.begin[n] = 0, e.end[n] = 0, e.strides[n] = 1, e.beginMask |= 1 << n, e.endMask |= 1 << n, e.finalShapeGatherIndices.push(n), e.finalShapeGatherIndicesSparse.push(-1), e.inputShapeGatherIndicesSparse[n] = t;
    } else if (1 << t & r.newAxisMask)
      e.finalShapeGatherIndices.push(y), e.finalShapeGatherIndicesSparse.push(-1);
    else {
      if (n === e.begin.length)
        throw Error(`Index out of range using input dim ${n}; input has only ${e.dims} dims, ${e.begin.length}.`);
      r.begin != null && (e.begin[n] = r.begin[t]), r.end != null && (e.end[n] = r.end[t]), e.strides[n] = r.strides[t], r.beginMask & 1 << t && (e.beginMask |= 1 << n), r.endMask & 1 << t && (e.endMask |= 1 << n), r.shrinkAxisMask & 1 << t ? (e.finalShapeGatherIndices.push(N), e.finalShapeGatherIndicesSparse.push(-1), e.shrinkAxisMask |= 1 << n) : (e.finalShapeGatherIndices.push(n), e.finalShapeGatherIndicesSparse.push(t)), e.inputShapeGatherIndicesSparse[n] = t, n++;
    }
}
function V(r, e, n, t, l, a) {
  if (l[e])
    return n > 0 ? a[e] : a[e + 1 & 1];
  {
    const f = r < 0 ? t + r : r;
    return f < a[0] ? a[0] : f > a[1] ? a[1] : f;
  }
}
export {
  F as a,
  L as b,
  X as c,
  P as i,
  C as p,
  H as s
};
