<commit_before>/*-----------------------------------------------------------------------------
| Copyright (c) 2014-2015, S. <NAME>
|
| Distributed under the terms of the BSD 3-Clause License.
|
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
module phosphor.collections {

/**
 * An object which manages a collection of variable sized sections.
 *
 * A section list is commonly used to manage row heights in virtually
 * scrolling list controls. In such a control, most rows are uniform
 * height while a handful of rows are variable sized.
 *
 * A section list has guaranteed `O(log(n))` worst-case performance for
 * most operations, where `n` is the number of variable sized sections.
 */
export
class SectionList {
  /**
   * Get the total number of sections in the list.
   *
   * #### Notes
   * This operation has `O(1)` complexity.
   */
  get count(): number {
    return this._root !== null ? this._root.count : 0;
  }

  /**
   * Get the total size of all sections in the list.
   *
   * #### Notes
   * This operation has `O(1)` complexity.
   */
  get size(): number {
    return this._root !== null ? this._root.size : 0;
  }

  /**
   * Find the index of the section which covers the given offset.
   *
   * @param offset - The positive offset position of interest.
   *
   * @returns The index of the section which covers the given offset,
   *   or `-1` if the offset is out of range.
   *
   * #### Notes
   * This operation has `O(log(n))` complexity.
   */
  indexOf(offset: number): number {
    if (this._root === null || offset < 0 || offset >= this._root.size) {
      return -1;
    }
    return indexOf(this._root, offset);
  }

  /**
   * Find the offset position of the section at the given index.
   *
   * @param index - The index of the section of interest. If this value
   *   is negative, it is taken as an offset from the end of the list.
   *
   * @returns The offset position of the section at the given index, or
   *   `-1` if the index is out of range.
   *
   * #### Notes
   * This operation has `O(log(n))` complexity.
   */
  offsetOf(index: number): number {
    if (this._root === null) {
      return -1;
    }
    index = Math.floor(index);
    if (index < 0) {
      index += this._root.count;
    }
    if (index < 0 || index >= this._root.count) {
      return -1;
    }
    return offsetOf(this._root, index);
  }

  /**
   * Find the size of the section at the given index.
   *
   * @param index - The index of the section of interest. If this value
   *   is negative, it is taken as an offset from the end of the list.
   *
   * @returns The size of the section at the given index, or `-1` if
   *   the index is out of range.
   *
   * #### Notes
   * This operation has `O(log(n))` complexity.
   */
  sizeOf(index: number): number {
    if (this._root === null) {
      return -1;
    }
    index = Math.floor(index);
    if (index < 0) {
      index += this._root.count;
    }
    if (index < 0 || index >= this._root.count) {
      return -1;
    }
    return sizeOf(this._root, index);
  }

  /**
   * Insert new sections into the list.
   *
   * @param index - The index at which to insert the first section. If
   *   this value is negative, it is taken as an offset from the end of
   *   the list. The value is clamped to the range `[0, list.count]`.
   *
   * @param count - The number of sections to insert. If this value is
   *   `<= 0`, this method is a no-op.
   *
   * @param size - The size of each section. This value is clamped to
   *   the range `[0, Infinity]`.
   *
   * #### Notes
   * This operation has `O(log(n))` complexity.
   */
  insert(index: number, count: number, size: number): void {
    count = Math.floor(count);
    if (count <= 0) {
      return;
    }
    if (this._root === null) {
      this._root = createLeaf(count, count * Math.max(0, size));
      return;
    }
    index = Math.floor(index);
    if (index < 0) {
      index = Math.max(0, index + this._root.count);
    } else {
      index = Math.min(index, this._root.count);
    }
    this._root = insert(this._root, index, count, Math.max(0, size));
  }

  /**
   * Remove existing sections from the list.
   *
   * @param index - The index of the first section to remove. If this
   *   value is negative, it is taken as an offset from the end of the
   *   list. The value is clamped to the range `[0, Infinity]`. If
   *   the value is `>= list.count` this method is a no-op.
   *
   * @param count - The number of sections to remove. If this value is
   *   `<= 0`, this method is a no-op. If this value is more than the
   *   the availble number of sections, the extra count is ignored.
   *
   * #### Notes
   * This operation has `O(log(n))` complexity.
   */
  remove(index: number, count: number): void {
    if (this._root === null) {
      return;
    }
    count = Math.floor(count);
    if (count <= 0) {
      return;
    }
    index = Math.floor(index);
    if (index < 0) {
      index = Math.max(0, index + this._root.count);
    }
    if (index >= this._root.count) {
      return;
    }
    this._root = remove(this._root, index, count);
  }<commit_msg>add 'resize' method to sectionlist<commit_after>/*-----------------------------------------------------------------------------
| Copyright (c) 2014-2015, <NAME>
|
| Distributed under the terms of the BSD 3-Clause License.
|
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
module phosphor.collections {

/**
 * An object which manages a collection of variable sized sections.
 *
 * A section list is commonly used to manage row heights in virtually
 * scrolling list controls. In such a control, most rows are uniform
 * height while a handful of rows are variable sized.
 *
 * A section list has guaranteed `O(log(n))` worst-case performance for
 * most operations, where `n` is the number of variable sized sections.
 */
export
class SectionList {
  /**
   * Get the total number of sections in the list.
   *
   * #### Notes
   * This operation has `O(1)` complexity.
   */
  get count(): number {
    return this._root !== null ? this._root.count : 0;
  }

