Skip to content

Commit

Permalink
Version 0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
Dijji committed Aug 20, 2016
1 parent ad5c009 commit 89c6d99
Show file tree
Hide file tree
Showing 15 changed files with 1,503 additions and 258 deletions.
27 changes: 19 additions & 8 deletions BTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,27 @@ class BTree<T> where T : TreeNode
{
public TreeIntermediate Root { get; private set; } = new TreeIntermediate();

public T Lookup(UInt32 key)
public T Lookup(UInt32 key, Action<TreeIntermediate> readDeferred = null)
{
return (T)LookupTreeNode(Root, key);
return (T)LookupTreeNode(Root, key, readDeferred);
}

public T Lookup(UInt64 key)
public T Lookup(UInt64 key, Action<TreeIntermediate> readDeferred = null)
{
return (T)LookupTreeNode(Root, key);
return (T)LookupTreeNode(Root, key, readDeferred);
}

// Perform a lookup in the b-tree
private static TreeNode LookupTreeNode(TreeIntermediate parent, UInt64 key)
private static TreeNode LookupTreeNode(TreeIntermediate parent, UInt64 key, Action<TreeIntermediate> readDeferred)
{
if (parent.ReadDeferred)
{
if (readDeferred != null)
readDeferred(parent);
else
throw new Exception("Deferred index found, but no reader supplied");
}

TreeIntermediate next = null;
foreach (var n in parent.Children)
{
Expand All @@ -39,20 +47,20 @@ private static TreeNode LookupTreeNode(TreeIntermediate parent, UInt64 key)
else if (key < n.Key)
{
if (next != null)
return LookupTreeNode(next, key);
return LookupTreeNode(next, key, readDeferred);
else
return null; // Key does not exist
}
else // key matches
{
if (n is TreeIntermediate)
return LookupTreeNode((TreeIntermediate)n, key);
return LookupTreeNode((TreeIntermediate)n, key, readDeferred);
else
return n;
}
}
if (next != null)
return LookupTreeNode(next, key);
return LookupTreeNode(next, key, readDeferred);
else
return null; // Key does not exist
}
Expand All @@ -68,6 +76,8 @@ class TreeNode
class TreeIntermediate : TreeNode
{
public List<TreeNode> Children = new List<TreeNode>();
public ulong? fileOffset = null;
public bool ReadDeferred { get { return fileOffset != null; } }
}

// Terminal node in node tree
Expand All @@ -87,6 +97,7 @@ class DataRef : TreeNode
{
public UInt64 Offset;
public int Length;
public int InflatedLength; // Only used for Unicode4K
public bool IsInternal { get { return (Key & 0x02) != 0; } }
}
}
Loading

0 comments on commit 89c6d99

Please sign in to comment.