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