def sortDictionary(d): from algorithms import findItemInList values = [] keys = [] for key in d: value = d[key] i = findItemInList(values, value)[0] values = values[:i+1] + [value] + values[i+1:] keys = keys[:i+1] + [key] + keys[i+1:] return (keys, values)
def hasSeq(self, start, length): """ Checks whether a sequence starting at a given index with a given length is stored in the container. @param start: The starting index to look for. @param length: The length to look for @return: A boolean flag indicating the condition. """ tmp = findItemInList(self.ranges.keys(), start) if not tmp[1]: return False if not length in self.ranges[self.ranges.keys()[tmp[0]]]: return False return True
def _findRange(self, offset): rangeOffsets = self.ranges.keys() rangeOffsets.sort() index = findItemInList(rangeOffsets, offset)[0] return index, rangeOffsets
def getCommonRanges(self, s): """ Gets the ranges in two sequences that are common. @param s: The sequence to compare to. @return: A tuple containing the consecutive common ranges for both sequences. Example: S0 = [1,2,3,4,4,5,4,7,4,7,8,9] S1 = [1,2,3,0,0,7,8,9,5] return = ([range(0,3), range(9,12)], [range(0,3), range(5,8)]) Note how the common sub-sequences [5] and [7] are not taken into account. """ # get common sub-sequences commonSubSeqs = self.findCommonSubSequences(s) # now sort them according to their order of appearance in both sequences subSeqsPrimary = {} subSeqsSecondary = {} for commonSubSeq in commonSubSeqs: for indexPrimary in commonSubSeq.indexesParent: if indexPrimary not in subSeqsPrimary or subSeqsPrimary[indexPrimary].len < commonSubSeq.len: subSeqsPrimary[indexPrimary] = commonSubSeq for indexSecondary in commonSubSeq.indexesOther: if indexSecondary not in subSeqsSecondary or subSeqsSecondary[indexSecondary].len < commonSubSeq.len: subSeqsSecondary[indexSecondary] = commonSubSeq indexesPrimary = subSeqsPrimary.keys() indexesSecondary = subSeqsSecondary.keys() indexesPrimary.sort() indexesSecondary.sort() rangesPrimary = [] rangesSecondary = [] i = 0 j = 0 while i in range(len(indexesPrimary)) and j in range(len(indexesSecondary)): while i in range(len(indexesPrimary)) and j in range(len(indexesSecondary)): indexPrimary = indexesPrimary[i] indexSecondary = indexesSecondary[j] subSeqPrimary = subSeqsPrimary[indexPrimary] subSeqSecondary = subSeqsSecondary[indexSecondary] if subSeqPrimary == subSeqSecondary: # everything is fine rangesPrimary.append(range(indexPrimary, indexPrimary + subSeqPrimary.len)) rangesSecondary.append(range(indexSecondary, indexSecondary + subSeqSecondary.len)) break elif len(subSeqPrimary) > len(subSeqSecondary): j += 1 else: i += 1 # update indexes i, exactMatch = findItemInList(indexesPrimary, indexPrimary + subSeqPrimary.len) if not exactMatch: i += 1 j, exactMatch = findItemInList(indexesSecondary, indexSecondary + subSeqSecondary.len) if not exactMatch: j += 1 return (rangesPrimary, rangesSecondary)