1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /*                                                                           */
3 /*   File....: bound.c                                                       */
4 /*   Name....: Bound value                                                   */
5 /*   Author..: Thorsten Koch                                                 */
6 /*   Copyright by Author, All rights reserved                                */
7 /*                                                                           */
8 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
9 /*
10  * Copyright (C) 2001-2018 by Thorsten Koch <koch@zib.de>
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 3
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
25  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <assert.h>
31 
32 #include <stdbool.h>
33 #include "zimpl/mshell.h"
34 #include "zimpl/mme.h"
35 #include "zimpl/numb.h"
36 #include "zimpl/bound.h"
37 
38 #define BOUND_SID     0x426F756E
39 
40 struct bound
41 {
42    SID
43    BoundType type;
44    Numb*     value;
45 };
46 
bound_new(BoundType type,const Numb * value)47 Bound* bound_new(BoundType type, const Numb* value)
48 {
49    Bound* bound = calloc(1, sizeof(*bound));
50 
51    assert(bound != NULL);
52 
53    bound->type = type;
54 
55    if (bound->type == BOUND_VALUE)
56    {
57       assert(value != NULL);
58 
59       bound->value = numb_copy(value);
60    }
61    SID_set(bound, BOUND_SID);
62 
63    assert(bound_is_valid(bound));
64 
65    return bound;
66 }
67 
bound_free(Bound * bound)68 void bound_free(Bound* bound)
69 {
70    assert(bound_is_valid(bound));
71 
72    if (bound->type == BOUND_VALUE)
73       numb_free(bound->value);
74 
75    SID_del(bound);
76 
77    free(bound);
78 }
79 
bound_is_valid(const Bound * bound)80 bool bound_is_valid(const Bound* bound)
81 {
82    if (bound == NULL || !SID_ok(bound, BOUND_SID)
83       || (bound->type == BOUND_VALUE && bound->value == NULL)
84       || (bound->type != BOUND_VALUE && bound->value != NULL))
85       return false;
86 
87    mem_check(bound);
88 
89    return true;
90 }
91 
bound_copy(const Bound * source)92 Bound* bound_copy(const Bound* source)
93 {
94    assert(bound_is_valid(source));
95 
96    return bound_new(source->type, source->value);
97 }
98 
bound_get_type(const Bound * bound)99 BoundType bound_get_type(const Bound* bound)
100 {
101    assert(bound_is_valid(bound));
102 
103    return bound->type;
104 }
105 
bound_get_value(const Bound * bound)106 const Numb* bound_get_value(const Bound* bound)
107 {
108    assert(bound_is_valid(bound));
109    assert(bound->type == BOUND_VALUE);
110 
111    return bound->value;
112 }
113 
bound_print(FILE * fp,const Bound * bound)114 void bound_print(FILE* fp, const Bound* bound)
115 {
116    switch(bound->type)
117    {
118    case BOUND_INFTY :
119       fprintf(fp, "oo");
120       break;
121    case BOUND_MINUS_INFTY :
122       fprintf(fp, "-oo");
123       break;
124    case BOUND_VALUE :
125       numb_print(fp, bound->value);
126       break;
127    default :
128       abort();
129    }
130 }
131