f47e1dd6256a2b54bcd2ab0ad7c34a9884ab8c6e
[public/netxms.git] / src / libnxmap / vertex.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Network Maps Library
4 ** Copyright (C) 2006, 2007, 2008 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** File: vertex.cpp
21 **
22 **/
23
24 #include "libnxmap.h"
25
26
27 //
28 // Constructor
29 //
30
31 nxmap_Vertex::nxmap_Vertex(DWORD dwId)
32 {
33 m_dwId = dwId;
34 m_posX = 0;
35 m_posY = 0;
36 m_dwNumChilds = 0;
37 m_ppChildList = NULL;
38 m_dwNumParents = 0;
39 m_ppParentList = NULL;
40 }
41
42
43 //
44 // Destructor
45 //
46
47 nxmap_Vertex::~nxmap_Vertex()
48 {
49 safe_free(m_ppChildList);
50 safe_free(m_ppParentList);
51 }
52
53
54 //
55 // Link another vertex as child
56 //
57
58 void nxmap_Vertex::LinkChild(nxmap_Vertex *pVtx)
59 {
60 DWORD i;
61
62 for(i = 0; i < m_dwNumChilds; i++)
63 if (m_ppChildList[i] == pVtx)
64 break; // Already linked
65 if (i == m_dwNumChilds)
66 {
67 m_dwNumChilds++;
68 m_ppChildList = (nxmap_Vertex **)realloc(m_ppChildList, sizeof(nxmap_Vertex *) * m_dwNumChilds);
69 m_ppChildList[i] = pVtx;
70 pVtx->LinkParent(this);
71 }
72 }
73
74
75 //
76 // Unlink child vertex
77 //
78
79 void nxmap_Vertex::UnlinkChild(nxmap_Vertex *pVtx)
80 {
81 DWORD i;
82
83 for(i = 0; i < m_dwNumChilds; i++)
84 if (m_ppChildList[i] == pVtx)
85 {
86 m_dwNumChilds--;
87 memmove(&m_ppChildList[i], &m_ppChildList[i + 1], (m_dwNumChilds - i) * sizeof(nxmap_Vertex *));
88 pVtx->UnlinkParent(this);
89 break;
90 }
91 }
92
93
94 //
95 // Link another vertex as parent
96 //
97
98 void nxmap_Vertex::LinkParent(nxmap_Vertex *pVtx)
99 {
100 DWORD i;
101
102 for(i = 0; i < m_dwNumParents; i++)
103 if (m_ppParentList[i] == pVtx)
104 break; // Already linked
105 if (i == m_dwNumParents)
106 {
107 m_dwNumParents++;
108 m_ppParentList = (nxmap_Vertex **)realloc(m_ppParentList, sizeof(nxmap_Vertex *) * m_dwNumParents);
109 m_ppParentList[i] = pVtx;
110 }
111 }
112
113
114 //
115 // Unlink parent vertex
116 //
117
118 void nxmap_Vertex::UnlinkParent(nxmap_Vertex *pVtx)
119 {
120 DWORD i;
121
122 for(i = 0; i < m_dwNumParents; i++)
123 if (m_ppParentList[i] == pVtx)
124 {
125 m_dwNumParents--;
126 memmove(&m_ppParentList[i], &m_ppParentList[i + 1], (m_dwNumParents - i) * sizeof(nxmap_Vertex *));
127 break;
128 }
129 }
130
131
132 //
133 // Check if link to given vertex exist
134 //
135
136 BOOL nxmap_Vertex::IsParentOf(nxmap_Vertex *pVtx)
137 {
138 DWORD i;
139 BOOL bRet = FALSE;
140
141 for(i = 0; i < m_dwNumChilds; i++)
142 if (m_ppChildList[i] == pVtx)
143 {
144 bRet = TRUE;
145 break;
146 }
147 return bRet;
148 }