xref: /386bsd/usr/include/nonstd/gnu/g++/gen/AVec.ccP (revision a2142627)
1// This may look like C code, but it is really -*- C++ -*-
2/*
3Copyright (C) 1988 Free Software Foundation
4    written by Doug Lea (dl@rocky.oswego.edu)
5
6This file is part of GNU CC.
7
8GNU CC is distributed in the hope that it will be useful,
9but WITHOUT ANY WARRANTY.  No author or distributor
10accepts responsibility to anyone for the consequences of using it
11or for whether it serves any particular purpose or works at all,
12unless he says so in writing.  Refer to the GNU CC General Public
13License for full details.
14
15Everyone is granted permission to copy, modify and redistribute
16GNU CC, but only under the conditions described in the
17GNU CC General Public License.   A copy of this license is
18supposed to have been given to you along with GNU CC so you
19can know your rights and responsibilities.  It should be in a
20file named COPYING.  Among other things, the copyright notice
21and this notice must be preserved on all copies.
22*/
23
24#ifdef __GNUG__
25#pragma implementation
26#endif
27#include <stream.h>
28#include "<T>.AVec.h"
29
30/*
31 The following brought to you by the department of redundancy department
32*/
33
34<T>AVec& <T>AVec::operator = (<T>AVec& v)
35{
36  if (len != 0 && len != v.capacity())
37    error("nonconformant vectors.");
38  if (len == 0)
39    s = new <T> [len = v.capacity()];
40  if (s != v.vec())
41  {
42    for (int i = 0; i < len; ++i)
43      s[i] = v.vec()[i];
44  }
45  return *this;
46}
47
48<T>AVec& <T>AVec::operator = (<T&> f)
49{
50  for (int i = 0; i < len; ++i) s[i] = f;
51  return *this;
52}
53
54
55<T>AVec concat(<T>AVec & a, <T>AVec & b)
56{
57  int newl = a.capacity() + b.capacity();
58  <T>* news = new <T> [newl];
59  <T>* p = news;
60  <T>* top = &(a.vec()[a.capacity()]);
61  <T>* t = a.vec();
62  while (t < top) *p++ = *t++;
63  top = &(b.vec()[b.capacity()]);
64  t = b.vec();
65  while (t < top) *p++ = *t++;
66  return <T>AVec(newl, news);
67}
68
69
70<T>AVec combine(<T>Combiner f, <T>AVec& a, <T>AVec& b)
71{
72  int newl = (a.capacity() < b.capacity())? a.capacity() : b.capacity();
73  <T>* news = new <T> [newl];
74  <T>* p = news;
75  <T>* top = &(a.vec()[newl]);
76  <T>* t = a.vec();
77  <T>* u = b.vec();
78  while (t < top) *p++ = (*f)(*t++, *u++);
79  return <T>AVec(newl, news);
80}
81
82<T>AVec reverse(<T>AVec& a)
83{
84  <T>* news = new <T> [a.capacity()];
85  if (a.capacity() != 0)
86  {
87    <T>* lo = news;
88    <T>* hi = &(news[a.capacity() - 1]);
89    while (lo < hi)
90    {
91      <T> tmp = *lo;
92      *lo++ = *hi;
93      *hi-- = tmp;
94    }
95  }
96  return <T>AVec(a.capacity(), news);
97}
98
99<T>AVec map(<T>Mapper f, <T>AVec& a)
100{
101  <T>* news = new <T> [a.capacity()];
102  <T>* p = news;
103  <T>* top = &(a.vec()[a.capacity()]);
104  <T>* t = a.vec();
105  while(t < top) *p++ = (*f)(*t++);
106  return <T>AVec(a.capacity(), news);
107}
108
109<T>AVec <T>AVec::at(int from, int n)
110{
111  int to;
112  if (n < 0)
113  {
114    n = len - from;
115    to = len - 1;
116  }
117  else
118    to = from + n - 1;
119  if ((unsigned)from > to)
120    range_error();
121  <T>* news = new <T> [n];
122  <T>* p = news;
123  <T>* t = &(s[from]);
124  <T>* top = &(s[to]);
125  while (t <= top) *p++ = *t++;
126  return <T>AVec(n, news);
127}
128
129<T>AVec merge(<T>AVec & a, <T>AVec & b, <T>Comparator f)
130{
131  int newl = a.capacity() + b.capacity();
132  <T>* news = new <T> [newl];
133  <T>* p = news;
134  <T>* topa = &(a.vec()[a.capacity()]);
135  <T>* as = a.vec();
136  <T>* topb = &(b.vec()[b.capacity()]);
137  <T>* bs = b.vec();
138
139  for (;;)
140  {
141    if (as >= topa)
142    {
143      while (bs < topb) *p++ = *bs++;
144      break;
145    }
146    else if (bs >= topb)
147    {
148      while (as < topa) *p++ = *as++;
149      break;
150    }
151    else if ((*f)(*as, *bs) <= 0)
152      *p++ = *as++;
153    else
154      *p++ = *bs++;
155  }
156  return <T>AVec(newl, news);
157}
158
159<T>AVec operator + (<T>AVec& a, <T>AVec& b)
160{
161  a.check_len(b.capacity());
162  <T>* news = new <T> [a.capacity()];
163  <T>* p = news;
164  <T>* top = &(a.vec()[a.capacity()]);
165  <T>* t = a.vec();
166  <T>* u = b.vec();
167  while (t < top) *p++ = *t++ + *u++;
168  return <T>AVec(a.capacity(), news);
169}
170
171<T>AVec operator - (<T>AVec& a, <T>AVec& b)
172{
173  a.check_len(b.capacity());
174  <T>* news = new <T> [a.capacity()];
175  <T>* p = news;
176  <T>* top = &(a.vec()[a.capacity()]);
177  <T>* t = a.vec();
178  <T>* u = b.vec();
179  while (t < top) *p++ = *t++ - *u++;
180  return <T>AVec(a.capacity(), news);
181}
182
183<T>AVec  product (<T>AVec& a, <T>AVec& b)
184{
185  a.check_len(b.capacity());
186  <T>* news = new <T> [a.capacity()];
187  <T>* p = news;
188  <T>* top = &(a.vec()[a.capacity()]);
189  <T>* t = a.vec();
190  <T>* u = b.vec();
191  while (t < top) *p++ = *t++ * *u++;
192  return <T>AVec(a.capacity(), news);
193}
194
195<T>AVec quotient(<T>AVec& a, <T>AVec& b)
196{
197  a.check_len(b.capacity());
198  <T>* news = new <T> [a.capacity()];
199  <T>* p = news;
200  <T>* top = &(a.vec()[a.capacity()]);
201  <T>* t = a.vec();
202  <T>* u = b.vec();
203  while (t < top) *p++ = *t++ / *u++;
204  return <T>AVec(a.capacity(), news);
205}
206
207<T>AVec operator + (<T>AVec& a, <T&> b)
208{
209  <T>* news = new <T> [a.capacity()];
210  <T>* p = news;
211  <T>* top = &(a.vec()[a.capacity()]);
212  <T>* t = a.vec();
213  while (t < top) *p++ = *t++ + b;
214  return <T>AVec(a.capacity(), news);
215}
216
217<T>AVec operator - (<T>AVec& a, <T&> b)
218{
219  <T>* news = new <T> [a.capacity()];
220  <T>* p = news;
221  <T>* top = &(a.vec()[a.capacity()]);
222  <T>* t = a.vec();
223  while (t < top) *p++ = *t++ - b;
224  return <T>AVec(a.capacity(), news);
225}
226
227<T>AVec operator * (<T>AVec& a, <T&> b)
228{
229  <T>* news = new <T> [a.capacity()];
230  <T>* p = news;
231  <T>* top = &(a.vec()[a.capacity()]);
232  <T>* t = a.vec();
233  while (t < top) *p++ = *t++ * b;
234  return <T>AVec(a.capacity(), news);
235}
236
237<T>AVec operator / (<T>AVec& a, <T&> b)
238{
239  <T>* news = new <T> [a.capacity()];
240  <T>* p = news;
241  <T>* top = &(a.vec()[a.capacity()]);
242  <T>* t = a.vec();
243  while (t < top) *p++ = *t++ / b;
244  return <T>AVec(a.capacity(), news);
245}
246
247<T>AVec <T>AVec::operator - ()
248{
249  <T>* news = new <T> [len];
250  <T>* p = news;
251  <T>* top = &(s[len]);
252  <T>* t = s;
253  while (t < top) *p++ = -(*t++);
254  return <T>AVec(len, news);
255}
256
257<T>AVec& <T>AVec::operator += (<T>AVec& b)
258{
259  check_len(b.capacity());
260  <T>* u = b.vec();
261  <T>* top = &(s[len]);
262  <T>* t = s;
263  while (t < top) *t++ += *u++;
264  return *this;
265}
266
267<T>AVec& <T>AVec::operator -= (<T>AVec& b)
268{
269  check_len(b.capacity());
270  <T>* u = b.vec();
271  <T>* top = &(s[len]);
272  <T>* t = s;
273  while (t < top) *t++ -= *u++;
274  return *this;
275}
276
277<T>AVec& <T>AVec::product(<T>AVec& b)
278{
279  check_len(b.capacity());
280  <T>* u = b.vec();
281  <T>* top = &(s[len]);
282  <T>* t = s;
283  while (t < top) *t++ *= *u++;
284  return *this;
285}
286
287<T>AVec& <T>AVec::quotient(<T>AVec& b)
288{
289  check_len(b.capacity());
290  <T>* u = b.vec();
291  <T>* top = &(s[len]);
292  <T>* t = s;
293  while (t < top) *t++ /= *u++;
294  return *this;
295}
296
297<T>AVec& <T>AVec::operator += (<T&> b)
298{
299  <T>* top = &(s[len]);
300  <T>* t = s;
301  while (t < top) *t++ += b;
302  return *this;
303}
304
305<T>AVec& <T>AVec::operator -= (<T&> b)
306{
307  <T>* top = &(s[len]);
308  <T>* t = s;
309  while (t < top) *t++ -= b;
310  return *this;
311}
312
313<T>AVec& <T>AVec::operator *= (<T&> b)
314{
315  <T>* top = &(s[len]);
316  <T>* t = s;
317  while (t < top) *t++ *= b;
318  return *this;
319}
320
321<T>AVec& <T>AVec::operator /= (<T&> b)
322{
323  <T>* top = &(s[len]);
324  <T>* t = s;
325  while (t < top) *t++ /= b;
326  return *this;
327}
328
329<T> <T>AVec::max()
330{
331  if (len == 0)
332    return 0;
333  <T>* top = &(s[len]);
334  <T>* t = s;
335  <T> res = *t++;
336  for (; t < top; ++t) if (*t > res) res = *t;
337  return res;
338}
339
340int <T>AVec::max_index()
341{
342  if (len == 0)
343    return -1;
344  int ind = 0;
345  for (int i = 1; i < len; ++i)
346    if (s[i] > s[ind])
347      ind = i;
348  return ind;
349}
350
351<T> <T>AVec::min()
352{
353  if (len == 0)
354    return 0;
355  <T>* top = &(s[len]);
356  <T>* t = s;
357  <T> res = *t++;
358  for (; t < top; ++t) if (*t < res) res = *t;
359  return res;
360}
361
362int <T>AVec::min_index()
363{
364  if (len == 0)
365    return -1;
366  int ind = 0;
367  for (int i = 1; i < len; ++i)
368    if (s[i] < s[ind])
369      ind = i;
370  return ind;
371}
372
373<T> <T>AVec::sum()
374{
375  <T> res = 0;
376  <T>* top = &(s[len]);
377  <T>* t = s;
378  while (t < top) res += *t++;
379  return res;
380}
381
382
383<T> <T>AVec::sumsq()
384{
385  <T> res = 0;
386  <T>* top = &(s[len]);
387  <T>* t = s;
388  for (; t < top; ++t) res += *t * *t;
389  return res;
390}
391
392<T> operator * (<T>AVec& a, <T>AVec& b)
393{
394  a.check_len(b.capacity());
395  <T>* top = &(a.vec()[a.capacity()]);
396  <T>* t = a.vec();
397  <T>* u = b.vec();
398  <T> res = 0;
399  while (t < top) res += *t++ * *u++;
400  return res;
401}
402