aboutsummaryrefslogtreecommitdiffstats
path: root/test/monniaux/glpk-4.65/src/api/prob3.c
blob: d7edbd3370d959614b7d2a69231f333f3794a509 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* prob3.c (problem row/column searching routines) */

/***********************************************************************
*  This code is part of GLPK (GNU Linear Programming Kit).
*
*  Copyright (C) 2000-2013 Andrew Makhorin, Department for Applied
*  Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
*  reserved. E-mail: <mao@gnu.org>.
*
*  GLPK is free software: you can redistribute it and/or modify it
*  under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  GLPK is distributed in the hope that it will be useful, but WITHOUT
*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
*  License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with GLPK. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************/

#include "env.h"
#include "prob.h"

/***********************************************************************
*  NAME
*
*  glp_create_index - create the name index
*
*  SYNOPSIS
*
*  void glp_create_index(glp_prob *lp);
*
*  DESCRIPTION
*
*  The routine glp_create_index creates the name index for the
*  specified problem object. The name index is an auxiliary data
*  structure, which is intended to quickly (i.e. for logarithmic time)
*  find rows and columns by their names.
*
*  This routine can be called at any time. If the name index already
*  exists, the routine does nothing. */

void glp_create_index(glp_prob *lp)
{     GLPROW *row;
      GLPCOL *col;
      int i, j;
      /* create row name index */
      if (lp->r_tree == NULL)
      {  lp->r_tree = avl_create_tree(avl_strcmp, NULL);
         for (i = 1; i <= lp->m; i++)
         {  row = lp->row[i];
            xassert(row->node == NULL);
            if (row->name != NULL)
            {  row->node = avl_insert_node(lp->r_tree, row->name);
               avl_set_node_link(row->node, row);
            }
         }
      }
      /* create column name index */
      if (lp->c_tree == NULL)
      {  lp->c_tree = avl_create_tree(avl_strcmp, NULL);
         for (j = 1; j <= lp->n; j++)
         {  col = lp->col[j];
            xassert(col->node == NULL);
            if (col->name != NULL)
            {  col->node = avl_insert_node(lp->c_tree, col->name);
               avl_set_node_link(col->node, col);
            }
         }
      }
      return;
}

/***********************************************************************
*  NAME
*
*  glp_find_row - find row by its name
*
*  SYNOPSIS
*
*  int glp_find_row(glp_prob *lp, const char *name);
*
*  RETURNS
*
*  The routine glp_find_row returns the ordinal number of a row,
*  which is assigned (by the routine glp_set_row_name) the specified
*  symbolic name. If no such row exists, the routine returns 0. */

int glp_find_row(glp_prob *lp, const char *name)
{     AVLNODE *node;
      int i = 0;
      if (lp->r_tree == NULL)
         xerror("glp_find_row: row name index does not exist\n");
      if (!(name == NULL || name[0] == '\0' || strlen(name) > 255))
      {  node = avl_find_node(lp->r_tree, name);
         if (node != NULL)
            i = ((GLPROW *)avl_get_node_link(node))->i;
      }
      return i;
}

/***********************************************************************
*  NAME
*
*  glp_find_col - find column by its name
*
*  SYNOPSIS
*
*  int glp_find_col(glp_prob *lp, const char *name);
*
*  RETURNS
*
*  The routine glp_find_col returns the ordinal number of a column,
*  which is assigned (by the routine glp_set_col_name) the specified
*  symbolic name. If no such column exists, the routine returns 0. */

int glp_find_col(glp_prob *lp, const char *name)
{     AVLNODE *node;
      int j = 0;
      if (lp->c_tree == NULL)
         xerror("glp_find_col: column name index does not exist\n");
      if (!(name == NULL || name[0] == '\0' || strlen(name) > 255))
      {  node = avl_find_node(lp->c_tree, name);
         if (node != NULL)
            j = ((GLPCOL *)avl_get_node_link(node))->j;
      }
      return j;
}

/***********************************************************************
*  NAME
*
*  glp_delete_index - delete the name index
*
*  SYNOPSIS
*
*  void glp_delete_index(glp_prob *lp);
*
*  DESCRIPTION
*
*  The routine glp_delete_index deletes the name index previously
*  created by the routine glp_create_index and frees the memory
*  allocated to this auxiliary data structure.
*
*  This routine can be called at any time. If the name index does not
*  exist, the routine does nothing. */

void glp_delete_index(glp_prob *lp)
{     int i, j;
      /* delete row name index */
      if (lp->r_tree != NULL)
      {  for (i = 1; i <= lp->m; i++) lp->row[i]->node = NULL;
         avl_delete_tree(lp->r_tree), lp->r_tree = NULL;
      }
      /* delete column name index */
      if (lp->c_tree != NULL)
      {  for (j = 1; j <= lp->n; j++) lp->col[j]->node = NULL;
         avl_delete_tree(lp->c_tree), lp->c_tree = NULL;
      }
      return;
}

/* eof */