File: vec.cpp

package info (click to toggle)
libitpp 4.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,520 kB
  • ctags: 6,341
  • sloc: cpp: 51,608; sh: 9,248; makefile: 636; fortran: 8
file content (1015 lines) | stat: -rw-r--r-- 32,165 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
/*!
 * \file
 * \brief Templated Vector Class Implementation
 * \author Tony Ottosson, Tobias Ringstrom, Adam Piatyszek and Conrad Sanderson
 *
 * -------------------------------------------------------------------------
 *
 * IT++ - C++ library of mathematical, signal processing, speech processing,
 *        and communications classes and functions
 *
 * Copyright (C) 1995-2008  (see AUTHORS file for a list of contributors)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * -------------------------------------------------------------------------
 */

#include <itpp/base/vec.h>
#include <itpp/base/converters.h>
#include <limits>

//! \cond

namespace itpp {


  template<class Num_T>
  std::string Vec<Num_T>::replace_commas(const std::string &str_in)
  {
    // copy an input sting into a local variable str
    std::string str(str_in);
    // find first occurence of comma in string str
    std::string::size_type index = str.find(',', 0);
    while (index != std::string::npos) {
      // replace character at position index with space
      str.replace(index, 1, 1, ' ');
      // find next occurence of comma in string str
      index = str.find(',', index);
    }
    return str;
  }


