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