File: additive.c

package info (click to toggle)
deal 3.1.9-12
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,552 kB
  • sloc: ansic: 5,224; cpp: 4,186; tcl: 3,125; makefile: 200; sh: 10
file content (212 lines) | stat: -rw-r--r-- 6,292 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * additive.c - Quick way to define integer additive holding procedures
 *         This is old code, somewhat superceded by "holdingProc"
 *         procedures
 *
 * Copyright (C) 1996-2001, Thomas Andrews
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * This file encapsulates a specific class of bridge functions, namely,
 * "additive" functions.  An additive function is one that
 * is computed suit-by-suit, with the total equalling the sum
 * of the values for each suit.
 *
 * Examples include:
 *
 *        hcp        - high card points
 *        controls   - A=2, K=1
 *        other "vectors" - AKQ-points, etc
 *        losers     - the losing trick count
 *
 * All of these functions have the same interface, namely, they
 * can be called suit-by-suit or for the entire hand.  For example:
 *
 *        [hcp south] computes the total hcp for south
 *        [hcp south spades] computes the hcp held by south in spades
 * 
 * Additive functions are created with five parameters:
 *    interp - Tcl interpreter where the function is to be defined
 *    name   - The name for the additive function
 *    func   - ptr to function of type 
 *              "int func(int hand, int suit, void *data)"
 *    data   - Arbitrary data used by "func"
 *    freefunc - Function for freeing the data, ignored if NULL
 *
 * Note, additive functions are mostly superceded by the holdingProc
 * code in holdings.c. 
 *
 * Additive functions only return integers.
 *
 */


#include "deal.h"
#include "additive.h"
#include "dealtypes.h"


/* Data structure for holding information about an additive function */
typedef struct additivecounter {
  int (*func) PROTO((int /* holding */, void * /* data */));
  void *data;
  Tcl_CmdDeleteProc *freefunc;
} *AdditiveFunction;

#define AddFuncCall(addfunc,holding) \
        (addfunc)->func(holding,(addfunc)->data)

static AdditiveFunction newAdditiveFunction(func,data,freefunc)
     int (*func) PROTO((int /* holding */, void * /* data */));
     Tcl_CmdDeleteProc *freefunc;
     void *data;
{
  AdditiveFunction result=(AdditiveFunction) 
    Tcl_Alloc(sizeof(struct additivecounter));
  if (result==NULL) { return NULL; }

  result->func=func;
  result->data=data;
  result->freefunc=freefunc;
  return result;
}

static void deleteAdditiveFunction(ClientData ptr)
{
  AdditiveFunction addf=(AdditiveFunction)ptr;
  if (addf->freefunc!=0) {
    (addf->freefunc)(addf->data);
  }
  Tcl_Free((void*)ptr);
}

/*
 * This is the procedure as called from Tcl
 */
int tcl_count_additive( TCLOBJ_PARAMS ) TCLOBJ_DECL
{
  static int subCmdInit=1,
    handSubCmd=-1,
    holdingSubCmd=-1;
  int hand;
  int suit;
  AdditiveFunction addfunc=(AdditiveFunction)cd;
  int total=0;
  int i,subCmd;

  if (subCmdInit) {
    handSubCmd=Keyword_addKey("hand");
    holdingSubCmd=Keyword_addKey("holding");
    subCmdInit=0;
  }

  if (objc==1) {
    char *name=Tcl_GetString(objv[0]);
    Tcl_AppendResult(interp, "Usage Error: No args.  Us either:\n\t",
                     name," <handname> [ <suitname> ... ]\nor\n\t",
		     name," hand <hand> [ <suitname> ...]\nor\n\t",
                     name," holding <holding> [<holding> <holding> ...]",
                     (char *)NULL);
    return TCL_ERROR;
  }

  if (NOSEAT!=(hand=getHandNumFromObj(interp,objv[1]))) {

    /* First argument is a hand name - hand is set to the hand number */

    FINISH(hand); /* Make sure the hand is dealt */

    if (objc==2) {
      for (suit=0; suit<4; suit++) {
        total += AddFuncCall(addfunc,globalDeal.hand[hand].suit[suit]);
      } 
    } else {
      for (i=2; i<objc; i++) {
        suit=getSuitNumFromObj(interp,objv[i]);
        if (suit==NOSUIT) {
	  char *badparam=Tcl_GetString(objv[i]);
	  Tcl_AppendResult(interp,Tcl_GetString(objv[0]),": Got ", badparam, 
			   " when expecting a suit name",NULL);
	  return TCL_ERROR;
	}
        total += AddFuncCall(addfunc,globalDeal.hand[hand].suit[suit]);
      }
    }
    Tcl_SetObjResult(interp,Tcl_NewIntObj(total));
    return TCL_OK;
  }

  subCmd=Keyword_getIdFromObj(interp,objv[1]);

  if (subCmd==handSubCmd) {
    int total=0,suit,hnum[4];

    if (objc!=3) {
       Tcl_WrongNumArgs(interp,2,objv,"<hand>");
       return TCL_ERROR;
    }

    if (TCL_OK!=getHandHoldingsFromObj(interp,objv[2],hnum)) {
      Tcl_AppendResult(interp,"\nError: '",Tcl_GetString(objv[2]),
                              "' is not a proper hand",NULL);
      return TCL_ERROR;
    }
    for (suit=0; suit<4; suit++) {
      total += AddFuncCall(addfunc,hnum[suit]);
    }
    Tcl_SetObjResult(interp,Tcl_NewIntObj(total));
    return TCL_OK;
  }

  if (subCmd==holdingSubCmd) {
    int total=0;
    for (i=2; i<objc; i++) {
      int holding=getHoldingNumFromObj(interp,objv[i]);
      if (holding<0) { 
	Tcl_AppendResult(interp,"invalid holding ",
			 Tcl_GetString(objv[i]),NULL);
			 
	return TCL_ERROR;
      }
      total += AddFuncCall(addfunc,holding);
    }
    Tcl_SetObjResult(interp,Tcl_NewIntObj(total));
    return TCL_OK;
  }

  {
    Tcl_AppendResult(interp,
		     "Nonexistant hand name '",Tcl_GetString(objv[1]),
		     "' used with ", Tcl_GetString(objv[0])," command",(char *)NULL);
    return TCL_ERROR;
  }


}

void *tcl_create_additive(interp,name,func,data,freefunc) 
     Tcl_Interp *interp;
     char *name;
     int (*func) PROTO((int /* holding */, void * /* data */));
     void *data;
     Tcl_CmdDeleteProc freefunc;
{
  AdditiveFunction af=newAdditiveFunction(func,data,freefunc);
  Tcl_CreateObjCommand(interp,name,tcl_count_additive,(ClientData)af,
		       deleteAdditiveFunction);
  return (void *)af;
}