home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c017 / 7.ddi / GPPLIB.ZIP / AVEC.CC < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-17  |  8.3 KB  |  399 lines

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