Skip to content

Commit

Permalink
CountVector: Add abs
Browse files Browse the repository at this point in the history
This is generally useful and will help me evolving the clone detection
routines further.
  • Loading branch information
sils committed Jun 24, 2015
1 parent d0a09fb commit 5ac2ac1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions bears/codeclone_detection/CountVector.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from math import sqrt


class CountVector:
def __init__(self, name, conditions=None, weightings=None):
"""
Expand Down Expand Up @@ -52,6 +55,9 @@ def __len__(self):
def __iter__(self):
return iter(self.count_vector)

def __abs__(self):
return sqrt(sum(x**2 for x in self))

def difference(self, other):
"""
Calculates a normalized difference. This value can be used to indicate
Expand Down
10 changes: 10 additions & 0 deletions bears/tests/codeclone_detection/CountVectorTest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from math import sqrt
import sys

sys.path.insert(0, ".")
Expand Down Expand Up @@ -62,6 +63,15 @@ def test_cloning(self):
self.assertEqual(clone.conditions, uut.conditions)
self.assertEqual(clone.count_vector, [0])

def test_abs(self):
uut = CountVector("varname",
[lambda x: True, lambda x: x])
self.assertEqual(abs(uut), 0)
uut.count_reference(True)
self.assertEqual(abs(uut), sqrt(2))
uut.count_reference(False)
self.assertEqual(abs(uut), sqrt(5))

def check_difference(self, cv1, cv2, expected_difference):
"""
Checks the difference between the given count vectors.
Expand Down

1 comment on commit 5ac2ac1

@fneu
Copy link
Contributor

@fneu fneu commented on 5ac2ac1 Jun 24, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack

Please sign in to comment.