  /**
   * Get the total size of all sections in the list.
   *
   * #### Notes
   * This operation has `O(1)` complexity.
   */
  get size(): number {
    return this._root !== null ? this._root.size : 0;
  }

  /**
   * Find the index of the section which covers the given offset.
   *
   * @param offset - The positive offset position of interest.
   *
   * @returns The index of the section which covers the given offset,
   *   or `-1` if the offset is out of range.
   *
   * #### Notes
   * This operation has `O(log(n))` complexity.
   */
  indexOf(offset: number): number {
    if (this._root === null || offset < 0 || offset >= this._root.size) {
      return -1;
    }
    return indexOf(this._root, offset);
  }

  /**
   * Find the offset position of the section at the given index.
   *
   * @param index - The index of the section of interest. If this value
   *   is negative, it is taken as an offset from the end of the list.
   *
   * @returns The offset position of the section at the given index, or
   *   `-1` if the index is out of range.
   *
   * #### Notes
   * This operation has `O(log(n))` complexity.
   */
  offsetOf(index: number): number {
    if (this._root === null) {
      return -1;
    }
    index = Math.floor(index);
    if (index < 0) {
      index += this._root.count;
    }
    if (index < 0 || index >= this._root.count) {
      return -1;
    }
    return offsetOf(this._root, index);
  }

  /**
   * Find the size of the section at the given index.
   *
   * @param index - The index of the section of interest. If this value
   *   is negative, it is taken as an offset from the end of the list.
   *
   * @returns The size of the section at the given index, or `-1` if
   *   the index is out of range.
   *
   * #### Notes
   * This operation has `O(log(n))` complexity.
   */
  sizeOf(index: number): number {
    if (this._root === null) {
      return -1;
    }
    index = Math.floor(index);
    if (index < 0) {
      index += this._root.count;
    }
    if (index < 0 || index >= this._root.count) {
      return -1;
    }
    return sizeOf(this._root, index);
  }

  /**
   * Insert new sections into the list.
   *
   * @param index - The index at which to insert the first section. If
   *   this value is negative, it is taken as an offset from the end of
   *   the list. The value is clamped to the range `[0, list.count]`.
   *
   * @param count - The number of sections to insert. If this value is
   *   `<= 0`, this method is a no-op.
   *
   * @param size - The size of each section. This value is clamped to
   *   the range `[0, Infinity]`.
   *
   * #### Notes
   * This operation has `O(log(n))` complexity.
   */
  insert(index: number, count: number, size: number): void {
    count = Math.floor(count);
    if (count <= 0) {
      return;
    }
    if (this._root === null) {
      this._root = createLeaf(count, count * Math.max(0, size));
      return;
    }
    index = Math.floor(index);
    if (index < 0) {
      index = Math.max(0, index + this._root.count);
    } else {
      index = Math.min(index, this._root.count);
    }
    this._root = insert(this._root, index, count, Math.max(0, size));
  }

  /**
   * Remove existing sections from the list.
   *
   * @param index - The index of the first section to remove. If this
   *   value is negative, it is taken as an offset from the end of the
   *   list. The value is clamped to the range `[0, Infinity]`. If
   *   the value is `>= list.count` this method is a no-op.
   *
   * @param count - The number of sections to remove. If this value is
   *   `<= 0`, this method is a no-op. If this value is more than the
   *   the availble number of sections, the extra count is ignored.
   *
   * #### Notes
   * This operation has `O(log(n))` complexity.
   */
  remove(index: number, count: number): void {
    if (this._root === null) {
      return;
    }
    count = Math.floor(count);
    if (count <= 0) {
      return;
    }
    index = Math.floor(index);
    if (index < 0) {
      index = Math.max(0, index + this._root.count);
    }
    if (index >= this._root.count) {
      return;
    }
    this._root = remove(this._root, index, count);
  }

  /**
   * Resize existing sections in the list.
   *
   * @param index - The index of the first section to resize. If this
   *   value is negative, it is taken as an offset from the end of the
   *   list. The value is clamped to the range `[0, Infinity]`. If
   *   the value is `>= list.count` this method is a no-op.
   *
   * @param count - The number of sections to resize. If this value is
   *   `<= 0`, this method is a no-op. If this value is more than the
   *   the availble number of sections, the extra count is ignored.
   *
   * @param size - The new size of each section. This value is clamped
   *   to the range `[0, Infinity]`.
   */
  resize(index: number, count: number, size: number): void {
    if (this._root === null) {
      return;
    }
    count = Math.floor(count);
    if (count <= 0) {
      return;
    }
    index = Math.floor(index);
    if (index < 0) {
      index = Math.max(0, index + this._root.count);
    }
    if (index >= this._root.count) {
      return;
    }
    count = Math.min(count, this._root.count - index);
    this._root = remove(this._root, index, count);
    if (this._root === null) {
      this._root = createLeaf(count, count * Math.max(0, size));
    } else {
      this._root = insert(this._root, index, count, Math.max(0, size));
    }
  }
{'fraction_non_alphanumeric': 0.14715350728566587, 'fraction_numerical': 0.007285665875974246, 'mean_word_length': 2.1429712460063897, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4015756', 'n_tokens_mistral': 3693, 'n_tokens_neox': 3621, 'n_words': 1408}