Java Code Examples for it.unimi.dsi.fastutil.objects.ObjectIterator#remove()

The following examples show how to use it.unimi.dsi.fastutil.objects.ObjectIterator#remove() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FastCollectionsUtils.java    From fastjgame with Apache License 2.0 6 votes vote down vote up
/**
 * 移除map中符合条件的元素,并对删除的元素执行后续的操作。
 *
 * @param map       必须是可修改的map
 * @param predicate 过滤条件,为真的删除
 * @param then      元素删除之后执行的逻辑
 * @param <V>       the type of value
 * @return 删除的元素数量
 */
public static <V> int removeIfAndThen(final Long2ObjectMap<V> map, final LongObjPredicate<? super V> predicate, LongObjConsumer<V> then) {
    if (map.size() == 0) {
        return 0;
    }

    ObjectIterator<Long2ObjectMap.Entry<V>> itr = map.long2ObjectEntrySet().iterator();
    int removeNum = 0;
    Long2ObjectMap.Entry<V> entry;
    long k;
    V v;
    while (itr.hasNext()) {
        entry = itr.next();
        k = entry.getLongKey();
        v = entry.getValue();
        if (predicate.test(k, v)) {
            itr.remove();
            removeNum++;
            then.accept(k, v);
        }
    }
    return removeNum;
}
 
Example 2
Source File: FastCollectionsUtils.java    From fastjgame with Apache License 2.0 6 votes vote down vote up
/**
 * 移除map中符合条件的元素,并对删除的元素执行后续的操作
 *
 * @param map       必须是可修改的map
 * @param predicate 过滤条件,为真的删除
 * @param then      元素删除之后执行的逻辑
 * @param <V>       the type of value
 * @return 删除的元素数量
 */
public static <V> int removeIfAndThen(final Int2ObjectMap<V> map, final IntObjPredicate<? super V> predicate, IntObjConsumer<V> then) {
    if (map.size() == 0) {
        return 0;
    }

    ObjectIterator<Int2ObjectMap.Entry<V>> itr = map.int2ObjectEntrySet().iterator();
    int removeNum = 0;
    Int2ObjectMap.Entry<V> entry;
    int k;
    V v;
    while (itr.hasNext()) {
        entry = itr.next();
        k = entry.getIntKey();
        v = entry.getValue();
        if (predicate.test(k, v)) {
            itr.remove();
            removeNum++;
            then.accept(k, v);
        }
    }
    return removeNum;
}
 
Example 3
Source File: FastCollectionsUtils.java    From fastjgame with Apache License 2.0 6 votes vote down vote up
/**
 * 移除map中符合条件的元素,并对删除的元素执行后续的操作
 *
 * @param map       必须是可修改的map
 * @param predicate 过滤条件,为真的删除
 * @param then      元素删除之后执行的逻辑
 * @param <V>       the type of value
 * @return 删除的元素数量
 */
public static <V> int removeIfAndThen(final Short2ObjectMap<V> map, final ShortObjPredicate<? super V> predicate, ShortObjConsumer<V> then) {
    if (map.size() == 0) {
        return 0;
    }

    ObjectIterator<Short2ObjectMap.Entry<V>> itr = map.short2ObjectEntrySet().iterator();
    int removeNum = 0;
    Short2ObjectMap.Entry<V> entry;
    short k;
    V v;
    while (itr.hasNext()) {
        entry = itr.next();
        k = entry.getShortKey();
        v = entry.getValue();
        if (predicate.test(k, v)) {
            itr.remove();
            removeNum++;
            then.accept(k, v);
        }
    }
    return removeNum;
}
 
Example 4
Source File: TofuTeleporter.java    From TofuCraftReload with MIT License 6 votes vote down vote up
/**
 * called periodically to remove out-of-date portal locations from the cache list. Argument par1 is a
 * WorldServer.getTotalWorldTime() value.
 */
@Override
public void removeStalePortalLocations(long worldTime) {
    if (worldTime % 100L == 0L) {
        long i = worldTime - 300L;
        ObjectIterator<PortalPosition> objectiterator = this.destinationCoordinateCache.values().iterator();

        while (objectiterator.hasNext()) {
            PortalPosition teleporter$portalposition = (PortalPosition) objectiterator.next();

            if (teleporter$portalposition == null || teleporter$portalposition.lastUpdateTime < i) {
                objectiterator.remove();
            }
        }
    }
}
 
