Skip to content

Commit

Permalink
Map Storage Improved
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Sep 2, 2024
1 parent a03bb4f commit 54c9639
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 39 deletions.
81 changes: 44 additions & 37 deletions YantraJS.Core/Core/Storage/SAUint32Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,47 +96,47 @@ public IEnumerable<KeyValue> All
{
if (!this.nodes.IsEmpty)
{
if (!this.roots.IsEmpty)
{
foreach(var c in this.EnumerateNode(this.roots))
{
yield return c;
}
}
//for (int i = 0; i < this.storage.Length; i++)
//if (!this.roots.IsEmpty)
//{
// var node = this.storage[i];
// if (node.HasValue)
// foreach(var c in this.EnumerateNode(this.roots))
// {
// yield return (node.Key, node.Value);
// yield return c;
// }
//}
}
}

private IEnumerable<(uint Key, T Value)> EnumerateNode(VirtualArray nodes)
{
for (var i = 0; i<nodes.Length;i++)
{
var n = this.nodes[nodes, i];
if (n.HasValue)
for (int i = 0; i < this.nodes.Count; i++)
{
yield return (n.Key, n.Value);
}
}
for (var i = 0; i < nodes.Length; i++)
{
var n = this.nodes[nodes, i];
if (!n.Children.IsEmpty)
{
foreach (var c in this.EnumerateNode(n.Children))
var node = this.nodes.GetAt(i);
if (node.HasValue)
{
yield return c;
yield return (node.Key, node.Value);
}
}
}
}

//private IEnumerable<(uint Key, T Value)> EnumerateNode(VirtualArray nodes)
//{
// for (var i = 0; i<nodes.Length;i++)
// {
// var n = this.nodes[nodes, i];
// if (n.HasValue)
// {
// yield return (n.Key, n.Value);
// }
// }
// for (var i = 0; i < nodes.Length; i++)
// {
// var n = this.nodes[nodes, i];
// if (!n.Children.IsEmpty)
// {
// foreach (var c in this.EnumerateNode(n.Children))
// {
// yield return c;
// }
// }
// }
//}

public bool HasKey(uint key)
{
ref var node = ref GetNode(key);
Expand Down Expand Up @@ -215,13 +215,14 @@ private ref Node GetNode(uint originalKey, bool create = false)
}
// extend...
this.roots = this.nodes.Allocate(4);
this.nodes[this.roots, 0].State = NodeState.Filled;
}

//if (originalKey == 0)
//{
// node = ref this.nodes[this.roots, 0];
// return ref node;
//}
if (originalKey == 0)
{
node = ref this.nodes[this.roots, 0];
return ref node;
}

var leaves = this.roots;

Expand Down Expand Up @@ -251,26 +252,32 @@ private ref Node GetNode(uint originalKey, bool create = false)
}
if (node.Key > originalKey)
{
// need to make this non recursive...
var oldKey = node.Key;
var oldValue = node.Value;
var oldChild = node.Children;
// var oldChild = node.Children;
node.Key = originalKey;
node.State = NodeState.Filled;
node.Value = default;
ref var newChild = ref GetNode(oldKey, true);
newChild.Key = oldKey;
newChild.Value = oldValue;
// var newChildren = newChild.Children;
// newChild.Children = oldChild;
newChild.State |= NodeState.HasValue;
// this is case when array is resized
// and we still might have reference to old node
node = ref this.nodes[leaves, index];
// node.Children = newChildren;
return ref node;
}
node.State |= NodeState.Filled;
if (node.Children.IsEmpty)
{
node.Children = this.nodes.Allocate(4);
var c = this.nodes.Allocate(4);
// allocation may have moved node
node = ref this.nodes[leaves, index];
node.Children = c;
}
}
var next = node.Children;
Expand Down
12 changes: 10 additions & 2 deletions YantraJS.Core/Core/Storage/VirtualMemory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

namespace YantraJS.Core.Core.Storage
Expand All @@ -10,11 +11,13 @@ public struct VirtualMemory<T>
private T[] nodes = null;
private int last = 0;

public bool IsEmpty => this.nodes == null;
public bool IsEmpty => this.Count == 0;

public int Count => this.nodes?.Length ?? 0;

public VirtualMemory()
{

}

public ref T this[VirtualArray a, int index]
Expand All @@ -25,6 +28,11 @@ public VirtualMemory()
}
}

[Browsable(false)]
public ref T GetAt(int index) {
return ref this.nodes[index];
}

public VirtualArray Allocate(int length)
{
var max = this.last + length;
Expand Down

0 comments on commit 54c9639

Please sign in to comment.