當前位置: 首頁>>代碼示例>>Python>>正文


Python QMap.begin方法代碼示例

本文整理匯總了Python中pyqtcore.QMap.begin方法的典型用法代碼示例。如果您正苦於以下問題:Python QMap.begin方法的具體用法?Python QMap.begin怎麽用?Python QMap.begin使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在pyqtcore.QMap的用法示例。


在下文中一共展示了QMap.begin方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: RangeSet

# 需要導入模塊: from pyqtcore import QMap [as 別名]
# 或者: from pyqtcore.QMap import begin [as 別名]
class RangeSet():

    # This class is based on std.map rather than QMap since std.map's insert
    # method has an overload that takes a hint about where to insert the new
    # pair.
    def __init__(self):
        self.mMap = QMap()

    ##
    # Insert \a value in the set of ranges. Has no effect when the value is
    # already part of an existing range. When possible, an existing range is
    # extended to include the new value, otherwise a new range is inserted.
    ##
    def insert(self, value):
        if (self.mMap.empty()):
            self.mMap.insert(value, value)
            return

        # We can now assume that 'it' will be at most one end of the range
        # This is the only full-tree search of the map, everything else is
        # relative to this
        it = self.mMap.lowerBound(value)
        itValue = self.mMap.itemByIndex(it)
        begin = self.mMap.begin()
        end = self.mMap.end()
        if (it == end):
            # Check whether the value is included in the last range
            # assert: it != begin
            it -= 1
            itValue = self.mMap.itemByIndex(it)
            
            # assert: it.first < value
            if (itValue[1] >= value):
                return
            # Try to add the value to the end of the previous range
            itValue[1] += 1
            if (itValue[1] == value):
                return
            # Didn't work, restore the previous range
            itValue[1] -= 1
            # We have to insert a new range
            self.mMap.insert(it, [value, value])
            return

        # Now we can dereference 'it' itself
        # assert: it.first >= value
        if (itValue[0] == value):
            return
        # Check whether we can extend the range downwards to include value
        if (itValue[0] == value + 1):
            # When extending the range downwards, it may need to be merged
            # with the previous range.
            # Remember 'prev' for the insertion hint. It is not necessarily
            # before the value, if it == begin.
            prev = itValue
            if (it != begin):
                prev = self.mMap.itemByIndex(prev-1)
                if (prev[1] == value - 1):
                    # The new value fills the gab. Merge the ranges, leaving
                    # only the first, but with a larger range.
                    prev[1] = itValue[1]
                    self.mMap.erase(itValue[0])
                    return

            # No merge needed
            # To change the key, we have to both add and remove. Add first,
            # then remove, to avoid invalidating the iterator too early.
            self.mMap.insert(prev, [value, itValue[1]])
            self.mMap.erase(it)
            return

        # Check if we can grow the previous range upwards to include value
        if (it != begin):
            it -= 1
            itValue = self.mMap.itemByIndex(it)
            if (itValue[1] == value - 1):
                itValue[1] += 1
                return

        # 'it' now points below the range, unless it was already begin
        # We couldn't increase an existing range
        self.mMap.insert(it, [value, value])

    ##
    # Removes all ranges from this set.
    ##
    def clear(self):
        self.mMap.clear()

    # Only are provided, because it is not safe to modify the
    # underlying list. Note that const_iterator is a typedef for Range.
    def begin(self):
        return self.mMap.begin()

    def end(self):
        return self.mMap.end()

    def isEmpty(self):
        return self.mMap.empty()

#.........這裏部分代碼省略.........
開發者ID:theall,項目名稱:Python-Tiled,代碼行數:103,代碼來源:rangeset.py


注:本文中的pyqtcore.QMap.begin方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。