<commit_before>/* Copyright 2013-2014 Extended Mind Technologies Oy
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 /*global angular */
 'use strict';

/**
* Helper service for array manipulation.
*/
function ArrayService($rootScope, UISessionService) {

  function emitChangeEvent(data, type, item) {
    $rootScope.$emit('arrayChanged',
                     {data: data,
                      type: type,
                      item: item,
                      ownerUUID: UISessionService.getActiveUUID()});
  }

  // Modified from:
  // http://stackoverflow.com/questions/1344500/efficient-way-to-insert-a-number-into-a-sorted-array-of-numbers
  function insertItemToArray(element, array, field, reverse) {
    array.splice(locationOfItemInArray(element, array, field, reverse) + 1, 0, element);
    return array;
  }

  function locationOfItemInArray(element, array, field, reverse, start, end) {

    function compareTransField(a, b, field) {
      return a.trans[field] - b.trans[field];
    }

    var compareFn = typeof field === 'function' ? field : compareTransField;

    start = start || 0;
    end = end || array.length;

    var pivot = parseInt(start + (end - start) / 2, 10);

    if (end - start <= 1) {
      if (reverse) {
        if (array[pivot] && compareFn(array[pivot], element, field) < 0)
          return pivot - 1;
        else
          return pivot;
      }
      else {
        if (array[pivot] && compareFn(array[pivot], element, field) > 0)
          return pivot - 1;
        else
          return pivot;
      }
    }
    var compareResult = compareFn(array[pivot], element, field);
    if ((!reverse && compareResult < 0) || (reverse && compareResult > 0))
      return locationOfItemInArray(element, array, field, reverse, pivot, end);
    else
      return locationOfItemInArray(element, array, field, reverse, start, pivot);
  }

  function getFirstMatchingArrayInfoByProperty(item, otherArrays) {
    if (otherArrays) {
      for (var i=0, len=otherArrays.length; i<len; i++) {
        if (item[otherArrays[i].id]) {
          return otherArrays[i];
        }
      }
    }
  }

  function getFirstMatchingArrayInfoByUUID(uuid, otherArrays) {
    if (otherArrays) {
      for (var i=0, len=otherArrays.length; i<len; i++) {
        var itemInOtherArray = otherArrays[i].array.findFirstObjectByKeyValue('uuid', uuid, 'trans');
        if (itemInOtherArray) {
          return otherArrays[i];
        }
      }
    }
  }

  return {
    // Based on given backend response, sets active array, deleted array
    // and optionally other arrays, which are objects of type {array: [], id: ''}.
    // Returns the latest (biggest) modified value.
    setArrays: function(itemType, response, activeArray, deletedArray, otherArrays) {
      // First clear existing arrays..
      var changedArrays = [];
      activeArray.length = 0;
      changedArrays.push({type: 'active', array: activeArray});
      deletedArray.length = 0;
      changedArrays.push({type: 'deleted', array: deletedArray});
      if (otherArrays) {
        for (var i=0, len=otherArrays.length; i<len; i++) {
          otherArrays[i].array.length = 0;
          changedArrays.push({type: otherArrays[i].id, array: otherArrays[i].array});
        }
      }

      // ..then loop through response
      var latestModified;
      if (response) {
        var index = 0;
        while (response[index]) {
          var modified = this.setItem(itemType, response[index], activeArray, deletedArray, otherArrays,
                                      true);
          if (!latestModified || latestModified < modified) {
            latestModified = modified;
          }
          index++;
        }
      }

      emitChangeEvent(changedArrays, itemType);
      return latestModified;
    },
    // Based on given backend response, updates all given arrays and returns the
    // latest (biggest) modified value.
    updateArrays: function(itemType, response, activeArray, deletedArray, otherArrays) {
      if (response) {
        var i = 0;
        var latestModified;
        var skipEmit = response.length > 1;
        while (response[i]) {
          var modified = this.updateItem(itemType, response[i], activeArray, deletedArray, otherArrays,
                                         skipEmit);
          if (!latestModified || latestModified < response[i].modified) {
            latestModified = modified;
          }
          i++;
        }<commit_msg>Fixed cached task arrays not updating on complete in another client<commit_after>/* Copyright 2013-2014 Extended Mind Technologies Oy
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 /*global angular */
 'use strict';

/**
* Helper service for array manipulation.
*/
function ArrayService($rootScope, UISessionService) {

  function emitChangeEvent(data, type, item) {
    $rootScope.$emit('arrayChanged',
                     {data: data,
                      type: type,
                      item: item,
                      ownerUUID: UISessionService.getActiveUUID()});
  }

  // Modified from:
  // http://stackoverflow.com/questions/1344500/efficient-way-to-insert-a-number-into-a-sorted-array-of-numbers
  function insertItemToArray(element, array, field, reverse) {
    array.splice(locationOfItemInArray(element, array, field, reverse) + 1, 0, element);
    return array;
  }

  function locationOfItemInArray(element, array, field, reverse, start, end) {

    function compareTransField(a, b, field) {
      return a.trans[field] - b.trans[field];
    }

    var compareFn = typeof field === 'function' ? field : compareTransField;

    start = start || 0;
    end = end || array.length;

    var pivot = parseInt(start + (end - start) / 2, 10);

    if (end - start <= 1) {
      if (reverse) {
        if (array[pivot] && compareFn(array[pivot], element, field) < 0)
          return pivot - 1;
        else
          return pivot;
      }
      else {
        if (array[pivot] && compareFn(array[pivot], element, field) > 0)
          return pivot - 1;
        else
          return pivot;
      }
    }
    var compareResult = compareFn(array[pivot], element, field);
    if ((!reverse && compareResult < 0) || (reverse && compareResult > 0))
      return locationOfItemInArray(element, array, field, reverse, pivot, end);
    else
      return locationOfItemInArray(element, array, field, reverse, start, pivot);
  }

  function getFirstMatchingArrayInfoByProperty(item, otherArrays) {
    if (otherArrays) {
      for (var i=0, len=otherArrays.length; i<len; i++) {
        if (item[otherArrays[i].id]) {
          return otherArrays[i];
        }
      }
    }
  }

  function getFirstMatchingArrayInfoByUUID(uuid, otherArrays) {
    if (otherArrays) {
      for (var i=0, len=otherArrays.length; i<len; i++) {
        var itemInOtherArray = otherArrays[i].array.findFirstObjectByKeyValue('uuid', uuid, 'trans');
        if (itemInOtherArray) {
          return otherArrays[i];
        }
      }
    }
  }

  return {
    // Based on given backend response, sets active array, deleted array
    // and optionally other arrays, which are objects of type {array: [], id: ''}.
    // Returns the latest (biggest) modified value.
    setArrays: function(itemType, response, activeArray, deletedArray, otherArrays) {
      // First clear existing arrays..
      var changedArrays = [];
      activeArray.length = 0;
      changedArrays.push({type: 'active', array: activeArray});
      deletedArray.length = 0;
      changedArrays.push({type: 'deleted', array: deletedArray});
      if (otherArrays) {
        for (var i=0, len=otherArrays.length; i<len; i++) {
          otherArrays[i].array.length = 0;
          changedArrays.push({type: otherArrays[i].id, array: otherArrays[i].array});
        }
      }

      // ..then loop through response
      var latestModified;
      if (response) {
        var index = 0;
        while (response[index]) {
          var modified = this.setItem(itemType, response[index], activeArray, deletedArray, otherArrays,
                                      true);
          if (!latestModified || latestModified < modified) {
            latestModified = modified;
          }
          index++;
        }
      }

      emitChangeEvent(changedArrays, itemType);
      return latestModified;
    },
    // Based on given backend response, updates all given arrays and returns the
    // latest (biggest) modified value.
    updateArrays: function(itemType, response, activeArray, deletedArray, otherArrays) {
      if (response) {
        var i = 0;
        var latestModified;
        var skipEmit = response.length > 1;
        while (response[i]) {
          var modified = this.updateItem(itemType, response[i], activeArray, deletedArray, otherArrays,
                                         skipEmit);
          if (!latestModified || latestModified < response[i].modified) {
            latestModified = modified;
          }
          i++;
        }
        if (skipEmit){
          // Emit change event for all arrays just in case when there are more than one item updated
          var changedArrays = [];
          changedArrays.push({type: 'active', array: activeArray});
          changedArrays.push({type: 'deleted', array: deletedArray});
          if (otherArrays) {
            for (var i=0, len=otherArrays.length; i<len; i++) {
              changedArrays.push({type: otherArrays[i].id, array: otherArrays[i].array});
            }
          }
          emitChangeEvent(changedArrays, itemType);
        }
{'fraction_non_alphanumeric': 0.11225745385707525, 'fraction_numerical': 0.007856128726928537, 'mean_word_length': 2.4372153545868573, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2141309', 'n_tokens_mistral': 2971, 'n_tokens_neox': 2922, 'n_words': 857}