alpar@9: /* glpapi03.c (row and column searching routines) */ alpar@9: alpar@9: /*********************************************************************** alpar@9: * This code is part of GLPK (GNU Linear Programming Kit). alpar@9: * alpar@9: * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, alpar@9: * 2009, 2010, 2011 Andrew Makhorin, Department for Applied Informatics, alpar@9: * Moscow Aviation Institute, Moscow, Russia. All rights reserved. alpar@9: * E-mail: . alpar@9: * alpar@9: * GLPK is free software: you can redistribute it and/or modify it alpar@9: * under the terms of the GNU General Public License as published by alpar@9: * the Free Software Foundation, either version 3 of the License, or alpar@9: * (at your option) any later version. alpar@9: * alpar@9: * GLPK is distributed in the hope that it will be useful, but WITHOUT alpar@9: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY alpar@9: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public alpar@9: * License for more details. alpar@9: * alpar@9: * You should have received a copy of the GNU General Public License alpar@9: * along with GLPK. If not, see . alpar@9: ***********************************************************************/ alpar@9: alpar@9: #include "glpapi.h" alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_create_index - create the name index alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_create_index(glp_prob *lp); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_create_index creates the name index for the alpar@9: * specified problem object. The name index is an auxiliary data alpar@9: * structure, which is intended to quickly (i.e. for logarithmic time) alpar@9: * find rows and columns by their names. alpar@9: * alpar@9: * This routine can be called at any time. If the name index already alpar@9: * exists, the routine does nothing. */ alpar@9: alpar@9: void glp_create_index(glp_prob *lp) alpar@9: { GLPROW *row; alpar@9: GLPCOL *col; alpar@9: int i, j; alpar@9: /* create row name index */ alpar@9: if (lp->r_tree == NULL) alpar@9: { lp->r_tree = avl_create_tree(avl_strcmp, NULL); alpar@9: for (i = 1; i <= lp->m; i++) alpar@9: { row = lp->row[i]; alpar@9: xassert(row->node == NULL); alpar@9: if (row->name != NULL) alpar@9: { row->node = avl_insert_node(lp->r_tree, row->name); alpar@9: avl_set_node_link(row->node, row); alpar@9: } alpar@9: } alpar@9: } alpar@9: /* create column name index */ alpar@9: if (lp->c_tree == NULL) alpar@9: { lp->c_tree = avl_create_tree(avl_strcmp, NULL); alpar@9: for (j = 1; j <= lp->n; j++) alpar@9: { col = lp->col[j]; alpar@9: xassert(col->node == NULL); alpar@9: if (col->name != NULL) alpar@9: { col->node = avl_insert_node(lp->c_tree, col->name); alpar@9: avl_set_node_link(col->node, col); alpar@9: } alpar@9: } alpar@9: } alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_find_row - find row by its name alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * int glp_find_row(glp_prob *lp, const char *name); alpar@9: * alpar@9: * RETURNS alpar@9: * alpar@9: * The routine glp_find_row returns the ordinal number of a row, alpar@9: * which is assigned (by the routine glp_set_row_name) the specified alpar@9: * symbolic name. If no such row exists, the routine returns 0. */ alpar@9: alpar@9: int glp_find_row(glp_prob *lp, const char *name) alpar@9: { AVLNODE *node; alpar@9: int i = 0; alpar@9: if (lp->r_tree == NULL) alpar@9: xerror("glp_find_row: row name index does not exist\n"); alpar@9: if (!(name == NULL || name[0] == '\0' || strlen(name) > 255)) alpar@9: { node = avl_find_node(lp->r_tree, name); alpar@9: if (node != NULL) alpar@9: i = ((GLPROW *)avl_get_node_link(node))->i; alpar@9: } alpar@9: return i; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_find_col - find column by its name alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * int glp_find_col(glp_prob *lp, const char *name); alpar@9: * alpar@9: * RETURNS alpar@9: * alpar@9: * The routine glp_find_col returns the ordinal number of a column, alpar@9: * which is assigned (by the routine glp_set_col_name) the specified alpar@9: * symbolic name. If no such column exists, the routine returns 0. */ alpar@9: alpar@9: int glp_find_col(glp_prob *lp, const char *name) alpar@9: { AVLNODE *node; alpar@9: int j = 0; alpar@9: if (lp->c_tree == NULL) alpar@9: xerror("glp_find_col: column name index does not exist\n"); alpar@9: if (!(name == NULL || name[0] == '\0' || strlen(name) > 255)) alpar@9: { node = avl_find_node(lp->c_tree, name); alpar@9: if (node != NULL) alpar@9: j = ((GLPCOL *)avl_get_node_link(node))->j; alpar@9: } alpar@9: return j; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_delete_index - delete the name index alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_delete_index(glp_prob *lp); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_delete_index deletes the name index previously alpar@9: * created by the routine glp_create_index and frees the memory alpar@9: * allocated to this auxiliary data structure. alpar@9: * alpar@9: * This routine can be called at any time. If the name index does not alpar@9: * exist, the routine does nothing. */ alpar@9: alpar@9: void glp_delete_index(glp_prob *lp) alpar@9: { int i, j; alpar@9: /* delete row name index */ alpar@9: if (lp->r_tree != NULL) alpar@9: { for (i = 1; i <= lp->m; i++) lp->row[i]->node = NULL; alpar@9: avl_delete_tree(lp->r_tree), lp->r_tree = NULL; alpar@9: } alpar@9: /* delete column name index */ alpar@9: if (lp->c_tree != NULL) alpar@9: { for (j = 1; j <= lp->n; j++) lp->col[j]->node = NULL; alpar@9: avl_delete_tree(lp->c_tree), lp->c_tree = NULL; alpar@9: } alpar@9: return; alpar@9: } alpar@9: alpar@9: /* eof */