本文整理汇总了C#中System.Collections.BitArray.SafeGet方法的典型用法代码示例。如果您正苦于以下问题:C# BitArray.SafeGet方法的具体用法?C# BitArray.SafeGet怎么用?C# BitArray.SafeGet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.BitArray
的用法示例。
在下文中一共展示了BitArray.SafeGet方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DoGet
internal virtual void DoGet(BitArray a, LongBitSet b)
{
long max = b.Length();
for (int i = 0; i < max; i++)
{
if (a.SafeGet(i) != b.Get(i))
{
Assert.Fail("mismatch: BitSet=[" + i + "]=" + a.SafeGet(i));
}
}
}
示例2: DoPrevSetBit
internal virtual void DoPrevSetBit(BitArray a, LongBitSet b)
{
int aa = a.Count + Random().Next(100);
long bb = aa;
do
{
//aa = a.PrevSetBit(aa-1);
aa--;
while ((aa >= 0) && (!a.SafeGet(aa)))
{
aa--;
}
if (b.Length() == 0)
{
bb = -1;
}
else if (bb > b.Length() - 1)
{
bb = b.PrevSetBit(b.Length() - 1);
}
else if (bb < 1)
{
bb = -1;
}
else
{
bb = bb >= 1 ? b.PrevSetBit(bb - 1) : -1;
}
Assert.AreEqual(aa, bb);
} while (aa >= 0);
}
示例3: AssertSubsetOf
/// <summary>
/// Asserts that the documents returned by <code>q1</code>
/// are a subset of those returned by <code>q2</code>.
///
/// Both queries will be filtered by <code>filter</code>
/// </summary>
protected internal virtual void AssertSubsetOf(Query q1, Query q2, Filter filter)
{
// TRUNK ONLY: test both filter code paths
if (filter != null && Random().NextBoolean())
{
q1 = new FilteredQuery(q1, filter, TestUtil.RandomFilterStrategy(Random()));
q2 = new FilteredQuery(q2, filter, TestUtil.RandomFilterStrategy(Random()));
filter = null;
}
// not efficient, but simple!
TopDocs td1 = S1.Search(q1, filter, Reader.MaxDoc);
TopDocs td2 = S2.Search(q2, filter, Reader.MaxDoc);
Assert.IsTrue(td1.TotalHits <= td2.TotalHits);
// fill the superset into a bitset
var bitset = new BitArray(td2.ScoreDocs.Length);
for (int i = 0; i < td2.ScoreDocs.Length; i++)
{
bitset.SafeSet(td2.ScoreDocs[i].Doc, true);
}
// check in the subset, that every bit was set by the super
for (int i = 0; i < td1.ScoreDocs.Length; i++)
{
Assert.IsTrue(bitset.SafeGet(td1.ScoreDocs[i].Doc));
}
}
示例4: DoRandomSets
internal virtual void DoRandomSets(int maxSize, int iter, int mode)
{
BitArray a0 = null;
LongBitSet b0 = null;
for (int i = 0; i < iter; i++)
{
int sz = TestUtil.NextInt(Random(), 2, maxSize);
BitArray a = new BitArray(sz);
LongBitSet b = new LongBitSet(sz);
// test the various ways of setting bits
if (sz > 0)
{
int nOper = Random().Next(sz);
for (int j = 0; j < nOper; j++)
{
int idx;
idx = Random().Next(sz);
a.SafeSet(idx, true);
b.Set(idx);
idx = Random().Next(sz);
a.SafeSet(idx, false);
b.Clear(idx);
idx = Random().Next(sz);
a.SafeSet(idx, !a.SafeGet(idx));
b.Flip(idx, idx + 1);
idx = Random().Next(sz);
a.SafeSet(idx, !a.SafeGet(idx));
b.Flip(idx, idx + 1);
bool val2 = b.Get(idx);
bool val = b.GetAndSet(idx);
Assert.IsTrue(val2 == val);
Assert.IsTrue(b.Get(idx));
if (!val)
{
b.Clear(idx);
}
Assert.IsTrue(b.Get(idx) == val);
}
}
// test that the various ways of accessing the bits are equivalent
DoGet(a, b);
// test ranges, including possible extension
int fromIndex, toIndex;
fromIndex = Random().Next(sz / 2);
toIndex = fromIndex + Random().Next(sz - fromIndex);
BitArray aa = (BitArray)a.Clone();
aa.Flip(fromIndex, toIndex);
LongBitSet bb = b.Clone();
bb.Flip(fromIndex, toIndex);
fromIndex = Random().Next(sz / 2);
toIndex = fromIndex + Random().Next(sz - fromIndex);
aa = (BitArray)a.Clone();
aa.Clear(fromIndex, toIndex);
bb = b.Clone();
bb.Clear(fromIndex, toIndex);
DoNextSetBit(aa, bb); // a problem here is from clear() or nextSetBit
DoPrevSetBit(aa, bb);
fromIndex = Random().Next(sz / 2);
toIndex = fromIndex + Random().Next(sz - fromIndex);
aa = (BitArray)a.Clone();
aa.Set(fromIndex, toIndex);
bb = b.Clone();
bb.Set(fromIndex, toIndex);
DoNextSetBit(aa, bb); // a problem here is from set() or nextSetBit
DoPrevSetBit(aa, bb);
if (b0 != null && b0.Length() <= b.Length())
{
Assert.AreEqual(a.Cardinality(), b.Cardinality());
BitArray a_and = (BitArray)a.Clone();
a_and = a_and.And_UnequalLengths(a0);
BitArray a_or = (BitArray)a.Clone();
a_or = a_or.Or_UnequalLengths(a0);
BitArray a_xor = (BitArray)a.Clone();
a_xor = a_xor.Xor_UnequalLengths(a0);
BitArray a_andn = (BitArray)a.Clone();
a_andn.AndNot(a0);
LongBitSet b_and = b.Clone();
Assert.AreEqual(b, b_and);
b_and.And(b0);
LongBitSet b_or = b.Clone();
b_or.Or(b0);
//.........这里部分代码省略.........
示例5: TestCompact
public virtual void TestCompact()
{
BytesRef @ref = new BytesRef();
int num = AtLeast(2);
for (int j = 0; j < num; j++)
{
int numEntries = 0;
const int size = 797;
BitArray bits = new BitArray(size);
for (int i = 0; i < size; i++)
{
string str;
do
{
str = TestUtil.RandomRealisticUnicodeString(Random(), 1000);
} while (str.Length == 0);
@ref.CopyChars(str);
int key = Hash.Add(@ref);
if (key < 0)
{
Assert.IsTrue(bits.SafeGet((-key) - 1));
}
else
{
Assert.IsFalse(bits.SafeGet(key));
bits.SafeSet(key, true);
numEntries++;
}
}
Assert.AreEqual(Hash.Size(), bits.Cardinality());
Assert.AreEqual(numEntries, bits.Cardinality());
Assert.AreEqual(numEntries, Hash.Size());
int[] compact = Hash.Compact();
Assert.IsTrue(numEntries < compact.Length);
for (int i = 0; i < numEntries; i++)
{
bits.SafeSet(compact[i], false);
}
Assert.AreEqual(0, bits.Cardinality());
Hash.Clear();
Assert.AreEqual(0, Hash.Size());
Hash.Reinit();
}
}
示例6: IsFinite
/// <summary>
/// Checks whether there is a loop containing s. (this is sufficient since
/// there are never transitions to dead states.)
/// </summary>
// TODO: not great that this is recursive... in theory a
// large automata could exceed java's stack
private static bool IsFinite(State s, BitArray path, BitArray visited)
{
path.SafeSet(s.number, true);
foreach (Transition t in s.Transitions)
{
if (path.SafeGet(t.To.number) || (!visited.SafeGet(t.To.number) && !IsFinite(t.To, path, visited)))
{
return false;
}
}
path.SafeSet(s.number, false);
visited.SafeSet(s.number, true);
return true;
}
示例7: DoTermConjunctions
public virtual int DoTermConjunctions(IndexSearcher s, int termsInIndex, int maxClauses, int iter)
{
int ret = 0;
long nMatches = 0;
for (int i = 0; i < iter; i++)
{
int nClauses = Random().Next(maxClauses - 1) + 2; // min 2 clauses
BooleanQuery bq = new BooleanQuery();
BitArray termflag = new BitArray(termsInIndex);
for (int j = 0; j < nClauses; j++)
{
int tnum;
// don't pick same clause twice
tnum = Random().Next(termsInIndex);
if (termflag.SafeGet(tnum))
{
tnum = termflag.NextClearBit(tnum);
}
if (tnum < 0 || tnum >= termsInIndex)
{
tnum = termflag.NextClearBit(0);
}
termflag.SafeSet(tnum, true);
Query tq = new TermQuery(Terms[tnum]);
bq.Add(tq, BooleanClause.Occur.MUST);
}
CountingHitCollector hc = new CountingHitCollector();
s.Search(bq, hc);
nMatches += hc.Count;
ret += hc.Sum;
}
if (VERBOSE)
{
Console.WriteLine("Average number of matches=" + (nMatches / iter));
}
return ret;
}
示例8: MinimizeHopcroft
/// <summary>
/// Minimizes the given automaton using Hopcroft's algorithm.
/// </summary>
public static void MinimizeHopcroft(Automaton a)
{
a.Determinize();
if (a.Initial.numTransitions == 1)
{
Transition t = a.Initial.TransitionsArray[0];
if (t.To == a.Initial && t.Min_Renamed == Character.MIN_CODE_POINT && t.Max_Renamed == Character.MAX_CODE_POINT)
{
return;
}
}
a.Totalize();
// initialize data structures
int[] sigma = a.StartPoints;
State[] states = a.NumberedStates;
int sigmaLen = sigma.Length, statesLen = states.Length;
List<State>[,] reverse = new List<State>[statesLen, sigmaLen];
HashSet<State>[] partition = new HashSet<State>[statesLen];
List<State>[] splitblock = new List<State>[statesLen];
int[] block = new int[statesLen];
StateList[,] active = new StateList[statesLen, sigmaLen];
StateListNode[,] active2 = new StateListNode[statesLen, sigmaLen];
LinkedList<IntPair> pending = new LinkedList<IntPair>();
BitArray pending2 = new BitArray(sigmaLen * statesLen);
BitArray split = new BitArray(statesLen), refine = new BitArray(statesLen), refine2 = new BitArray(statesLen);
for (int q = 0; q < statesLen; q++)
{
splitblock[q] = new List<State>();
partition[q] = new HashSet<State>();
for (int x = 0; x < sigmaLen; x++)
{
active[q, x] = new StateList();
}
}
// find initial partition and reverse edges
for (int q = 0; q < statesLen; q++)
{
State qq = states[q];
int j = qq.accept ? 0 : 1;
partition[j].Add(qq);
block[q] = j;
for (int x = 0; x < sigmaLen; x++)
{
//List<State>[] r = reverse[qq.Step(sigma[x]).number];
var r = qq.Step(sigma[x]).number;
if (reverse[r, x] == null)
{
reverse[r, x] = new List<State>();
}
reverse[r, x].Add(qq);
}
}
// initialize active sets
for (int j = 0; j <= 1; j++)
{
for (int x = 0; x < sigmaLen; x++)
{
foreach (State qq in partition[j])
{
if (reverse[qq.number, x] != null)
{
active2[qq.number, x] = active[j, x].Add(qq);
}
}
}
}
// initialize pending
for (int x = 0; x < sigmaLen; x++)
{
int j = (active[0, x].Size <= active[1, x].Size) ? 0 : 1;
pending.AddLast(new IntPair(j, x));
pending2.SafeSet(x * statesLen + j, true);
}
// process pending until fixed point
int k = 2;
while (pending.Count > 0)
{
IntPair ip = pending.First.Value;
pending.RemoveFirst();
int p = ip.N1;
int x = ip.N2;
pending2.SafeSet(x * statesLen + p, false);
// find states that need to be split off their blocks
for (StateListNode m = active[p, x].First; m != null; m = m.Next)
{
List<State> r = reverse[m.q.number, x];
if (r != null)
{
foreach (State s in r)
{
int i = s.number;
if (!split.SafeGet(i))
{
split.SafeSet(i, true);
int j = block[i];
splitblock[j].Add(s);
//.........这里部分代码省略.........
示例9: RemoveDeadTransitions
/// <summary>
/// Removes transitions to dead states and calls <seealso cref="#reduce()"/>.
/// (A state is "dead" if no accept state is
/// reachable from it.)
/// </summary>
public virtual void RemoveDeadTransitions()
{
State[] states = NumberedStates;
//clearHashCode();
if (IsSingleton)
{
return;
}
State[] live = LiveStates;
BitArray liveSet = new BitArray(states.Length);
foreach (State s in live)
{
liveSet.SafeSet(s.number, true);
}
foreach (State s in states)
{
// filter out transitions to dead states:
int upto = 0;
for (int i = 0; i < s.numTransitions; i++)
{
Transition t = s.TransitionsArray[i];
if (liveSet.SafeGet(t.To.Number))
{
s.TransitionsArray[upto++] = s.TransitionsArray[i];
}
}
s.numTransitions = upto;
}
for (int i = 0; i < live.Length; i++)
{
live[i].number = i;
}
if (live.Length > 0)
{
NumberedStates = live;
}
else
{
// sneaky corner case -- if machine accepts no strings
ClearNumberedStates();
}
Reduce();
}