  template<>
  void Vec<double>::set(const std::string &str)
  {
    std::istringstream buffer(replace_commas(str));
    double b, c, eps_margin;
    bool b_parsed = false;
    bool c_parsed = false;
    bool negative = false;
    bool nan_inf = false;
    int pos = 0, maxpos = 10;

    free();
    alloc(maxpos);

    while (buffer.peek() != EOF) {
      switch (buffer.peek()) {
	// skip spaces
      case ' ': case '\t':
	buffer.seekg(1, std::ios_base::cur);
	break;

	// skip '+' sign
      case '+':
	// check for not handled '-' sign
	it_assert(!negative, "Vec<double>::set(): Improper data string (-)");
	buffer.seekg(1, std::ios_base::cur);
	break;

	// check for '-' sign
      case '-':
	buffer.seekg(1, std::ios_base::cur);
	negative = true;
	break;

	// check for NaN
      case 'N': case 'n':
	buffer.seekg(1, std::ios_base::cur);
	it_assert((buffer.peek() == 'A') || (buffer.peek() == 'a'),
		  "Vec<double>::set(): Improper data string (NaN)");
	buffer.seekg(1, std::ios_base::cur);
	it_assert((buffer.peek() == 'N') || (buffer.peek() == 'n'),
		  "Vec<double>::set(): Improper data string (NaN)");
	buffer.seekg(1, std::ios_base::cur);
	it_assert(!negative, "Vec<double>::set(): Improper data string "
		  "(-NaN not exist)");
	if (++pos > maxpos) {
	  maxpos <<= 1;
	  set_size(maxpos, true);
	}
	if (std::numeric_limits<double>::has_quiet_NaN) {
	  data[pos-1] = std::numeric_limits<double>::quiet_NaN();
	}
	else if (std::numeric_limits<double>::has_signaling_NaN) {
	  data[pos-1] = std::numeric_limits<double>::signaling_NaN();
	}
	else {
	  it_error("Vec<double::set(): NaN not supported");
	}
	nan_inf = true;
	break; // case 'N'...

	// check for Inf
      case 'I': case 'i':
	buffer.seekg(1, std::ios_base::cur);
	it_assert((buffer.peek() == 'N') || (buffer.peek() == 'n'),
		  "Vec<double>::set(): Improper data string (Inf)");
	buffer.seekg(1, std::ios_base::cur);
	it_assert((buffer.peek() == 'F') || (buffer.peek() == 'f'),
		  "Vec<double>::set(): Improper data string (Inf)");
	buffer.seekg(1, std::ios_base::cur);
	it_assert(std::numeric_limits<double>::has_infinity,
		  "Vec<double::set(): Inf not supported");
	if (++pos > maxpos) {
	  maxpos <<= 1;
	  set_size(maxpos, true);
	}
	if (negative) {
	  data[pos-1] = -std::numeric_limits<double>::infinity();
	  negative = false;
	}
	else {
	  data[pos-1] = std::numeric_limits<double>::infinity();
	}
	nan_inf = true;
	break; // case 'I'...

      case ':': // reads format a:b:c or a:b
	it_assert(!negative, "Vec<double>::set(): Improper data string (-)");
	it_assert(!nan_inf, "Vec<double>::set(): Improper data string (Nan/Inf "
		  " can not be used with a:b or a:b:c)");
	it_assert(pos == 1, "Vec<double>::set(): Improper data string (a:b)");
	buffer.seekg(1, std::ios_base::cur);
	// parse b
	while (buffer.peek() != EOF) {
	  switch (buffer.peek()) {
	  case ' ': case '\t':
	    buffer.seekg(1, std::ios_base::cur);
	    break;

	  case ':':
	    it_assert(b_parsed, "Vec<double>::set(): Improper data string "
		      "(a:b)");
	    buffer.seekg(1, std::ios_base::cur);
	    // parse c
	    while (buffer.peek() != EOF) {
	      switch (buffer.peek()) {
	      case ' ': case '\t':
		buffer.seekg(1, std::ios_base::cur);
		break;

	      default:
		it_assert(!c_parsed, "Vec<double>::set(): Improper data "
			  "string (a:b:c)");
		buffer.clear();
		buffer >> c;
		it_assert(!buffer.fail(), "Vec<double>::set(): Stream "
			  "operation failed (buffer >> c)");
		c_parsed = true;
	      }
	    }
	    it_assert(c_parsed, "Vec<double>::set(): Improper data string "
		      "(a:b:c)");
	    break;

	  default:
	    it_assert(!b_parsed, "Vec<double>::set(): Improper data string "
		      "(a:b)");
	    buffer.clear();
	    buffer >> b;
	    it_assert(!buffer.fail(), "Vec<double>::set(): Stream operation "
		      "failed (buffer >> b)");
	    b_parsed = true;
	  }
	}
	it_assert(b_parsed, "Vec<double>::set(): Improper data string (a:b)");

	if (c_parsed) {
	  // Adding this margin fixes precision problems in e.g. "0:0.2:3",
	  // where the last value was 2.8 instead of 3.
	  eps_margin = std::fabs((c - data[pos-1]) / b) * eps;
	  if (b > 0 && c >= data[pos-1]) {
	    while (data[pos-1] + b <= c + eps_margin) {
	      if (++pos > maxpos) {
		maxpos <<= 1;
		set_size(maxpos, true);
	      }
	      data[pos-1] = data[pos-2] + b;
	    }
	  }
	  else if (b < 0 && c <= data[pos-1]) {
	    while (data[pos-1] + b >= c - eps_margin) {
	      if (++pos > maxpos) {
		maxpos <<= 1;
		set_size(maxpos, true);
	      }
	      data[pos-1] = data[pos-2] + b;
	    }
	  }
	  else if (b == 0 && c == data[pos-1]) {
	    break;
	  }
	  else {
	    it_error("Vec<double>::set(): Improper data string (a:b:c)");
	  }
	} // if (c_parsed)
	else if (b_parsed) {
	  eps_margin = std::fabs(b - data[pos-1]) * eps;
	  if (b < data[pos-1]) {
	    while (data[pos-1] -1.0 >= b - eps_margin) {
	      if (++pos > maxpos) {
		maxpos <<= 1;
		set_size(maxpos, true);
	      }
	      data[pos-1] = data[pos-2] - 1.0;
	    }
	  }
	  else {
	    while (data[pos-1] + 1.0 <= b + eps_margin) {
	      if (++pos > maxpos) {
		maxpos <<= 1;
		set_size(maxpos, true);
	      }
	      data[pos-1] = data[pos-2] + 1.0;
	    }
	  }
	} // else if (b_parsed)
	else {
	  it_error("Vec<double>::set(): Improper data string (a:b)");
	}
	break; // case ':'

      default:
	if (++pos > maxpos) {
	  maxpos <<= 1;
	  set_size(maxpos, true);
	}
	buffer >> data[pos-1];
	it_assert(!buffer.fail(), "Vec<double>::set(): Stream operation "
		  "failed (buffer >> data)");
	if (negative) {
	  data[pos-1] = -data[pos-1];
	  negative = false;
	}
	break; // default
      }
    }
    set_size(pos, true);
  }


