home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / clisp-li.000 / clisp-li / clisp-1996-07-22 / queens / callqueens.c next >
Encoding:
C/C++ Source or Header  |  1996-07-21  |  2.1 KB  |  83 lines

  1. /*
  2.  * Interface to the queens() function
  3.  */
  4.  
  5. #include "clisp.h"
  6.  
  7. #define nmax 100
  8.  
  9. /* extern declaration */
  10. extern unsigned int queens (unsigned int n);
  11.  
  12. /* (USER::QUEENS n) returns the number of solutions to the n-queens problem.
  13.  * n ought to be an integer > 0, <= nmax. Otherwise it returns NIL.
  14.  */
  15. LISPFUNN(queens,1)
  16. { /*
  17.    * No garbage collection is a problem. So we get the argument from the
  18.    * STACK immediately.
  19.    */
  20.   var object arg = popSTACK(); /* clean up STACK at the same time */
  21.   /*
  22.    * If arg is an integer > 0, <= 100, ist must be a nonnegative fixnum.
  23.    * We do the argument check in two steps: 1. check whether arg is a
  24.    * nonnegative fixnum. 2. Extract its value. 3. Check its value.
  25.    */
  26.   if (!posfixnump(arg)) goto bad_arg;
  27.  {var uintL n = posfixnum_to_L(arg);
  28.   if (!(n>0 && n<=nmax)) goto bad_arg;
  29.  
  30.   /* Arguments are checked. Do our job: */
  31.   { var uint32 result;
  32.     begin_call();
  33.     result = queens(n); /* call external function */
  34.     end_call();
  35.     /*
  36.      * Assume result is >=0 and <2^32 (which is guaranteed by the type
  37.      * of problem we have and the amount of time queens() may have run).
  38.      * So an `uint32' is enough, and the following call is appropriate.
  39.      */
  40.     value1 = uint32_to_I(result); /* convert result to nonnegative integer */
  41.     mv_count=1; /* no "multiple" values */
  42.   }
  43.   return;
  44.  }
  45.  
  46.   bad_arg:
  47.     /* We could issue an error. We prefer to return NIL here. */
  48.     value1 = NIL; mv_count=1; return;
  49. }
  50.  
  51.  
  52. /* linkable module for CLISP */
  53.  
  54. #undef LISPFUN
  55. #define LISPFUN LISPFUN_F
  56. #undef LISPSYM
  57. #define LISPSYM(name,printname,package)  { package, printname },
  58.  
  59. subr_ module__queens__subr_tab [1] = {
  60.   LISPFUNN(queens,1)
  61. };
  62.  
  63. uintC module__queens__subr_tab_size = 1;
  64.  
  65. subr_initdata module__queens__subr_tab_initdata[1] = {
  66.   LISPSYM(queens,"QUEENS","USER")
  67. };
  68.  
  69. object module__queens__object_tab [1];
  70.  
  71. uintC module__queens__object_tab_size = 0;
  72.  
  73. object_initdata module__queens__object_tab_initdata[1];
  74.  
  75. void module__queens__init_function_1(module)
  76.   var module_* module;
  77.   { }
  78.  
  79. void module__queens__init_function_2(module)
  80.   var module_* module;
  81.   { }
  82.  
  83.