# all files / ol/ array.js

96.15% Statements 50/52
100% Branches 26/26
100% Functions 5/5
96.15% Lines 50/52

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    1181×

    1×
    1×

    1×
    11×

    22×

    11×
    3×
    8×
    1×
    7×
    1×

    6×
    6×
    6×
    2×

    4×

    1×
    222×

    1×
    7339×
    7339×
    258×
    7081×
    326×

    6755×
    6755×
    7×
    11×
    7×

    6748×
    7×
    10×
    7×

    6741×
    112764×
    6703×
    106061×
    38×
    15×

    23×

    1×
    65×

    65×

    65×
    227×
    227×
    227×
    227×
    227×

     goog.provide('ol.array');

    goog.require('goog.array');
    goog.require('goog.asserts');

    /**
     * @param {Array.<number>} arr Array.
     * @param {number} target Target.
     * @return {number} Index.
     */
    ol.array.binaryFindNearest = function(arr, target) {
      var index = goog.array.binarySearch(arr, target,
          /**
           * @param {number} a A.
           * @param {number} b B.
           * @return {number} b minus a.
           */
          function(a, b) {
            return b - a;
          });
      if (index >= 0) {
        return index;
      } else if (index == -1) {
        return 0;
      } else if (index == -arr.length - 1) {
        return arr.length - 1;
      } else {
        var left = -index - 2;
        var right = -index - 1;
        if (arr[left] - target < target - arr[right]) {
          return left;
        } else {
          return right;
        }
      }
    };

    /**
     * Whether the array contains the given object.
     * @param {Array.<*>} arr The array to test for the presence of the element.
     * @param {*} obj The object for which to test.
     * @return {boolean} The object is in the array.
     */
    ol.array.includes = function(arr, obj) {
      return arr.indexOf(obj) >= 0;
    };

    /**
     * @param {Array.<number>} arr Array.
     * @param {number} target Target.
     * @param {number} direction 0 means return the nearest, > 0
     *    means return the largest nearest, < 0 means return the
     *    smallest nearest.
     * @return {number} Index.
     */
    ol.array.linearFindNearest = function(arr, target, direction) {
      var n = arr.length;
      if (arr[0] <= target) {
        return 0;
      } else if (target <= arr[n - 1]) {
        return n - 1;
      } else {
        var i;
        if (direction > 0) {
          for (i = 1; i < n; ++i) {
            if (arr[i] < target) {
              return i - 1;
            }
          }
        } else if (direction < 0) {
          for (i = 1; i < n; ++i) {
            if (arr[i] <= target) {
              return i;
            }
          }
        } else {
          for (i = 1; i < n; ++i) {
            if (arr[i] == target) {
              return i;
            } else if (arr[i] < target) {
              if (arr[i - 1] - target < target - arr[i]) {
                return i - 1;
              } else {
                return i;
              }
            }
          }
        }
        // We should never get here, but the compiler complains
        // if it finds a path for which no number is returned.
        goog.asserts.fail();
        return n - 1;
      }
    };

    /**
     * @param {Array.<*>} arr Array.
     * @param {number} begin Begin index.
     * @param {number} end End index.
     */
    ol.array.reverseSubArray = function(arr, begin, end) {
      goog.asserts.assert(begin >= 0,
          'Array begin index should be equal to or greater than 0');
      goog.asserts.assert(end < arr.length,
          'Array end index should be less than the array length');
      while (begin < end) {
        var tmp = arr[begin];
        arr[begin] = arr[end];
        arr[end] = tmp;
        ++begin;
        --end;
      }
    };