Skip to content

Commit d9f37f6

Browse files
committed
Refactor to encapsulate flags data structure
- rename interfaces and classes
1 parent ad4ea2d commit d9f37f6

File tree

10 files changed

+433
-656
lines changed

10 files changed

+433
-656
lines changed
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
6+
namespace SIL.Machine.FeatureModel
7+
{
8+
internal class BitArraySymbolicFeatureValueFlags : ISymbolicFeatureValueFlags
9+
{
10+
private readonly SymbolicFeature _feature;
11+
private readonly BitArray _flags;
12+
13+
public BitArraySymbolicFeatureValueFlags(SymbolicFeature feature)
14+
{
15+
_feature = feature;
16+
_flags = new BitArray(feature.PossibleSymbols.Count, false);
17+
}
18+
19+
private BitArraySymbolicFeatureValueFlags(SymbolicFeature feature, BitArray flags)
20+
{
21+
_feature = feature;
22+
_flags = flags;
23+
}
24+
25+
public bool HasAnySet()
26+
{
27+
return HasAnySet(_flags);
28+
}
29+
30+
public bool HasAllSet()
31+
{
32+
return HasAllSet(_flags);
33+
}
34+
35+
public bool Get(FeatureSymbol symbol)
36+
{
37+
return _flags.Get(symbol.Index);
38+
}
39+
40+
public FeatureSymbol GetFirst()
41+
{
42+
return _feature.PossibleSymbols.FirstOrDefault(Get);
43+
}
44+
45+
public void Set(IEnumerable<FeatureSymbol> symbols)
46+
{
47+
foreach (FeatureSymbol symbol in symbols)
48+
_flags.Set(symbol.Index, true);
49+
}
50+
51+
public bool IsSupersetOf(bool not, ISymbolicFeatureValueFlags other, bool notOther)
52+
{
53+
var otherBitArray = (BitArraySymbolicFeatureValueFlags)other;
54+
if (!not && !notOther)
55+
{
56+
return AreEqual(Copy(_flags).And(otherBitArray._flags), otherBitArray._flags);
57+
}
58+
else if (!not)
59+
{
60+
BitArray notOtherFlags = Copy(otherBitArray._flags).Not();
61+
return AreEqual(Copy(_flags).And(notOtherFlags), notOtherFlags);
62+
}
63+
else if (!notOther)
64+
{
65+
return AreEqual(Copy(_flags).Not().And(otherBitArray._flags), otherBitArray._flags);
66+
}
67+
else
68+
{
69+
BitArray notOtherFlags = Copy(otherBitArray._flags).Not();
70+
return AreEqual(Copy(_flags).Not().And(notOtherFlags), notOtherFlags);
71+
}
72+
}
73+
74+
public bool Overlaps(bool not, ISymbolicFeatureValueFlags other, bool notOther)
75+
{
76+
var otherBitArray = (BitArraySymbolicFeatureValueFlags)other;
77+
if (!not && !notOther)
78+
{
79+
return HasAnySet(Copy(_flags).And(otherBitArray._flags));
80+
}
81+
else if (!not)
82+
{
83+
BitArray notOtherFlags = Copy(otherBitArray._flags).Not();
84+
return AreEqual(Copy(_flags).And(notOtherFlags), notOtherFlags);
85+
}
86+
else if (!notOther)
87+
{
88+
return AreEqual(Copy(_flags).Not().And(otherBitArray._flags), otherBitArray._flags);
89+
}
90+
else
91+
{
92+
BitArray notOtherFlags = Copy(otherBitArray._flags).Not();
93+
return AreEqual(Copy(_flags).Not().And(notOtherFlags), notOtherFlags);
94+
}
95+
}
96+
97+
public void IntersectWith(bool not, ISymbolicFeatureValueFlags other, bool notOther)
98+
{
99+
var otherBitArray = (BitArraySymbolicFeatureValueFlags)other;
100+
if (!not && !notOther)
101+
{
102+
_flags.And(otherBitArray._flags);
103+
}
104+
else if (!not)
105+
{
106+
_flags.And(Copy(otherBitArray._flags).Not());
107+
}
108+
else if (!notOther)
109+
{
110+
_flags.Not().And(otherBitArray._flags);
111+
}
112+
else
113+
{
114+
_flags.Not().And(Copy(otherBitArray._flags).Not());
115+
}
116+
}
117+
118+
public void UnionWith(bool not, ISymbolicFeatureValueFlags other, bool notOther)
119+
{
120+
var otherBitArray = (BitArraySymbolicFeatureValueFlags)other;
121+
if (!not && !notOther)
122+
{
123+
_flags.Or(otherBitArray._flags);
124+
}
125+
else if (!not)
126+
{
127+
_flags.Or(Copy(otherBitArray._flags).Not());
128+
}
129+
else if (!notOther)
130+
{
131+
_flags.Not().Or(otherBitArray._flags);
132+
}
133+
else
134+
{
135+
_flags.Not().Or(Copy(otherBitArray._flags).Not());
136+
}
137+
}
138+
139+
public void ExceptWith(bool not, ISymbolicFeatureValueFlags other, bool notOther)
140+
{
141+
var otherBitArray = (BitArraySymbolicFeatureValueFlags)other;
142+
if (!not && !notOther)
143+
{
144+
_flags.And(Copy(otherBitArray._flags).Not());
145+
}
146+
else if (!not)
147+
{
148+
_flags.And(otherBitArray._flags);
149+
}
150+
else if (!notOther)
151+
{
152+
_flags.Not().And(Copy(otherBitArray._flags).Not());
153+
}
154+
else
155+
{
156+
_flags.Not().And(otherBitArray._flags);
157+
}
158+
}
159+
160+
public ISymbolicFeatureValueFlags Not()
161+
{
162+
return new BitArraySymbolicFeatureValueFlags(_feature, Copy(_flags).Not());
163+
}
164+
165+
public bool ValueEquals(ISymbolicFeatureValueFlags other)
166+
{
167+
var otherBitArray = (BitArraySymbolicFeatureValueFlags)other;
168+
return AreEqual(_flags, otherBitArray._flags);
169+
}
170+
171+
public int GetValuesHashCode()
172+
{
173+
int hash = 0;
174+
foreach (bool value in _flags)
175+
{
176+
hash ^= (value ? 2 : 1);
177+
}
178+
return hash;
179+
}
180+
181+
public ISymbolicFeatureValueFlags Clone()
182+
{
183+
return new BitArraySymbolicFeatureValueFlags(_feature, Copy(_flags));
184+
}
185+
186+
private static bool AreEqual(BitArray array1, BitArray array2)
187+
{
188+
return array1.Cast<bool>().SequenceEqual(array2.Cast<bool>());
189+
}
190+
191+
private static bool HasAnySet(BitArray flags)
192+
{
193+
foreach (bool flag in flags)
194+
{
195+
if (flag)
196+
return true;
197+
}
198+
return false;
199+
}
200+
201+
private static bool HasAllSet(BitArray flags)
202+
{
203+
foreach (bool flag in flags)
204+
{
205+
if (!flag)
206+
return false;
207+
}
208+
return true;
209+
}
210+
211+
private static BitArray Copy(BitArray flags)
212+
{
213+
return new BitArray(flags);
214+
}
215+
}
216+
}

