home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / bc-1.03-base.tgz / bc-1.03-base.tar / fsf / bc / dc-array.c < prev    next >
C/C++ Source or Header  |  1994-10-31  |  3KB  |  109 lines

  1. /* 
  2.  * implement arrays for dc
  3.  *
  4.  * Copyright (C) 1994 Free Software Foundation, Inc.
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2, or (at your option)
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, you can either send email to this
  18.  * program's author (see below) or write to: The Free Software Foundation,
  19.  * Inc.; 675 Mass Ave. Cambridge, MA 02139, USA.
  20.  */
  21.  
  22. /* This module is the only one that knows what arrays look like. */
  23.  
  24. #include "config.h"
  25.  
  26. #include <stdio.h>    /* "dc-proto.h" wants this */
  27. #ifdef HAVE_STDLIB_H
  28. /* get size_t definition from "almost ANSI" compiling environments. */
  29. #include <stdlib.h>
  30. #endif
  31. #include "dc.h"
  32. #include "dc-proto.h"
  33. #include "dc-regdef.h"
  34.  
  35. /* what's most useful: quick access or sparse arrays? */
  36. /* I'll go with sparse arrays for now */
  37. struct dc_array {
  38.     int Index;
  39.     dc_data value;
  40.     struct dc_array *next;
  41. };
  42. typedef struct dc_array dc_array;
  43.  
  44. /* I can find no reason not to place arrays in their own namespace... */
  45. static dc_array *dc_array_register[DC_REGCOUNT];
  46.  
  47.  
  48. /* initialize the arrays to their initial values */
  49. void
  50. dc_array_init DC_DECLVOID()
  51. {
  52.     int i;
  53.  
  54.     for (i=0; i<DC_REGCOUNT; ++i)
  55.         dc_array_register[i] = NULL;
  56. }
  57.  
  58. /* store value into array_id[Index] */
  59. void
  60. dc_array_set DC_DECLARG((array_id, Index, value))
  61.     int array_id DC_DECLSEP
  62.     int Index DC_DECLSEP
  63.     dc_data value DC_DECLEND
  64. {
  65.     dc_array *cur;
  66.     dc_array *prev=NULL;
  67.     dc_array *newentry;
  68.  
  69.     array_id = regmap(array_id);
  70.     cur = dc_array_register[array_id];
  71.     while (cur && cur->Index < Index){
  72.         prev = cur;
  73.         cur = cur->next;
  74.     }
  75.     if (cur && cur->Index == Index){
  76.         if (cur->value.dc_type == DC_NUMBER)
  77.             dc_free_num(&cur->value.v.number);
  78.         else if (cur->value.dc_type == DC_STRING)
  79.             dc_free_str(&cur->value.v.string);
  80.         else
  81.             dc_garbage(" in array", array_id);
  82.         cur->value = value;
  83.     }else{
  84.         newentry = dc_malloc(sizeof *newentry);
  85.         newentry->Index = Index;
  86.         newentry->value = value;
  87.         newentry->next = cur;
  88.         if (prev)
  89.             prev->next = newentry;
  90.         else
  91.             dc_array_register[array_id] = newentry;
  92.     }
  93. }
  94.  
  95. /* retrieve a dup of a value from array_id[Index] */
  96. /* A zero value is returned if the specified value is unintialized. */
  97. dc_data
  98. dc_array_get DC_DECLARG((array_id, Index))
  99.     int array_id DC_DECLSEP
  100.     int Index DC_DECLEND
  101. {
  102.     dc_array *cur;
  103.  
  104.     for (cur=dc_array_register[regmap(array_id)]; cur; cur=cur->next)
  105.         if (cur->Index == Index)
  106.             return dc_dup(cur->value);
  107.     return dc_int2data(0);
  108. }
  109.