NCBI C++ ToolKit
links.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: links.cpp 71954 2016-04-07 17:48:29Z boratyng $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's offical duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================*/
25 
26 /*****************************************************************************
27 
28 File name: links.cpp
29 
30 Author: Greg Boratyn
31 
32 Contents: Implementation of CLinks
33 
34 ******************************************************************************/
35 
36 #include <ncbi_pch.hpp>
37 #include <algo/cobalt/links.hpp>
38 
39 /// @file links.cpp
40 /// Implementation of CLinks
41 
43 BEGIN_SCOPE(cobalt)
44 
45 
46 // Compare links based on node indexes
48  const CLinks::SLink* link2)
49 {
50  if (link1->first == link2->first) {
51  return link1->second < link2->second;
52  }
53 
54  return link1->first < link2->first;
55 }
56 
58 {}
59 
60 void CLinks::AddLink(int first, int second, double weight)
61 {
62  // make the link nodes ordered
63  if (first > second) {
64  swap(first, second);
65  }
66  if (second >= (int)m_NumElements) {
67  NCBI_THROW(CLinksException, eInvalidNode, "Adding node with index "
68  " larger than number of elements attempted");
69  }
70  m_Links.push_back(SLink(first, second, weight));
71 
72  m_NumLinks++;
73  if (weight > m_MaxWeight) {
75  }
76 
77  // adding a new link makes the list unsorted
78  m_IsSorted = false;
79 }
80 
81 bool CLinks::IsLink(int first, int second) const
82 {
83  if (!m_IsSorted) {
84  NCBI_THROW(CLinksException, eUnsortedLinks, "Links must be sorted "
85  "before checks for links can be made");
86  }
87  if (first >= (int)m_NumElements || second >= (int)m_NumElements) {
88  NCBI_THROW(CLinksException, eInvalidNode, "Adding node with index "
89  " larger than number of elements attempted");
90  }
91 
92  if (first > second) {
93  swap(first, second);
94  }
95 
96  return x_IsLinkPtr(first, second);
97 }
98 
99 bool CLinks::IsLink(const vector<int>& elems1, const vector<int>& elems2,
100  double& dist) const
101 {
102  if (!m_IsSorted) {
103  NCBI_THROW(CLinksException, eUnsortedLinks, "Links must be sorted "
104  "before checks for links can be made");
105  }
106 
107  double sum = 0.0;
108  ITERATE (vector<int>, it1, elems1) {
109  ITERATE (vector<int>, it2, elems2) {
110  const SLink* link = x_GetLink(*it1, *it2);
111  if (!link) {
112  return false;
113  }
114  else {
115  sum += link->weight;
116  }
117  }
118  }
119 
120  dist = sum / ((double)(elems1.size() * elems2.size()));
121  return true;
122 }
123 
124 void CLinks::Sort(void)
125 {
126  // sort according to weights
127  m_Links.sort();
128  m_IsSorted = true;
129 
130  // initialize list of link pointers
131  x_InitLinkPtrs();
132 }
133 
134 
136 {
137  m_LinkPtrs.clear();
138 
139  // create list of link pointers
140  NON_CONST_ITERATE (list<SLink>, it, m_Links) {
141  m_LinkPtrs.push_back(&*it);
142  }
143 
144  // sort by node indexes
146 }
147 
148 bool CLinks::x_IsLinkPtr(int first, int second) const
149 {
150  _ASSERT(!m_LinkPtrs.empty());
151  _ASSERT(first <= second);
152 
153  SLink link(first, second, 0.0);
154  return binary_search(m_LinkPtrs.begin(), m_LinkPtrs.end(), &link,
156 }
157 
158 const CLinks::SLink* CLinks::x_GetLink(int first, int second) const
159 {
160  _ASSERT(!m_LinkPtrs.empty());
161 
162  if (first > second) {
163  swap(first, second);
164  }
165 
166  SLink link(first, second, 0.0);
167  vector<SLink*>::const_iterator it = lower_bound(m_LinkPtrs.begin(),
168  m_LinkPtrs.end(),
169  &link,
171 
172  if (it != m_LinkPtrs.end() && (*it)->first == first
173  && (*it)->second == second) {
174 
175  return *it;
176  }
177 
178  return NULL;
179 }
180 
181 
182 END_SCOPE(cobalt)
#define static
Exceptions for CLinks class.
Definition: links.hpp:199
static DLIST_TYPE *DLIST_NAME() first(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:46
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define NON_CONST_ITERATE(Type, Var, Cont)
Non constant version of ITERATE macro.
Definition: ncbimisc.hpp:822
void swap(NCBI_NS_NCBI::pair_base_member< T1, T2 > &pair1, NCBI_NS_NCBI::pair_base_member< T1, T2 > &pair2)
Definition: ncbimisc.hpp:1508
#define NULL
Definition: ncbistd.hpp:225
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:75
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
n font weight
constexpr auto sort(_Init &&init)
#define _ASSERT
#define const
Definition: zconf.h:232
Modified on Sun Jun 16 04:35:11 2024 by modify_doxy.py rev. 669887