source: branches/Couenne/Couenne/src/readnl/invmap.c @ 582

Last change on this file since 582 was 582, checked in by andreasw, 12 years ago

minor changes to make code compile with MSVC++; reran autotools

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