home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume28 / m0 / part02 / l_array.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-06  |  3.2 KB  |  159 lines

  1. /*
  2.     array.c
  3. */
  4. /*  Copyright (c) 1994 Christian F. Tschudin. All rights reserved.
  5.  
  6.     Distributed under the terms of the GNU General Public License
  7.     version 2 of june 1991 as published by the Free Software
  8.     Foundation, Inc.
  9.  
  10.              This file is part of M0.
  11.  
  12. M0 is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY.  No author or distributor accepts responsibility to anyone for
  14. the consequences of using it or for whether it serves any particular
  15. purpose or works at all, unless he says so in writing.  Refer to the GNU
  16. General Public License for full details. 
  17.  
  18. Everyone is granted permission to copy, modify and redistribute M0, but
  19. only under the conditions described in the GNU General Public License. 
  20. A copy of this license is supposed to have been given to you along with
  21. M0 so you can know your rights and responsibilities.  It should be in a
  22. file named LICENSE.  Among other things, the copyright notice and this
  23. notice must be preserved on all copies.  */
  24.  
  25. #include "l_proto.h"
  26.  
  27.  
  28. eindex
  29. new_array(mproc p, uint len)
  30. {
  31.     eindex ei = new_element(p, T_ARRAY), *ip;
  32.     eptr ep;
  33.  
  34.     if (!ei )
  35.         return 0;
  36.  
  37.     ep = eaddr(p,ei);
  38.     if (len) {
  39.         ep->V.arr.a = (eindex*) malloc(len * sizeof(eindex));
  40.         if (!ep->V.arr.a) {
  41.             free_element(p,ei);
  42.             return 0;
  43.         }
  44.     }
  45.     eplen(ep) = len;
  46.     ep->V.arr.alen = len;
  47.     for (ip = ep->V.arr.a; len > 0; len--, ip++)
  48.         *ip = null_val;
  49.     gaddr(null_val)->R += eplen(ep);
  50.     return ei;
  51. }
  52.  
  53.  
  54. eindex
  55. array_get(mproc p, eindex a, uint offs)
  56. {
  57.     eptr ep = eaddr(p,a);
  58.  
  59.     if (offs<0 || offs >= eplen(ep))
  60.         return 0;
  61.  
  62.     if (epattr(ep)&A_SUB)
  63.         return array_get(p, ep->V.sub.e, ep->V.sub.offset + offs);
  64.  
  65.     return ep->V.arr.a[offs];
  66. }
  67.  
  68.  
  69. retcode
  70. array_put(mproc p, eindex a, uint offs, eindex e)
  71. {
  72.     eptr ap = eaddr(p,a);
  73.  
  74.     if (offs<0 || offs >= eplen(ap))
  75.         return ERR_RANGE_CHECK;
  76.  
  77.     if (epattr(ap)&A_SUB)
  78.         return array_put(p, ap->V.sub.e, ap->V.sub.offset + offs, e);
  79.  
  80.     if (a < 0) {
  81.         eindex e2 = make_global(p, e);
  82.         decref(p, e);
  83.         e = e2;
  84.     }
  85.     decref(p, ap->V.arr.a[offs]);
  86.     ap->V.arr.a[offs] = e;
  87.     return OK;
  88. }
  89.  
  90. /* make_array does NOT increment the refcount of the included members */
  91. eindex
  92. make_array(mproc p, eindex *ip, uint len)
  93. {
  94.     eindex ei = new_element(p, T_ARRAY), *ip2;
  95.     eptr ep;
  96.  
  97.     if (!ei )
  98.         return 0;
  99.  
  100.     ep = eaddr(p,ei);
  101.     if (len) {
  102.         int i;
  103.         ep->V.arr.a = (eindex*) malloc(len * sizeof(eindex));
  104.         if (!ep->V.arr.a) {
  105.             free_element(p,ei);
  106.             return 0;
  107.         }
  108.         for (i=len, ip2 = ep->V.arr.a; i > 0; i--)
  109.             *ip2++ = *ip++;
  110.         eplen(ep) = ep->V.arr.alen = len;
  111.     }
  112.     return ei;
  113. }
  114.  
  115.  
  116. /* should be called by free_element() only! */
  117. void
  118. array_free(mproc p, eindex ei)
  119. {
  120.     eindex *ea;
  121.     eptr ep = eaddr(p, ei);
  122.     int i;
  123.  
  124.     eptype(ep) = T_EMPTY;
  125.     for (i=eplen(ep), ea = ep->V.arr.a; i>0; i--, ea++)
  126.         if (*ea) {
  127.             eindex e = *ea;
  128.             *ea = 0;
  129.             decref(p, e);
  130.         }
  131.     free(ep->V.arr.a);
  132. }
  133.  
  134.  
  135. retcode
  136. array_copy(mproc p, eindex e, eptr from, eptr to)
  137. {
  138.     int i = eplen(from);
  139.     eindex *ip = to->V.arr.a = (eindex*) malloc(i * sizeof(eindex));
  140.     int offs = 0;
  141.  
  142.     if (!ip)
  143.         return ERR_MALLOC_FAILED;
  144.  
  145.     while (epattr(from) & A_SUB) {
  146.         offs += from->V.sub.offset;
  147.         from = eaddr(p, from->V.sub.e);
  148.     }
  149.  
  150.     memcpy(ip, from->V.arr.a + offs, i * sizeof(eindex));
  151.  
  152.     for (; i > 0; i--, ip++)
  153.         incref(p,*ip);
  154.  
  155.     epattr(to) = A_ALL;
  156.     return OK;
  157. }
  158.  
  159.