  template<>
  void Vec<std::complex<double> >::set(const std::string &str)
  {
    std::istringstream buffer(str);
    int pos = 0, maxpos = 10;

    free();
    alloc(maxpos);

    while (buffer.peek() != EOF) {
      switch (buffer.peek()) {
      case ':':
        it_error("Vec<complex>::set(): a:b:c and a:b expressions not valid "
		 "for cvec");
	break;
      case ' ': case '\t': case ',':
	buffer.seekg(1, std::ios_base::cur);
	break;
      default:
	if (++pos > maxpos) {
	  maxpos <<= 1;
	  set_size(maxpos, true);
	}
	buffer >> data[pos-1];
	it_assert(!buffer.fail(), "Vec<complex>::set(): Stream operation "
		  "failed (buffer >> data)");
      }
    }
    set_size(pos, true);
  }


  template<>
  void Vec<bin>::set(const std::string &str)
  {
    std::istringstream buffer(replace_commas(str));
    int pos = 0, maxpos = 10;

    free();
    alloc(maxpos);

    while (buffer.peek() != EOF) {
      switch (buffer.peek()) {
      case ':':
        it_error("Vec<bin>::set(): a:b:c and a:b expressions not valid "
		 "for bvec");
	break;
      case ' ': case '\t':
	buffer.seekg(1, std::ios_base::cur);
	break;
      default:
	if (++pos > maxpos) {
	  maxpos <<= 1;
	  set_size(maxpos, true);
	}
	buffer >> data[pos-1];
	it_assert(!buffer.fail(), "Vec<bin>::set(): Stream operation failed "
		  "(buffer >> data)");
      }
    }
    set_size(pos, true);
  }


