// This may look like C code, but it is really -*- C++ -*- /* Copyright (C) 1988 Free Software Foundation written by Doug Lea (dl@rocky.oswego.edu) This file is part of GNU CC. GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. No author or distributor accepts responsibility to anyone for the consequences of using it or for whether it serves any particular purpose or works at all, unless he says so in writing. Refer to the GNU CC General Public License for full details. Everyone is granted permission to copy, modify and redistribute GNU CC, but only under the conditions described in the GNU CC General Public License. A copy of this license is supposed to have been given to you along with GNU CC so you can know your rights and responsibilities. It should be in a file named COPYING. Among other things, the copyright notice and this notice must be preserved on all copies. */ #ifdef __GNUG__ #pragma implementation #endif #include #include ".AVec.h" /* The following brought to you by the department of redundancy department */ AVec& AVec::operator = (AVec& v) { if (len != 0 && len != v.capacity()) error("nonconformant vectors."); if (len == 0) s = new [len = v.capacity()]; if (s != v.vec()) { for (int i = 0; i < len; ++i) s[i] = v.vec()[i]; } return *this; } AVec& AVec::operator = ( f) { for (int i = 0; i < len; ++i) s[i] = f; return *this; } AVec concat(AVec & a, AVec & b) { int newl = a.capacity() + b.capacity(); * news = new [newl]; * p = news; * top = &(a.vec()[a.capacity()]); * t = a.vec(); while (t < top) *p++ = *t++; top = &(b.vec()[b.capacity()]); t = b.vec(); while (t < top) *p++ = *t++; return AVec(newl, news); } AVec combine(Combiner f, AVec& a, AVec& b) { int newl = (a.capacity() < b.capacity())? a.capacity() : b.capacity(); * news = new [newl]; * p = news; * top = &(a.vec()[newl]); * t = a.vec(); * u = b.vec(); while (t < top) *p++ = (*f)(*t++, *u++); return AVec(newl, news); } AVec reverse(AVec& a) { * news = new [a.capacity()]; if (a.capacity() != 0) { * lo = news; * hi = &(news[a.capacity() - 1]); while (lo < hi) { tmp = *lo; *lo++ = *hi; *hi-- = tmp; } } return AVec(a.capacity(), news); } AVec map(Mapper f, AVec& a) { * news = new [a.capacity()]; * p = news; * top = &(a.vec()[a.capacity()]); * t = a.vec(); while(t < top) *p++ = (*f)(*t++); return AVec(a.capacity(), news); } AVec AVec::at(int from, int n) { int to; if (n < 0) { n = len - from; to = len - 1; } else to = from + n - 1; if ((unsigned)from > to) range_error(); * news = new [n]; * p = news; * t = &(s[from]); * top = &(s[to]); while (t <= top) *p++ = *t++; return AVec(n, news); } AVec merge(AVec & a, AVec & b, Comparator f) { int newl = a.capacity() + b.capacity(); * news = new [newl]; * p = news; * topa = &(a.vec()[a.capacity()]); * as = a.vec(); * topb = &(b.vec()[b.capacity()]); * bs = b.vec(); for (;;) { if (as >= topa) { while (bs < topb) *p++ = *bs++; break; } else if (bs >= topb) { while (as < topa) *p++ = *as++; break; } else if ((*f)(*as, *bs) <= 0) *p++ = *as++; else *p++ = *bs++; } return AVec(newl, news); } AVec operator + (AVec& a, AVec& b) { a.check_len(b.capacity()); * news = new [a.capacity()]; * p = news; * top = &(a.vec()[a.capacity()]); * t = a.vec(); * u = b.vec(); while (t < top) *p++ = *t++ + *u++; return AVec(a.capacity(), news); } AVec operator - (AVec& a, AVec& b) { a.check_len(b.capacity()); * news = new [a.capacity()]; * p = news; * top = &(a.vec()[a.capacity()]); * t = a.vec(); * u = b.vec(); while (t < top) *p++ = *t++ - *u++; return AVec(a.capacity(), news); } AVec product (AVec& a, AVec& b) { a.check_len(b.capacity()); * news = new [a.capacity()]; * p = news; * top = &(a.vec()[a.capacity()]); * t = a.vec(); * u = b.vec(); while (t < top) *p++ = *t++ * *u++; return AVec(a.capacity(), news); } AVec quotient(AVec& a, AVec& b) { a.check_len(b.capacity()); * news = new [a.capacity()]; * p = news; * top = &(a.vec()[a.capacity()]); * t = a.vec(); * u = b.vec(); while (t < top) *p++ = *t++ / *u++; return AVec(a.capacity(), news); } AVec operator + (AVec& a, b) { * news = new [a.capacity()]; * p = news; * top = &(a.vec()[a.capacity()]); * t = a.vec(); while (t < top) *p++ = *t++ + b; return AVec(a.capacity(), news); } AVec operator - (AVec& a, b) { * news = new [a.capacity()]; * p = news; * top = &(a.vec()[a.capacity()]); * t = a.vec(); while (t < top) *p++ = *t++ - b; return AVec(a.capacity(), news); } AVec operator * (AVec& a, b) { * news = new [a.capacity()]; * p = news; * top = &(a.vec()[a.capacity()]); * t = a.vec(); while (t < top) *p++ = *t++ * b; return AVec(a.capacity(), news); } AVec operator / (AVec& a, b) { * news = new [a.capacity()]; * p = news; * top = &(a.vec()[a.capacity()]); * t = a.vec(); while (t < top) *p++ = *t++ / b; return AVec(a.capacity(), news); } AVec AVec::operator - () { * news = new [len]; * p = news; * top = &(s[len]); * t = s; while (t < top) *p++ = -(*t++); return AVec(len, news); } AVec& AVec::operator += (AVec& b) { check_len(b.capacity()); * u = b.vec(); * top = &(s[len]); * t = s; while (t < top) *t++ += *u++; return *this; } AVec& AVec::operator -= (AVec& b) { check_len(b.capacity()); * u = b.vec(); * top = &(s[len]); * t = s; while (t < top) *t++ -= *u++; return *this; } AVec& AVec::product(AVec& b) { check_len(b.capacity()); * u = b.vec(); * top = &(s[len]); * t = s; while (t < top) *t++ *= *u++; return *this; } AVec& AVec::quotient(AVec& b) { check_len(b.capacity()); * u = b.vec(); * top = &(s[len]); * t = s; while (t < top) *t++ /= *u++; return *this; } AVec& AVec::operator += ( b) { * top = &(s[len]); * t = s; while (t < top) *t++ += b; return *this; } AVec& AVec::operator -= ( b) { * top = &(s[len]); * t = s; while (t < top) *t++ -= b; return *this; } AVec& AVec::operator *= ( b) { * top = &(s[len]); * t = s; while (t < top) *t++ *= b; return *this; } AVec& AVec::operator /= ( b) { * top = &(s[len]); * t = s; while (t < top) *t++ /= b; return *this; } AVec::max() { if (len == 0) return 0; * top = &(s[len]); * t = s; res = *t++; for (; t < top; ++t) if (*t > res) res = *t; return res; } int AVec::max_index() { if (len == 0) return -1; int ind = 0; for (int i = 1; i < len; ++i) if (s[i] > s[ind]) ind = i; return ind; } AVec::min() { if (len == 0) return 0; * top = &(s[len]); * t = s; res = *t++; for (; t < top; ++t) if (*t < res) res = *t; return res; } int AVec::min_index() { if (len == 0) return -1; int ind = 0; for (int i = 1; i < len; ++i) if (s[i] < s[ind]) ind = i; return ind; } AVec::sum() { res = 0; * top = &(s[len]); * t = s; while (t < top) res += *t++; return res; } AVec::sumsq() { res = 0; * top = &(s[len]); * t = s; for (; t < top; ++t) res += *t * *t; return res; } operator * (AVec& a, AVec& b) { a.check_len(b.capacity()); * top = &(a.vec()[a.capacity()]); * t = a.vec(); * u = b.vec(); res = 0; while (t < top) res += *t++ * *u++; return res; }