source: trunk/Couenne/src/readnl/invmap.cpp @ 79

Last change on this file since 79 was 79, checked in by pbelotti, 11 years ago

name fixing. Trying to fix a crippling cast conversion for function pointer comparison (dangerous...)

File size: 2.1 KB
Line 
1/*
2 * Name: invmap.cpp
3 * Author: Pietro Belotti
4 * Purpose: create a bijection between ASL's efunc and integer to
5 *          inversely map e->op fields into constant operators
6 *
7 * (C) Carnegie-Mellon University, 2006.
8 * This file is licensed under the Common Public License (CPL)
9 */
10
11#include <stdlib.h>
12
13#include "asl.h"
14#include "opcode.hd"
15#include "nlp.h"
16#include "r_opn.hd"
17
18#if defined(_MSC_VER)
19typedef void * fcn_ptr;
20#else
21typedef long int fcn_ptr;
22#endif
23
24
25/* couples an ASL function pointer with the relative operator constant */
26
27typedef struct {
28  efunc *fp;
29  int    op;
30} AslCouPair;
31
32
33/* compare two AslCoupair's, used in qsort and bsearch below */
34
35/* AW: 2007-06-11: changed b/c of problems with MSVC++ */
36/* inline int pair_compare (const void *p1, const void *p2) { */
37static int pair_compare (const void *p1, const void *p2) {
38
39  /* FIX! weak cast for 64 bit machines */
40
41  register fcn_ptr f1 = (fcn_ptr) (((AslCouPair *) p1) -> fp); 
42  register fcn_ptr f2 = (fcn_ptr) (((AslCouPair *) p2) -> fp); 
43
44  if      (f1 < f2) return -1;
45  else if (f1 > f2) return  1;
46  else return 0;
47}
48
49
50/* array of pairs (efunc2*, int) that relates all operators */
51
52AslCouPair opmap [N_OPS];
53
54
55/* binary search to get operator number from its efunc2* (the type of e->op) */
56
57int getOperator (efunc *f) {
58
59  static char first_call = 1;
60  AslCouPair key, *res;
61
62  /* FIX cast fo 64 bit machines */
63
64  if (((fcn_ptr) f <  N_OPS) && 
65      ((fcn_ptr) f > -N_OPS))
66    return (fcn_ptr) f;
67
68  key.fp = f;
69
70  if (first_call) { /* opmap is still empty, fill it using values from r_ops [] */
71
72    register int i=0;
73    register AslCouPair *ops = opmap;
74
75    /* fill opmap vector with inverse correspondence pairs efunc -> int */
76    while (i<N_OPS) {
77      ops -> fp = r_ops [ops -> op = i++];
78      ops++;
79    }
80
81    /* sort opmap for later use with bsearch */
82    qsort (opmap, N_OPS, sizeof (AslCouPair), pair_compare);
83    first_call = 0;
84  }
85
86  /* find int operator through binary search */
87  res = (AslCouPair *) bsearch (&key, opmap, N_OPS, sizeof (AslCouPair), pair_compare);
88
89  if (!res) 
90    return -1;
91
92  return res -> op;
93}
Note: See TracBrowser for help on using the repository browser.