glpapi03.c 5.1 KB

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