Sort data and merge join data level by level.
18211
function(node, deepLevel) {
          return new Ember.RSVP.Promise((resolve, reject) => {
            if (node.deepLevel === deepLevel) {
              let processData = () => {
                if (node.relationType === 'belongsTo') {
                  if (node.parent.sorting !== node.propNameInParent) {
                    sortData(node.parent.data, node.propNameInParent);
                    node.parent.sorting = node.propNameInParent;
                  }

                  if (node.sorting !== node.primaryKeyName) {
                    sortData(node.data, node.primaryKeyName);
                    node.sorting = node.primaryKeyName;
                  }

                  joinSortedDataArrays(node.parent.data, node.propNameInParent, node.data, node.primaryKeyName, node.modelName, node.parent.modelName);

                } else {
                  joinHasManyData(node.parent.data, node.propNameInParent,
                    getDetailsHashMap(node.data, node.primaryKeyName),
                    node.modelName, node.parent.modelName);
                }

                // Remove node from parent.expand.
                let masters = Object.keys(node.parent.expand);
                let mastersCount = masters.length;
                let masterName;
                let masterFound = false;
                for (let i = 0; i < mastersCount; i++) {
                  masterName = masters[i];
                  if (node.parent.expand[masterName].propNameInParent === node.propNameInParent) {
                    masterFound = true;
                    break;
                  }
                }

                if (masterFound) {
                  delete node.parent.expand[masterName];
                }
              };

              // Load and join data.
              let loadPromises = [];

              // If parent data is one record and relationship is belongsTo then apply filter by master id from parent data.
              let filterById;

              if (!node.parent.data) {
                // Load parent data.
                let nodeTable = _this._db.table(node.parent.modelName);
                let loadPromise = new Ember.RSVP.Promise((loadResolve, loadReject) => {
                  nodeTable.toArray().then((data) => {
                    node.parent.data = data;
                    node.parent.sorting = node.parent.primaryKeyName;
                    loadResolve();
                  }, loadReject);});
                loadPromises.push(loadPromise);
              } else if (node.parent.data.length === 1 && node.relationType === 'belongsTo') {
                filterById = node.parent.data[0][node.propNameInParent];
              }

              if (!node.data) {
                // Load data.
                let nodeTable = _this._db.table(node.modelName);
                if (filterById) {
                  nodeTable = nodeTable.where(node.primaryKeyName).equals(filterById);
                }

                let loadPromise = new Ember.RSVP.Promise((loadResolve, loadReject) => {nodeTable.toArray().then((data) => {
                  node.data = data;
                  node.sorting = node.primaryKeyName;
                  loadResolve();
                }, loadReject);});
                loadPromises.push(loadPromise);
              }

              Ember.RSVP.all(loadPromises).then(() => {
                processData();
                resolve();
              }, reject);

            } else {
              if (node.expand) {
                let joinRelationsQueue = Queue.create();
                joinRelationsQueue.set('continueOnError', false);

                let masters = Object.keys(node.expand);
                let mastersCount = masters.length;
                let attachScanDeepLevelToRelationsQueue = (masterName) => {
                  joinRelationsQueue.attach((queryItemResolve, queryItemReject) => {
                    // load data for optimal performance.
                    let expandedMaster = node.expand[masterName];
                    let loadPromise;
                    let skipScan = false;
                    if (node.data && !expandedMaster.data) {
                      let nodeDataLength = node.data.length;
                      if (nodeDataLength === 0) {
                        skipScan = true;
                      } else if (nodeDataLength <= 50) { // Magical constant which depend on list form max rows.
                        let anyOfKeys = [];

                        for (let i = 0; i < nodeDataLength; i++) {
                          let relationKeyValue = node.data[i][masterName];

                          if (relationKeyValue) {
                            if (expandedMaster.relationType === 'belongsTo') {
                              anyOfKeys.push(relationKeyValue);
                            } else {
                              anyOfKeys = anyOfKeys.concat(relationKeyValue);
                            }
                          }
                        }

                        if (anyOfKeys.length === 0) {
                          skipScan = true;
                        } else {
                          loadPromise = new Ember.RSVP.Promise((loadResolve, loadReject) => {
                            _this._db.table(expandedMaster.modelName)
                            .where(expandedMaster.primaryKeyName)
                            .anyOf(anyOfKeys)
                            .toArray()
                            .then((loadedData) => {
                              expandedMaster.data = loadedData;
                              expandedMaster.sorting = expandedMaster.primaryKeyName;
                              loadResolve();
                            }, loadReject);
                          });
                        }
                      }
                    }

                    Ember.RSVP.all([loadPromise]).then(() => {
                      if (!skipScan) {
                        scanDeepLevel(expandedMaster, deepLevel).then(queryItemResolve, queryItemReject);
                      } else {
                        queryItemResolve();
                      }
                    });
                  });
                };

                for (let i = 0; i < mastersCount; i++) {
                  let masterName = masters[i];
                  attachScanDeepLevelToRelationsQueue(masterName);
                }

                joinRelationsQueue.attach((queueItemResolve) => {
                  resolve();
                  queueItemResolve();
                });
              } else {
                resolve();
              }
            }
          });
        }
