File: vector.c

package info (click to toggle)
marlais 0.6.4-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,192 kB
  • ctags: 2,068
  • sloc: ansic: 16,958; yacc: 1,150; lex: 870; makefile: 639; lisp: 248
file content (202 lines) | stat: -rw-r--r-- 4,763 bytes parent folder | download
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
/*

   vector.c

   This software is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This software 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this software; if not, write to the Free
   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

   Original copyright notice follows:

   Copyright, 1993, Brent Benson.  All Rights Reserved.
   0.4 & 0.5 Revisions Copyright 1994, Joseph N. Wilson.  All Rights Reserved.

   Permission to use, copy, and modify this software and its
   documentation is hereby granted only under the following terms and
   conditions.  Both the above copyright notice and this permission
   notice must appear in all copies of the software, derivative works
   or modified version, and both notices must appear in supporting
   documentation.  Users of this software agree to the terms and
   conditions set forth in this notice.

 */

#include "vector.h"

#include "alloc.h"
#include "collection.h"
#include "error.h"
#include "list.h"
#include "number.h"
#include "prim.h"
#include "symbol.h"

/* primitives */

static Object vector_element_setter (Object vec, Object index, Object val);
static Object vector_size (Object vec);

static struct primitive vector_prims[] =
{
    {"%vector-element", prim_3, vector_element},
    {"%vector-element-setter", prim_3, vector_element_setter},
    {"%vector-size", prim_1, vector_size},
    {"%vector", prim_1, make_sov},
};

/* function definitions */

void
init_vector_prims (void)
{
  int num = sizeof (vector_prims) / sizeof (struct primitive);
  init_prims (num, vector_prims);
}

Object
make_sov (Object el_list)
{
  Object obj, els;
  int size, i;

  obj = allocate_object (sizeof (struct simple_object_vector));

  SOVTYPE (obj) = SimpleObjectVector;
  size = 0;
  els = el_list;
  while (PAIRP (els)) {
    size++;
    els = CDR (els);
  }
  SOVSIZE (obj) = size;
  SOVELS (obj) = (Object *) checking_malloc (size * sizeof (Object));

  els = el_list;
  i = 0;
  while (PAIRP (els)) {
    SOVELS (obj)[i++] = CAR (els);
    els = CDR (els);
  }
  return (obj);
}

Object
make_vector (int size, Object fill_obj)
{
  Object res;
  int i;

  /* actually fabricate the vector */
  res = allocate_object (sizeof (struct simple_object_vector));

  SOVTYPE (res) = SimpleObjectVector;
  SOVSIZE (res) = size;
  SOVELS (res) = (Object *) checking_malloc (size * sizeof (Object));

  for (i = 0; i < size; ++i) {
    SOVELS (res)[i] = fill_obj;
  }
  return (res);
}

/* Called with args to make */
Object
make_vector_driver (Object args)
{
  int size;
  Object size_obj, fill_obj;

  size = 0;
  size_obj = NULL;
  fill_obj = NULL;

  while (!EMPTYLISTP (args)) {
    if (FIRST (args) == size_keyword) {
      size_obj = SECOND (args);
    } else if (FIRST (args) == fill_keyword) {
      fill_obj = SECOND (args);
    } else {
      error ("make: unsupported keyword for <vector> class", 
	     FIRST (args), NULL);
    }
    args = CDR (CDR (args));
  }
  if (size_obj) {
    if (!INTEGERP (size_obj)) {
      error ("make: value of size: argument must be an integer", 
	     size_obj, NULL);
    }
    size = INTVAL (size_obj);
  }
  if (!fill_obj) {
    fill_obj = false_object;
  }
  return make_vector (size, fill_obj);
}

Object
vector_to_list (Object vec)
{
  int i;
  Object first, cur, acons;

  cur = make_empty_list ();
  for (i = 0; i < SOVSIZE (vec); ++i) {
    acons = cons (SOVELS (vec)[i], make_empty_list ());
    if (!EMPTYLISTP (cur)) {
      CDR (cur) = acons;
    } else {
      first = acons;
    }
    cur = acons;
  }
  return (first);
}

/* primitives */

Object
vector_element (Object vec, Object index, Object default_ob)
{
  int i, size;
    
  i = INTVAL (index);
  size = SOVSIZE (vec);
  if ((i < 0) || (i >= size)) {
    if (default_ob == default_object) {
      error ("element: index out of range", vec, index, NULL);
    } else {
      return default_ob;
    }
  }
  return (SOVELS (vec)[i]);
}

static Object
vector_element_setter (Object vec, Object index, Object val)
{
  int i, size;

  i = INTVAL (index);
  size = SOVSIZE (vec);
  if ((i < 0) || (i >= size)) {
    error ("element-setter: index out of range", vec, index, NULL);
  }
  return (SOVELS (vec)[i] = val);
}

static Object
vector_size (Object vec)
{
  return (make_integer (SOVSIZE (vec)));
}