  template<>
  void Vec<int>::set(const std::string &str)
  {
    std::istringstream buffer(replace_commas(str));
    int b, c;
    bool b_parsed = false;
    bool c_parsed = false;
    bool negative = false;
    int pos = 0;
    int maxpos = 10;

    free();
    alloc(maxpos);

    while (buffer.peek() != EOF) {
      switch (buffer.peek()) {
	// skip spaces and tabs
      case ' ': case '\t':
	buffer.seekg(1, std::ios_base::cur);
	break;

	// skip '+' sign
      case '+':
	// check for not handled '-' sign
	it_assert(!negative, "Vec<double>::set(): Improper data string (-)");
	buffer.seekg(1, std::ios_base::cur);
	break;

	// check for '-' sign
      case '-':
	buffer.seekg(1, std::ios_base::cur);
	negative = true;
	break;

	// hexadecimal number or octal number or zero
      case '0':
	buffer.seekg(1, std::ios_base::cur);
	switch (buffer.peek()) {
	  // hexadecimal number
	case 'x': case 'X':
	  buffer.clear();
	  buffer.seekg(-1, std::ios_base::cur);
	  if (++pos > maxpos) {
	    maxpos <<= 1;
	    set_size(maxpos, true);
	  }
	  buffer >> std::hex >> data[pos-1];
	  it_assert(!buffer.fail(), "Vec<int>::set(): Stream operation "
		    "failed (buffer >> hex >> data)");
	  break; // case 'x'...

	  // octal number
	case '1': case '2': case '3': case '4':	case '5': case '6': case '7':
	  buffer.clear();
	  buffer.seekg(-1, std::ios_base::cur);
	  if (++pos > maxpos) {
	    maxpos <<= 1;
	    set_size(maxpos, true);
	  }
	  buffer >> std::oct >> data[pos-1];
	  it_assert(!buffer.fail(), "Vec<int>::set(): Stream operation "
		    "failed (buffer >> oct >> data)");
	  break; // case '1'...

	  // zero
	case EOF: case ' ': case '\t': case ':': case '0':
	  buffer.clear();
	  buffer.seekg(-1, std::ios_base::cur);
	  if (++pos > maxpos) {
	    maxpos <<= 1;
	    set_size(maxpos, true);
	  }
	  buffer >> std::dec >> data[pos-1];
	  it_assert(!buffer.fail(), "Vec<int>::set(): Stream operation "
		    "failed (buffer >> dec >> data)");
	  break; // case EOF...

	default:
	  it_error("Vec<int>::set(): Improper data string");
	}
	// check if just parsed data was negative
	if (negative) {
	  data[pos-1] = -data[pos-1];
	  negative = false;
	}
	break; // case '0'

	// decimal number
      case '1': case '2': case '3': case '4': case '5': case '6': case '7':
      case '8': case '9':
	buffer.clear();
	if (++pos > maxpos) {
	  maxpos <<= 1;
	  set_size(maxpos, true);
	}
	buffer >> std::dec >> data[pos-1];
	it_assert(!buffer.fail(), "Vec<int>::set(): Stream operation "
		  "failed (buffer >> dec >> data)");
	// check if just parsed data was negative
	if (negative) {
	  data[pos-1] = -data[pos-1];
	  negative = false;
	}
	break; // case '1'...

	// parse format a:b:c or a:b
      case ':':
	it_assert(pos == 1, "Vec<int>::set(): Improper data string (a:b)");
	buffer.seekg(1, std::ios_base::cur);
	// parse b
	while (buffer.peek() != EOF) {
	  switch (buffer.peek()) {
	  case ' ': case '\t':
	    buffer.seekg(1, std::ios_base::cur);
	    break;

	    // skip '+' sign
	  case '+':
	    // check for not handled '-' sign
	    it_assert(!negative, "Vec<double>::set(): Improper data string "
		      "(-)");
	    buffer.seekg(1, std::ios_base::cur);
	    break;

	    // check for '-' sign
	  case '-':
	    buffer.seekg(1, std::ios_base::cur);
	    negative = true;
	    break;

	    // hexadecimal number or octal number or zero
	  case '0':
	    it_assert(!b_parsed, "Vec<int>::set(): Improper data string "
		      "(a:b)");
	    buffer.seekg(1, std::ios_base::cur);
	    switch (buffer.peek()) {
	      // hexadecimal number
	    case 'x': case 'X':
	      buffer.clear();
	      buffer.seekg(-1, std::ios_base::cur);
	      buffer >> std::hex >> b;
	      it_assert(!buffer.fail(), "Vec<int>::set(): Stream operation "
			"failed (buffer >> hex >> data)");
	      break; // case 'x'...

	      // octal number
	    case '1': case '2': case '3': case '4': case '5': case '6':
	    case '7':
	      buffer.clear();
	      buffer.seekg(-1, std::ios_base::cur);
	      buffer >> std::oct >> b;
	      it_assert(!buffer.fail(), "Vec<int>::set(): Stream operation "
			"failed (buffer >> oct >> data)");
	      break; // case '1'...

	      // zero
	    case EOF: case ' ': case '\t': case ':': case '0':
	      buffer.clear();
	      buffer.seekg(-1, std::ios_base::cur);
	      buffer >> std::dec >> b;
	      it_assert(!buffer.fail(), "Vec<int>::set(): Stream operation "
			"failed (buffer >> dec >> data)");
	      break; // case EOF...

	    default:
	      it_error("Vec<int>::set(): Improper data string (a:b)");
	    } // switch (buffer.peek())
	    // check if just parsed data was negative
	    if (negative) {
	      b = -b;
	      negative = false;
	    }
	    b_parsed = true;
	    break; // case '0'

	    // decimal number
	  case '1': case '2': case '3': case '4': case '5': case '6': case '7':
	  case '8': case '9':
	    it_assert(!b_parsed, "Vec<int>::set(): Improper data string "
		      "(a:b)");
	    buffer.clear();
	    buffer >> std::dec >> b;
	    it_assert(!buffer.fail(), "Vec<int>::set(): Stream operation "
		      "failed (buffer >> dec >> data)");
	    // check if just parsed data was negative
	    if (negative) {
	      b = -b;
	      negative = false;
	    }
	    b_parsed = true;
	    break; // case '1'...

	  case ':':
	    it_assert(b_parsed, "Vec<int>::set(): Improper data string (a:b)");
	    buffer.seekg(1, std::ios_base::cur);
	    // parse c
	    while (buffer.peek() != EOF) {
	      switch (buffer.peek()) {
	      case ' ': case '\t':
		buffer.seekg(1, std::ios_base::cur);
		break;

		// skip '+' sign
	      case '+':
		// check for not handled '-' sign
		it_assert(!negative, "Vec<double>::set(): Improper data "
			  "string (-)");
		buffer.seekg(1, std::ios_base::cur);
		break;

		// check for '-' sign
	      case '-':
		buffer.seekg(1, std::ios_base::cur);
		negative = true;
		break;

		// hexadecimal number or octal number or zero
	      case '0':
		it_assert(!c_parsed, "Vec<int>::set(): Improper data string "
			  "(a:b:c)");
		buffer.seekg(1, std::ios_base::cur);
		switch (buffer.peek()) {
		  // hexadecimal number
		case 'x': case 'X':
		  buffer.clear();
		  buffer.seekg(-1, std::ios_base::cur);
		  buffer >> std::hex >> c;
		  it_assert(!buffer.fail(), "Vec<int>::set(): Stream "
			    "operation failed (buffer >> hex >> data)");
		  break; // case 'x'...

		  // octal number
		case '1': case '2': case '3': case '4':	case '5': case '6':
		case '7':
		  buffer.clear();
		  buffer.seekg(-1, std::ios_base::cur);
		  buffer >> std::oct >> c;
		  it_assert(!buffer.fail(), "Vec<int>::set(): Stream "
			    "operation failed (buffer >> oct >> data)");
		  break; // case '1'...

		  // zero
		case EOF: case ' ': case '\t': case '0':
		  buffer.clear();
		  buffer.seekg(-1, std::ios_base::cur);
		  buffer >> std::dec >> c;
		  it_assert(!buffer.fail(), "Vec<int>::set(): Stream "
			    "operation failed (buffer >> dec >> data)");
		  break; // case EOF...

		default:
		  it_error("Vec<int>::set(): Improper data string (a:b:c)");
		}
		c_parsed = true;
		break; // case '0'

		// decimal number
	      case '1': case '2': case '3': case '4': case '5': case '6':
	      case '7': case '8': case '9':
		it_assert(!c_parsed, "Vec<int>::set(): Improper data string "
			  "(a:b:c)");
		buffer.clear();
		buffer.seekg(-1, std::ios_base::cur);
		buffer >> std::dec >> c;
		it_assert(!buffer.fail(), "Vec<int>::set(): Stream operation "
			  "failed (buffer >> dec >> data)");
		c_parsed = true;
		break;

	      default:
		it_error("Vec<int>::set(): Improper data string (a:b:c)");
	      } // switch (buffer.peek())
	    } // while (buffer.peek() != EOF)
	    // check if just parsed data was negative
	    if (negative) {
	      c = -c;
	      negative = false;
	    }
	    it_assert(c_parsed, "Vec<int>::set(): Improper data string "
		      "(a:b:c)");
	    break; // case ':'

	  default:
	    it_error("Vec<int>::set(): Improper data string (a:b)");
	  } // switch (buffer.peek())
	} // while (buffer.peek() != EOF)

	if (c_parsed) {
	  if (b > 0 && c >= data[pos-1]) {
	    while (data[pos-1] + b <= c) {
	      if (++pos > maxpos) {
		maxpos <<= 1;
		set_size(maxpos, true);
	      }
	      data[pos-1] = data[pos-2] + b;
	    }
	  }
	  else if (b < 0 && c <= data[pos-1]) {
	    while (data[pos-1] + b >= c) {
	      if (++pos > maxpos) {
		maxpos <<= 1;
		set_size(maxpos, true);
	      }
	      data[pos-1] = data[pos-2] + b;
	    }
	  }
	  else if (b == 0 && c == data[pos-1]) {
	    break;
	  }
	  else {
	    it_error("Vec<int>::set(): Improper data string (a:b:c)");
	  }
	} // if (c_parsed)
	else if (b_parsed) {
	  if (b < data[pos-1]) {
	    while (data[pos-1] > b) {
	      if (++pos > maxpos) {
		maxpos <<= 1;
		set_size(maxpos, true);
	      }
	      data[pos-1] = data[pos-2] - 1;
	    }
	  }
	  else {
	    while (data[pos-1] < b) {
	      if (++pos > maxpos) {
		maxpos <<= 1;
		set_size(maxpos, true);
	      }
	      data[pos-1] = data[pos-2] + 1;
	    }
	  }
	} // else if (b_parsed)
	else {
	  it_error("Vec<int>::set(): Improper data string (a:b)");
	}
	break; // case ':'

      default:
	it_error("Vec<int>::set(): Improper data string");
      }
    }
    // resize the parsed vector to its final length
    set_size(pos, true);
  }