Example 5
Source File: BaseLevelProvider.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void doGarbageCollection() {
    int limit = (int) (System.currentTimeMillis() - 50);
    synchronized (regions) {
        if (regions.isEmpty()) {
            return;
        }

        ObjectIterator<BaseRegionLoader> iter = regions.values().iterator();
        while (iter.hasNext()) {
            BaseRegionLoader loader = iter.next();

            if (loader.lastUsed <= limit) {
                try {
                    loader.close();
                } catch (IOException e) {
                    throw new RuntimeException("Unable to close RegionLoader", e);
                }
                lastRegion.set(null);
                iter.remove();
            }

        }
    }
}
 
Example 6
Source File: BaseLevelProvider.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public synchronized void close() {
    this.unloadChunks();
    synchronized (regions) {
        ObjectIterator<BaseRegionLoader> iter = this.regions.values().iterator();

        while (iter.hasNext()) {
            try {
                iter.next().close();
            } catch (IOException e) {
                throw new RuntimeException("Unable to close RegionLoader", e);
            }
            lastRegion.set(null);
            iter.remove();
        }
    }
    this.level = null;
}
 
Example 7
Source File: TeleporterPaths.java    From TFC2 with GNU General Public License v3.0 6 votes vote down vote up
/**
 * called periodically to remove out-of-date portal locations from the cache list. Argument par1 is a
 * WorldServer.getTotalWorldTime() value.
 */
@Override
public void removeStalePortalLocations(long worldTime)
{
	if (worldTime % 100L == 0L)
	{
		long i = worldTime - 600L;
		ObjectIterator<Teleporter.PortalPosition> objectiterator = this.destinationCoordinateCache.values().iterator();

		while (objectiterator.hasNext())
		{
			Teleporter.PortalPosition teleporter$portalposition = (Teleporter.PortalPosition)objectiterator.next();

			if (teleporter$portalposition == null || teleporter$portalposition.lastUpdateTime < i)
			{
				objectiterator.remove();
			}
		}
	}
}
 
Example 8
Source File: BukkitBridge.java    From BungeeTabListPlus with GNU General Public License v3.0 6 votes vote down vote up
@Override
@Nullable
Server getConnection() {
    synchronized (this) {
        ObjectIterator<Server> iterator = connections.iterator();
        while (iterator.hasNext()) {
            Server server = iterator.next();
            if (server.isConnected()) {
                return server;
            } else {
                iterator.remove();
            }
        }
        return null;
    }
}
 
Example 9
Source File: BaseLevelProvider.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void unloadChunks() {
    ObjectIterator<BaseFullChunk> iter = chunks.values().iterator();
    while (iter.hasNext()) {
        iter.next().unload(true, false);
        iter.remove();
    }
}
 
Example 10
Source File: Level.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void doChunkGarbageCollection() {
    this.timings.doChunkGC.startTiming();
    // remove all invaild block entities.
    if (!blockEntities.isEmpty()) {
        ObjectIterator<BlockEntity> iter = blockEntities.values().iterator();
        while (iter.hasNext()) {
            BlockEntity blockEntity = iter.next();
            if (blockEntity != null) {
                if (!blockEntity.isValid()) {
                    iter.remove();
                    blockEntity.close();
                }
            } else {
                iter.remove();
            }
        }
    }

    for (Map.Entry<Long, ? extends FullChunk> entry : provider.getLoadedChunks().entrySet()) {
        long index = entry.getKey();
        if (!this.unloadQueue.containsKey(index)) {
            FullChunk chunk = entry.getValue();
            int X = chunk.getX();
            int Z = chunk.getZ();
            if (!this.isSpawnChunk(X, Z)) {
                this.unloadChunkRequest(X, Z, true);
            }
        }
    }

    this.provider.doGarbageCollection();
    this.timings.doChunkGC.stopTiming();
}
 
