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

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

trying again compatibility with MSVC

File size: 2.0 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//#define Intcast (int)(long)
19
20/* couples an ASL function pointer with the relative operator constant */
21
22typedef struct {
23  efunc *fp;
24  int    op;
25} AslCouPair;
26
27
28/* compare two AslCoupair's, used in qsort and bsearch below */
29
30/* AW: 2007-06-11: changed b/c of problems with MSVC++ */
31/* inline int pair_compare (const void *p1, const void *p2) { */
32static int pair_compare (const void *p1, const void *p2) {
33
34  /* FIX! weak cast for 64 bit machines */
35
36  register int f1 = Intcast (((AslCouPair *) p1) -> fp); 
37  register int f2 = Intcast (((AslCouPair *) p2) -> fp); 
38
39  if      (f1 < f2) return -1;
40  else if (f1 > f2) return  1;
41  else return 0;
42}
43
44
45/* array of pairs (efunc2*, int) that relates all operators */
46
47AslCouPair opmap [N_OPS];
48
49
50/* binary search to get operator number from its efunc2* (the type of e->op) */
51
52int getOperator (efunc *f) {
53
54  static char first_call = 1;
55  AslCouPair key, *res;
56
57  /* FIX cast fo 64 bit machines */
58
59  if ((Intcast f <  N_OPS) && 
60      (Intcast f > -N_OPS))
61    return Intcast f;
62
63  key.fp = f;
64
65  if (first_call) { /* opmap is still empty, fill it using values from r_ops [] */
66
67    register int i=0;
68    register AslCouPair *ops = opmap;
69
70    /* fill opmap vector with inverse correspondence pairs efunc -> int */
71    while (i<N_OPS) {
72      ops -> fp = r_ops [ops -> op = i++];
73      ops++;
74    }
75
76    /* sort opmap for later use with bsearch */
77    qsort (opmap, N_OPS, sizeof (AslCouPair), pair_compare);
78    first_call = 0;
79  }
80
81  /* find int operator through binary search */
82  res = (AslCouPair *) bsearch (&key, opmap, N_OPS, sizeof (AslCouPair), pair_compare);
83
84  if (!res) 
85    return -1;
86
87  return res -> op;
88}
Note: See TracBrowser for help on using the repository browser.