1231326
def _merge_slices(match_slices):
        """"""
        # Sort by earliest range, then by largest range.
        match_slices.sort(key=lambda x: (x[0], -x[1]))
        merged_slices = [match_slices.pop(0)]
        for slice_indices in match_slices:
            last_end = merged_slices[-1][1]
            if slice_indices[0] <= last_end:
                if slice_indices[1] > last_end:
                    merged_slices[-1][1] = slice_indices[1]
            else:
                merged_slices.append(slice_indices)
        return merged_slices
1251826
def merge_contextual(self, other):
        """"""
        # TODO: This is currently dependent on our data model? Make more robust to schema changes
        # Currently we assume all lists at Compound level, with 1 further potential nested level of lists
        for k in self.keys():
            # print('key: %s' % k)
            for item in self[k]:
                # print('item: %s' % item)
                for other_item in other.get(k, []):
                    # Skip text properties (don't merge names, labels, roles)
                    if isinstance(other_item, six.text_type):
                        continue
                    for otherk in other_item.keys():
                        if isinstance(other_item[otherk], list):
                            if len(other_item[otherk]) > 0 and len(item[otherk]) > 0:
                                other_nested_item = other_item[otherk][0]
                                for othernestedk in other_nested_item.keys():
                                    for nested_item in item[otherk]:
                                        if not nested_item[othernestedk]:
                                            nested_item[othernestedk] = other_nested_item[othernestedk]
                        elif not item[otherk]:
                            item[otherk] = other_item[otherk]
        log.debug('Result: %s' % self.serialize())
        return self
968518
def merge(self, datasets=None, separate_datasets=False):
        """"""
        self.logger.info("merging")
        if separate_datasets:
            warnings.warn("The option seperate_datasets=True is"
                          "not implemented yet. Performing merging, but"
                          "neglecting the option.")
        else:
            if datasets is None:
                datasets = list(range(len(self.datasets)))
            first = True
            for dataset_number in datasets:
                if first:
                    dataset = self.datasets[dataset_number]
                    first = False
                else:
                    dataset = self._append(dataset, self.datasets[dataset_number])
                    for raw_data_file, file_size in zip(self.datasets[dataset_number].raw_data_files,
                                                        self.datasets[dataset_number].raw_data_files_length):
                        dataset.raw_data_files.append(raw_data_file)
                        dataset.raw_data_files_length.append(file_size)
            self.datasets = [dataset]
            self.number_of_datasets = 1
        return self
30595
def merge_with(self, other):
        """"""
        result = ValuesAggregation()
        result.total = self.total + other.total
        result.count = self.count + other.count
        result.min = min(self.min, other.min)
        result.max = max(self.max, other.max)
        return result
827012
def merge(bedfiles):
    
    if isinstance(bedfiles, list):
        catted = concat(bedfiles)
    else:
        catted = concat([bedfiles])
    if catted:
        return concat(bedfiles).sort().merge()
    else:
        return catted
791987
def re_sort(data):
    
    keys = sorted(data.keys())
    new_data = {}
    for number, key in enumerate(keys):
        new_data[number] = data[key]
    return new_data
822174
def merge_recursive(obj_a, obj_b, level=False):
    '''
    
    '''
    return aggregate(obj_a, obj_b, level,
                     map_class=AggregatedMap,
                     sequence_class=AggregatedSequence)
