using System;
using System.Collections.Generic;
using Nibriboard.RippleSpace;
namespace Nibriboard.Client
{
///
/// Represents a cache of chunk references. Useful for keeping track which chunks
/// a remote party is currently keeping in memory.
///
public class ChunkCache
{
List cache = new List();
///
/// Creates a new empty chunk cache.
///
public ChunkCache()
{
}
///
/// Adds a chunk reference to the cache.
/// If the chunk is already in the cache, then it won't be added again.
///
/// The chunk reference to add.
public void Add(ChunkReference chunkRef)
{
// If this cache already contains the specified chunk reference, then we
// probably shouldn't add it to the cache twice
if(cache.Contains(chunkRef))
return;
cache.Add(chunkRef);
}
///
/// Adds the given chunk references to the cache.
/// Quietly skips over duplicate chunk references.
///
/// The chunk references to add.
public void Add(IEnumerable chunkRefs)
{
foreach(ChunkReference chunkRef in chunkRefs)
Add(chunkRef);
}
///
/// Remvoes a chunk reference from the cache.
///
/// The chunk reference to remove.
public void Remove(ChunkReference chunkRef)
{
cache.Remove(chunkRef);
}
///
/// Removes a list of chunk references from the cache.
///
/// The chunk references to remove.
public void Remove(IEnumerable chunkRefs)
{
foreach(ChunkReference chunkRef in chunkRefs)
Remove(chunkRef);
}
///
/// Returns whether this cache contains the specified chunk reference.
///
/// The chunk reference to check for.
/// Whether this cache contaisn the specified chunk reference..
public bool Contains(ChunkReference chunkRef)
{
return cache.Contains(chunkRef);
}
///
/// Given a list of chunk references, return another list of chunk references
/// that aren't in this chunk cache.
///
/// The list of chunk references to check against this chunk cache.
/// The chunk references missing from this chunk cache.
public List FindMissing(IEnumerable sourceChunkRefs)
{
List result = new List();
foreach(ChunkReference sourceChunkRef in sourceChunkRefs)
{
if(!Contains(sourceChunkRef))
result.Add(sourceChunkRef);
}
return result;
}
}
}