source: stable/0.1/Couenne/src/readnl/invmap.cpp @ 81

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

merged changes from Couenne-trunk:80 (fix casts for AMPL function pointers)

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