view commons/core/launcher/LauncherUtils.py @ 9:1eb55963fe39

Updated CompareOverlappingSmall*.py
author m-zytnicki
date Thu, 14 Mar 2013 05:23:05 -0400
parents 769e306b7933
children 94ab73e8a190
line wrap: on
line source

class LauncherUtils(object):

    @staticmethod
    def createHomogeneousSizeList(lStringSizeTuples, maxSize):
        lStringSizeTuplesSorted = sorted(lStringSizeTuples, key=lambda stringSizeTuple:(stringSizeTuple[1], stringSizeTuple[0]), reverse = True)
        lStringSizeList = []
        lStringSize = []
        sumTupleSize = 0
        iteratorFromBegin = 0
        iteratorFromEnd = len(lStringSizeTuplesSorted) - 1
        for tuple in lStringSizeTuplesSorted:
            if sumTupleSize + tuple[1] < maxSize:
                lStringSize.append(tuple[0])
                sumTupleSize += tuple[1]
            elif tuple[1] >= maxSize:
                lStringSizeList.append([tuple[0]])
            else:
                tupleFromEnd = lStringSizeTuplesSorted[iteratorFromEnd]
                while sumTupleSize + tupleFromEnd[1] < maxSize and iteratorFromBegin < iteratorFromEnd:
                    lStringSize.append(tupleFromEnd[0])
                    sumTupleSize += tupleFromEnd[1]
                    del lStringSizeTuplesSorted[iteratorFromEnd]
                    iteratorFromEnd -= 1
                    tupleFromEnd = lStringSizeTuplesSorted[iteratorFromEnd]
                lStringSizeList.append(lStringSize)
                lStringSize = [tuple[0]]
                sumTupleSize = tuple[1]
            iteratorFromBegin += 1
        if lStringSize:
            lStringSizeList.append(lStringSize)
        return lStringSizeList