alpar@1
|
1 |
/* glpapi03.c (row and column searching routines) */
|
alpar@1
|
2 |
|
alpar@1
|
3 |
/***********************************************************************
|
alpar@1
|
4 |
* This code is part of GLPK (GNU Linear Programming Kit).
|
alpar@1
|
5 |
*
|
alpar@1
|
6 |
* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
|
alpar@1
|
7 |
* 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
|
alpar@1
|
8 |
* Moscow Aviation Institute, Moscow, Russia. All rights reserved.
|
alpar@1
|
9 |
* E-mail: <mao@gnu.org>.
|
alpar@1
|
10 |
*
|
alpar@1
|
11 |
* GLPK is free software: you can redistribute it and/or modify it
|
alpar@1
|
12 |
* under the terms of the GNU General Public License as published by
|
alpar@1
|
13 |
* the Free Software Foundation, either version 3 of the License, or
|
alpar@1
|
14 |
* (at your option) any later version.
|
alpar@1
|
15 |
*
|
alpar@1
|
16 |
* GLPK is distributed in the hope that it will be useful, but WITHOUT
|
alpar@1
|
17 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
alpar@1
|
18 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
alpar@1
|
19 |
* License for more details.
|
alpar@1
|
20 |
*
|
alpar@1
|
21 |
* You should have received a copy of the GNU General Public License
|
alpar@1
|
22 |
* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
|
alpar@1
|
23 |
***********************************************************************/
|
alpar@1
|
24 |
|
alpar@1
|
25 |
#include "glpapi.h"
|
alpar@1
|
26 |
|
alpar@1
|
27 |
/***********************************************************************
|
alpar@1
|
28 |
* NAME
|
alpar@1
|
29 |
*
|
alpar@1
|
30 |
* glp_create_index - create the name index
|
alpar@1
|
31 |
*
|
alpar@1
|
32 |
* SYNOPSIS
|
alpar@1
|
33 |
*
|
alpar@1
|
34 |
* void glp_create_index(glp_prob *lp);
|
alpar@1
|
35 |
*
|
alpar@1
|
36 |
* DESCRIPTION
|
alpar@1
|
37 |
*
|
alpar@1
|
38 |
* The routine glp_create_index creates the name index for the
|
alpar@1
|
39 |
* specified problem object. The name index is an auxiliary data
|
alpar@1
|
40 |
* structure, which is intended to quickly (i.e. for logarithmic time)
|
alpar@1
|
41 |
* find rows and columns by their names.
|
alpar@1
|
42 |
*
|
alpar@1
|
43 |
* This routine can be called at any time. If the name index already
|
alpar@1
|
44 |
* exists, the routine does nothing. */
|
alpar@1
|
45 |
|
alpar@1
|
46 |
void glp_create_index(glp_prob *lp)
|
alpar@1
|
47 |
{ GLPROW *row;
|
alpar@1
|
48 |
GLPCOL *col;
|
alpar@1
|
49 |
int i, j;
|
alpar@1
|
50 |
/* create row name index */
|
alpar@1
|
51 |
if (lp->r_tree == NULL)
|
alpar@1
|
52 |
{ lp->r_tree = avl_create_tree(avl_strcmp, NULL);
|
alpar@1
|
53 |
for (i = 1; i <= lp->m; i++)
|
alpar@1
|
54 |
{ row = lp->row[i];
|
alpar@1
|
55 |
xassert(row->node == NULL);
|
alpar@1
|
56 |
if (row->name != NULL)
|
alpar@1
|
57 |
{ row->node = avl_insert_node(lp->r_tree, row->name);
|
alpar@1
|
58 |
avl_set_node_link(row->node, row);
|
alpar@1
|
59 |
}
|
alpar@1
|
60 |
}
|
alpar@1
|
61 |
}
|
alpar@1
|
62 |
/* create column name index */
|
alpar@1
|
63 |
if (lp->c_tree == NULL)
|
alpar@1
|
64 |
{ lp->c_tree = avl_create_tree(avl_strcmp, NULL);
|
alpar@1
|
65 |
for (j = 1; j <= lp->n; j++)
|
alpar@1
|
66 |
{ col = lp->col[j];
|
alpar@1
|
67 |
xassert(col->node == NULL);
|
alpar@1
|
68 |
if (col->name != NULL)
|
alpar@1
|
69 |
{ col->node = avl_insert_node(lp->c_tree, col->name);
|
alpar@1
|
70 |
avl_set_node_link(col->node, col);
|
alpar@1
|
71 |
}
|
alpar@1
|
72 |
}
|
alpar@1
|
73 |
}
|
alpar@1
|
74 |
return;
|
alpar@1
|
75 |
}
|
alpar@1
|
76 |
|
alpar@1
|
77 |
/***********************************************************************
|
alpar@1
|
78 |
* NAME
|
alpar@1
|
79 |
*
|
alpar@1
|
80 |
* glp_find_row - find row by its name
|
alpar@1
|
81 |
*
|
alpar@1
|
82 |
* SYNOPSIS
|
alpar@1
|
83 |
*
|
alpar@1
|
84 |
* int glp_find_row(glp_prob *lp, const char *name);
|
alpar@1
|
85 |
*
|
alpar@1
|
86 |
* RETURNS
|
alpar@1
|
87 |
*
|
alpar@1
|
88 |
* The routine glp_find_row returns the ordinal number of a row,
|
alpar@1
|
89 |
* which is assigned (by the routine glp_set_row_name) the specified
|
alpar@1
|
90 |
* symbolic name. If no such row exists, the routine returns 0. */
|
alpar@1
|
91 |
|
alpar@1
|
92 |
int glp_find_row(glp_prob *lp, const char *name)
|
alpar@1
|
93 |
{ AVLNODE *node;
|
alpar@1
|
94 |
int i = 0;
|
alpar@1
|
95 |
if (lp->r_tree == NULL)
|
alpar@1
|
96 |
xerror("glp_find_row: row name index does not exist\n");
|
alpar@1
|
97 |
if (!(name == NULL || name[0] == '\0' || strlen(name) > 255))
|
alpar@1
|
98 |
{ node = avl_find_node(lp->r_tree, name);
|
alpar@1
|
99 |
if (node != NULL)
|
alpar@1
|
100 |
i = ((GLPROW *)avl_get_node_link(node))->i;
|
alpar@1
|
101 |
}
|
alpar@1
|
102 |
return i;
|
alpar@1
|
103 |
}
|
alpar@1
|
104 |
|
alpar@1
|
105 |
/***********************************************************************
|
alpar@1
|
106 |
* NAME
|
alpar@1
|
107 |
*
|
alpar@1
|
108 |
* glp_find_col - find column by its name
|
alpar@1
|
109 |
*
|
alpar@1
|
110 |
* SYNOPSIS
|
alpar@1
|
111 |
*
|
alpar@1
|
112 |
* int glp_find_col(glp_prob *lp, const char *name);
|
alpar@1
|
113 |
*
|
alpar@1
|
114 |
* RETURNS
|
alpar@1
|
115 |
*
|
alpar@1
|
116 |
* The routine glp_find_col returns the ordinal number of a column,
|
alpar@1
|
117 |
* which is assigned (by the routine glp_set_col_name) the specified
|
alpar@1
|
118 |
* symbolic name. If no such column exists, the routine returns 0. */
|
alpar@1
|
119 |
|
alpar@1
|
120 |
int glp_find_col(glp_prob *lp, const char *name)
|
alpar@1
|
121 |
{ AVLNODE *node;
|
alpar@1
|
122 |
int j = 0;
|
alpar@1
|
123 |
if (lp->c_tree == NULL)
|
alpar@1
|
124 |
xerror("glp_find_col: column name index does not exist\n");
|
alpar@1
|
125 |
if (!(name == NULL || name[0] == '\0' || strlen(name) > 255))
|
alpar@1
|
126 |
{ node = avl_find_node(lp->c_tree, name);
|
alpar@1
|
127 |
if (node != NULL)
|
alpar@1
|
128 |
j = ((GLPCOL *)avl_get_node_link(node))->j;
|
alpar@1
|
129 |
}
|
alpar@1
|
130 |
return j;
|
alpar@1
|
131 |
}
|
alpar@1
|
132 |
|
alpar@1
|
133 |
/***********************************************************************
|
alpar@1
|
134 |
* NAME
|
alpar@1
|
135 |
*
|
alpar@1
|
136 |
* glp_delete_index - delete the name index
|
alpar@1
|
137 |
*
|
alpar@1
|
138 |
* SYNOPSIS
|
alpar@1
|
139 |
*
|
alpar@1
|
140 |
* void glp_delete_index(glp_prob *lp);
|
alpar@1
|
141 |
*
|
alpar@1
|
142 |
* DESCRIPTION
|
alpar@1
|
143 |
*
|
alpar@1
|
144 |
* The routine glp_delete_index deletes the name index previously
|
alpar@1
|
145 |
* created by the routine glp_create_index and frees the memory
|
alpar@1
|
146 |
* allocated to this auxiliary data structure.
|
alpar@1
|
147 |
*
|
alpar@1
|
148 |
* This routine can be called at any time. If the name index does not
|
alpar@1
|
149 |
* exist, the routine does nothing. */
|
alpar@1
|
150 |
|
alpar@1
|
151 |
void glp_delete_index(glp_prob *lp)
|
alpar@1
|
152 |
{ int i, j;
|
alpar@1
|
153 |
/* delete row name index */
|
alpar@1
|
154 |
if (lp->r_tree != NULL)
|
alpar@1
|
155 |
{ for (i = 1; i <= lp->m; i++) lp->row[i]->node = NULL;
|
alpar@1
|
156 |
avl_delete_tree(lp->r_tree), lp->r_tree = NULL;
|
alpar@1
|
157 |
}
|
alpar@1
|
158 |
/* delete column name index */
|
alpar@1
|
159 |
if (lp->c_tree != NULL)
|
alpar@1
|
160 |
{ for (j = 1; j <= lp->n; j++) lp->col[j]->node = NULL;
|
alpar@1
|
161 |
avl_delete_tree(lp->c_tree), lp->c_tree = NULL;
|
alpar@1
|
162 |
}
|
alpar@1
|
163 |
return;
|
alpar@1
|
164 |
}
|
alpar@1
|
165 |
|
alpar@1
|
166 |
/* eof */
|