我正在做一些文件分析,我在文件中标记已探索的区域。现在我想找到未探索的区域,这样我就知道接下来要看什么了。这很像碎片整理软件显示的免费和已用区域。
示例:
在这张图片中,假设已探索区域为红色,未探索区域为灰色。我需要确定这些红色区域的灰色区域边界。
我当前的代码是一个自定义二进制读取器,用于记录已读取的内容:
public class CustomBinaryReader : BinaryReader {
private readonly List<Block> _blocks;
public CustomBinaryReader([NotNull] Stream input) : this(input, Encoding.Default) { }
public CustomBinaryReader(Stream input, Encoding encoding, bool leaveOpen = true) : base(input, encoding, leaveOpen) {
_blocks = new List<Block>();
}
public override byte[] ReadBytes(int count) {
Log(count);
return base.ReadBytes(count);
}
private void Log(int count) {
_blocks.Add(new Block(BaseStream.Position, count));
}
private IEnumerable<Block> GetUnreadBlocks() {
// how to get unread blocks in the stream, from read blocks ?
throw new NotImplementedException();
}
}
定义区域的类型:
public class Block {
public Block(long position, long length) {
Position = position;
Length = length;
}
public long Position { get; }
public long Length { get; }
}
问题:
是否有一类算法或数据结构来解决此类问题(如树或图)?如果不存在这样的事情,你能给我一些解决这个问题的方法或提示吗?
最佳答案
按位置顺序对使用的区域进行排序。
找到每个的上限为 position+length
。
从那里开始,每个开放区域都从一个区域的上限开始,直到(但不包括)下一个区域的下限。
关于c# - 哪种算法用于在数字范围内查找空数字范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45578506/