  template<>
  void Vec<short int>::set(const std::string &str)
  {
    // parser for "short int" is the same as for "int", so reuse it here
    ivec iv(str);
    this->operator=(to_svec(iv));
  }


  template<>
  bvec Vec<std::complex<double> >::operator==(std::complex<double>) const
  {
    it_error("operator==: not implemented for complex");
    bvec temp;
    return temp;
  }

  template<>
  bvec Vec<std::complex<double> >::operator!=(std::complex<double>) const
  {
    it_error("operator!=: not implemented for complex");
    bvec temp;
    return temp;
  }

  template<>
  bvec Vec<std::complex<double> >::operator<=(std::complex<double>) const
  {
    it_error("operator<=: not implemented for complex");
    bvec temp;
    return temp;
  }

  template<>
  bvec Vec<std::complex<double> >::operator>(std::complex<double>) const
  {
    it_error("operator>: not implemented for complex");
    bvec temp;
    return temp;
  }

  template<>
  bvec Vec<std::complex<double> >::operator<(std::complex<double>) const
  {
    it_error("operator<: not implemented for complex");
    bvec temp;
    return temp;
  }

  template<>
  bvec Vec<std::complex<double> >::operator>=(std::complex<double>) const
  {
    it_error("operator>=: not implemented for complex");
    bvec temp;
    return temp;
  }

