No contains method exists. Let me look at how the covers function works in group_dedup.go:
func covers(outer, inner *CodeLocation, iOuter, iInner int) bool {
if outer.FilePath != inner.FilePath {
return false
}
if outer.StartLine > inner.StartLine || outer.EndLine < inner.EndLine {
return false
}
if outer.StartLine == inner.StartLine && outer.EndLine == inner.EndLine {
return iOuter < iInner
}
return true
}
So covers(outer, inner) returns true if outer covers inner. That's what I need for the inter-group dedup: covers(f2.Location, f1.Location) means f2's location covers f1's location.
Let me update my implementation to use the covers function.
Also, I notice that the covers function takes indices iOuter and iInner for the tiebreak on equal ranges. For the inter-group dedup, I don't have indices, but I can handle the equal-range case differently.
Let me revise the implementation:
// isSubsetStrict checks if group g1 is a strict subset of group g2 (or identical, for dedup).
// Returns true if g1 should be suppressed in favor of g2.
func isSubsetStrict(g1, g2 *CloneGroup) bool {
if g1.Size != g2.Size || g1.Size == 0 {
return false
}
// Build a map from file path to fragment for g2
fragMap := make(map[string]*CodeFragment)
for _, f := range g2.Fragments {
if f != nil && f.Location != nil {
fragMap[f.Location.FilePath] = f
}
}
// Check each fragment in g1 has a corresponding fragment in g2
for _, f1 := range g1.Fragments {
if f1 == nil || f1.Location == nil {
continue
}
filePath := f1.Location.FilePath
f2, ok := fragMap[filePath]
if !ok || f2 == nil || f2.Location == nil {
return false
}
// f1 must be covered by f2 (f2's range contains f1's range)
if !locCovers(f2.Location, f1.Location) {
return false
}
}
return true
}
// locCovers reports whether outer covers inner (outer's range contains inner's range).
func locCovers(outer, inner *CodeLocation) bool {
if outer.FilePath != inner.FilePath {
return false
}
return outer.StartLine <= inner.StartLine && outer.EndLine >= inner.EndLine
}
Then for the dedup:
- If
isSubsetStrict(g1, g2) AND isSubsetStrict(g2, g1): both are identical, suppress g2 (keep first)
- If
isSubsetStrict(g1, g2) AND NOT isSubsetStrict(g2, g1): g1 is strict subset of g2, suppress g1
- If NOT
isSubsetStrict(g1, g2) AND isSubsetStrict(g2, g1): g2 is strict subset of g1, suppress g2
- Otherwise: neither is a subset, keep both
Actually, simpler:
- If g1 ⊆ g2 (all members of g1 are covered by corresponding members of g2):
- If also g2 ⊆ g1: identical, suppress g2
- If not g2 ⊆ g1: g1 is strict subset, suppress g1
Let me implement this.
Hmm, actually let me think about this more carefully. The key question is: when should group g1 be suppressed in favor of group g2?
Answer: when g1 is a strict subset of g2, OR when g1 and g2 are identical (in which case we suppress the second one).
So the condition for suppressing g1 in favor of g2 is: isSubset(g1, g2) (g1's members are all contained within g2's corresponding members).
And the condition for suppressing g2 in favor of g1 is: isSubset(g2, g1).
If both are true, they're identical, and we suppress g2 (the second one).
If only g1 is suppressed, it's a strict subset.
If only g2 is suppressed, g1 is a strict subset.
If neither is suppressed, neither is a subset.
So the algorithm is:
for i := 0; i < len(groups); i++ {
if suppressed[i] { continue }
for j := i + 1; j < len(groups); j++ {
if suppressed[j] { continue }
if isSubset(groups[i], groups[j]) {
suppressed[i] = true // i is subset of j, suppress i
break
}
if isSubset(groups[j], groups[i]) {
suppressed[j] = true // j is subset of i, suppress j
}
}
}
Wait, but what if i ⊆ j and j ⊆ k? We'd suppress i when comparing with j, then suppress j when comparing with k. That leaves k as the only survivor. Good.
What if i ⊆ j and i ⊆ k (both j and k contain i, but j and k are not related)? We'd suppress i when comparing with j (first match), then j and k remain. Good.
What if i and j are identical? isSubset(i, j) is true and isSubset(j, i) is also true. In the loop:
- When comparing i (i=0) and j (j=1):
isSubset(groups[0], groups[1]) → true → suppress groups[0]
- break
Wait, that's wrong! If i and j are identical, we should suppress j (the second one), not i (the first one). Let me fix this.
Actually, looking at the existing dedupeStrictSubsetGroupMembers, the approach is:
- If i is a subset of j, suppress i
- If j is a subset of i, suppress j
But for identical groups, isSubset(i, j) is true (all members of i are covered by j, since they're equal), so we'd suppress i. That's wrong.
Hmm, let me think about this differently. For identical groups, we want to keep the first one. So:
-
If isSubset(g1, g2):
- If
isStrictSubset(g1, g2): suppress g1
- If
isIdentical(g1, g2): suppress g2
-
If isSubset(g2, g1):
- If
isStrictSubset(g2, g1): suppress g2
- If
isIdentical(g2, g1): suppress g1 (same as above, already handled)
So for the loop:
if isSubset(g1, g2) {
if isStrictSubset(g1, g2) {
suppressed[i] = true // i is strict subset, suppress
}
// If identical, don't suppress i (keep first)
break
}
if isSubset(g2, g1) {
if isStrictSubset(g2, g1) {
suppressed[j] = true // j is strict subset, suppress
}
break
}
Wait, but this doesn't handle the case where both are strict subsets of each other... which is impossible (can't have both g1 ⊂ g2 and g2 ⊂ g1 unless they're equal).
Actually, I think the logic is simpler:
- If
isSubset(g1, g2) (g1's members ⊆ g2's members):
- If there's at least one strict containment: suppress g1 (g1 is strict subset)
- If all containments are equal: suppress g2 (identical, keep first)
Wait, but "if all containments are equal" means g1 == g2. In that case, we suppress g2 (the second one).
Hmm, but in my loop above, when comparing g1 (i) and g2 (j), if isSubset(g1, g2) is true:
- If g1 is strict subset: suppress g1 (break)
- If identical: don't suppress g1, but should we suppress g2?
If I suppress g2 when identical, then when the outer loop reaches j, it's already suppressed. Good.
But wait, I need to be careful about the order. If g1 ⊆ g2 and they're identical:
- When comparing g1 and g2: suppress g2 (because they're identical)
- When the outer loop reaches g2: it's suppressed, skip
That works. But what if there's a g3 that g2 ⊆ g3? Then:
- When comparing g1 and g2: suppress g2 (identical)
- When comparing g1 and g3: suppress g1 (strict subset of g3)
Or:
- When comparing g1 and g2: suppress g2 (identical)
- When the inner loop starts with i=2 (g3) and j=0 (g1): hmm, j starts from i+1, so j > i. When i=2, j would start from 3.
Wait, the loop is:
for i := 0; i < len(groups); i++ {
if suppressed[i] { continue }
for j := i + 1; j < len(groups); j++ {
...
}
}
So when i=0 (g1):
- Compare g1 with g2: suppress g2 (identical)
- Compare g1 with g3: suppress g1 (strict subset)
- i is now suppressed, outer loop continues with i=1
When i=1 (g2):
- suppressed[1] is true, skip
When i=2 (g3):
- Not suppressed, compare with... nothing (j > 2)
- g3 remains
So the final result is {g3}, which is correct (g3 is the superset of both g1 and g2).
But wait, there's an issue. When comparing g1 with g2 and finding they're identical, I suppress g2. But then when comparing g1 with g3, if g1 ⊂ g3, I suppress g1. But what if g2 ⊆ g3 as well? Well, g2 was already suppressed, so we don't need to worry about it.
Actually, there might be an issue. If g1 ⊂ g3 and g2 ⊂ g3, and g1 == g2 (identical):
- When comparing g1 (i=0) with g2 (j=1): identical → suppress g2
- When comparing g1 (i=0) with g3 (j=2): g1 ⊂ g3 → suppress g1
- Result: {g3}
That's correct.
But what if the comparison order is different? If g1 ⊂ g3 and g2 == g3:
- When comparing g1 (i=0) with g2 (j=1): g1 ⊂ g2 → suppress g1
- When comparing g1 (i=0) with g3 (j=2): g1 is already suppressed, so...
Wait, the if suppressed[i] check is at the top of the outer loop. After suppressing g1 in the comparison with g2, the inner loop breaks. Then the outer loop checks if suppressed[i] for i=1 (g2):
- g2 is not suppressed
- Compare g2 with g3: g2 == g3 → suppress g3
Hmm, but we wanted to suppress g2 and g1, keeping g3. Let me trace through more carefully.
Initial: groups = [g1, g2, g3], suppressed = {}
i = 0 (g1): not suppressed
j = 1 (g2): not suppressed
isSubset(g1, g2): true (g1 ⊂ g2)
isStrictSubset(g1, g2): true
→ suppressed[0] = true, break
i = 1 (g2): not suppressed
j = 2 (g3): not suppressed
isSubset(g2, g3): true (g2 == g3, so g2 ⊆ g3)
isStrictSubset(g2, g3): false
→ identical, should suppress g3 (the second one)
→ suppressed[2] = true, break
i = 2 (g3): suppressed, skip
Result: {g2}
That's correct! g2 is kept (it's the superset of g1 and identical to g3).
But wait, what about the case where g1 == g2 == g3 (all identical)? Let me trace:
Initial: groups = [g1, g2, g3], suppressed = {}
i = 0 (g1): not suppressed
j = 1 (g2): not suppressed
isSubset(g1, g2): true
isStrictSubset(g1, g2): false
→ identical, suppress g2 (the second one)
→ suppressed[1] = true, break
i = 1 (g2): suppressed, skip
i = 2 (g3): not suppressed
j = 3: out of bounds, inner loop doesn't execute
Result: {g1, g3}
Hmm, that's wrong. g3 should also be suppressed because it's identical to g1.
The issue is that g3 is not compared with g1 because j starts from i+1. When i=2 (g3), j=3 is out of bounds.
I need to handle this differently. Let me think...
One option: don't break after the first suppression, and check all pairs:
for i := 0; i < len(groups); i++ {
if suppressed[i] { continue }
for j := i + 1; j < len(groups); j++ {
if suppressed[j] { continue }
if isSubset(groups[i], groups[j]) {
if isStrictSubset(groups[i], groups[j]) {
suppressed[i] = true
} else {
// identical, suppress j
suppressed[j] = true
}
break
}
if isSubset(groups[j], groups[i]) {
if isStrictSubset(groups[j], groups[i]) {
suppressed[j] = true
}
// else: already handled above (shouldn't happen if isSubset(i,j) was false)
break
}
}
}
Wait, but if isSubset(i,j) is true and isStrictSubset(i,j) is false (identical), we suppress j. Then when the outer loop reaches j, it's suppressed, so we skip. But what if g3 is identical to g1?
Let me trace again:
Initial: groups = [g1, g2, g3], suppressed = {}, all identical
i = 0 (g1): not suppressed
j = 1 (g2): not suppressed
isSubset(g1, g2): true
isStrictSubset(g1, g2): false
→ identical, suppress j (g2)
→ suppressed[1] = true, break
i = 1 (g2): suppressed, skip
i = 2 (g3): not suppressed
j = 3: out of bounds
Result: {g1, g3}
Still wrong. g3 should be suppressed.
The issue is that g3 is only compared with groups after it (j > i), but not with groups before it. Since g1 was already compared with g2 and g3 is after g1, g3 should be compared with g1... but g3 is compared with j > 2, and there's nothing after g3.
I need a different approach. Let me think...
Option 1: Compare each group with all previous (non-suppressed) groups:
for j := 0; j < len(groups); j++ {
if suppressed[j] { continue }
for i := 0; i < j; i++ {
if suppressed[i] { continue }
if isSubset(groups[i], groups[j]) {
// i is subset of j, suppress i (keep the superset j)
suppressed[i] = true
break // i is suppressed, move to next j
}
if isSubset(groups[j], groups[i]) {
// j is subset of i, suppress j
suppressed[j] = true
break // j is suppressed, move to next j
}
}
}
Let me trace:
Initial: groups = [g1, g2, g3], suppressed = {}, all identical
j = 0 (g1): no previous groups to compare, skip
j = 1 (g2): not suppressed
i = 0 (g1): not suppressed
isSubset(g1, g2): true
isStrictSubset(g1, g2): false
→ identical, but we suppress i (g1)? No, that's wrong. We should suppress j (g2), not i (g1).
Hmm, the logic is tricky. Let me think again.
For two identical groups, we want to suppress the later one (j). For g1 ⊂ g2, we want to suppress g1 (the subset). So:
if isSubset(groups[i], groups[j]) {
if isStrictSubset(groups[i], groups[j]) {
suppressed[i] = true // i is strict subset, suppress
}
// if identical: don't suppress i, suppress j instead (handled below)
}
if isSubset(groups[j], groups[i]) {
if isStrictSubset(groups[j], groups[i]) {
suppressed[j] = true // j is strict subset, suppress
}
// if identical: suppress j (the later one)
suppressed[j] = true
break
}
Wait, this is getting confusing. Let me simplify.
For identical groups, we want to keep the first one and suppress the second. So:
for j := 0; j < len(groups); j++ {
if suppressed[j] { continue }
for i := 0; i < j; i++ {
if suppressed[i] { continue }
if isSubset(groups[j], groups[i]) {
// j is subset of i (including identical), suppress j
suppressed[j] = true
break
}
if isSubset(groups[i], groups[j]) && isStrictSubset(groups[i], groups[j]) {
// i is strict subset of j, suppress i
suppressed[i] = true
// Note: we don't break here because i's suppression doesn't
// necessarily mean j is fully deduped. But actually, since
// we're iterating j from 0 to len-1, and for each j we check
// against all previous non-suppressed i, if we suppress i,
// the next iteration of j will use the next i.
// Hmm, but suppressing i in the middle of iterating j could
// cause issues.
}
}
}
Actually, let me take a step back. The key insight is:
- For each group, if it's a strict subset of another group, suppress it.
- For identical groups, suppress all but the first one.
So the algorithm could be:
// Phase 1: Suppress strict subsets
for i := 0; i < len(groups); i++ {
for j := 0; j < len(groups); j++ {
if i == j || suppressed[i] || suppressed[j] { continue }
if isStrictSubset(groups[i], groups[j]) {
suppressed[i] = true
break
}
}
}
// Phase 2: Suppress duplicates of earlier non-suppressed groups
for j := 0; j < len(groups); j++ {
if suppressed[j] { continue }
for i := 0; i < j; i++ {
if suppressed[i] { continue }
if isSubset(groups[j], groups[i]) {
// j is identical to i (since j is a subset of i and vice versa
// would have been caught in phase 1), suppress j
suppressed[j] = true
break
}
}
}
Wait, but this is O(n²) in each phase. For large n, this could be slow. But I think for typical use cases, n is small enough.
Actually, I think there's a simpler approach. Let me just check: for group j, is there any earlier group i (i < j) such that g_j ⊆ g_i? If yes, suppress g_j. This handles both:
- Strict subset: g_j ⊂ g_i → suppress g_j
- Identical: g_j == g_i → suppress g_j
The algorithm is:
for j := 1; j < len(groups); j++ {
if suppressed[j] { continue }
for i := 0; i < j; i++ {
if suppressed[i] { continue }
if isSubset(groups[j], groups[i]) {
suppressed[j] = true
break
}
}
}
Let me trace:
Initial: groups = [g1, g2, g3], suppressed = {}, g1 == g2 == g3
j = 1 (g2): not suppressed
i = 0 (g1): not suppressed
isSubset(g2, g1): true (identical)
→ suppressed[1] = true, break
j = 2 (g3): not suppressed
i = 0 (g1): not suppressed
isSubset(g3, g1): true (identical)
→ suppressed[2] = true, break
Result: {g1}
That's correct! g1 is kept, g2 and g3 are suppressed.
Let me trace another case: g1 ⊂ g2 ⊂ g3
Initial: groups = [g1, g2, g3], suppressed = {}, g1 ⊂ g2 ⊂ g3
j = 1 (g2): not suppressed
i = 0 (g1): not suppressed
isSubset(g2, g1): false (g2 is not a subset of g1)
no match, g2 not suppressed
j = 2 (g3): not suppressed
i = 0 (g1): not suppressed
isSubset(g3, g1): false (g3 is not a subset of g1)
i = 1 (g2): not suppressed
isSubset(g3, g2): false (g3 is not a subset of g2)
no match, g3 not suppressed
Result: {g1, g2, g3}
Hmm, that's wrong! g1 and g2 should be suppressed (they're strict subsets of g3).
So the issue is that we're only checking if g_j ⊆ g_i for i < j. But g1 (i=0) is checked against g2 (j=1), which fails because g1 ⊂ g2, not g1 ⊇ g2.
I need to check both directions. For group j, if any earlier group i has g_j ⊆ g_i, suppress g_j. But also, if any earlier group i is a strict subset of g_j, suppress i.
Hmm, let me try a different approach:
for j := 1; j < len(groups); j++ {
if suppressed[j] { continue }
for i := 0; i < j; i++ {
if suppressed[i] && suppressed[j] { continue }
// Check if j is subset of i (suppress j)
if isSubset(groups[j], groups[i]) {
suppressed[j] = true
break
}
// Check if i is strict subset of j (suppress i)
if !suppressed[i] && isStrictSubset(groups[i], groups[j]) {
suppressed[i] = true
}
}
}
Let me trace:
Initial: groups = [g1, g2, g3], suppressed = {}, g1 ⊂ g2 ⊂ g3
j = 1 (g2): not suppressed
i = 0 (g1): not suppressed
isSubset(g2, g1): false
isStrictSubset(g1, g2): true → suppressed[0] = true
g2 not suppressed
j = 2 (g3): not suppressed
i = 0 (g1): suppressed, skip
i = 1 (g2): not suppressed
isSubset(g3, g2): false
isStrictSubset(g2, g3): true → suppressed[1] = true
g3 not suppressed
Result: {g3}
That's correct! g1 and g2 are suppressed, g3 is kept.
Let me trace the identical case again:
Initial: groups = [g1, g2, g3], suppressed = {}, g1 == g2 == g3
j = 1 (g2): not suppressed
i = 0 (g1): not suppressed
isSubset(g2, g1): true → suppressed[1] = true, break
(we don't check strictSubset because we already suppressed j)
j = 2 (g3): not suppressed
i = 0 (g1): not suppressed
isSubset(g3, g1): true → suppressed[2] = true, break
Result: {g1}
Correct!
Let me trace the bug case:
Group 6: {A2: 299-315, B2: 318-334} (smaller)
Group 14: {A: 298-315, B: 317-334} (larger)
Assuming groups are sorted by similarity descending (which is the case in the bug report: group 6 has similarity 0.9692, group 14 has 0.9613). So group 6 comes first.
Wait, but the issue is that group 6 (the smaller one) has HIGHER similarity. So the groups would be:
- index 0: Group 6 (similarity 0.9692, smaller fragments)
- index 1: Group 14 (similarity 0.9613, larger fragments)
Let me trace:
j = 1 (Group 14): not suppressed
i = 0 (Group 6): not suppressed
isSubset(Group 14, Group 6): false (Group 14 is NOT a subset of Group 6)
isStrictSubset(Group 6, Group 14): true → suppressed[0] = true
Result: {Group 14}
Correct! Group 6 (the smaller one) is suppressed, Group 14 (the larger one) is kept.
But wait, is the logic correct here? isStrictSubset(Group 6, Group 14) is true because Group 6's fragments are strict subsets of Group 14's fragments. So we suppress Group 6 (the strict subset). That's correct!
OK, the algorithm works. Let me implement it.
Actually, let me also consider the performance. For n groups, this is O(n²) comparisons. Each comparison is O(m) where m is the number of fragments per group. For typical use cases, n and m are small (n < 100, m < 10), so this is fine.
Let me also consider edge cases:
- Empty groups: already handled (return early)
- Groups with different numbers of fragments:
isSubset returns false (different sizes)
- Groups with different files:
isSubset returns false (fragment not found)
- Groups with overlapping but not subset fragments:
isSubset returns false (not contained)
All good.
Now let me implement the code.
Actually, I also realize I need to think about the ordering. The groups are sorted by similarity (descending), size (descending), and then fragment location. The dedupeStrictSubsetGroupMembers function calls sortCloneGroups if any groups were modified. And sortCloneGroups sorts by similarity (descending), size (descending), and then fragment location.
For the inter-group dedup, the order matters. If two groups are identical, we want to suppress the later one. If one is a strict subset of the other, we want to suppress the subset. But which order do we get?
Looking at the bug report:
- Group 6: similarity 0.9692, smaller fragments
- Group 14: similarity 0.9613, larger fragments
So the smaller fragments have higher similarity (because the with self.subTest(...) header line is not in the comparison, making the content more similar). And smaller fragments would have smaller size.
After sorting by similarity (descending), size (descending), and fragment location:
- Group 6: similarity 0.9692 (higher), size smaller
- Group 14: similarity 0.9613 (lower), size larger
So Group 6 comes before Group 14. That's what I expected from my trace above.
But what if the order were reversed? If Group 14 had higher similarity, it would come first:
j = 1 (Group 6): not suppressed
i = 0 (Group 14): not suppressed
isSubset(Group 6, Group 14): true → suppressed[1] = true, break
Result: {Group 14}
Same result. Good.
OK, the algorithm is robust regardless of the initial order. Let me implement it.
Actually wait, there's one more consideration. What if we have three groups:
- G1: fragments at lines 1-10, 20-30
- G2: fragments at lines 1-10, 21-30
- G3: fragments at lines 1-10, 22-30
G2 is a strict subset of G1 (21-30 ⊂ 20-30). G3 is a strict subset of both G1 and G2.
With my algorithm:
j = 1 (G2):
i = 0 (G1): isSubset(G2, G1): true → suppressed[1] = true, break
j = 2 (G3):
i = 0 (G1): isSubset(G3, G1): true → suppressed[2] = true, break
Result: {G1}
Correct! G1 is kept, G2 and G3 are suppressed.
But what if the order is G3, G2, G1 (all with same similarity)?
j = 1 (G2):
i = 0 (G3): isSubset(G2, G3): false (G2 is not a subset of G3)
isStrictSubset(G3, G2): true → suppressed[0] = true
j = 2 (G1):
i = 0 (G3): suppressed, skip
i = 1 (G2): isSubset(G1, G2): false
isStrictSubset(G2, G1): true → suppressed[1] = true
Result: {G1}
Correct! G1 is the superset of both G2 and G3.
OK, the algorithm handles all these cases correctly. Let me implement it now.
Actually, there's another edge case. What if we have:
- G1: {A: 1-10, B: 20-30}
- G2: {A: 1-10, C: 25-35}
G1 and G2 share file A (same fragment 1-10), but have different fragments in the other file. G1 has B (20-30) and G2 has C (25-35).
IsSubset(G1, G2):
- File A: 1-10 ⊆ 1-10: yes
- File B (G1) → is there a corresponding file in G2? No, G2 has C, not B.
→ Fragment not found in G2's file map → false
So G1 is not a subset of G2. Similarly, G2 is not a subset of G1. Both groups are kept. Correct!
What about:
- G1: {A: 1-10, B: 20-30}
- G2: {A: 1-10, B: 25-35}
IsSubset(G1, G2):
- File A: 1-10 ⊆ 1-10: yes
- File B: 20-30 ⊆ 25-35: NO (20 < 25)
So G1 is not a subset of G2. Both groups are kept. Correct!
What about:
- G1: {A: 1-10, B: 20-30}
- G2: {A: 1-10, B: 20-35}
IsSubset(G1, G2):
- File A: 1-10 ⊆ 1-10: yes
- File B: 20-30 ⊆ 20-35: yes
→ G1 is a subset of G2
IsStrictSubset(G1, G2):
- At least one strict containment: File B is strict (20-30 ⊂ 20-35)
→ G1 is a strict subset of G2
So G1 is suppressed. Correct!
OK, the algorithm is robust. Let me implement it now.
Hmm, but I realize there's another issue. What if a group has fragments in different files, and we're comparing with another group that has fragments in the same set of files, but the fragments are not in the same order?
For example:
- G1: {A: 1-10, B: 20-30}
- G2: {B: 20-30, A: 1-10}
These are the same group, just with fragments in different order. My algorithm uses a map from file path to fragment, so the order doesn't matter. Let me check:
fragMap from G2: {B: (20-30), A: (1-10)}
isSubset(G1, G2):
- File A from G1: fragMap["A"] = (1-10) from G2
1-10 ⊆ 1-10: yes
- File B from G1: fragMap["B"] = (20-30) from G2
20-30 ⊆ 20-30: yes
→ G1 is a subset of G2
isStrictSubset(G1, G2):
- No strict containments
→ G1 is identical to G2
So we suppress G2 (the second one). Correct!
OK, the algorithm is correct. Let me implement it now.
One more thing: the locCovers function I need. Let me check if there's already a similar function.
Looking at group_dedup.go:
func covers(outer, inner *CodeLocation, iOuter, iInner int) bool {
if outer.FilePath != inner.FilePath {
return false
}
if outer.StartLine > inner.StartLine || outer.EndLine < inner.EndLine {
return false
}
if outer.StartLine == inner.StartLine && outer.EndLine == inner.EndLine {
return iOuter < iInner
}
return true
}
This is for intra-group dedup, where the iOuter < iInner tiebreak is used for equal ranges. For inter-group dedup, I don't have indices, so I can't use this exact function. I'll create a simpler locCovers function:
// locCovers reports whether outer covers inner (outer's range contains inner's range).
// This is a pure range containment check without index-based tiebreaks.
func locCovers(outer, inner *CodeLocation) bool {
if outer.FilePath != inner.FilePath {
return false
}
return outer.StartLine <= inner.StartLine && outer.EndLine >= inner.EndLine
}
OK, let me now implement the full solution.
Wait, I also need to think about what the existing dedupeStrictSubsetGroupMembers does. It returns a groupDedupeResult with both the deduped groups and a map of suppressed fragments. My new dedupeDuplicateGroups function should also return a similar structure, or at least integrate with the existing flow.
Looking at the existing code:
func (cd *CloneDetector) groupClonesWithStrategy(strategy GroupingStrategy) {
if strategy == nil {
cd.cloneGroups = []*CloneGroup{}
return
}
dedupeResult := dedupeStrictSubsetGroupMembers(strategy.GroupClones(cd.clonePairs), cd.clonePairs)
cd.cloneGroups = dedupeResult.groups
cd.clonePairs = filterClonePairsWithSuppressedMembers(cd.clonePairs, dedupeResult.suppressed)
}
For the inter-group dedup, I could:
- Add a new function that returns suppressed groups (or fragments)
- Filter the clone groups after this dedup
- Optionally filter the clone pairs
For simplicity, let me just add the inter-group dedup as a separate step after the intra-group dedup, and only suppress the groups (not the pairs).
Actually, I could extend the existing flow:
func (cd *CloneDetector) groupClonesWithStrategy(strategy GroupingStrategy) {
if strategy == nil {
cd.cloneGroups = []*CloneGroup{}
return
}
dedupeResult := dedupeStrictSubsetGroupMembers(strategy.GroupClones(cd.clonePairs), cd.clonePairs)
cd.cloneGroups = dedupeDuplicateGroups(dedupeResult.groups)
cd.clonePairs = filterClonePairsWithSuppressedMembers(cd.clonePairs, dedupeResult.suppressed)
}
But wait, if I suppress a group in dedupeDuplicateGroups, the fragments in that group are not marked as suppressed in dedupeResult.suppressed. So the pairs won't be filtered. This is fine for now.
Actually, I think it would be cleaner to extend the existing dedupeStrictSubsetGroupMembers function to also handle inter-group dedup. But that would require passing more information (like the original pairs) to the function.
Alternatively, I could create a new function that handles both intra-group and inter-group dedup together.
Let me go with the simpler approach: add a separate dedupeDuplicateGroups function and call it after dedupeStrictSubsetGroupMembers.
Now, let me think about what to do with clone pairs. If group G1 (with fragments A2 and B2) is suppressed by the inter-group dedup, should the pair (A2, B2) also be suppressed?
I think for now, I'll leave the pairs alone. The pair (A2, B2) is still a valid pair (it represents a real duplication). The fact that it's "covered" by pair (A, B) is a higher-level concern.
If I wanted to also suppress pairs, I could extend the dedupeDuplicateGroups function to return a list of suppressed fragments, and then filter the pairs. But that would be more complex and might remove useful information.
OK, let me implement the simple version: only suppress groups, not pairs.
Actually, wait. I realize there's another subtlety. If group G1 has fragments {A2, B2} and it's suppressed because G1 ⊆ G2 (where G2 has fragments {A, B}), then A2 ⊆ A and B2 ⊆ B. The pair (A2, B2) is "covered" by the pair (A, B). But the pair (A2, B2) is a distinct pair from (A, B). If we keep both pairs, the total pair count would be inflated.
But this is a separate issue from the group dedup. The pair dedup would require comparing all pairs for subset relationships, which is O(n²) in the number of pairs. For now, I'll focus on the group dedup.
OK, let me implement the solution.
Actually, one more consideration: the issue says "Group 14's members each start exactly one line earlier than group 6's". So the fragments are:
- Group 6: (299-315, 318-334)
- Group 14: (298-315, 317-334)
Group 6's fragments are strict subsets of Group 14's fragments:
- 299-315 ⊂ 298-315 (start line differs by 1)
- 318-334 ⊂ 317-334 (start line differs by 1)
So my algorithm would correctly suppress Group 6.
But what if the fragments overlap but don't have a strict subset relationship? For example:
- G1: {A: 1-10, B: 20-30}
- G2: {A: 5-15, B: 20-30}
IsSubset(G1, G2):
- File A: 1-10 ⊆ 5-15? No (1 < 5)
→ Not a subset
Both groups are kept. Correct!
What about:
- G1: {A: 1-10, B: 20-30}
- G2: {A: 1-10, B: 25-35}
IsSubset(G1, G2):
- File A: 1-10 ⊆ 1-10: yes
- File B: 20-30 ⊆ 25-35? No (20 < 25)
→ Not a subset
Both groups are kept. Correct!
OK, the algorithm is correct. Let me implement it.
Let me now write the actual code. I'll add the dedupeDuplicateGroups function to group_dedup.go.
Let me plan the implementation:
- Add helper function
locCovers(outer, inner *CodeLocation) bool
- Add helper function
isSameFragmentSet(g1, g2 *CloneGroup) bool - checks if groups have the same set of files
- Add helper function
isSubsetAll(g1, g2 *CloneGroup) bool - checks if all members of g1 are covered by corresponding members of g2
- Add helper function
isStrictSubset(g1, g2 *CloneGroup) bool - checks if at least one member of g1 is strictly covered by the corresponding member of g2
- Add main function
dedupeDuplicateGroups(groups []*CloneGroup) []*CloneGroup - removes duplicate groups
Actually, I can simplify by combining isSubsetAll and isStrictSubset:
// groupCovered checks if all fragments in g1 are covered by corresponding
// fragments in g2, and whether at least one coverage is strict.
func groupCovered(g1, g2 *CloneGroup) (bool, bool) {
if g1.Size != g2.Size || g1.Size == 0 {
return false, false
}
fragMap := make(map[string]*CodeFragment)
for _, f := range g2.Fragments {
if f != nil && f.Location != nil {
fragMap[f.Location.FilePath] = f
}
}
allCovered := true
anyStrict := false
for _, f1 := range g1.Fragments {
if f1 == nil || f1.Location == nil {
continue
}
f2, ok := fragMap[f1.Location.FilePath]
if !ok || f2 == nil || f2.Location == nil {
allCovered = false
break
}
if locCovers(f2.Location, f1.Location) {
if f2.Location.StartLine != f1.Location.StartLine || f2.Location.EndLine != f1.Location.EndLine {
anyStrict = true
}
} else {
allCovered = false
break
}
}
return allCovered, anyStrict
}
Then:
isSubsetAll(g1, g2) = groupCovered(g1, g2).allCovered
isStrictSubset(g1, g2) = groupCovered(g1, g2).anyStrict
And the main dedup function:
func dedupeDuplicateGroups(groups []*CloneGroup) []*CloneGroup {
if len(groups) <= 1 {
return groups
}
suppressed := make([]bool, len(groups))
for j := 1; j < len(groups); j++ {
if suppressed[j] {
continue
}
for i := 0; i < j; i++ {
if suppressed[i] {
continue
}
allCovered, anyStrict := groupCovered(groups[j], groups[i])
if allCovered {
// j is a subset of i (possibly identical)
// Always suppress j (keep the earlier group)
suppressed[j] = true
break
}
// Check if i is a strict subset of j (suppress i)
allCovered, anyStrict = groupCovered(groups[i], groups[j])
if allCovered && anyStrict {
suppressed[i] = true
// Note: we don't break here because i's suppression doesn't
// necessarily mean j is fully deduped
}
}
}
out := make([]*CloneGroup, 0, len(groups))
for i, g := range groups {
if !suppressed[i] {
out = append(out, g)
}
}
return out
}
Wait, but this approach might have an issue. If we suppress group i (a strict subset of group j), and later find that group j is a strict subset of some group k, we'd suppress j too. So both i and j are suppressed, leaving only k. That's correct.
But what if we suppress group i (a strict subset of j), and j is later found to be identical to some group k? Then we'd suppress j (the second one in the comparison), but i was already suppressed. So only k remains. That's also correct.
But there's a subtle issue. When we suppress i in the comparison of j with i, we then continue to the next i. But what if i was also a strict subset of some later group j'? We'd miss that comparison.
Hmm, let me trace through an example:
Groups: [G1, G2, G3], where G1 ⊂ G2 ⊂ G3
j = 1 (G2):
i = 0 (G1): groupCovered(G2, G1) → false (G2 is not covered by G1)
groupCovered(G1, G2) → true, anyStrict = true
→ suppressed[0] = true
(i loop ends, j=1 is not suppressed)
j = 2 (G3):
i = 0 (G1): suppressed, skip
i = 1 (G2): not suppressed
groupCovered(G3, G2) → false (G3 is not covered by G2)
groupCovered(G2, G3) → true, anyStrict = true
→ suppressed[1] = true
(i loop ends, j=2 is not suppressed)
Result: {G3}
Correct!
But wait, there's a performance issue. In the inner loop, for each j, we iterate over all i < j. If we suppress some i in the middle of the loop, we continue to the next i. But what if the suppressed i was a subset of a later j? We'd miss that comparison.
Hmm, but actually, we're iterating j from 1 to len-1, and for each j, we compare with all i < j. If we suppress i because i ⊂ j, then i is a strict subset of j. If j is later found to be a subset of k, we suppress j too. So the chain of subsets is handled correctly.
But what if i is a strict subset of both j and k (where j and k are not related)? We'd suppress i when comparing with j (first match). Then when comparing k with i, i is already suppressed, so we skip. And when comparing k with j, neither is a subset of the other, so both are kept.
Is this correct? Let me think...
Groups: [G1, G2, G3], where G1 ⊂ G2, G1 ⊂ G3, G2 not subset of G3
j = 1 (G2):
i = 0 (G1): groupCovered(G1, G2) → true, anyStrict = true
→ suppressed[0] = true
(i loop ends, j=1 is not suppressed)
j = 2 (G3):
i = 0 (G1): suppressed, skip
i = 1 (G2): not suppressed
groupCovered(G3, G2) → false (G3 is not covered by G2)
groupCovered(G2, G3) → false (G2 is not covered by G3)
(neither is a subset, no suppression)
(i loop ends, j=2 is not suppressed)
Result: {G2, G3}
Correct! G1 is suppressed (it's a subset of G2), and G2 and G3 are both kept (they're not subsets of each other).
But wait, is G3 correctly retained? G1 ⊂ G3, so G3 is a superset of G1. But G3 doesn't have any groups that contain it. So it should be kept. Correct!
OK, the algorithm handles this case correctly.
But there's another subtle issue. When we suppress i in the inner loop, we don't break. So we continue to the next i. What if we suppress multiple i's? That's fine, they're all subsets of j.
Actually, wait. Let me re-read the code:
for j := 1; j < len(groups); j++ {
if suppressed[j] {
continue
}
for i := 0; i < j; i++ {
if suppressed[i] {
continue
}
allCovered, anyStrict := groupCovered(groups[j], groups[i])
if allCovered {
// j is a subset of i (possibly identical)
suppressed[j] = true
break
}
allCovered, anyStrict = groupCovered(groups[i], groups[j])
if allCovered && anyStrict {
suppressed[i] = true
// Don't break, continue to next i
}
}
}
Hmm, there's an issue. When we suppress i (because i is a strict subset of j), we don't break. So we continue to the next i. But what if the next i is also a strict subset of j? We'd suppress it too. That's correct.
But what if the next i is a strict subset of some later group j' (not j)? We'd suppress it now (because it's a subset of j), but it should be compared with j' later.
Wait, that's fine. If i is a subset of j, we suppress i regardless of whether i is also a subset of j'. The result is the same: i is suppressed.
But what if j' is a superset of j, and we want to keep j'? Then j would be suppressed later (when comparing j with j'), and j' would be kept. And i would already be suppressed (because it's a subset of j, and we suppress all subsets).
Let me trace:
Groups: [G1, G2, G3], where G1 ⊂ G2 ⊂ G3
j = 1 (G2):
i = 0 (G1): groupCovered(G1, G2) → true, anyStrict = true
→ suppressed[0] = true
j = 2 (G3):
i = 0 (G1): suppressed, skip
i = 1 (G2): groupCovered(G2, G3) → true, anyStrict = true
→ suppressed[1] = true
Result: {G3}
Correct!
Now let me trace the case where G1 ⊂ G3 but G1 is not a subset of G2:
Groups: [G1, G2, G3], where G1 ⊂ G3, G2 not related to G1 or G3
j = 1 (G2):
i = 0 (G1): groupCovered(G2, G1) → false, groupCovered(G1, G2) → false
(neither is a subset, no suppression)
j = 2 (G3):
i = 0 (G1): groupCovered(G3, G1) → false, groupCovered(G1, G3) → true, anyStrict = true
→ suppressed[0] = true
Result: {G2, G3}
Correct! G1 is suppressed (it's a subset of G3), G2 is kept (it's not a subset of any other group).
OK, the algorithm is correct. Let me implement it.
Actually, I just realized there might be a performance issue with calling groupCovered twice for each pair (i, j). Once to check if j ⊆ i, and once to check if i ⊆ j. I can optimize by combining these checks.
Let me create a combined function:
// groupCoverRelationship returns the relationship between g1 and g2:
// - "none": neither is a subset of the other
// - "equal": g1 == g2 (identical)
// - "g1SubsetG2": g1 is a strict subset of g2
// - "g2SubsetG1": g2 is a strict subset of g1
func groupCoverRelationship(g1, g2 *CloneGroup) string {
if g1.Size != g2.Size || g1.Size == 0 {
return "none"
}
fragMap := make(map[string]*CodeFragment)
for _, f := range g2.Fragments {
if f != nil && f.Location != nil {
fragMap[f.Location.FilePath] = f
}
}
g1CoversG2 := true
g2CoversG1 := true
anyStrict := false
// Check if all fragments in g1 are covered by corresponding fragments in g2
// and vice versa
allFragments := make(map[*CodeFragment]bool) // to track unique fragments
// Actually, we need to check both directions separately
// Check if g2's fragments cover g1's fragments
for _, f1 := range g1.Fragments {
if f1 == nil || f1.Location == nil {
continue
}
f2, ok := fragMap[f1.Location.FilePath]
if !ok || f2 == nil || f2.Location == nil || !locCovers(f2.Location, f1.Location) {
g1CoversG2 = false
break
}
if f2.Location.StartLine != f1.Location.StartLine || f2.Location.EndLine != f1.Location.EndLine {
anyStrict = true
}
}
if !g1CoversG2 {
return "none"
}
// Now check if g1's fragments cover g2's fragments (for equality check)
fragMap2 := make(map[string]*CodeFragment)
for _, f1 := range g1.Fragments {
if f1 != nil && f1.Location != nil {
fragMap2[f1.Location.FilePath] = f1
}
}
for _, f2 := range g2.Fragments {
if f2 == nil || f2.Location == nil {
continue
}
f1, ok := fragMap2[f2.Location.FilePath]
if !ok || f1 == nil || f1.Location == nil || !locCovers(f1.Location, f2.Location) {
g2CoversG1 = false
break
}
}
if g1CoversG2 && g2CoversG1 {
return "equal"
}
if g1CoversG2 && !g2CoversG1 {
return "g1SubsetG2"
}
return "none"
}
Wait, this is getting complicated. Let me simplify by using two separate helper functions and just call them as needed. The O(n²) performance is fine for typical use cases.
Let me finalize the implementation and write the code.
Actually, I realize the groupCovered function can be simpler. Let me just use the existing covers function from group_dedup.go, but without the index-based tiebreak.
Wait, the existing covers function is:
func covers(outer, inner *CodeLocation, iOuter, iInner int) bool {
if outer.FilePath != inner.FilePath {
return false
}
if outer.StartLine > inner.StartLine || outer.EndLine < inner.EndLine {
return false
}
if outer.StartLine == inner.StartLine && outer.EndLine == inner.EndLine {
return iOuter < iInner
}
return true
}
This function is designed for intra-group dedup where indices matter for the tiebreak. For inter-group dedup, I don't need the tiebreak because the groups have different IDs (they're different groups, so their fragments have different identities even if they have the same location).
Let me create a separate function locCovers that doesn't use the index-based tiebreak:
// locCovers reports whether outer's location covers inner's location.
// Returns true if outer starts at or before inner and ends at or after inner.
func locCovers(outer, inner *CodeLocation) bool {
if outer.FilePath != inner.FilePath {
return false
}
return outer.StartLine <= inner.StartLine && outer.EndLine >= inner.EndLine
}
OK, let me now write the full implementation.
Hmm, actually I just thought of something. What if the groups have different numbers of fragments? For example, one group has 3 fragments and another has 4 fragments. In this case, they can't be duplicates (different number of files).
My groupCovered function already handles this by checking g1.Size != g2.Size.
What if the groups have the same number of fragments but different files? For example:
- G1: {A: 1-10, B: 20-30}
- G2: {C: 1-10, B: 20-30}
groupCovered(G1, G2) would check:
- File A from G1 → fragMap[A] → not found (G2 has no file A, it has C)
→
allCovered = false
So G1 is not a subset of G2. Correct!
OK, the algorithm is correct. Let me implement it now.
One more thing: should I also sort the output? The existing code sorts groups after dedupeStrictSubsetGroupMembers if any groups were modified. I should do the same for dedupeDuplicateGroups.
Actually, let me check: after dedupeStrictSubsetGroupMembers, the groups are already sorted (by the strategy). My dedupeDuplicateGroups function filters groups but doesn't change their order. So the output is still sorted.
But if I suppress some groups, the remaining groups might not have consecutive IDs. Is that an issue? Looking at the existing code, the group IDs are assigned by the strategy (e.g., ConnectedGrouping.GroupClones assigns IDs incrementally). After filtering, the IDs are still unique and incrementing (except for the suppressed ones). That's fine.
OK, let me implement the code now.
Let me also add a test case for the inter-group dedup.
Now, let me write the actual code.
Actually, I realize I should also think about what happens when we have multiple duplicate groups. For example:
- G1: {A: 1-10, B: 20-30}
- G2: {A: 1-10, B: 20-30} // identical to G1
- G3: {A: 1-10, B: 20-30} // identical to G1
All three are identical. My algorithm would suppress G2 and G3, keeping only G1. Correct!
What about:
- G1: {A: 1-10, B: 20-30}
- G2: {A: 1-10, B: 20-30} // identical to G1
- G3: {A: 1-10, B: 20-35} // superset of G1 and G2
My algorithm:
j = 1 (G2):
i = 0 (G1): groupCovered(G2, G1) → true, allCovered = true, anyStrict = false (identical)
→ suppressed[1] = true, break
j = 2 (G3):
i = 0 (G1): groupCovered(G3, G1) → false (G3 is not covered by G1), groupCovered(G1, G3) → true, anyStrict = true
→ suppressed[0] = true
Result: {G3}
Correct! G3 (the superset) is kept, G1 and G2 (the identical subsets) are suppressed.
But wait, is this the right behavior? What if we want to keep the first occurrence (G1) and suppress G3? That would be a different policy.
I think the policy of keeping the superset (larger fragment) is correct because:
- It's more informative (includes more context)
- It's consistent with the intra-group dedup which keeps maximal fragments
If someone wants to keep the first occurrence instead, they can add a flag to the configuration.
OK, I'm confident in the implementation. Let me write the code.
Wait, one more thing. Looking at the existing dedupeStrictSubsetGroupMembers function:
func dedupeStrictSubsetGroupMembers(groups []*CloneGroup, pairs []*ClonePair) groupDedupeResult {
result := groupDedupeResult{
groups: groups,
suppressed: make(map[*CodeFragment]struct{}),
}
if len(groups) == 0 {
return result
}
out := make([]*CloneGroup, 0, len(groups))
var similarities map[string]float64
var cloneTypes map[string]CloneType
metadataReady := false
anyChanged := false
for _, g := range groups {
if g == nil {
continue
}
kept, suppressed := filterMaximalPerFile(g.Fragments)
for fragment := range suppressed {
result.suppressed[fragment] = struct{}{}
}
groupChanged := len(suppressed) > 0
anyChanged = anyChanged || groupChanged
if len(kept) < 2 {
continue
}
g.Fragments = kept
g.Size = len(kept)
if groupChanged {
if !metadataReady {
similarities, cloneTypes = clonePairMetadata(pairs)
metadataReady = true
}
refreshGroupMetadata(g, similarities, cloneTypes)
}
out = append(out, g)
}
if anyChanged {
sortCloneGroups(out)
}
result.groups = out
return result
}
This function takes both groups and pairs as input. The pairs are needed for computing metadata (similarity and clone type) when a group is modified.
For my dedupeDuplicateGroups function, I don't need pairs because I'm only suppressing entire groups, not modifying them. The metadata is already computed by the strategy.
OK, let me write the implementation.
Actually, I just realized: if I suppress a group, should I also filter the clone pairs that belong exclusively to that group? Looking at the existing code, filterClonePairsWithSuppressedMembers filters pairs based on suppressed fragments. But in my case, I'm suppressing groups, not fragments. The fragments in the suppressed group are still valid fragments (they just happen to be subsets of fragments in another group).
So if I suppress group G1 (with fragments A2 and B2), the pair (A2, B2) is still a valid pair. Should it be kept or suppressed?
I think for simplicity, I'll leave it as is: only suppress groups, not pairs. The pair output is separate from the group output.
If users want to filter pairs, they can do so in post-processing.
OK, let me write the final implementation.
Let me now write the code and tests.
I'll add the following to group_dedup.go:
- Helper function
locCovers(outer, inner *CodeLocation) bool
- Helper function
groupIsSubset(g1, g2 *CloneGroup) (bool, bool) - returns (isSubset, isStrictSubset)
- Main function
dedupeDuplicateGroups(groups []*CloneGroup) []*CloneGroup
- Update
groupClonesWithStrategy to call dedupeDuplicateGroups
And I'll add tests to group_dedup_test.go.
Let me start writing the code.
Hmm, actually, I realize that dedupeDuplicateGroups should be called in the same flow as dedupeStrictSubsetGroupMembers. Let me look at the flow again:
func (cd *CloneDetector) groupClonesWithStrategy(strategy GroupingStrategy) {
if strategy == nil {
cd.cloneGroups = []*CloneGroup{}
return
}
dedupeResult := dedupeStrictSubsetGroupMembers(strategy.GroupClones(cd.clonePairs), cd.clonePairs)
cd.cloneGroups = dedupeResult.groups
cd.clonePairs = filterClonePairsWithSuppressedMembers(cd.clonePairs, dedupeResult.suppressed)
}
I'll add the inter-group dedup after the intra-group dedup:
func (cd *CloneDetector) groupClonesWithStrategy(strategy GroupingStrategy) {
if strategy == nil {
cd.cloneGroups = []*CloneGroup{}
return
}
dedupeResult := dedupeStrictSubsetGroupMembers(strategy.GroupClones(cd.clonePairs), cd.clonePairs)
cd.cloneGroups = dedupeDuplicateGroups(dedupeResult.groups)
cd.clonePairs = filterClonePairsWithSuppressedMembers(cd.clonePairs, dedupeResult.suppressed)
}
This way, the flow is:
- Strategy groups pairs into groups
- Intra-group dedup removes strict subsets within groups
- Inter-group dedup removes duplicate groups
- Pair filtering removes pairs whose members are suppressed
OK, let me implement this now.
Wait, actually there's one more thing. The filterClonePairsWithSuppressedMembers function filters pairs based on suppressed fragments. But in the inter-group dedup, we're suppressing entire groups (not individual fragments). So the pairs won't be filtered by this mechanism.
Should I also filter pairs in the inter-group dedup? Let me think...
If group G1 (with fragments A2 and B2) is suppressed, should the pair (A2, B2) also be suppressed?
Well, A2 is a subset of A (from group G2), and B2 is a subset of B (from group G2). So A2 and B2 are "covered" by A and B respectively. The pair (A2, B2) is "covered" by the pair (A, B).
But the pair (A2, B2) is still a real duplication. It's just that the duplication is a subset of the larger duplication between A and B.
I think for simplicity, I'll leave the pairs alone. The pair output is separate from the group output. Users can filter pairs in post-processing if needed.
If I wanted to filter pairs, I'd need to track which fragments belong to suppressed groups and filter pairs that have both fragments in suppressed groups. But that's more complex and might remove useful information.
OK, I'll go with the simple approach: only suppress groups, not pairs.
Let me write the code now.
Actually, wait. I just realized there might be an issue with the existing pair filtering. The filterClonePairsWithSuppressedMembers function filters pairs whose EITHER fragment is in the suppressed set. But in the intra-group dedup, we suppress fragments that are strict subsets within a group. The pair (A2, C) where A2 is suppressed would be filtered, even if C is not suppressed.
This is intentional: if A2 is a subset of A, then pair (A2, C) is "covered" by pair (A, C). So filtering (A2, C) makes sense.
But in the inter-group dedup, we suppress group G1 (with fragments A2 and B2) because G1 is a subset of G2 (with fragments A and B). If we also suppress A2 and B2 (the fragments), then pairs (A2, B2) and any other pairs involving A2 or B2 would be filtered. This would be too aggressive because A2 and B2 might be used in other groups too.
Hmm, but actually, after intra-group dedup, each group has at most one fragment per file. And the fragments in different groups have different line numbers (otherwise they'd be the same fragment object). So A2 (from G1) and A (from G2) are different fragments (different objects with different line numbers).
If we suppress A2 because it's a subset of A, then pairs involving A2 would be filtered. But A2 might also be used in another pair (A2, D) where D is from a completely different group G3. In this case, filtering (A2, D) would remove useful information.
Hmm, but (A2, D) would likely be a subset of (A, D), so filtering (A2, D) in favor of (A, D) makes sense. But if (A, D) is not a valid pair (for whatever reason), then we'd lose the (A2, D) pair.
This is getting complicated. Let me just leave the pairs alone for now. The group dedup is the main concern, and I'll implement it without touching the pairs.
OK, final plan:
- Add
locCovers(outer, inner *CodeLocation) bool to group_dedup.go
- Add
groupIsSubset(g1, g2 *CloneGroup) (bool, bool) that returns (isSubset, isStrictSubset)
- Add
dedupeDuplicateGroups(groups []*CloneGroup) []*CloneGroup
- Call
dedupeDuplicateGroups after dedupeStrictSubsetGroupMembers in groupClonesWithStrategy
- Add tests for the new functionality
Let me implement this.
Hmm, I'm going to go ahead and implement this now. Let me start coding.
Actually, I also want to reconsider the approach. The dedupeStrictSubsetGroupMembers function already handles strict subsets within groups. But the issue is about strict subsets ACROSS groups.
Let me think about whether there's a way to integrate the two dedup steps.
Actually, the two steps are fundamentally different:
- Intra-group dedup: removes fragments from within groups
- Inter-group dedup: removes entire groups that are duplicates
They're separate concerns and should be implemented separately.
OK, let me write the code now.
Let me also think about the test cases I need to add:
- Basic case: two identical groups, keep first one
- Subset case: one group is a strict subset of another, keep the superset
- Transitive case: G1 ⊂ G2 ⊂ G3, keep only G3
- No-op case: neither group is a subset of the other, keep both
- Different files case: groups have different files, keep both
- Complex case: groups with multiple members per file
Let me write these tests.
OK let me just code it now.
<tool_call>
<function=bash_terminal>
<parameter=command>
cat /pyscn/internal/analyzer/group_dedup.go
</tool_call>