01: /*
02: * ====================================================================
03: * Copyright (c) 2004 Marc Strapetz, marc.strapetz@smartsvn.com.
04: * All rights reserved.
05: *
06: * This software is licensed as described in the file COPYING, which
07: * you should have received as part of this distribution. Use is
08: * subject to license terms.
09: * ====================================================================
10: */
11:
12: package de.regnis.q.sequence;
13:
14: import java.util.*;
15:
16: import de.regnis.q.sequence.core.*;
17:
18: /**
19: * @author Marc Strapetz
20: */
21: public class QSequenceCommonBlocks {
22:
23: // Accessing ==============================================================
24:
25: public static List createBlocks(boolean[] leftCommonPoints,
26: boolean[] rightCommonPoints,
27: QSequenceCommonBlockFactory factory) {
28: final List blocks = new ArrayList();
29:
30: int leftIndex = 0;
31: int rightIndex = 0;
32:
33: while (leftIndex < leftCommonPoints.length
34: || rightIndex < rightCommonPoints.length) {
35: int leftStart = leftIndex;
36: int rightStart = rightIndex;
37:
38: while (leftIndex < leftCommonPoints.length
39: && rightIndex < rightCommonPoints.length
40: && leftCommonPoints[leftIndex]
41: && rightCommonPoints[rightIndex]) {
42: leftIndex++;
43: rightIndex++;
44: }
45:
46: if (leftIndex > leftStart && rightIndex > rightStart) {
47: final Object block = factory.createCommonBlock(
48: leftStart, leftIndex - 1, rightStart,
49: rightIndex - 1);
50: if (block != null) {
51: blocks.add(block);
52: }
53: }
54:
55: leftStart = leftIndex;
56: rightStart = rightIndex;
57:
58: while ((leftIndex < leftCommonPoints.length && !leftCommonPoints[leftIndex])
59: || (rightIndex < rightCommonPoints.length && !rightCommonPoints[rightIndex])) {
60: if (leftIndex < leftCommonPoints.length
61: && !leftCommonPoints[leftIndex]) {
62: leftIndex++;
63: }
64: if (rightIndex < rightCommonPoints.length
65: && !rightCommonPoints[rightIndex]) {
66: rightIndex++;
67: }
68: }
69:
70: if (leftIndex > leftStart || rightIndex > rightStart) {
71: final Object block = factory.createDistinctBlock(
72: leftStart, leftIndex - 1, rightStart,
73: rightIndex - 1);
74: if (block != null) {
75: blocks.add(block);
76: }
77: }
78:
79: QSequenceAssert
80: .assertTrue((leftIndex >= leftCommonPoints.length && rightIndex >= rightCommonPoints.length)
81: || (leftCommonPoints[leftIndex] && rightCommonPoints[rightIndex]));
82: }
83:
84: return blocks;
85: }
86: }
|