Skip to content

Commit

Permalink
more trivial fvi faffery
Browse files Browse the repository at this point in the history
  • Loading branch information
sdcondon committed Oct 12, 2024
1 parent d907475 commit 14d7592
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,11 @@ public IAsyncEnumerable<TValue> GetSubsuming(CNFClause clause)

return ExpandNode(root, 0);

async IAsyncEnumerable<TValue> ExpandNode(IAsyncFeatureVectorIndexNode<TFeature, TValue> node, int elementIndex)
async IAsyncEnumerable<TValue> ExpandNode(IAsyncFeatureVectorIndexNode<TFeature, TValue> node, int componentIndex)
{
if (elementIndex < featureVector.Count)
if (componentIndex < featureVector.Count)
{
var component = featureVector[elementIndex];
var component = featureVector[componentIndex];

// Recurse for children with matching feature and lower magnitude:
var matchingChildNodes = node
Expand All @@ -183,15 +183,15 @@ async IAsyncEnumerable<TValue> ExpandNode(IAsyncFeatureVectorIndexNode<TFeature,

await foreach (var ((childFeature, childMagnitude), childNode) in node.ChildrenAscending)
{
await foreach (var value in ExpandNode(childNode, elementIndex + 1))
await foreach (var value in ExpandNode(childNode, componentIndex + 1))
{
yield return value;
}
}

// Matching feature might not be there at all in stored clauses, which means it has an implicit
// value of zero, and we thus can't preclude subsumption - so we also just skip the current key element:
await foreach (var value in ExpandNode(node, elementIndex + 1))
// magnitude of zero, and we thus can't preclude subsumption - so we also just skip the current key element:
await foreach (var value in ExpandNode(node, componentIndex + 1))
{
yield return value;
}
Expand Down Expand Up @@ -221,24 +221,24 @@ public IAsyncEnumerable<TValue> GetSubsumed(CNFClause clause)

return ExpandNode(root, 0);

async IAsyncEnumerable<TValue> ExpandNode(IAsyncFeatureVectorIndexNode<TFeature, TValue> node, int elementIndex)
async IAsyncEnumerable<TValue> ExpandNode(IAsyncFeatureVectorIndexNode<TFeature, TValue> node, int componentIndex)
{
if (elementIndex < featureVector.Count)
if (componentIndex < featureVector.Count)
{
await foreach (var ((childFeature, childMagnitude), childNode) in node.ChildrenDescending)
{
// todo: is this right? or do we need by feature AND magnitude here?
// todo-bug: think answer to above is that its wrong - think need to look at greater feature AND same feature with equal or higher mag? add a test!
// todo: can be made more efficient now that node children are ordered
if (elementIndex == 0 || root.FeatureComparer.Compare(childFeature, featureVector[elementIndex - 1].Feature) > 0)
if (componentIndex == 0 || root.FeatureComparer.Compare(childFeature, featureVector[componentIndex - 1].Feature) > 0)
{
var childComparedToCurrent = root.FeatureComparer.Compare(childFeature, featureVector[elementIndex].Feature);
var childComparedToCurrent = root.FeatureComparer.Compare(childFeature, featureVector[componentIndex].Feature);

if (childComparedToCurrent <= 0)
{
var keyElementIndexOffset = childComparedToCurrent == 0 && childMagnitude >= featureVector[elementIndex].Magnitude ? 1 : 0;
var keyElementIndexOffset = childComparedToCurrent == 0 && childMagnitude >= featureVector[componentIndex].Magnitude ? 1 : 0;

await foreach (var value in ExpandNode(childNode, elementIndex + keyElementIndexOffset))
await foreach (var value in ExpandNode(childNode, componentIndex + keyElementIndexOffset))
{
yield return value;
}
Expand Down Expand Up @@ -274,7 +274,7 @@ async IAsyncEnumerable<TValue> GetAllDescendentValues(IAsyncFeatureVectorIndexNo
}
}

private IList<FeatureVectorComponent<TFeature>> MakeAndSortFeatureVector(CNFClause clause)
private List<FeatureVectorComponent<TFeature>> MakeAndSortFeatureVector(CNFClause clause)
{
// todo-performance: if we need a list anyway, probably faster to make the list, then sort it in place? test me
return featureVectorSelector(clause).OrderBy(kvp => kvp.Feature, root.FeatureComparer).ToList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ public bool Remove(CNFClause key)

return ExpandNode(root, 0);

bool ExpandNode(IFeatureVectorIndexNode<TFeature, TValue> node, int vectorComponentIndex)
bool ExpandNode(IFeatureVectorIndexNode<TFeature, TValue> node, int componentIndex)
{
if (vectorComponentIndex < featureVector.Count)
if (componentIndex < featureVector.Count)
{
var element = featureVector[vectorComponentIndex];
var element = featureVector[componentIndex];

if (!node.TryGetChild(element, out var childNode) || !ExpandNode(childNode, vectorComponentIndex + 1))
if (!node.TryGetChild(element, out var childNode) || !ExpandNode(childNode, componentIndex + 1))
{
return false;
}
Expand Down Expand Up @@ -189,7 +189,7 @@ IEnumerable<TValue> ExpandNode(IFeatureVectorIndexNode<TFeature, TValue> node, i
}

// Matching feature might not be there at all in stored clauses, which means it has an implicit
// value of zero, and we thus can't preclude subsumption - so we also just skip the current key element:
// magnitude of zero, and we thus can't preclude subsumption - so we also just skip the current key element:
foreach (var value in ExpandNode(node, componentIndex + 1))
{
yield return value;
Expand Down Expand Up @@ -223,25 +223,25 @@ public IEnumerable<TValue> GetSubsumed(CNFClause clause)
// NB: subsumed clauses will have equal or higher vector elements.
// We allow zero-valued elements to be omitted from the vectors (so that we don't have to know what features are possible ahead of time).
// This makes the logic here a little similar to what you'd find in a set trie when querying for supersets.
IEnumerable<TValue> ExpandNode(IFeatureVectorIndexNode<TFeature, TValue> node, int vectorComponentIndex)
IEnumerable<TValue> ExpandNode(IFeatureVectorIndexNode<TFeature, TValue> node, int componentIndex)
{
if (vectorComponentIndex < featureVector.Count)
if (componentIndex < featureVector.Count)
{
foreach (var ((childFeature, childMagnitude), childNode) in node.ChildrenDescending)
{
// todo: is this right? or do we need by feature AND magnitude here?
// todo-bug: think answer to above is that its wrong - think need to look at greater feature AND same feature with equal or higher mag? add a test!
// todo: can be made more efficient now that node children are ordered

if (vectorComponentIndex == 0 || root.FeatureComparer.Compare(childFeature, featureVector[vectorComponentIndex - 1].Feature) > 0)
if (componentIndex == 0 || root.FeatureComparer.Compare(childFeature, featureVector[componentIndex - 1].Feature) > 0)
{
var childComparedToCurrent = root.FeatureComparer.Compare(childFeature, featureVector[vectorComponentIndex].Feature);
var childComparedToCurrent = root.FeatureComparer.Compare(childFeature, featureVector[componentIndex].Feature);

if (childComparedToCurrent <= 0)
{
var queryComponentIndexOffset = childComparedToCurrent == 0 && childMagnitude >= featureVector[vectorComponentIndex].Magnitude ? 1 : 0;
var queryComponentIndexOffset = childComparedToCurrent == 0 && childMagnitude >= featureVector[componentIndex].Magnitude ? 1 : 0;

foreach (var value in ExpandNode(childNode, vectorComponentIndex + queryComponentIndexOffset))
foreach (var value in ExpandNode(childNode, componentIndex + queryComponentIndexOffset))
{
yield return value;
}
Expand Down Expand Up @@ -277,7 +277,7 @@ IEnumerable<TValue> GetAllDescendentValues(IFeatureVectorIndexNode<TFeature, TVa
}
}

private IList<FeatureVectorComponent<TFeature>> MakeAndSortFeatureVector(CNFClause clause)
private List<FeatureVectorComponent<TFeature>> MakeAndSortFeatureVector(CNFClause clause)
{
// todo-performance: if we need a list anyway, probably faster to make the list, then sort it in place? test me
return featureVectorSelector(clause).OrderBy(kvp => kvp.Feature, root.FeatureComparer).ToList();
Expand Down

0 comments on commit 14d7592

Please sign in to comment.