src/SIL.Machine/FeatureModel/ISymbolicFeatureValue.cs

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Collections.Generic;
2+
using SIL.ObjectModel;
3+
4+
namespace SIL.Machine.FeatureModel
5+
{
6+
internal interface ISymbolicFeatureValueFlags : ICloneable<ISymbolicFeatureValueFlags>
7+
{
8+
bool HasAnySet();
9+
bool HasAllSet();
10+
11+
bool Get(FeatureSymbol symbol);
12+
FeatureSymbol GetFirst();
13+
void Set(IEnumerable<FeatureSymbol> symbols);
14+
15+
bool IsSupersetOf(bool not, ISymbolicFeatureValueFlags other, bool notOther);
16+
bool Overlaps(bool not, ISymbolicFeatureValueFlags other, bool notOther);
17+
18+
void IntersectWith(bool not, ISymbolicFeatureValueFlags other, bool notOther);
19+
void UnionWith(bool not, ISymbolicFeatureValueFlags other, bool notOther);
20+
void ExceptWith(bool not, ISymbolicFeatureValueFlags other, bool notOther);
21+
22+
ISymbolicFeatureValueFlags Not();
23+
24+
bool ValueEquals(ISymbolicFeatureValueFlags other);
25+
int GetValuesHashCode();
26+
}
27+
}
Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Collections;
21
using System.Collections.Generic;
32
using SIL.ObjectModel;
43

@@ -7,8 +6,6 @@ namespace SIL.Machine.FeatureModel
76
public class SymbolicFeature : Feature
87
{
98
private readonly PossibleSymbolCollection _possibleSymbols;
10-
private readonly ulong _mask;
11-
private readonly BitArray _maskBA = new BitArray(sizeof(ulong) * 8, false);
129

1310
public SymbolicFeature(string id, params FeatureSymbol[] possibleSymbols)
1411
: this(id, (IEnumerable<FeatureSymbol>)possibleSymbols) { }
@@ -23,16 +20,6 @@ public SymbolicFeature(string id, IEnumerable<FeatureSymbol> possibleSymbols)
2320
symbol.Feature = this;
2421
symbol.Index = i++;
2522
}
26-
27-
int symbolCount = _possibleSymbols.Count;
28-
if (symbolCount > SymbolicFeatureValue.NeedToUseBitArray)
29-
{
30-
_maskBA = new BitArray(symbolCount, true);
31-
}
32-
else
33-
{
34-
_mask = (1UL << symbolCount) - 1UL;
35-
}
3623
}
3724

3825
/// <summary>
@@ -48,14 +35,5 @@ public string DefaultSymbolID
4835
{
4936
set { DefaultValue = new SymbolicFeatureValue(_possibleSymbols[value]); }
5037
}
51-
52-
internal ulong MaskUlong
53-
{
54-
get { return _mask; }
55-
}
56-
internal BitArray MaskBitArray
57-
{
58-
get { return _maskBA; }
59-
}
6038
}
6139
}

0 commit comments

Comments
 (0)