assets/src/services/crdt_linear.js
/**
 * Return the index of the item if found
 * If not found, return the index of the character where it should be if inserted when using "at"
 * return the index of the character that precedes it when using "before"
 *
 * @template {*} U
 * @template {*} V
 * @typedef {(a: V, b: U) => number} Comparator
 * @param {List<U>} list
 * @param {V} item
 * @param {Comparator} comparator
 * @param {"at" | "before"} notFoundBehavior
 * @returns {Number}
 */
Return the index of the item if found
If not found, return the index of the character where it should be if inserted when using "at"
return the index of the character that precedes it when using "before"


Return the index of the item if found If not found return the index of the character where it should be if inserted when using " at " return the index of the character that precedes it when using " before "
function binarySearch (list, item, comparator, notFoundBehavior) {
  return _binarySearch(0, list.size)
  function _binarySearch (start, end) {
    if (start >= end) {
      switch (notFoundBehavior) {
        case 'at':
          return start
        case 'before':
          return start - 1
        default:
          throw new Error('Unknown behavior')
      }
    } else {
      const mid = Math.floor((start + end) / 2)
      const comp = comparator(item, list.get(mid))
      if (comp < 0) {
        return _binarySearch(start, mid)
      } else if (comp > 0) {
        return _binarySearch(mid + 1, end)
      } else {
        return mid
      }
    }
  }
}
function binarySearch ( list , item , comparator , notFoundBehavior ) { return _binarySearch ( 0 , list . size ) function _binarySearch ( start , end ) { if ( start >= end ) { switch ( notFoundBehavior ) { case 'at' : return start case 'before' : return start - 1 default : throw new Error ( 'Unknown behavior' ) } } else { const mid = Math . floor ( ( start + end ) / 2 ) const comp = comparator ( item , list . get ( mid ) ) if ( comp < 0 ) { return _binarySearch ( start , mid ) } else if ( comp > 0 ) { return _binarySearch ( mid + 1 , end ) } else { return mid } } } }
Return the index of the item if found
 If not found, return the index of the character where it should be if inserted when using "at"
 return the index of the character that precedes it when using "before"
Return the index of the item if found If not found return the index of the character where it should be if inserted when using " at " return the index of the character that precedes it when using " before "
/**
   * Implments binarySearch recursively.
   *
   * @param {Number} start
   * @param {Number} end
   * @returns {Number}
   */