source: PSPA/madxPSPA/tools/numdiff/src/constraint.h @ 430

Last change on this file since 430 was 430, checked in by touze, 11 years ago

import madx-5.01.00

File size: 2.6 KB
Line 
1#ifndef CONSTRAINT_H
2#define CONSTRAINT_H
3
4/*
5 o---------------------------------------------------------------------o
6 |
7 | Numdiff
8 |
9 | Copyright (c) 2012+ laurent.deniau@cern.ch
10 | Gnu General Public License
11 |
12 o---------------------------------------------------------------------o
13 
14   Purpose:
15     create constraints content
16     print, scan constraints from file
17 
18 o---------------------------------------------------------------------o
19*/
20
21#include <stdio.h>
22#include <string.h>
23#include "slice.h"
24#include "error.h"
25
26// ----- constants
27
28enum eps_cmd {
29  eps_invalid =   0u,  // invalid command
30
31// must be firsts (weak commands)
32  eps_dig     =   1u,  // relative input eps
33  eps_rel     =   2u,  // relative eps
34  eps_abs     =   4u,  // absolute eps
35  eps_equ     =   8u,  // equal string
36  eps_ign     =  16u,  // ignore value
37
38// intermediate (in between commands)
39  eps_omit    =  32u,  // omit indentifier
40
41// must be lasts (strong commands)
42  eps_skip    =  64u,  // skip line
43  eps_goto    = 128u,  // goto line
44  eps_last,
45
46// unions
47  eps_dra     =  eps_dig|eps_rel|eps_abs
48};
49
50// ----- types
51
52struct eps {
53  enum eps_cmd cmd;
54  bool   either;
55  double dig, rel, abs;
56  char   tag[48];
57};
58
59struct constraint {
60  struct slice row;
61  struct slice col;
62  struct eps   eps;
63  int          line;
64};
65
66// ----- interface
67
68#define T struct constraint
69
70static inline struct eps
71eps_init(enum eps_cmd cmd, double val)
72{
73  ensure(cmd > eps_invalid && cmd < eps_last, "invalid eps command");
74  return (struct eps) { cmd, 0, cmd&eps_dig ? val : 0, cmd&eps_rel ? val : 0, cmd&eps_abs ? val : 0, {0} };
75}
76
77static inline struct eps
78eps_initNum(enum eps_cmd cmd, bool either, double dig, double rel, double abs)
79{
80  ensure(cmd > eps_invalid && cmd < eps_last, "invalid eps command");
81  return (struct eps) { cmd, either, dig, rel, abs, {0} };
82}
83
84static inline struct eps
85eps_initTag(enum eps_cmd cmd, const char *tag)
86{
87  ensure((cmd & eps_goto) || (cmd & eps_omit), "invalid eps goto or omit command");
88  struct eps eps = (struct eps) { .cmd = cmd };
89  enum { sz = sizeof eps.tag };
90  strncpy(eps.tag, tag, sz); eps.tag[sz-1] = 0;
91  return eps;
92}
93
94static inline const char*
95eps_cstr(enum eps_cmd cmd)
96{
97  extern const char * const eps_cmd_cstr[];
98  return cmd > eps_invalid && cmd < eps_last && eps_cmd_cstr[cmd]
99         ? eps_cmd_cstr[cmd]
100         : eps_cmd_cstr[eps_invalid];
101}
102
103static inline T
104constraint_init(const struct slice row, const struct slice col, const struct eps eps, int line)
105{
106  return (T){ row, col, eps, line };
107}
108
109void constraint_print(const T* cst, FILE *out);
110void constraint_scan (      T* cst, FILE *in, int *row);
111
112#undef T
113
114#endif
115
Note: See TracBrowser for help on using the repository browser.