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