How to use the @lumino/algorithm.ArrayExt.lowerBound function in @lumino/algorithm

To help you get started, we’ve selected a few @lumino/algorithm examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jupyterlab / lumino / packages / datagrid / src / sectionlist.ts View on Github external
// Clamp the index to the bounds of the list.
    index = Math.max(0, Math.min(index, this._count));

    // Add the new sections to the totals.
    let span = count * this._defaultSize;
    this._count += count;
    this._length += span;

    // Bail early if there are no modified sections to update.
    if (this._sections.length === 0) {
      return;
    }

    // Find the modified section for the given index.
    let i = ArrayExt.lowerBound(this._sections, index, Private.indexCmp);

    // Update all modified sections after the insert location.
    for (let n = this._sections.length; i < n; ++i) {
      let section = this._sections[i];
      section.index += count;
      section.offset += span;
    }
  }
github jupyterlab / lumino / packages / datastore / src / mapfield.ts View on Github external
function removeFromMetadata(metadata: MapField.Metadata, key: string, id: string): T | null {
    // Fetch the id and value arrays for the given key.
    let ids = metadata.ids[key] || (metadata.ids[key] = []);
    let values = metadata.values[key] || (metadata.values[key] = []);

    // Find the insert index for the id.
    let i = ArrayExt.lowerBound(ids, id, StringExt.cmp);

    // Find and remove the index for the id.
    if (ids[i] === id) {
      ArrayExt.removeAt(ids, i);
      ArrayExt.removeAt(values, i);
    }

    // Return the current value for the key.
    return values.length ? values[values.length - 1] : null;
  }
}
github jupyterlab / lumino / packages / datastore / src / listfield.ts View on Github external
function findRemovedChunks(ids: ReadonlyArray, metadata: ListField.Metadata): RemoveChunk[] {
    // Set up the chunks array.
    let chunks: RemoveChunk[] = [];

    // Set up the iteration index.
    let i = 0;

    // Fetch the identifier array length.
    let n = ids.length;

    // Iterate over the identifiers to remove.
    while (i < n) {
      // Find the boundary identifier for the current id.
      let j = ArrayExt.lowerBound(metadata.ids, ids[i], StringExt.cmp);

      // If the boundary is at the end of the array, or if the boundary id
      // does not match the id we are looking for, then we are dealing with
      // a concurrently deleted value. In that case, increment its reference
      // in the cemetery and continue processing ids.
      if (j === metadata.ids.length || metadata.ids[j] !== ids[i]) {
        let count = metadata.cemetery[ids[i]] || 0;
        metadata.cemetery[ids[i]] = count + 1;
        i++;
        continue;
      }

      // Set up the chunk index.
      let index = j;

      // Set up the chunk count.
github jupyterlab / lumino / packages / datagrid / src / sectionlist.ts View on Github external
if (this._sections.length === 0) {
      this._count -= count;
      this._length -= count * this._defaultSize;
      return;
    }

    // Handle the simple case of removing all sections.
    if (count === this._count) {
      this._length = 0;
      this._count = 0;
      this._sections.length = 0;
      return;
    }

    // Find the modified section for the start index.
    let i = ArrayExt.lowerBound(this._sections, index, Private.indexCmp);

    // Find the modified section for the end index.
    let j = ArrayExt.lowerBound(this._sections, index + count, Private.indexCmp);

    // Remove the relevant modified sections.
    let removed = this._sections.splice(i, j - i);

    // Compute the total removed span.
    let span = (count - removed.length) * this._defaultSize;
    for (let k = 0, n = removed.length; k < n; ++k) {
      span += removed[k].size;
    }

    // Adjust the totals.
    this._count -= count;
    this._length -= span;
github jupyterlab / lumino / packages / datagrid / src / sectionlist.ts View on Github external
// Clamp the move count to the limit.
    count = Math.min(count, this._count - index);

    // Clamp the destination index to the limit.
    destination = Math.min(Math.max(0, destination), this._count - count);

    // Bail early if there is no effective move.
    if (index === destination) {
      return;
    }

    // Compute the first affected index.
    let i1 = Math.min(index, destination);

    // Look up the first affected modified section.
    let k1 = ArrayExt.lowerBound(this._sections, i1, Private.indexCmp);

    // Bail early if there are no affected modified sections.
    if (k1 === this._sections.length) {
      return;
    }

    // Compute the last affected index.
    let i2 = Math.max(index + count - 1, destination + count - 1);

    // Look up the last affected modified section.
    let k2 = ArrayExt.upperBound(this._sections, i2, Private.indexCmp) - 1;

    // Bail early if there are no affected modified sections.
    if (k2 < k1) {
      return;
    }
github jupyterlab / lumino / packages / datastore / src / mapfield.ts View on Github external
function insertIntoMetadata(metadata: MapField.Metadata, key: string, id: string, value: T | null): T | null {
    // Fetch the id and value arrays for the given key.
    let ids = metadata.ids[key] || (metadata.ids[key] = []);
    let values = metadata.values[key] || (metadata.values[key] = []);

    // Find the insert index for the id.
    let i = ArrayExt.lowerBound(ids, id, StringExt.cmp);

    // Overwrite or insert the value as appropriate.
    if (i < ids.length && ids[i] === id) {
      values[i] = value;
    } else {
      ArrayExt.insert(ids, i, id);
      ArrayExt.insert(values, i, value);
    }

    // Return the current value for the key.
    return values[values.length - 1];
  }
github jupyterlab / lumino / packages / datagrid / src / sectionlist.ts View on Github external
offsetOf(index: number): number {
    // Bail early if the index is out of range.
    if (index < 0 || index >= this._count) {
      return -1;
    }

    // Handle the simple case of no modified sections.
    if (this._sections.length === 0) {
      return index * this._defaultSize;
    }

    // Find the modified section for the given index.
    let i = ArrayExt.lowerBound(this._sections, index, Private.indexCmp);

    // Return the offset of an exact match.
    if (i < this._sections.length && this._sections[i].index === index) {
      return this._sections[i].offset;
    }

    // Handle the case of no modified sections before the index.
    if (i === 0) {
      return index * this._defaultSize;
    }

    // Compute the offset from the previous modified section.
    let section = this._sections[i - 1];
    let span = index - section.index - 1;
    return section.offset + section.size + span * this._defaultSize;
  }
github jupyterlab / lumino / packages / widgets / src / commandpalette.ts View on Github external
if (match && match.score <= score) {
        score = match.score;
        indices = match.indices;
      }
    }

    // Bail if there was no match.
    if (!indices || score === Infinity) {
      return null;
    }

    // Compute the pivot index between category and label text.
    let pivot = category.length + 1;

    // Find the slice index to separate matched indices.
    let j = ArrayExt.lowerBound(indices, pivot, (a, b) => a - b);

    // Extract the matched category and label indices.
    let categoryIndices = indices.slice(0, j);
    let labelIndices = indices.slice(j);

    // Adjust the label indices for the pivot offset.
    for (let i = 0, n = labelIndices.length; i < n; ++i) {
      labelIndices[i] -= pivot;
    }

    // Handle a pure label match.
    if (categoryIndices.length === 0) {
      return {
        matchType: MatchType.Label,
        categoryIndices: null,
        labelIndices,
github jupyterlab / lumino / packages / datastore / src / textfield.ts View on Github external
function findRemovedChunks(ids: ReadonlyArray, metadata: TextField.Metadata): RemoveChunk[] {
    // Set up the chunks array.
    let chunks: RemoveChunk[] = [];

    // Set up the iteration index.
    let i = 0;

    // Fetch the identifier array length.
    let n = ids.length;

    // Iterate over the identifiers to remove.
    while (i < n) {
      // Find the boundary identifier for the current id.
      let j = ArrayExt.lowerBound(metadata.ids, ids[i], StringExt.cmp);

      // If the boundary is at the end of the array, or if the boundary id
      // does not match the id we are looking for, then we are dealing with
      // a concurrently deleted value. In that case, increment its reference
      // in the cemetery and continue processing ids.
      if (j === metadata.ids.length || metadata.ids[j] !== ids[i]) {
        let count = metadata.cemetery[ids[i]] || 0;
        metadata.cemetery[ids[i]] = count + 1;
        i++
        continue;
      }

      // Set up the chunk index.
      let index = j;

      // Set up the chunk count.