  template<>
  Mat<std::complex<double> > Vec<std::complex<double> >::hermitian_transpose() const
  {
    Mat<std::complex<double> > temp(1, datasize);
    for (int i=0; i<datasize; i++)
      temp(i) = std::conj(data[i]);

    return temp;
  }


  //---------------------------------------------------------------------
  // Instantiations
  //---------------------------------------------------------------------

  template class Vec<double>;
  template class Vec<int>;
  template class Vec<short int>;
  template class Vec<std::complex<double> >;
  template class Vec<bin>;

  // addition operator

  template vec operator+(const vec &v1, const vec &v2);
  template cvec operator+(const cvec &v1, const cvec &v2);
  template ivec operator+(const ivec &v1, const ivec &v2);
  template svec operator+(const svec &v1, const svec &v2);
  template bvec operator+(const bvec &v1, const bvec &v2);

  template vec operator+(const vec &v1, double t);
  template cvec operator+(const cvec &v1, std::complex<double> t);
  template ivec operator+(const ivec &v1, int t);
  template svec operator+(const svec &v1, short t);
  template bvec operator+(const bvec &v1, bin t);

  template vec operator+(double t, const vec &v1);
  template cvec operator+(std::complex<double> t, const cvec &v1);
  template ivec operator+(int t, const ivec &v1);
  template svec operator+(short t, const svec &v1);
  template bvec operator+(bin t, const bvec &v1);

  // subraction operator

  template vec operator-(const vec &v1, const vec &v2);
  template cvec operator-(const cvec &v1, const cvec &v2);
  template ivec operator-(const ivec &v1, const ivec &v2);
  template svec operator-(const svec &v1, const svec &v2);
  template bvec operator-(const bvec &v1, const bvec &v2);

  template vec operator-(const vec &v, double t);
  template cvec operator-(const cvec &v, std::complex<double> t);
  template ivec operator-(const ivec &v, int t);
  template svec operator-(const svec &v, short t);
  template bvec operator-(const bvec &v, bin t);

  template vec operator-(double t, const vec &v);
  template cvec operator-(std::complex<double> t, const cvec &v);
  template ivec operator-(int t, const ivec &v);
  template svec operator-(short t, const svec &v);
  template bvec operator-(bin t, const bvec &v);

  // unary minus

  template vec operator-(const vec &v);
  template cvec operator-(const cvec &v);
  template ivec operator-(const ivec &v);
  template svec operator-(const svec &v);
  template bvec operator-(const bvec &v);

  // multiplication operator

#if !defined(HAVE_BLAS)
  template double dot(const vec &v1, const vec &v2);
#if !(defined(HAVE_ZDOTUSUB) || defined(HAVE_ZDOTU_VOID))
  template std::complex<double> dot(const cvec &v1, const cvec &v2);
#endif // !(HAVE_ZDOTUSUB || HAVE_ZDOTU_VOID)
#endif // HAVE_BLAS
  template int dot(const ivec &v1, const ivec &v2);
  template short dot(const svec &v1, const svec &v2);
  template bin dot(const bvec &v1, const bvec &v2);

#if !defined(HAVE_BLAS)
  template double operator*(const vec &v1, const vec &v2);
  template std::complex<double> operator*(const cvec &v1, const cvec &v2);
#endif
  template int operator*(const ivec &v1, const ivec &v2);
  template short operator*(const svec &v1, const svec &v2);
  template bin operator*(const bvec &v1, const bvec &v2);

#if !defined(HAVE_BLAS)
  template mat outer_product(const vec &v1, const vec &v2, bool hermitian);
#endif
  template imat outer_product(const ivec &v1, const ivec &v2, bool hermitian);
  template smat outer_product(const svec &v1, const svec &v2, bool hermitian);
  template bmat outer_product(const bvec &v1, const bvec &v2, bool hermitian);

