/// <summary> /// Two rows are equal if they contain the same pairs in the same order. /// </summary> override public bool Equals(Object o) { if (!(o is ExpandedRow)) { return(false); } ExpandedRow that = (ExpandedRow)o; return(Pairs.Equals(that.Pairs) && IsReversed == that.IsReversed); }
// Try to construct a valid rows sequence // Recursion is used to implement backtracking private List <ExpandedPair> checkRows(List <ExpandedRow> collectedRows, int currentRow) { for (int i = currentRow; i < rows.Count; i++) { ExpandedRow row = rows[i]; pairs.Clear(); int size = collectedRows.Count; for (int j = 0; j < size; j++) { pairs.AddRange(collectedRows[j].Pairs); } pairs.AddRange(row.Pairs); if (!isValidSequence(pairs)) { continue; } if (checkChecksum()) { return(this.pairs); } List <ExpandedRow> rs = new List <ExpandedRow>(); rs.AddRange(collectedRows); rs.Add(row); // Recursion: try to add more rows var result = checkRows(rs, i + 1); if (result == null) { // We failed, try the next candidate continue; } return(result); } return(null); }
private void storeRow(int rowNumber, bool wasReversed) { // Discard if duplicate above or below; otherwise insert in order by row number. int insertPos = 0; bool prevIsSame = false; bool nextIsSame = false; while (insertPos < rows.Count) { ExpandedRow erow = rows[insertPos]; if (erow.RowNumber > rowNumber) { nextIsSame = erow.IsEquivalent(pairs); break; } prevIsSame = erow.IsEquivalent(pairs); insertPos++; } if (nextIsSame || prevIsSame) { return; } // When the row was partially decoded (e.g. 2 pairs found instead of 3), // it will prevent us from detecting the barcode. // Try to merge partial rows // Check whether the row is part of an allready detected row if (isPartialRow(pairs, rows)) { return; } rows.Insert(insertPos, new ExpandedRow(pairs, rowNumber, wasReversed)); removePartialRows(pairs, rows); }