Skip to content
Snippets Groups Projects
Commit 0a03fbac authored by Radek Puš's avatar Radek Puš
Browse files

index comparsion optimization

parent 8519c0f4
No related branches found
No related tags found
No related merge requests found
...@@ -45,8 +45,9 @@ namespace TransactionComparator ...@@ -45,8 +45,9 @@ namespace TransactionComparator
for (int i = 0; i < Minor.Count; i++) for (int i = 0; i < Minor.Count; i++)
{ {
hintPointer = GetNeigbourPointer(i, hintPointer); hintPointer = GetNeigbourPointer(i, hintPointer);
Major[hintPointer].NeighbourIndexes.Add(i); double distance = Math.Abs(Major[hintPointer].Value - Minor[i].Value);
Minor[i].NeighbourIndexes.Add(hintPointer); Major[hintPointer].SetNeighbourIndex(i, distance);
Minor[i].SetNeighbourIndex(hintPointer, distance);
} }
RemovePairs(); RemovePairs();
} }
...@@ -75,10 +76,10 @@ namespace TransactionComparator ...@@ -75,10 +76,10 @@ namespace TransactionComparator
   
for (int i = 0; i < Minor.Count; i++) for (int i = 0; i < Minor.Count; i++)
{ {
int majorIndex = Minor[i].NeighbourIndexes[0]; int majorIndex = (int) Minor[i].NeighbourIndex;
if (Major[majorIndex] == null || GetMinLenghtID(Major[majorIndex].NeighbourIndexes, Major[majorIndex].Value) != i) if (Major[majorIndex] == null || Major[majorIndex].NeighbourIndex != i)
{ {
Minor[i].NeighbourIndexes.Clear(); Minor[i].Clear();
uniqueMinor.Add(Minor[i]); uniqueMinor.Add(Minor[i]);
continue; continue;
} }
...@@ -89,14 +90,13 @@ namespace TransactionComparator ...@@ -89,14 +90,13 @@ namespace TransactionComparator
} }
   
List<Vertex> uniqueMajor = new List<Vertex>(Major.Count - (Minor.Count - uniqueMinor.Count) + 1); List<Vertex> uniqueMajor = new List<Vertex>(Major.Count - (Minor.Count - uniqueMinor.Count) + 1);
uniqueMinor.TrimExcess();
Minor = uniqueMinor; Minor = uniqueMinor;
   
for (int i = 0; i < Major.Count; i++) for (int i = 0; i < Major.Count; i++)
{ {
if (Major[i] == null) if (Major[i] == null)
continue; continue;
Major[i].NeighbourIndexes.Clear(); Major[i].Clear();
uniqueMajor.Add(Major[i]); uniqueMajor.Add(Major[i]);
} }
   
...@@ -124,24 +124,6 @@ namespace TransactionComparator ...@@ -124,24 +124,6 @@ namespace TransactionComparator
} }
   
#region helper methods #region helper methods
private int GetMinLenghtID(List<int> pointers, double value)
{
double min = Math.Abs(Minor[pointers[0]].Value - value);
int bestPointer = 0;
for (int i = 1; i < pointers.Count; i++)
{
double length = Math.Abs(Minor[i].Value - value);
if (length < min)
{
min = length;
bestPointer = i;
}
}
return bestPointer;
}
private int IncreaseLength(int minorPointer, int majorHintPointer) private int IncreaseLength(int minorPointer, int majorHintPointer)
{ {
double minorValue = Minor[minorPointer].Value; double minorValue = Minor[minorPointer].Value;
......
...@@ -11,15 +11,18 @@ namespace TransactionComparator.Logic ...@@ -11,15 +11,18 @@ namespace TransactionComparator.Logic
   
public double Value { get; set; } public double Value { get; set; }
   
public Vertex Neighbour; public int? NeighbourIndex { get; private set; }
private double NeighbourDistance;
   
public List<int> NeighbourIndexes = new List<int>(); public void Clear()
public int NeighbourIndex; {
NeighbourIndex = null;
NeighbourDistance = 0;
}
   
public Vertex(IElement e) public Vertex(IElement e)
{ {
Value = e.Value; Value = e.Value;
} }
   
public int CompareTo(object obj) public int CompareTo(object obj)
...@@ -27,7 +30,7 @@ namespace TransactionComparator.Logic ...@@ -27,7 +30,7 @@ namespace TransactionComparator.Logic
if (!(obj is IElement)) if (!(obj is IElement))
throw new ArgumentException("Argument not allowed"); throw new ArgumentException("Argument not allowed");
   
IElement another = (IElement) obj; IElement another = (IElement)obj;
if (Value < another.Value) if (Value < another.Value)
return -1; return -1;
   
...@@ -36,5 +39,16 @@ namespace TransactionComparator.Logic ...@@ -36,5 +39,16 @@ namespace TransactionComparator.Logic
   
return 0; return 0;
} }
public bool SetNeighbourIndex(int index, double value)
{
if (NeighbourIndex != null && NeighbourDistance <= value)
return false;
NeighbourIndex = index;
NeighbourDistance = value;
return true;
}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment