/// <summary> /// Creates a new tile. /// </summary> /// <param name="producerId">the id of the producer of this tile.</param> /// <param name="level">the quadtree level of this tile.</param> /// <param name="tx">tx the quadtree x coordinate of this tile.</param> /// <param name="ty">ty the quadtree y coordinate of this tile.</param> /// <param name="task">task the task that will produce the tile data.</param> public Tile(int producerId, int level, int tx, int ty, CreateTileTask task) { if (task == null) { throw new ArgumentNullException("task can not be null"); } TId = new TId(producerId, level, tx, ty); Users = 0; m_task = task; }
/// <summary> /// Call this if a tile is needed. Will move the tile from the unused to the used cache if its is found there. /// If the tile is not found then a new tile will be created with a new slot.If there are no more free /// slots then the cache capacity has not been set to a high enough value and the program must abort. /// </summary> public Tile GetTile(int producerId, int level, int tx, int ty) { //If this producer id does not exist can not create tile. if (!m_producers.ContainsKey(producerId)) { throw new InvalidOperationException("Producer id not been inserted into cache"); } TId id = new TId(producerId, level, tx, ty); Tile tile = null; //If tile is not in the used cache if (!m_usedTiles.ContainsKey(id)) { //If tile is also not in the unused cache if (!m_unusedTiles.ContainsKey(id)) { List <Slot> slot = NewSlot(); //if there are no more free slots then start recyling slots from the unused tiles if (slot == null && !m_unusedTiles.Empty()) { //Remove the tile and recylce its slot slot = m_unusedTiles.RemoveFirst().Slot; } //If a slot is found create a new tile with a new task if (slot != null) { CreateTileTask task = m_producers[producerId].CreateTask(level, tx, ty, slot); tile = new Tile(producerId, level, tx, ty, task); } //If a free slot is not found then program has must abort. Try setting the cache capacity to higher value. if (slot == null) { throw new CacheCapacityException("No more free slots found. Insufficient storage capacity for cache " + name); } } else { //else if the tile is in the unused cache remove it and keep a reference to it tile = m_unusedTiles.Remove(id); } if (tile != null) { m_usedTiles.Add(id, tile); } } else { tile = m_usedTiles[id]; } //Should never be null be this stage if (tile == null) { throw new ArgumentNullException("Tile should not be null"); } //Keep track of the max number of tiles ever used for debug purposes if (m_usedTiles.Count > m_maxUsedTiles) { m_maxUsedTiles = m_usedTiles.Count; } //inc the num of users tile.IncrementUsers(); return(tile); }