1 | #include "muscle.h" |
---|
2 | #include "distfunc.h" |
---|
3 | #include <assert.h> |
---|
4 | |
---|
5 | DistFunc::DistFunc() |
---|
6 | { |
---|
7 | m_Dists = 0; |
---|
8 | m_uCount = 0; |
---|
9 | m_uCacheCount = 0; |
---|
10 | m_Names = 0; |
---|
11 | m_Ids = 0; |
---|
12 | } |
---|
13 | |
---|
14 | DistFunc::~DistFunc() |
---|
15 | { |
---|
16 | if (0 != m_Names) |
---|
17 | { |
---|
18 | for (unsigned i = 0; i < m_uCount; ++i) |
---|
19 | free(m_Names[i]); |
---|
20 | } |
---|
21 | delete[] m_Dists; |
---|
22 | delete[] m_Names; |
---|
23 | delete[] m_Ids; |
---|
24 | } |
---|
25 | |
---|
26 | float DistFunc::GetDist(unsigned uIndex1, unsigned uIndex2) const |
---|
27 | { |
---|
28 | return m_Dists[VectorIndex(uIndex1, uIndex2)]; |
---|
29 | } |
---|
30 | |
---|
31 | unsigned DistFunc::GetCount() const |
---|
32 | { |
---|
33 | return m_uCount; |
---|
34 | } |
---|
35 | |
---|
36 | void DistFunc::SetCount(unsigned uCount) |
---|
37 | { |
---|
38 | m_uCount = uCount; |
---|
39 | if (uCount <= m_uCacheCount) |
---|
40 | return; |
---|
41 | delete[] m_Dists; |
---|
42 | m_Dists = new float[VectorLength()]; |
---|
43 | m_Names = new char *[m_uCount]; |
---|
44 | m_Ids = new unsigned[m_uCount]; |
---|
45 | m_uCacheCount = uCount; |
---|
46 | |
---|
47 | memset(m_Names, 0, m_uCount*sizeof(char *)); |
---|
48 | memset(m_Ids, 0xff, m_uCount*sizeof(unsigned)); |
---|
49 | memset(m_Dists, 0, VectorLength()*sizeof(float)); |
---|
50 | } |
---|
51 | |
---|
52 | void DistFunc::SetDist(unsigned uIndex1, unsigned uIndex2, float dDist) |
---|
53 | { |
---|
54 | m_Dists[VectorIndex(uIndex1, uIndex2)] = dDist; |
---|
55 | m_Dists[VectorIndex(uIndex2, uIndex1)] = dDist; |
---|
56 | } |
---|
57 | |
---|
58 | unsigned DistFunc::VectorIndex(unsigned uIndex1, unsigned uIndex2) const |
---|
59 | { |
---|
60 | assert(uIndex1 < m_uCount && uIndex2 < m_uCount); |
---|
61 | return uIndex1*m_uCount + uIndex2; |
---|
62 | } |
---|
63 | |
---|
64 | unsigned DistFunc::VectorLength() const |
---|
65 | { |
---|
66 | return m_uCount*m_uCount; |
---|
67 | } |
---|
68 | |
---|
69 | void DistFunc::SetName(unsigned uIndex, const char szName[]) |
---|
70 | { |
---|
71 | assert(uIndex < m_uCount); |
---|
72 | m_Names[uIndex] = strsave(szName); |
---|
73 | } |
---|
74 | |
---|
75 | void DistFunc::SetId(unsigned uIndex, unsigned uId) |
---|
76 | { |
---|
77 | assert(uIndex < m_uCount); |
---|
78 | m_Ids[uIndex] = uId; |
---|
79 | } |
---|
80 | |
---|
81 | const char *DistFunc::GetName(unsigned uIndex) const |
---|
82 | { |
---|
83 | assert(uIndex < m_uCount); |
---|
84 | return m_Names[uIndex]; |
---|
85 | } |
---|
86 | |
---|
87 | unsigned DistFunc::GetId(unsigned uIndex) const |
---|
88 | { |
---|
89 | assert(uIndex < m_uCount); |
---|
90 | return m_Ids[uIndex]; |
---|
91 | } |
---|
92 | |
---|
93 | void DistFunc::LogMe() const |
---|
94 | { |
---|
95 | Log("DistFunc::LogMe count=%u\n", m_uCount); |
---|
96 | Log(" "); |
---|
97 | for (unsigned i = 0; i < m_uCount; ++i) |
---|
98 | Log(" %7u", i); |
---|
99 | Log("\n"); |
---|
100 | |
---|
101 | Log(" "); |
---|
102 | for (unsigned i = 0; i < m_uCount; ++i) |
---|
103 | Log(" %7.7s", m_Names[i] ? m_Names[i] : ""); |
---|
104 | Log("\n"); |
---|
105 | |
---|
106 | for (unsigned i = 0; i < m_uCount; ++i) |
---|
107 | { |
---|
108 | Log("%4u %10.10s : ", i, m_Names[i] ? m_Names[i] : ""); |
---|
109 | for (unsigned j = 0; j <= i; ++j) |
---|
110 | Log(" %7.4g", GetDist(i, j)); |
---|
111 | Log("\n"); |
---|
112 | } |
---|
113 | } |
---|