  template vec operator*(const vec &v, double t);
  template cvec operator*(const cvec &v, std::complex<double> t);
  template ivec operator*(const ivec &v, int t);
  template svec operator*(const svec &v, short t);
  template bvec operator*(const bvec &v, bin t);

  template vec operator*(double t, const vec &v);
  template cvec operator*(std::complex<double> t, const cvec &v);
  template ivec operator*(int t, const ivec &v);
  template svec operator*(short t, const svec &v);
  template bvec operator*(bin t, const bvec &v);

  // elementwise multiplication

  template vec elem_mult(const vec &a, const vec &b);
  template cvec elem_mult(const cvec &a, const cvec &b);
  template ivec elem_mult(const ivec &a, const ivec &b);
  template svec elem_mult(const svec &a, const svec &b);
  template bvec elem_mult(const bvec &a, const bvec &b);

  template void elem_mult_out(const vec &a, const vec &b, vec &out);
  template void elem_mult_out(const cvec &a, const cvec &b, cvec &out);
  template void elem_mult_out(const ivec &a, const ivec &b, ivec &out);
  template void elem_mult_out(const svec &a, const svec &b, svec &out);
  template void elem_mult_out(const bvec &a, const bvec &b, bvec &out);

  template vec elem_mult(const vec &a, const vec &b, const vec &c);
  template cvec elem_mult(const cvec &a, const cvec &b, const cvec &c);
  template ivec elem_mult(const ivec &a, const ivec &b, const ivec &c);
  template svec elem_mult(const svec &a, const svec &b, const svec &c);
  template bvec elem_mult(const bvec &a, const bvec &b, const bvec &c);

  template void elem_mult_out(const vec &a, const vec &b, const vec &c,
                              vec &out);
  template void elem_mult_out(const cvec &a, const cvec &b, const cvec &c,
                              cvec &out);
  template void elem_mult_out(const ivec &a, const ivec &b, const ivec &c,
                              ivec &out);
  template void elem_mult_out(const svec &a, const svec &b, const svec &c,
                              svec &out);
  template void elem_mult_out(const bvec &a, const bvec &b, const bvec &c,
                              bvec &out);

  template vec elem_mult(const vec &a, const vec &b, const vec &c,
                         const vec &d);
  template cvec elem_mult(const cvec &a, const cvec &b, const cvec &c,
                          const cvec &d);
  template ivec elem_mult(const ivec &a, const ivec &b, const ivec &c,
                          const ivec &d);
  template svec elem_mult(const svec &a, const svec &b, const svec &c,
                          const svec &d);
  template bvec elem_mult(const bvec &a, const bvec &b, const bvec &c,
                          const bvec &d);

  template void elem_mult_out(const vec &a, const vec &b, const vec &c,
                              const vec &d, vec &out);
  template void elem_mult_out(const cvec &a, const cvec &b, const cvec &c,
                              const cvec &d, cvec &out);
  template void elem_mult_out(const ivec &a, const ivec &b, const ivec &c,
                              const ivec &d, ivec &out);
  template void elem_mult_out(const svec &a, const svec &b, const svec &c,
                              const svec &d, svec &out);
  template void elem_mult_out(const bvec &a, const bvec &b, const bvec &c,
                              const bvec &d, bvec &out);

  // in-place elementwise multiplication

  template void elem_mult_inplace(const vec &a, vec &b);
  template void elem_mult_inplace(const cvec &a, cvec &b);
  template void elem_mult_inplace(const ivec &a, ivec &b);
  template void elem_mult_inplace(const svec &a, svec &b);
  template void elem_mult_inplace(const bvec &a, bvec &b);

  // elementwise multiplication followed by summation

  template double elem_mult_sum(const vec &a, const vec &b);
  template std::complex<double> elem_mult_sum(const cvec &a, const cvec &b);
  template int elem_mult_sum(const ivec &a, const ivec &b);
  template short elem_mult_sum(const svec &a, const svec &b);
  template bin elem_mult_sum(const bvec &a, const bvec &b);

  // division operator

  template vec operator/(const vec &v, double t);
  template cvec operator/(const cvec &v, std::complex<double> t);
  template ivec operator/(const ivec &v, int t);
  template svec operator/(const svec &v, short t);
  template bvec operator/(const bvec &v, bin t);

  template vec operator/(double t, const vec &v);
  template cvec operator/(std::complex<double> t, const cvec &v);
  template ivec operator/(int t, const ivec &v);
  template svec operator/(short t, const svec &v);
  template bvec operator/(bin t, const bvec &v);

