Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/SIL.Machine.Morphology.HermitCrab/CharacterDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ internal CharacterDefinition(IList<string> representations, FeatureStruct fs)

public FeatureSymbol Type
{
get { return (FeatureSymbol)_fs.GetValue(HCFeatureSystem.Type); }
get
{
FeatureSymbol fsym = SymbolicFeatureValue.GetFeatureSymbolFromFeatureStruct(_fs, HCFeatureSystem.Type);
return fsym;
}
}

public ReadOnlyCollection<string> Representations
Expand Down
4 changes: 2 additions & 2 deletions src/SIL.Machine.Morphology.HermitCrab/HCFeatureSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static HCFeatureSystem()
Modified = new SymbolicFeature(Guid.NewGuid().ToString(), Dirty, Clean)
{
Description = "Modified",
DefaultValue = new SymbolicFeatureValue(Clean)
DefaultValue = SymbolicFeatureValueFactory.Instance.Create(Clean)
};

Deleted = new FeatureSymbol(Guid.NewGuid().ToString()) { Description = "Deleted" };
Expand All @@ -60,7 +60,7 @@ static HCFeatureSystem()
Deletion = new SymbolicFeature(Guid.NewGuid().ToString(), Deleted, NotDeleted)
{
Description = "Deletion",
DefaultValue = new SymbolicFeatureValue(NotDeleted)
DefaultValue = SymbolicFeatureValueFactory.Instance.Create(NotDeleted)
};