830129
function(data, indexes, atLevel) {
    var done = false;
    var startSort = function() {
      for (var i = 0; i < indexes.length; i++) {
        var access_target = data[indexes[i]].access.split(/[\]\[]+/).filter(function (f) {
          return !!f;
        });
        var isContinuos = true;
        var lastContinousIndex = i;
        for (var j = i + 1; j < indexes.length; j++) {
          var access_comparison = data[indexes[j]].access.split(/[\]\[]+/).filter(function (f) {
            return !!f;
          });
          var areEqual = access_comparison[atLevel] === access_target[atLevel];
          if (areEqual && !isContinuos) {
            // we have a problem huston.
            var copy = data[indexes[j]];
            data.splice(indexes[j], 1);
            data.splice(lastContinousIndex+1, 0, copy);
            isContinuos = true;
            lastContinousIndex = i;
            j = i;
          }
          else if (!areEqual) isContinuos = false;
          else if (areEqual)  lastContinousIndex = j;
        }
      }
      done = true;
    };
    while (!done) {
      startSort();
    }
  }
1076461
def merge (d, o):
    """"""
    for k in o.keys():
        if type(o[k]) is dict and k in d:
            merge(d[k], o[k])
        else:
            d[k] = o[k]
    return d
230824
def _concat(self, first_data, second_data):
        """"""
        assert len(first_data) == len(
            second_data), 'data source should contain the same size'
        if first_data and second_data:
            return [
                concat(
                    first_data[x],
                    second_data[x],
                    dim=0
                ) for x in range(len(first_data))
            ]
        elif (not first_data) and (not second_data):
            return []
        else:
            return [
                first_data[0] if first_data else second_data[0]
                for x in range(len(first_data))
            ]
271506
function mergeData (to, from) {
	  var key, toVal, fromVal;
	  for (key in from) {
	    toVal = to[key];
	    fromVal = from[key];
	    if (!hasOwn(to, key)) {
	      set(to, key, fromVal);
	    } else if (isObject(toVal) && isObject(fromVal)) {
	      mergeData(toVal, fromVal);
	    }
	  }
	  return to
	}
1266446
private static int mergeSameBuckets(double[] values, double[] weights, int nextIndex)
    {
        sort(values, weights, nextIndex);

        int current = 0;
        for (int i = 1; i < nextIndex; i++) {
            if (values[current] == values[i]) {
                weights[current] += weights[i];
            }
            else {
                current++;
                values[current] = values[i];
                weights[current] = weights[i];
            }
        }
        return current + 1;
    }
210002
def __flushLevel(self, level):
        """"""
        objectsCount = len(self.objectsStack)

        while objectsCount > level:
            lastIndex = objectsCount - 1
            if lastIndex == 0:
                # We have exactly one element in the stack
                if self.objectsStack[0].__class__.__name__ == "Class":
                    self.classes.append(self.objectsStack[0])
                else:
                    self.functions.append(self.objectsStack[0])
                self.objectsStack = []
                break

            # Append to the previous level
            if self.objectsStack[lastIndex].__class__.__name__ == "Class":
                self.objectsStack[lastIndex - 1].classes. \
                    append(self.objectsStack[lastIndex])
            else:
                self.objectsStack[lastIndex - 1].functions. \
                    append(self.objectsStack[lastIndex])

            del self.objectsStack[lastIndex]
            objectsCount -= 1
629198
def dict_merge(set1, set2):
    """"""
    return dict(list(set1.items()) + list(set2.items()))
494050
def join_cols(cols):
    """"""
    return ", ".join([i for i in cols]) if isinstance(cols, (list, tuple, set)) else cols
736957
def _merge_list_fastqs(files, out_file, config):
    """"""
    if not all(map(fastq.is_fastq, files)):
        raise ValueError("Not all of the files to merge are fastq files: %s " % (files))
    assert all(map(utils.file_exists, files)), ("Not all of the files to merge "
                                                "exist: %s" % (files))
    if not file_exists(out_file):
        files = [_gzip_fastq(fn) for fn in files]
        if len(files) == 1:
            if "remove_source" in config and config["remove_source"]:
                shutil.move(files[0], out_file)
            else:
                os.symlink(files[0], out_file)
            return out_file
        with file_transaction(out_file) as file_txt_out:
            files_str = " ".join(list(files))
            cmd = "cat {files_str} > {file_txt_out}".format(**locals())
            do.run(cmd, "merge fastq files %s" % files)
    return out_file