Example 11
Source File: WorldObjectContainer.java    From WorldGrower with GNU General Public License v3.0 5 votes vote down vote up
public void moveItemsFrom(WorldObjectContainer otherInventory) {
	ObjectIterator<Entry<WorldObject>> iterator = otherInventory.worldObjects.int2ObjectEntrySet().fastIterator();
	while(iterator.hasNext()) {
		Entry<WorldObject> otherEntry = iterator.next();
		WorldObject otherWorldObject = otherEntry.getValue();
	
		if (otherWorldObject.getProperty(Constants.QUANTITY) == null) {
			throw new IllegalStateException("otherWorldObject.getProperty(Constants.QUANTITY) is null: " + otherWorldObject);
		}
		addQuantity(otherWorldObject, otherWorldObject.getProperty(Constants.QUANTITY));
		iterator.remove();
	}
}
 
Example 12
Source File: BukkitBridge.java    From BungeeTabListPlus with GNU General Public License v3.0 5 votes vote down vote up
private void removeObsoleteConnections() {
    synchronized (this) {
        ObjectIterator<Server> iterator = connections.iterator();
        while (iterator.hasNext()) {
            Server server = iterator.next();
            if (!server.isConnected()) {
                iterator.remove();
            }
        }
    }
}
 
Example 13
Source File: Player.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
protected void sendNextChunk() {
    if (!this.connected) {
        return;
    }

    Timings.playerChunkSendTimer.startTiming();

    if (!loadQueue.isEmpty()) {
        int count = 0;
        ObjectIterator<Long2ObjectMap.Entry<Boolean>> iter = loadQueue.long2ObjectEntrySet().fastIterator();
        while (iter.hasNext()) {
            Long2ObjectMap.Entry<Boolean> entry = iter.next();
            long index = entry.getLongKey();

            if (count >= this.chunksPerTick) {
                break;
            }
            int chunkX = Level.getHashX(index);
            int chunkZ = Level.getHashZ(index);

            ++count;

            this.usedChunks.put(index, false);
            this.level.registerChunkLoader(this, chunkX, chunkZ, false);

            if (!this.level.populateChunk(chunkX, chunkZ)) {
                if (this.spawned && this.teleportPosition == null) {
                    continue;
                } else {
                    break;
                }
            }

            iter.remove();

            PlayerChunkRequestEvent ev = new PlayerChunkRequestEvent(this, chunkX, chunkZ);
            this.server.getPluginManager().callEvent(ev);
            if (!ev.isCancelled()) {
                this.level.requestChunk(chunkX, chunkZ, this);
            }
        }
    }
    if (this.chunkLoadCount >= this.spawnThreshold && !this.spawned && this.teleportPosition == null) {
        this.doFirstSpawn();
    }
    Timings.playerChunkSendTimer.stopTiming();
}
 
Example 14
Source File: Player.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
protected void sendNextChunk() {
    if (!this.connected) {
        return;
    }

    Timings.playerChunkSendTimer.startTiming();

    if (!loadQueue.isEmpty()) {
        int count = 0;
        ObjectIterator<Long2ObjectMap.Entry<Boolean>> iter = loadQueue.long2ObjectEntrySet().fastIterator();
        while (iter.hasNext()) {
            Long2ObjectMap.Entry<Boolean> entry = iter.next();
            long index = entry.getLongKey();

            if (count >= this.chunksPerTick) {
                break;
            }
            int chunkX = Level.getHashX(index);
            int chunkZ = Level.getHashZ(index);

            ++count;

            this.usedChunks.put(index, false);
            this.level.registerChunkLoader(this, chunkX, chunkZ, false);

            if (!this.level.populateChunk(chunkX, chunkZ)) {
                if (this.spawned && this.teleportPosition == null) {
                    continue;
                } else {
                    break;
                }
            }

            iter.remove();

            PlayerChunkRequestEvent ev = new PlayerChunkRequestEvent(this, chunkX, chunkZ);
            this.server.getPluginManager().callEvent(ev);
            if (!ev.isCancelled()) {
                this.level.requestChunk(chunkX, chunkZ, this);
            }
        }
    }
    if (this.chunkLoadCount >= this.spawnThreshold && !this.spawned && this.teleportPosition == null) {
        this.doFirstSpawn();
    }
    Timings.playerChunkSendTimer.stopTiming();
}