From 14d7592b74a51720985549553abf7372a1fcb48d Mon Sep 17 00:00:00 2001 From: Simon Condon Date: Sat, 12 Oct 2024 22:23:36 +0100 Subject: [PATCH] more trivial fvi faffery --- ...syncFeatureVectorIndex{TFeature,TValue}.cs | 26 +++++++++---------- .../FeatureVectorIndex{TFeature,TValue}.cs | 24 ++++++++--------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/SCFirstOrderLogic/ClauseIndexing/AsyncFeatureVectorIndex{TFeature,TValue}.cs b/src/SCFirstOrderLogic/ClauseIndexing/AsyncFeatureVectorIndex{TFeature,TValue}.cs index 8876aa2..0101348 100644 --- a/src/SCFirstOrderLogic/ClauseIndexing/AsyncFeatureVectorIndex{TFeature,TValue}.cs +++ b/src/SCFirstOrderLogic/ClauseIndexing/AsyncFeatureVectorIndex{TFeature,TValue}.cs @@ -168,11 +168,11 @@ public IAsyncEnumerable GetSubsuming(CNFClause clause) return ExpandNode(root, 0); - async IAsyncEnumerable ExpandNode(IAsyncFeatureVectorIndexNode node, int elementIndex) + async IAsyncEnumerable ExpandNode(IAsyncFeatureVectorIndexNode 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 @@ -183,15 +183,15 @@ async IAsyncEnumerable ExpandNode(IAsyncFeatureVectorIndexNode GetSubsumed(CNFClause clause) return ExpandNode(root, 0); - async IAsyncEnumerable ExpandNode(IAsyncFeatureVectorIndexNode node, int elementIndex) + async IAsyncEnumerable ExpandNode(IAsyncFeatureVectorIndexNode 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; } @@ -274,7 +274,7 @@ async IAsyncEnumerable GetAllDescendentValues(IAsyncFeatureVectorIndexNo } } - private IList> MakeAndSortFeatureVector(CNFClause clause) + private List> 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(); diff --git a/src/SCFirstOrderLogic/ClauseIndexing/FeatureVectorIndex{TFeature,TValue}.cs b/src/SCFirstOrderLogic/ClauseIndexing/FeatureVectorIndex{TFeature,TValue}.cs index c982926..d67f410 100644 --- a/src/SCFirstOrderLogic/ClauseIndexing/FeatureVectorIndex{TFeature,TValue}.cs +++ b/src/SCFirstOrderLogic/ClauseIndexing/FeatureVectorIndex{TFeature,TValue}.cs @@ -99,13 +99,13 @@ public bool Remove(CNFClause key) return ExpandNode(root, 0); - bool ExpandNode(IFeatureVectorIndexNode node, int vectorComponentIndex) + bool ExpandNode(IFeatureVectorIndexNode 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; } @@ -189,7 +189,7 @@ IEnumerable ExpandNode(IFeatureVectorIndexNode 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; @@ -223,9 +223,9 @@ public IEnumerable 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 ExpandNode(IFeatureVectorIndexNode node, int vectorComponentIndex) + IEnumerable ExpandNode(IFeatureVectorIndexNode node, int componentIndex) { - if (vectorComponentIndex < featureVector.Count) + if (componentIndex < featureVector.Count) { foreach (var ((childFeature, childMagnitude), childNode) in node.ChildrenDescending) { @@ -233,15 +233,15 @@ IEnumerable ExpandNode(IFeatureVectorIndexNode node, i // 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; } @@ -277,7 +277,7 @@ IEnumerable GetAllDescendentValues(IFeatureVectorIndexNode> MakeAndSortFeatureVector(CNFClause clause) + private List> 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();