public static void BitArray_AndTest() { BitArray_Helper(Operator.And); // [] Test to make sure that 0 sized bit arrays can be And-ed BitArray b1 = new BitArray(0); BitArray b2 = new BitArray(0); b1.And(b2); }
static public int And(IntPtr l) { try { System.Collections.BitArray self = (System.Collections.BitArray)checkSelf(l); System.Collections.BitArray a1; checkType(l, 2, out a1); var ret = self.And(a1); pushValue(l, true); pushValue(l, ret); return(2); } catch (Exception e) { return(error(l, e)); } }
static void BitArrayDemo() { var bits1 = new BitArray(8); bits1.SetAll(true); bits1.Set(1, false); bits1[5] = false; bits1[7] = false; Console.Write("initialized: "); DisplayBits(bits1); Console.WriteLine(); DisplayBits(bits1); bits1.Not(); Console.Write(" not "); DisplayBits(bits1); Console.WriteLine(); var bits2 = new BitArray(bits1); bits2[0] = true; bits2[1] = false; bits2[4] = true; DisplayBits(bits1); Console.Write(" or "); DisplayBits(bits2); Console.Write(" : "); bits1.Or(bits2); DisplayBits(bits1); Console.WriteLine(); DisplayBits(bits2); Console.Write(" and "); DisplayBits(bits1); Console.Write(" : "); bits2.And(bits1); DisplayBits(bits2); Console.WriteLine(); DisplayBits(bits1); Console.Write(" xor "); DisplayBits(bits2); bits1.Xor(bits2); Console.Write(" : "); DisplayBits(bits1); Console.WriteLine(); }
public static void Subtract(BitArray a, BitArray b) { // a = a - b BitArray c = (BitArray) b.Clone(); a.And(c.Not()); }
// WhileStmt public override bool Walk(WhileStatement node) { // Walk the expression node.Test.Walk(this); BitArray opte = node.ElseStatement != null ? new BitArray(_bits) : null; BitArray exit = new BitArray(_bits.Length, true); PushLoop(exit); node.Body.Walk(this); PopLoop(); _bits.And(exit); if (node.ElseStatement != null) { // Flow the else BitArray save = _bits; _bits = opte; node.ElseStatement.Walk(this); // Restore the bits _bits = save; // Intersect _bits.And(opte); } return false; }
/// <summary> /// Given discriminator values (ordinally aligned with DiscriminatorColumns), determines /// the entity type to return. Throws a CommandExecutionException if the type is ambiguous. /// </summary> internal EntityType Discriminate(object[] discriminatorValues, int resultSetIndex) { FunctionImportStructuralTypeMappingKB resultMapping = this.GetResultMapping(resultSetIndex); Debug.Assert (resultMapping != null); // initialize matching types bit map BitArray typeCandidates = new BitArray(resultMapping.MappedEntityTypes.Count, true); foreach (var typeMapping in resultMapping.NormalizedEntityTypeMappings) { // check if this type mapping is matched bool matches = true; var columnConditions = typeMapping.ColumnConditions; for (int i = 0; i < columnConditions.Count; i++) { if (null != columnConditions[i] && // this discriminator doesn't matter for the given condition !columnConditions[i].ColumnValueMatchesCondition(discriminatorValues[i])) { matches = false; break; } } if (matches) { // if the type condition is met, narrow the set of type candidates typeCandidates = typeCandidates.And(typeMapping.ImpliedEntityTypes); } else { // if the type condition fails, all implied types are eliminated // (the type mapping fragment is a co-implication, so a type is no longer // a candidate if any condition referring to it is false) typeCandidates = typeCandidates.And(typeMapping.ComplementImpliedEntityTypes); } } // find matching type condition EntityType entityType = null; for (int i = 0; i < typeCandidates.Length; i++) { if (typeCandidates[i]) { if (null != entityType) { throw EntityUtil.CommandExecution(System.Data.Entity.Strings.ADP_InvalidDataReaderUnableToDetermineType); } entityType = resultMapping.MappedEntityTypes[i]; } } // if there is no match, raise an exception if (null == entityType) { throw EntityUtil.CommandExecution(System.Data.Entity.Strings.ADP_InvalidDataReaderUnableToDetermineType); } return entityType; }
void RunDataColumn(Report rpt, WorkClass wc, MatrixEntry rm, MatrixEntry cm, MatrixCellEntry[,] matrix, Rows _Data, int iRow, ref int iColumn, int level, int rowcell) { BitArray andData; MatrixRow mr = this.MatrixRows.Items[rowcell] as MatrixRow; float height = mr.Height == null? 0: mr.Height.Points; foreach (MatrixEntry ame in cm.GetSortedData(rpt)) { if (ame.ColumnGroup != LastCg) { RunDataColumn(rpt, wc, rm, ame, matrix, _Data, iRow, ref iColumn, level+1, rowcell); continue; } andData = new BitArray(ame.Rows); // copy the data andData.And(rm.Rows); // because And is destructive matrix[iRow, iColumn] = RunGetMatrixCell(rpt, ame, iRow, _Data, andData, Math.Max(rm.FirstRow, ame.FirstRow), Math.Min(rm.LastRow, ame.LastRow)); matrix[iRow, iColumn].Height = height; matrix[iRow, iColumn].Width = RunGetColumnWidth(matrix[iRow, iColumn]); matrix[iRow, iColumn].ColumnME = ame; matrix[iRow, iColumn].RowME = rm; iColumn++; } // do we need to subtotal this? ColumnGrouping cg = (ColumnGrouping) (_ColumnGroupings.Items[level]); if (cg.DynamicColumns != null && cg.DynamicColumns.Subtotal != null) { andData = new BitArray(cm.Rows); // copy the data andData.And(rm.Rows); // because And is destructive for (int i=0; i < this.CountMatrixCells; i++) { matrix[iRow, iColumn] = RunGetMatrixCell(rpt, cm, rowcell, i, _Data, andData, Math.Max(rm.FirstRow, cm.FirstRow), Math.Min(rm.LastRow, cm.LastRow)); matrix[iRow, iColumn].Height = height; matrix[iRow, iColumn].Width = RunGetColumnWidth(matrix[iRow, iColumn]); matrix[iRow, iColumn].ColumnME = cm; matrix[iRow, iColumn].RowME = rm; iColumn++; } } }
// IfStmt public override bool Walk(IfStatement node) { BitArray result = new BitArray(_bits.Length, true); BitArray save = _bits; _bits = new BitArray(_bits.Length); foreach (IfStatementTest ist in node.Tests) { // Set the initial branch value to bits _bits.SetAll(false); _bits.Or(save); // Flow the test first ist.Test.Walk(this); // Flow the body ist.Body.Walk(this); // Intersect result.And(_bits); } // Set the initial branch value to bits _bits.SetAll(false); _bits.Or(save); if (node.ElseStatement != null) { // Flow the else_ node.ElseStatement.Walk(this); } // Intersect result.And(_bits); _bits = save; // Remember the result _bits.SetAll(false); _bits.Or(result); return false; }
private bool UpdateEntrySet(BitArray entrySet, BitArray exitSet, BitArray useBeforeDef, BitArray notDef) { int card = entrySet.Cardinality(); entrySet.Or(exitSet); entrySet.And(notDef); entrySet.Or(useBeforeDef); return entrySet.Cardinality() != card; }
public static BitArray AndNot(BitArray b1, BitArray b2) { if (b1.Length > b2.Length) { b2.Length = b1.Length; } else if (b2.Length > b1.Length) { b1.Length = b2.Length; } b1.And(b2.Not()); return b1; }
private bool PrefixesEquals(CidrRange other) { var leftMask = new BitArray(Prefix); var leftSuffix = new BitArray(32, false); for (int i = 31; i >= 32 - Suffix; i--) { leftSuffix[i] = true; } leftMask = leftSuffix.And(leftMask); var rightMask = new BitArray(other.Prefix); var rightSuffix = new BitArray(32, false); for (int i = 31; i >= 32 - other.Suffix; i--) { rightSuffix[i] = true; } rightMask = rightSuffix.And(rightMask); var result = rightMask.Xor(leftMask); return result.Cast<bool>().ToArray().Skip(32 - Math.Min(Suffix, other.Suffix)).All(x => !x); }
public static void BitArray_AndTest_Negative() { // [] ArgumentException, length of arrays is different BitArray ba2 = new BitArray(11, false); BitArray ba3 = new BitArray(6, false); Assert.Throws<ArgumentException>(delegate { ba2.And(ba3); }); //"Err_32! wrong exception thrown." Assert.Throws<ArgumentException>(delegate { ba3.And(ba2); }); //"Err_33! wrong exception thrown." // [] ArgumentNullException, null. ba2 = new BitArray(6, false); ba3 = null; Assert.Throws<ArgumentNullException>(delegate { ba2.And(ba3); }); //"Err_34! wrong exception thrown." }
private Tuple<Type, Dictionary<string, int>> FinishAnalysis(bool scriptCmdlet = false) { List<Block> list = Block.GenerateReverseDepthFirstOrder(this._entryBlock); BitArray assignedBitArray = new BitArray(this._variables.Count); list[0]._visitData = assignedBitArray; this.AnalyzeBlock(assignedBitArray, list[0]); for (int i = 1; i < list.Count; i++) { Block block = list[i]; assignedBitArray = new BitArray(this._variables.Count); assignedBitArray.SetAll(true); block._visitData = assignedBitArray; int num2 = 0; foreach (Block block2 in block._predecessors) { if (block2._visitData != null) { num2++; assignedBitArray.And((BitArray) block2._visitData); } } this.AnalyzeBlock(assignedBitArray, block); } var v = this._variables.Values.Where(x => x.LocalTupleIndex == -2).SelectMany(x => x.AssociatedAsts); foreach (Ast ast in v) { FixTupleIndex(ast, -2); } VariableAnalysisDetails[] detailsArray = (from details in this._variables.Values where details.LocalTupleIndex >= 0 orderby details.LocalTupleIndex select details).ToArray<VariableAnalysisDetails>(); Dictionary<string, int> dictionary = new Dictionary<string, int>(0, StringComparer.OrdinalIgnoreCase); for (int j = 0; j < detailsArray.Length; j++) { VariableAnalysisDetails details = detailsArray[j]; string name = details.Name; dictionary.Add(name, j); if (details.LocalTupleIndex != j) { foreach (Ast ast2 in details.AssociatedAsts) { FixTupleIndex(ast2, j); } } } return Tuple.Create<Type, Dictionary<string, int>>(MutableTuple.MakeTupleType((from l in detailsArray select l.Type).ToArray<Type>()), dictionary); }
internal virtual BitArray AddClause(BooleanQuery bq, BitArray result) { BitArray rnd = Sets[Random().Next(Sets.Length)]; Query q = new ConstantScoreQuery(new FilterAnonymousInnerClassHelper(this, rnd)); bq.Add(q, BooleanClause.Occur.MUST); if (Validate) { if (result == null) { result = (BitArray)rnd.Clone(); } else { result = result.And(rnd); } } return result; }
// SwitchStatement protected internal override bool Walk(SwitchStatement node) { // The expression is evaluated always. // Then each case clause expression is evaluated until match is found. // Therefore, the effects of the case clause expressions accumulate. // Default clause is evaluated last (so all case clause expressions must // accumulate first) WalkNode(node.TestValue); // Flow all the cases, they all start with the same initial state int count; IList<SwitchCase> cases = node.Cases; if (cases != null && (count = cases.Count) > 0) { SwitchCase @default = null; // Save the initial state BitArray entry = _bits; // The state to progressively accumualte effects of the case clause expressions BitArray values = new BitArray(entry); // State to flow the case clause bodies. BitArray caseFlow = new BitArray(_bits.Length); // The state to accumulate results into BitArray result = new BitArray(_bits.Length, true); PushStatement(result); for (int i = 0; i < count; i++) { if (cases[i].IsDefault) { Debug.Assert(@default == null); // postpone the default case @default = cases[i]; continue; } // Set the state for the walking of the body caseFlow.SetAll(false); caseFlow.Or(values); // Walk the body _bits = caseFlow; WalkNode(cases[i].Body); // Accumulate the result into the overall case statement result. result.And(caseFlow); } // Walk the default at the end. if (@default != null) { // Initialize caseFlow.SetAll(false); caseFlow.Or(values); // Walk the default body _bits = caseFlow; WalkNode(@default.Body); // Accumulate. result.And(caseFlow); // If there's a default clause, exactly one case got executed. // The final state is 'and' across all cases, stored in 'result' entry.SetAll(false); entry.Or(result); } else { // In the absence of default clause, we may have executed case, // but didn't have to, so the result is an 'and' between the cases // and the initial state. entry.And(result); } PopStatement(); // Restore the original state. _bits = entry; } return false; }
// DoStatement protected internal override bool Walk(DoStatement node) { BitArray loop = new BitArray(_bits); // State at the loop entry with which the loop runs BitArray save = _bits; // Save the state at loop entry // Prepare loop exit state BitArray exit = new BitArray(_bits.Length, true); PushStatement(exit); // Loop will be flown starting from the current state _bits = loop; // Walk the loop WalkNode(node.Body); // Walk the test in the context of the loop WalkNode(node.Test); // Handle the loop exit PopStatement(); _bits.And(exit); // Restore the state after walking the loop _bits = save; _bits.And(loop); return false; }
private Tuple<Type, Dictionary<string, int>> FinishAnalysis(bool scriptCmdlet = false) { var blocks = Block.GenerateReverseDepthFirstOrder(_entryBlock); // The first block has no predecessors, so analyze outside the loop to "prime" the bitarray. var bitArray = new BitArray(_variables.Count); blocks[0]._visitData = bitArray; AnalyzeBlock(bitArray, blocks[0]); for (int index = 1; index < blocks.Count; index++) { var block = blocks[index]; bitArray = new BitArray(_variables.Count); bitArray.SetAll(true); block._visitData = bitArray; int predCount = 0; foreach (var pred in block._predecessors) { // VisitData can be null when the pred occurs because of a continue statement. if (pred._visitData != null) { predCount += 1; bitArray.And((BitArray)pred._visitData); } } Diagnostics.Assert(predCount != 0, "If we didn't and anything, there is a flaw in the logic and incorrect code may be generated."); AnalyzeBlock(bitArray, block); } Diagnostics.Assert(_exitBlock._predecessors.All(p => p._unreachable || p._visitData is BitArray), "VisitData wasn't set on a reachable block"); foreach (var details in _variables.Values) { if (details.LocalTupleIndex == ForceDynamic) { foreach (var ast in details.AssociatedAsts) { FixTupleIndex(ast, ForceDynamic); FixAssigned(ast, details); } } } var orderedLocals = (from details in _variables.Values where (details.LocalTupleIndex >= 0 || (details.LocalTupleIndex == ForceDynamic && details.Automatic)) orderby details.LocalTupleIndex select details).ToArray(); Diagnostics.Assert(!_disableOptimizations || orderedLocals.Length == (int)AutomaticVariable.NumberOfAutomaticVariables + (scriptCmdlet ? SpecialVariables.PreferenceVariables.Length : 0), "analysis is incorrectly allocating number of locals when optimizations are disabled."); var nameToIndexMap = new Dictionary<string, int>(0, StringComparer.OrdinalIgnoreCase); for (int i = 0; i < orderedLocals.Length; ++i) { var details = orderedLocals[i]; var name = details.Name; nameToIndexMap.Add(name, i); if (details.LocalTupleIndex != i) { foreach (var ast in details.AssociatedAsts) { FixTupleIndex(ast, i); } } // Automatic variables assign the type directly, not relying on any analysis. For // all other variables, we don't determine the type of the local until we're done // with the analysis. Diagnostics.Assert(details.Type != null, "Type should be resolved already"); } var tupleType = MutableTuple.MakeTupleType((from l in orderedLocals select l.Type).ToArray()); return Tuple.Create(tupleType, nameToIndexMap); }
private ContingencyTableNode MakeContabLeafList(Varset variables, BitArray records) { Varset variablesCp = new Varset(variables); if (variablesCp.Equals(zero)) { int count = 0; for (int i = 0; i < records.Count; i++) { if (records[i]) { count += 1; } } return new ContingencyTableNode(count, 0, 1); } int firstIndex = variables.FindFirst(); int cardinality = network.GetCardinality(firstIndex); ContingencyTableNode ct = new ContingencyTableNode(0, cardinality, 0); variablesCp.Set(firstIndex, false); Varset remainingVariables = new Varset(variablesCp); for (int k = 0; k < cardinality; k++) { BitArray r = new BitArray(recordCount); r = r.Or(records); r = r.And(consistentRecords[firstIndex][k]); int count = 0; for (int i = 0; i < r.Count; i++) { if (r[i]) { count += 1; } } if (count > 0) { ContingencyTableNode child = MakeContabLeafList(remainingVariables, r); ct.SetChild(k, child); ct.LeafCount += child.LeafCount; } } return ct; }
public void AndBits(HasseFingerprint FP) { fp.And(FP.fp); this.bitCount = this.CountBits(); }
public static void BitArray_Helper(Operator op) { BitArray ba2 = null; BitArray ba3 = null; BitArray ba4 = null; // [] Standard cases (1,1) (1,0) (0,0). ba2 = new BitArray(6, false); ba3 = new BitArray(6, false); ba2.Set(0, true); ba2.Set(1, true); ba3.Set(1, true); ba3.Set(2, true); switch (op) { case Operator.Xor: ba4 = ba2.Xor(ba3); Assert.True(ba4.Get(0)); //"Err_8! Expected ba4.Get(0) to be true" Assert.False(ba4.Get(1)); //"Err_9! Expected ba4.Get(1) to be false" Assert.True(ba4.Get(2)); //"Err_10! Expected ba4.Get(2) to be true" Assert.False(ba4.Get(4)); //"Err_11! Expected ba4.Get(4) to be false" break; case Operator.And: ba4 = ba2.And(ba3); Assert.False(ba4.Get(0)); //"Err_12! Expected ba4.Get(0) to be false" Assert.True(ba4.Get(1)); //"Err_13! Expected ba4.Get(1) to be true" Assert.False(ba4.Get(2)); //"Err_14! Expected ba4.Get(2) to be false" Assert.False(ba4.Get(4)); //"Err_15! Expected ba4.Get(4) to be false" break; case Operator.Or: ba4 = ba2.Or(ba3); Assert.True(ba4.Get(0)); //"Err_16! Expected ba4.Get(0) to be true" Assert.True(ba4.Get(1)); //"Err_17! Expected ba4.Get(1) to be true" Assert.True(ba4.Get(2)); //"Err_18! Expected ba4.Get(2) to be true" Assert.False(ba4.Get(4)); //"Err_19! Expected ba4.Get(4) to be false" break; } // [] Size stress cases. ba2 = new BitArray(0x1000F, false); ba3 = new BitArray(0x1000F, false); ba2.Set(0x10000, true); // The bit for 1 (2^0). ba2.Set(0x10001, true); // The bit for 2 (2^1). ba3.Set(0x10001, true); // The bit for 2 (2^1). switch (op) { case Operator.Xor: ba4 = ba2.Xor(ba3); Assert.True(ba4.Get(0x10000)); //"Err_20! Expected ba4.Get(0x10000) to be true" Assert.False(ba4.Get(0x10001)); //"Err_21! Expected ba4.Get(0x10001) to be false" Assert.False(ba4.Get(0x10002)); //"Err_22! Expected ba4.Get(0x10002) to be false" Assert.False(ba4.Get(0x10004)); //"Err_23! Expected ba4.Get(0x10004) to be false" break; case Operator.And: ba4 = ba2.And(ba3); Assert.False(ba4.Get(0x10000)); //"Err_24! Expected ba4.Get(0x10000) to be false" Assert.True(ba4.Get(0x10001)); //"Err_25! Expected ba4.Get(0x10001) to be true" Assert.False(ba4.Get(0x10002)); //"Err_26! Expected ba4.Get(0x10002) to be false" Assert.False(ba4.Get(0x10004)); //"Err_27! Expected ba4.Get(0x10004) to be false" break; case Operator.Or: ba4 = ba2.Or(ba3); Assert.True(ba4.Get(0x10000)); //"Err_28! Expected ba4.Get(0x10000) to be true" Assert.True(ba4.Get(0x10001)); //"Err_29! Expected ba4.Get(0x10001) to be true" Assert.False(ba4.Get(0x10002)); //"Err_30! Expected ba4.Get(0x10002) to be false" Assert.False(ba4.Get(0x10004)); //"Err_31! Expected ba4.Get(0x10004) to be false" break; } }
public Genome MakeChild(Genome other, BitArray mask) { BitArray leftCopy = new BitArray(this.genome); BitArray rightCopy = new BitArray(other.genome); BitArray maskCopy = new BitArray(mask); leftCopy.And(maskCopy); rightCopy.And(maskCopy.Not()); //BitArray temp2 = new BitArray(right.And(mask.Not())); return new Genome(leftCopy.Or(rightCopy)); }