00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "config.hpp"
00014
00015 #if !defined(ARAGELI_INCLUDE_CPP_WITH_EXPORT_TEMPLATE) || \
00016 defined(ARAGELI_INCLUDE_CPP_WITH_EXPORT_TEMPLATE_VECTOR)
00017
00018 #include <sstream>
00019 #include <vector>
00020 #include <string>
00021 #include <sstream>
00022 #include <list>
00023 #include <numeric>
00024
00025 #include "vector.hpp"
00026 #include "_utility.hpp"
00027
00028
00029 namespace Arageli
00030 {
00031
00032
00033 template <typename T, bool REFCNT>
00034 void vector<T, REFCNT>::assign_fromstr (const char* s)
00035 {
00036 std::istringstream buf(s);
00037
00038 static_cast<std::istream&>(buf) >> *this;
00039 if(!buf && !buf.eof())
00040 throw incorrect_string(s);
00041 }
00042
00043
00044 template <typename T, bool REFCNT>
00045 bool vector<T, REFCNT>::is_null () const
00046 {
00047 for(size_type i = 0; i < size(); ++i)
00048 if(!Arageli::is_null(rep(i)))return false;
00049 return true;
00050 }
00051
00052
00053 template <typename T, bool REFCNT>
00054 bool vector<T, REFCNT>::is_unit () const
00055 {
00056 if(is_empty())return false;
00057 for(size_type i = 0; i < size(); ++i)
00058 if(!Arageli::is_unit(rep(i)))return false;
00059 return true;
00060 }
00061
00062
00063 template <typename T, bool REFCNT>
00064 bool vector<T, REFCNT>::is_opposite_unit () const
00065 {
00066 if(is_empty())return false;
00067 for(size_type i = 0; i < size(); ++i)
00068 if(!Arageli::is_opposite_unit(rep(i)))return false;
00069 return true;
00070 }
00071
00072
00073 template <typename T, bool REFCNT>
00074 vector<T, REFCNT>& vector<T, REFCNT>::inverse ()
00075 {
00076 unique();
00077 for(size_type i = 0; i < size(); ++i)
00078 Arageli::inverse(&rep(i));
00079 return *this;
00080 }
00081
00082
00083 template <typename T, bool REFCNT>
00084 vector<T, REFCNT>& vector<T, REFCNT>::bitwise_not ()
00085 {
00086 unique();
00087 for(size_type i = 0; i < size(); ++i)
00088 Arageli::assign_bitwise_not(rep(i));
00089 return *this;
00090 }
00091
00092 template <typename T, bool REFCNT>
00093 vector<T, REFCNT>& vector<T, REFCNT>::logical_not ()
00094 {
00095 unique();
00096 for(size_type i = 0; i < size(); ++i)
00097 Arageli::assign_logical_not(rep(i));
00098 return *this;
00099 }
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 #undef _ARAGELI_VECTOR_OPERATOR_ASSIGN_1
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380 template <typename T1, bool REFCNT1>
00381 vector<T1, REFCNT1>& vector<T1, REFCNT1>::opposite ()
00382 {
00383 if(is_empty())return *this;
00384 unique();
00385
00386 Rep& repr1 = rep();
00387 for(size_type i = 0; i < size(); ++i)
00388 Arageli::opposite(&repr1[i]);
00389
00390 return *this;
00391 }
00392
00393
00394 template <typename T, bool REFCNT>
00395 bool vector<T, REFCNT>::do_pack ()
00396 {
00397 ARAGELI_ASSERT_1(size() < capacity());
00398
00399 unique();
00400 Rep trep = rep();
00401 trep.swap(rep());
00402 }
00403
00404
00405 template <typename T, bool REFCNT>
00406 template <typename SV, typename V>
00407 V& vector<T, REFCNT>::copy_subvector (const SV& sv, V& res) const
00408 {
00409 size_type sz = sv.size();
00410 res.resize(sz);
00411 const_iterator thisvec = begin();
00412 typename V::iterator iv = res.begin();
00413
00414 for
00415 (
00416 typename SV::const_iterator i = sv.begin(), svend = sv.end();
00417 i != svend;
00418 ++i, ++iv
00419 )
00420 {
00421 ARAGELI_ASSERT_0(*i < size());
00422 *iv = thisvec[*i];
00423 }
00424
00425 return res;
00426 }
00427
00428
00429 template <typename T, bool REFCNT>
00430 template <typename SV>
00431 void vector<T, REFCNT>::erase_subvector (const SV& sv)
00432 {
00433
00434
00435
00436
00437
00438 if(sv.is_empty())return;
00439
00440 SV svt = sv;
00441 typedef typename SV::iterator SViter;
00442 std::sort(svt.begin(), svt.end());
00443
00444 SViter
00445 svti = svt.begin(),
00446 svtend = std::unique(svti, svt.end());
00447
00448 size_type curerased = *svti;
00449
00450
00451
00452
00453
00454
00455 ARAGELI_ASSERT_0(*(svtend-1) < size());
00456 ARAGELI_ASSERT_0(curerased < size());
00457
00458 iterator
00459 thisdst = begin() + curerased,
00460 thissrc = thisdst + 1,
00461 thisend = end();
00462
00463 for(;;)
00464 {
00465 ++svti;
00466
00467 if(svti != svtend && *svti == curerased+1)
00468 {
00469 ++curerased;
00470 ++svti;
00471 ARAGELI_ASSERT_0(thissrc != thisend);
00472 ++thissrc;
00473 }
00474
00475 ARAGELI_ASSERT_1(thissrc != thisend || svti == svtend);
00476
00477 iterator tend =
00478 svti == svtend ?
00479 thisend : thissrc + (*svti - curerased - 1);
00480
00481 thisdst = std::copy(thissrc, tend, thisdst);
00482
00483 if(tend == thisend)
00484 break;
00485
00486 thissrc = tend + 1;
00487 ARAGELI_ASSERT_1(svti != svtend);
00488 curerased = *svti;
00489 }
00490
00491 ARAGELI_ASSERT_1(svti == svtend);
00492 ARAGELI_ASSERT_1(thisdst + (svtend - svt.begin()) == thisend);
00493 erase(thisdst, thisend);
00494 }
00495
00496
00497
00498
00499 template
00500 <
00501 typename T1, bool REFCNT1,
00502 typename T2, bool REFCNT2,
00503 typename T1_factory
00504 >
00505 T1 dotprod
00506 (
00507 const vector<T1, REFCNT1>& a,
00508 const vector<T2, REFCNT2>& b,
00509 const T1_factory& t1fctr
00510 )
00511 {
00512 ARAGELI_ASSERT_0(a.size() == b.size());
00513
00514 T1 res = a.is_empty() ? t1fctr.null() : t1fctr.null(a[0]);
00515 typename vector<T1, REFCNT1>::size_type size = a.size();
00516
00517 for(std::size_t i = 0; i < size; ++i)
00518 res += a[i]*b[i];
00519
00520 return res;
00521 }
00522
00523
00524 template <typename T, bool REFCNT, typename T2, typename T2_factory>
00525 T2& product (const vector<T, REFCNT>& x, T2& res, const T2_factory& t2fctr)
00526 {
00527 if(x.is_empty())return res = t2fctr.unit();
00528 res = x[0];
00529 for(std::size_t i = 1; i < x.size(); ++i)
00530 res *= x[i];
00531 return res;
00532 }
00533
00534
00535 template <typename T, bool REFCNT, typename T2, typename T2_factory>
00536 T2& sum (const vector<T, REFCNT>& x, T2& res, const T2_factory& t2fctr)
00537 {
00538 if(x.is_empty())return res = t2fctr.null();
00539 res = x[0];
00540 for(std::size_t i = 1; i < x.size(); ++i)
00541 res += x[i];
00542 return res;
00543 }
00544
00545
00546 template <typename In, typename T, bool REFCNT>
00547 In& input_list
00548 (
00549 In& in,
00550 vector<T, REFCNT>& x,
00551 const char* first_bracket,
00552 const char* second_bracket,
00553 const char* separator,
00554 const char* range
00555 )
00556 {
00557 ARAGELI_ASSERT_0(_Internal::is_not_contains_spaces(first_bracket));
00558 ARAGELI_ASSERT_0(_Internal::is_not_contains_spaces(second_bracket));
00559 ARAGELI_ASSERT_0(_Internal::is_not_contains_spaces(separator));
00560 ARAGELI_ASSERT_0(_Internal::is_not_contains_spaces(range));
00561
00562 if(_Internal::is_bad_read_literal(in, first_bracket))return in;
00563
00564
00565
00566
00567 if(*second_bracket && _Internal::read_literal(in, second_bracket))
00568 {
00569 x.resize(0);
00570 return in;
00571 }
00572
00573 std::list<T> buf;
00574
00575 T t;
00576
00577 for(;;)
00578 {
00579 in >> t;
00580
00581 if(_Internal::is_bad_input(in))return in;
00582 else if(_Internal::read_literal(in, separator))
00583 {
00584 buf.push_back(t);
00585 continue;
00586 }
00587 else if(_Internal::read_literal(in, range))
00588 {
00589 T t1;
00590 in >> t1;
00591
00592 if(_Internal::is_bad_input(in))return in;
00593 else if(_Internal::read_literal(in, separator))
00594 {
00595 generate_range_helper(t, t1, std::back_inserter(buf));
00596 continue;
00597 }
00598 else if(_Internal::read_literal(in, range))
00599 {
00600 T t2;
00601 in >> t2;
00602
00603 if(_Internal::is_bad_input(in))return in;
00604 else if(_Internal::read_literal(in, separator))
00605 {
00606 generate_range_helper(t, t1, t2, std::back_inserter(buf));
00607 continue;
00608 }
00609 else if
00610 (
00611 *second_bracket == 0 ||
00612 _Internal::read_literal(in, second_bracket)
00613 )
00614 {
00615 generate_range_helper(t, t1, t2, std::back_inserter(buf));
00616 break;
00617 }
00618 }
00619 else if(*second_bracket == 0 || _Internal::read_literal(in, second_bracket))
00620 {
00621 generate_range_helper(t, t1, std::back_inserter(buf));
00622 break;
00623 }
00624 }
00625 else if(*second_bracket == 0 || _Internal::read_literal(in, second_bracket))
00626 {
00627 buf.push_back(t);
00628 break;
00629 }
00630
00631 in.clear(std::ios_base::badbit);
00632 return in;
00633 }
00634
00635
00636
00637 x.resize(buf.size());
00638 std::copy(buf.begin(), buf.end(), x.begin());
00639 return in;
00640 }
00641
00642
00643 template <typename T, bool REFCNT>
00644 std::ostream& output_aligned
00645 (
00646 std::ostream& out,
00647 const vector<T, REFCNT>& x,
00648 const char* left_col,
00649 const char* right_col
00650 )
00651 {
00652 if(x.is_empty())return out;
00653
00654 std::vector<std::string> buf(x.size());
00655 std::size_t maxlen = 0;
00656
00657 for(std::size_t i = 0; i < x.size(); ++i)
00658 {
00659 std::ostringstream strbuf;
00660 strbuf.copyfmt(out);
00661 strbuf << x[i];
00662 buf[i] = strbuf.str();
00663
00664 if(buf[i].length() > maxlen)
00665 maxlen = buf[i].length();
00666 }
00667
00668 for(std::size_t i = 0; i < x.size(); ++i)
00669 {
00670 std::size_t numspace = maxlen - buf[i].length();
00671
00672 out
00673 << left_col << std::string(numspace/2, ' ')
00674 << buf[i] << std::string(numspace - numspace/2, ' ')
00675 << right_col << '\n';
00676 }
00677
00678 return out;
00679 }
00680
00681
00682 template <typename T, bool REFCNT>
00683 std::istream& input_polynom_internal
00684 (std::istream& in, vector<T, REFCNT>& x)
00685 {
00686 char ch = 0;
00687 in >> ch;
00688 if(!in && !in.eof() || ch != '+' && ch != '-')return in;
00689 vector<T, REFCNT> res;
00690 in >> res;
00691 if(!in && !in.eof())return in;
00692 if(ch == '-')res.opposite();
00693 x = res;
00694 return in;
00695 }
00696
00697
00698 #define ARAGELI_VECTOR_MATH_FUNCS1_IMPL(NAME) \
00699 template <typename T, bool REFCNT> \
00700 vector<T, true> NAME \
00701 (const vector<T, REFCNT>& x) \
00702 { \
00703 std::size_t size = x.size(); \
00704 vector<T, true> res(size); \
00705 \
00706 typename vector<T, REFCNT>::const_iterator \
00707 xbeg = x.begin(); \
00708 typename vector<T, true>::iterator \
00709 resbeg = res.begin(); \
00710 \
00711 for(std::size_t i = 0; i < size; ++i) \
00712 resbeg[i] = NAME(xbeg[i]); \
00713 \
00714 return res; \
00715 }
00716
00717
00718 ARAGELI_VECTOR_MATH_FUNCS1_IMPL(sin)
00719 ARAGELI_VECTOR_MATH_FUNCS1_IMPL(cos)
00720 ARAGELI_VECTOR_MATH_FUNCS1_IMPL(tan)
00721 ARAGELI_VECTOR_MATH_FUNCS1_IMPL(sinh)
00722 ARAGELI_VECTOR_MATH_FUNCS1_IMPL(cosh)
00723 ARAGELI_VECTOR_MATH_FUNCS1_IMPL(tanh)
00724 ARAGELI_VECTOR_MATH_FUNCS1_IMPL(asin)
00725 ARAGELI_VECTOR_MATH_FUNCS1_IMPL(acos)
00726 ARAGELI_VECTOR_MATH_FUNCS1_IMPL(atan)
00727 ARAGELI_VECTOR_MATH_FUNCS1_IMPL(abs)
00728 ARAGELI_VECTOR_MATH_FUNCS1_IMPL(exp)
00729 ARAGELI_VECTOR_MATH_FUNCS1_IMPL(floor)
00730 ARAGELI_VECTOR_MATH_FUNCS1_IMPL(ceil)
00731 ARAGELI_VECTOR_MATH_FUNCS1_IMPL(log)
00732 ARAGELI_VECTOR_MATH_FUNCS1_IMPL(log10)
00733 ARAGELI_VECTOR_MATH_FUNCS1_IMPL(sqrt)
00734
00735
00736 #undef ARAGELI_VECTOR_MATH_FUNCS1_IMPL
00737
00738
00739 template
00740 <
00741 typename T1, bool REFCNT1,
00742 typename T2, bool REFCNT2
00743 >
00744 vector<T1, REFCNT1> pow
00745 (
00746 const vector<T1, REFCNT1>& a,
00747 const vector<T2, REFCNT2>& b
00748 )
00749 {
00750 ARAGELI_ASSERT_0(a.size() == b.size());
00751 std::size_t size = a.size();
00752 vector<T1, REFCNT1> res(size);
00753 for(std::size_t i = 0; i < size; ++i)
00754 res[i] = pow(a[i], b[i]);
00755
00756 return res;
00757 }
00758
00759
00760 template
00761 <
00762 typename T, bool REFCNT,
00763 typename P
00764 >
00765 vector<T, REFCNT> pow
00766 (const vector<T, REFCNT>& a, const P& b)
00767 {
00768 std::size_t size = a.size();
00769 vector<T, REFCNT> res(size);
00770 for(std::size_t i = 0; i < size; ++i)
00771 res[i] = pow(a[i], b);
00772 }
00773
00774 }
00775
00776
00777 namespace std
00778 {
00779
00780
00781
00782 }
00783
00784 #endif // #ifndef ARAGELI_INCLUDE_CPP_WITH_EXPORT_TEMPLATE