LeftSide = new FeatureSymbol(Guid.NewGuid().ToString()) { Description = "LeftSide" };
Expand Down
35 changes: 29 additions & 6 deletions src/SIL.Machine.Morphology.HermitCrab/HermitCrabExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,29 @@ public static class HermitCrabExtensions
{
public static FeatureSymbol Type(this ShapeNode node)
{
return (FeatureSymbol)node.Annotation.FeatureStruct.GetValue(HCFeatureSystem.Type);
FeatureSymbol fSym = SymbolicFeatureValue.GetFeatureSymbolFromFeatureStruct(
node.Annotation.FeatureStruct,
HCFeatureSystem.Type
);
return fSym;
}

public static FeatureSymbol Type(this Annotation<ShapeNode> ann)
{
return (FeatureSymbol)ann.FeatureStruct.GetValue(HCFeatureSystem.Type);
FeatureSymbol fSym = SymbolicFeatureValue.GetFeatureSymbolFromFeatureStruct(
ann.FeatureStruct,
HCFeatureSystem.Type
);
return fSym;
}

public static FeatureSymbol Type(this Constraint<Word, ShapeNode> constraint)
{
return (FeatureSymbol)constraint.FeatureStruct.GetValue(HCFeatureSystem.Type);
FeatureSymbol fSym = SymbolicFeatureValue.GetFeatureSymbolFromFeatureStruct(
constraint.FeatureStruct,
HCFeatureSystem.Type
);
return fSym;
}

internal static FeatureStruct AntiFeatureStruct(this FeatureStruct fs)
Expand Down Expand Up @@ -55,8 +67,13 @@ internal static FeatureStruct AntiFeatureStruct(this FeatureStruct fs)

internal static bool IsDirty(this ShapeNode node)
{
return ((FeatureSymbol)node.Annotation.FeatureStruct.GetValue(HCFeatureSystem.Modified))
== HCFeatureSystem.Dirty;
FeatureSymbol fSym = SymbolicFeatureValue.GetFeatureSymbolFromFeatureStruct(
node.Annotation.FeatureStruct,
HCFeatureSystem.Modified
);
return fSym == HCFeatureSystem.Dirty;
//return ((FeatureSymbol)node.Annotation.FeatureStruct.GetValue(HCFeatureSystem.Modified))
// == HCFeatureSystem.Dirty;
}

internal static void SetDirty(this ShapeNode node, bool dirty)
Expand All @@ -71,7 +88,13 @@ internal static bool IsDeleted(this Annotation<ShapeNode> ann)
{
SymbolicFeatureValue sfv;
if (ann.FeatureStruct.TryGetValue(HCFeatureSystem.Deletion, out sfv))
return ((FeatureSymbol)sfv) == HCFeatureSystem.Deleted;
{
FeatureSymbol fSym = SymbolicFeatureValue.GetFeatureSymbolFromFeatureStruct(
ann.FeatureStruct,
HCFeatureSystem.Deleted.Feature
);
return fSym == HCFeatureSystem.Deleted;
}
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ Shape output
{
foreach (ShapeNode node in output.GetNodes(outputRange))
{
if ((FeatureSymbol)modifyFromFS.GetValue(HCFeatureSystem.Type) == node.Annotation.Type())
FeatureSymbol fsym = SymbolicFeatureValue.GetFeatureSymbolFromFeatureStruct(
modifyFromFS,
HCFeatureSystem.Type
);
if (fsym == node.Annotation.Type())
node.Annotation.FeatureStruct.Add(modifyFromFS, match.VariableBindings);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ IDictionary<string, int> capturedParts
Constraint<Word, ShapeNode> constraint in group
.GetNodesDepthFirst()
.OfType<Constraint<Word, ShapeNode>>()
.Where(c => c.Type() == (FeatureSymbol)_simpleCtxt.FeatureStruct.GetValue(HCFeatureSystem.Type))
.Where(c =>
c.Type()
== SymbolicFeatureValue.GetFeatureSymbolFromFeatureStruct(
_simpleCtxt.FeatureStruct,
HCFeatureSystem.Type
)
)
)
{
constraint.FeatureStruct.PriorityUnion(_simpleCtxt.FeatureStruct);
Expand All @@ -65,7 +71,10 @@ ShapeNode inputNode in GetSkippedOptionalNodes(match.Input.Shape, inputGroup.Ran
ShapeNode outputNode = inputNode.Clone();
if (
outputNode.Annotation.Type()
== (FeatureSymbol)_simpleCtxt.FeatureStruct.GetValue(HCFeatureSystem.Type)
== SymbolicFeatureValue.GetFeatureSymbolFromFeatureStruct(
_simpleCtxt.FeatureStruct,
HCFeatureSystem.Type
)
)
{
outputNode.Annotation.FeatureStruct.PriorityUnion(
Expand Down
2 changes: 1 addition & 1 deletion src/SIL.Machine.Morphology.HermitCrab/XmlLanguageLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,7 @@ Dictionary<string, Tuple<string, SymbolicFeature>> variables
var varID = (string)varElem.Attribute("variableFeature");
Tuple<string, SymbolicFeature> variable = variables[varID];
ctxtVars.Add(
new SymbolicFeatureValue(
SymbolicFeatureValueFactory.Instance.Create(
variable.Item2,
variable.Item1,
((string)varElem.Attribute("polarity") ?? "plus") == "plus"
Expand Down
5 changes: 4 additions & 1 deletion src/SIL.Machine.Morphology.HermitCrab/XmlLanguageWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,10 @@ private bool IsAnchor(PatternNode<Word, ShapeNode> node, FeatureSymbol type)
if (node is Constraint<Word, ShapeNode> constraint)
{
return constraint.Type() == HCFeatureSystem.Anchor
&& (FeatureSymbol)constraint.FeatureStruct.GetValue(HCFeatureSystem.AnchorType) == type;
&& SymbolicFeatureValue.GetFeatureSymbolFromFeatureStruct(
constraint.FeatureStruct,
HCFeatureSystem.AnchorType
) == type;
}

return false;
Expand Down
10 changes: 4 additions & 6 deletions src/SIL.Machine/FeatureModel/FeatureStruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public static IFeatureStructSyntax NewMutable(FeatureSystem featSys, FeatureStru

private readonly IDBearerDictionary<Feature, FeatureValue> _definite;
private int? _hashCode;
private readonly SymbolicFeatureValueFactory _valueFactory = SymbolicFeatureValueFactory.Instance;

/// <summary>
/// Initializes a new instance of the <see cref="FeatureStruct"/> class.
Expand Down Expand Up @@ -126,15 +127,12 @@ public void AddValue(SymbolicFeature feature, IEnumerable<FeatureSymbol> values)
throw new ArgumentNullException("values");

FeatureSymbol[] vals = values.ToArray();
AddValue(feature, vals.Length == 0 ? new SymbolicFeatureValue(feature) : new SymbolicFeatureValue(vals));
AddValue(feature, vals.Length == 0 ? _valueFactory.Create(feature) : _valueFactory.Create(vals));
}

public void AddValue(SymbolicFeature feature, params FeatureSymbol[] values)
{
AddValue(
feature,
values.Length == 0 ? new SymbolicFeatureValue(feature) : new SymbolicFeatureValue(values)
);
AddValue(feature, values.Length == 0 ? _valueFactory.Create(feature) : _valueFactory.Create(values));
}

public void AddValue(StringFeature feature, IEnumerable<string> values)
Expand Down Expand Up @@ -456,7 +454,7 @@ IDictionary<FeatureStruct, ISet<FeatureStruct>> visited
else if (otherValue is StringFeatureValue)
thisValue = new StringFeatureValue();
else
thisValue = new SymbolicFeatureValue((SymbolicFeature)featVal.Key);
thisValue = _valueFactory.Create((SymbolicFeature)featVal.Key);
_definite[featVal.Key] = thisValue;
}
if (!thisValue.AddImpl(otherValue, varBindings, visited))
Expand Down
5 changes: 3 additions & 2 deletions src/SIL.Machine/FeatureModel/Fluent/FeatureStructBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class FeatureStructBuilder : IFeatureStructSyntax, IFeatureValueSyntax
private readonly FeatureStruct _fs;
private readonly IDictionary<int, FeatureValue> _ids;
private readonly bool _mutable;
private readonly SymbolicFeatureValueFactory _valueFactory = SymbolicFeatureValueFactory.Instance;

private Feature _lastFeature;
private bool _not;
Expand Down Expand Up @@ -242,7 +243,7 @@ private bool AddSymbols(Feature feature, FeatureSymbol[] symbols, int id)
if (symbols.Any(s => s.Feature != feature))
return false;
var symbolFeature = (SymbolicFeature)feature;
var value = new SymbolicFeatureValue(_not ? symbolFeature.PossibleSymbols.Except(symbols) : symbols);
var value = _valueFactory.Create(_not ? symbolFeature.PossibleSymbols.Except(symbols) : symbols);
_fs.AddValue(symbolFeature, value);
_not = false;
if (id > -1)
Expand Down Expand Up @@ -441,7 +442,7 @@ private void AddVariable(string name, int id)
if (_lastFeature is StringFeature)
vfv = new StringFeatureValue(name, !_not);
else
vfv = new SymbolicFeatureValue((SymbolicFeature)_lastFeature, name, !_not);
vfv = _valueFactory.Create((SymbolicFeature)_lastFeature, name, !_not);
_fs.AddValue(_lastFeature, vfv);
_not = false;
if (id > -1)
Expand Down
6 changes: 5 additions & 1 deletion src/SIL.Machine/FeatureModel/SimpleFeatureValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ public abstract class SimpleFeatureValue : FeatureValue, ICloneable<SimpleFeatur
{
public static implicit operator SimpleFeatureValue(FeatureSymbol symbol)
{
return new SymbolicFeatureValue(symbol);
return SymbolicFeatureValueFactory.Instance.Create(symbol);
}

public static explicit operator FeatureSymbol(SimpleFeatureValue sfv)
{
if (sfv is SymbolicFeatureValueBA sfvBA)
{
return (FeatureSymbol)sfvBA;
}
return (FeatureSymbol)((SymbolicFeatureValue)sfv);
}

Expand Down
16 changes: 14 additions & 2 deletions src/SIL.Machine/FeatureModel/SymbolicFeature.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections;
using System.Collections.Generic;
using SIL.ObjectModel;

Expand All @@ -8,6 +9,8 @@ public class SymbolicFeature : Feature
private readonly PossibleSymbolCollection _possibleSymbols;
private readonly ulong _mask;

private readonly BitArray _maskBA = new BitArray(sizeof(ulong) * 8, false);

public SymbolicFeature(string id, params FeatureSymbol[] possibleSymbols)
: this(id, (IEnumerable<FeatureSymbol>)possibleSymbols) { }

Expand All @@ -21,7 +24,11 @@ public SymbolicFeature(string id, IEnumerable<FeatureSymbol> possibleSymbols)
symbol.Feature = this;
symbol.Index = i++;
}
_mask = (1UL << _possibleSymbols.Count) - 1UL;
int symbolCount = _possibleSymbols.Count;
if (symbolCount > SymbolicFeatureValueFactory.Instance.NeedToUseBitArray)
_maskBA = new BitArray(symbolCount, true);
else
_mask = (1UL << symbolCount) - 1UL;
}

/// <summary>
Expand All @@ -35,12 +42,17 @@ public IReadOnlyKeyedCollection<string, FeatureSymbol> PossibleSymbols

public string DefaultSymbolID
{
set { DefaultValue = new SymbolicFeatureValue(_possibleSymbols[value]); }
set { DefaultValue = SymbolicFeatureValueFactory.Instance.Create(_possibleSymbols[value]); }
}

internal ulong Mask
{
get { return _mask; }
}

internal BitArray MaskBA
{
get { return _maskBA; }
}
}
}
Loading
Loading