当前位置: 首页>>代码示例>>Python>>正文


Python QMap.itemByIndex方法代码示例

本文整理汇总了Python中pyqtcore.QMap.itemByIndex方法的典型用法代码示例。如果您正苦于以下问题:Python QMap.itemByIndex方法的具体用法?Python QMap.itemByIndex怎么用?Python QMap.itemByIndex使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pyqtcore.QMap的用法示例。


在下文中一共展示了QMap.itemByIndex方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: RandomPicker

# 需要导入模块: from pyqtcore import QMap [as 别名]
# 或者: from pyqtcore.QMap import itemByIndex [as 别名]
class RandomPicker():

    def __init__(self):
        self.mSum = 0.0
        self.mThresholds = QMap()

    def add(self, value, probability = 1.0):
        self.mSum += probability
        self.mThresholds.insert(self.mSum, value)
    
    def isEmpty(self):
        return self.mThresholds.isEmpty()
    
    def pick(self):
        random = (rand() / RAND_MAX) * self.mSum
        it = self.mThresholds.lowerBound(random)
        if (it != self.mThresholds.end()):
            return self.mThresholds.itemByIndex(it)[1]
        else:
            return self.mThresholds.itemByIndex(-1)[1]
    
    def clear(self):
        self.mSum = 0.0
        self.mThresholds.clear()
开发者ID:theall,项目名称:Python-Tiled,代码行数:26,代码来源:randompicker.py

示例2: RangeSet

# 需要导入模块: from pyqtcore import QMap [as 别名]
# 或者: from pyqtcore.QMap import itemByIndex [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

示例3: GidMapper

# 需要导入模块: from pyqtcore import QMap [as 别名]
# 或者: from pyqtcore.QMap import itemByIndex [as 别名]
class GidMapper():
    ##
    # Default constructor. Use \l insert to initialize the gid mapper
    # incrementally.
    ##
    def __init__(self, *args):
        self.mInvalidTile = None
        self.mTilesetColumnCounts = QMap()
        self.mFirstGidToTileset = QMap()
        if len(args)==1:
            ##
            # Constructor that initializes the gid mapper using the given \a tilesets.
            ##
            firstGid = 1
            tilesets = args[0]
            for tileset in tilesets:
                self.insert(firstGid, tileset)
                firstGid += tileset.tileCount()

    ##
    # Insert the given \a tileset with \a firstGid as its first global ID.
    ##
    def insert(self, firstGid, tileset):
        self.mFirstGidToTileset.insert(firstGid, tileset)

    ##
    # Clears the gid mapper, so that it can be reused.
    ##
    def clear(self):
        self.mFirstGidToTileset.clear()

    ##
    # Returns True when no tilesets are known to this gid mapper.
    ##
    def isEmpty(self):
        return self.mFirstGidToTileset.isEmpty()

    ##
    # Returns the GID of the invalid tile in case decodeLayerData() returns
    # the InvalidTile error.
    ##
    def invalidTile(self):
        return self.mInvalidTile

    ##
    # Returns the cell data matched by the given \a gid. The \a ok parameter
    # indicates whether an error occurred.
    ##
    def gidToCell(self, gid):
        result = Cell()
        # Read out the flags
        result.flippedHorizontally = (gid & FlippedHorizontallyFlag)
        result.flippedVertically = (gid & FlippedVerticallyFlag)
        result.flippedAntiDiagonally = (gid & FlippedAntiDiagonallyFlag)
        # Clear the flags
        gid &= ~(FlippedHorizontallyFlag |
                 FlippedVerticallyFlag |
                 FlippedAntiDiagonallyFlag)
        if (gid == 0):
            ok = True
        elif (self.isEmpty()):
            ok = False
        else:
            # Find the tileset containing this tile
            index = self.mFirstGidToTileset.upperBound(gid)
            if index==0:
                ok = False
            else:
                item = self.mFirstGidToTileset.itemByIndex(index-1)
                # Navigate one tileset back since upper bound finds the next
                tileId = gid - item[0]
                tileset = item[1]

                columnCount = self.mTilesetColumnCounts.value(tileset, 0)
                if (columnCount > 0 and columnCount != tileset.columnCount()):
                    # Correct tile index for changes in image width
                    row = int(tileId / columnCount)
                    column = int(tileId % columnCount)
                    tileId = row * tileset.columnCount() + column
                result.tile = tileset.tileAt(tileId)
                ok = True

        return result, ok

    ##
    # Returns the global tile ID for the given \a cell. Returns 0 when the
    # cell is empty or when its tileset isn't known.
    ##
    def cellToGid(self, cell):
        if (cell.isEmpty()):
            return 0
        tileset = cell.tile.tileset()
        # Find the first GID for the tileset
        for item in self.mFirstGidToTileset:
            if item[1] == tileset:
                gid = item[0] + cell.tile.id()
                if (cell.flippedHorizontally):
                    gid |= FlippedHorizontallyFlag
                if (cell.flippedVertically):
                    gid |= FlippedVerticallyFlag
#.........这里部分代码省略.........
开发者ID:theall,项目名称:Python-Tiled,代码行数:103,代码来源:gidmapper.py


注:本文中的pyqtcore.QMap.itemByIndex方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。