  // elementwise division operator

  template vec elem_div(const vec &a, const vec &b);
  template cvec elem_div(const cvec &a, const cvec &b);
  template ivec elem_div(const ivec &a, const ivec &b);
  template svec elem_div(const svec &a, const svec &b);
  template bvec elem_div(const bvec &a, const bvec &b);

  template vec elem_div(double t, const vec &v);
  template cvec elem_div(std::complex<double> t, const cvec &v);
  template ivec elem_div(int t, const ivec &v);
  template svec elem_div(short t, const svec &v);
  template bvec elem_div(bin t, const bvec &v);

  template void elem_div_out(const vec &a, const vec &b, vec &out);
  template void elem_div_out(const cvec &a, const cvec &b, cvec &out);
  template void elem_div_out(const ivec &a, const ivec &b, ivec &out);
  template void elem_div_out(const svec &a, const svec &b, svec &out);
  template void elem_div_out(const bvec &a, const bvec &b, bvec &out);

  // elementwise division followed by summation

  template double elem_div_sum(const vec &a, const vec &b);
  template std::complex<double> elem_div_sum(const cvec &a, const cvec &b);
  template int elem_div_sum(const ivec &a, const ivec &b);
  template short elem_div_sum(const svec &a, const svec &b);
  template bin elem_div_sum(const bvec &a, const bvec &b);

  // concat operator

  template vec concat(const vec &v, double a);
  template cvec concat(const cvec &v, std::complex<double> a);
  template ivec concat(const ivec &v, int a);
  template svec concat(const svec &v, short a);
  template bvec concat(const bvec &v, bin a);

  template vec concat(double a, const vec &v);
  template cvec concat(std::complex<double> a, const cvec &v);
  template ivec concat(int a, const ivec &v);
  template svec concat(short a, const svec &v);
  template bvec concat(bin a, const bvec &v);

  template vec concat(const vec &v1, const vec &v2);
  template cvec concat(const cvec &v1, const cvec &v2);
  template ivec concat(const ivec &v1, const ivec &v2);
  template svec concat(const svec &v1, const svec &v2);
  template bvec concat(const bvec &v1, const bvec &v2);

  template vec concat(const vec &v1, const vec &v2, const vec &v3);
  template cvec concat(const cvec &v1, const cvec &v2, const cvec &v3);
  template ivec concat(const ivec &v1, const ivec &v2, const ivec &v3);
  template svec concat(const svec &v1, const svec &v2, const svec &v3);
  template bvec concat(const bvec &v1, const bvec &v2, const bvec &v3);

  template vec concat(const vec &v1, const vec &v2,
                      const vec &v3, const vec &v4);
  template cvec concat(const cvec &v1, const cvec &v2,
                       const cvec &v3, const cvec &v4);
  template ivec concat(const ivec &v1, const ivec &v2,
                       const ivec &v3, const ivec &v4);
  template svec concat(const svec &v1, const svec &v2,
                       const svec &v3, const svec &v4);
  template bvec concat(const bvec &v1, const bvec &v2,
                       const bvec &v3, const bvec &v4);

  template vec concat(const vec &v1, const vec &v2, const vec &v3,
                      const vec &v4, const vec &v5);
  template cvec concat(const cvec &v1, const cvec &v2, const cvec &v3,
                       const cvec &v4, const cvec &v5);
  template ivec concat(const ivec &v1, const ivec &v2, const ivec &v3,
                       const ivec &v4, const ivec &v5);
  template svec concat(const svec &v1, const svec &v2, const svec &v3,
                       const svec &v4, const svec &v5);
  template bvec concat(const bvec &v1, const bvec &v2, const bvec &v3,
                       const bvec &v4, const bvec &v5);

  // I/O streams

  template std::ostream &operator<<(std::ostream& os, const vec &vect);
  template std::ostream &operator<<(std::ostream& os, const cvec &vect);
  template std::ostream &operator<<(std::ostream& os, const svec &vect);
  template std::ostream &operator<<(std::ostream& os, const ivec &vect);
  template std::ostream &operator<<(std::ostream& os, const bvec &vect);
  template std::istream &operator>>(std::istream& is, vec &vect);
  template std::istream &operator>>(std::istream& is, cvec &vect);
  template std::istream &operator>>(std::istream& is, svec &vect);
  template std::istream &operator>>(std::istream& is, ivec &vect);
  template std::istream &operator>>(std::istream& is, bvec &vect);

} // namespace itpp

//! \endcond