BSD 4_3_Net_2 release
[unix-history] / usr / src / lib / libg++ / g++-include / gen / Vec.hP
CommitLineData
936f52d6
C
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
25#ifndef _<T>Vec_h
26#ifdef __GNUG__
27#pragma once
28#pragma interface
29#endif
30#define _<T>Vec_h 1
31
32#ifndef _<T>_typedefs
33#define _<T>_typedefs 1
34typedef void (*<T>Procedure)(<T&>);
35typedef <T> (*<T>Mapper)(<T&>);
36typedef <T> (*<T>Combiner)(<T&>, <T&>);
37typedef int (*<T>Predicate)(<T&>);
38typedef int (*<T>Comparator)(<T&>, <T&>);
39#endif
40
41
42class <T>Vec
43{
44protected:
45 int len;
46 <T> *s;
47
48 <T>Vec(int l, <T>* d);
49public:
50 <T>Vec ();
51 <T>Vec (int l);
52 <T>Vec (int l, <T&> fill_value);
53 <T>Vec (<T>Vec&);
54 ~<T>Vec ();
55
56 <T>Vec & operator = (<T>Vec & a);
57 <T>Vec at(int from = 0, int n = -1);
58
59 int capacity();
60 void resize(int newlen);
61
62 <T>& operator [] (int n);
63 <T>& elem(int n);
64
65 friend <T>Vec concat(<T>Vec & a, <T>Vec & b);
66 friend <T>Vec map(<T>Mapper f, <T>Vec & a);
67 friend <T>Vec merge(<T>Vec & a, <T>Vec & b, <T>Comparator f);
68 friend <T>Vec combine(<T>Combiner f, <T>Vec & a, <T>Vec & b);
69 friend <T>Vec reverse(<T>Vec & a);
70
71 void reverse();
72 void sort(<T>Comparator f);
73 void fill(<T&> val, int from = 0, int n = -1);
74
75 void apply(<T>Procedure f);
76 <T> reduce(<T>Combiner f, <T&> base);
77 int index(<T&> targ);
78
79 friend int operator == (<T>Vec& a, <T>Vec& b);
80 friend int operator != (<T>Vec& a, <T>Vec& b);
81
82 void error(const char* msg);
83 void range_error();
84};
85
86extern void default_<T>Vec_error_handler(const char*);
87extern one_arg_error_handler_t <T>Vec_error_handler;
88
89extern one_arg_error_handler_t
90 set_<T>Vec_error_handler(one_arg_error_handler_t f);
91
92
93#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
94
95inline <T>Vec::<T>Vec()
96{
97 len = 0; s = 0;
98}
99
100inline <T>Vec::<T>Vec(int l)
101{
102 s = new <T> [len = l];
103}
104
105
106inline <T>Vec::<T>Vec(int l, <T>* d) :len(l), s(d) {}
107
108
109inline <T>Vec::~<T>Vec()
110{
111 delete[len] s;
112}
113
114
115inline <T>& <T>Vec::operator [] (int n)
116{
117 if ((unsigned)n >= len)
118 range_error();
119 return s[n];
120}
121
122inline <T>& <T>Vec::elem(int n)
123{
124 return s[n];
125}
126
127
128inline int <T>Vec::capacity()
129{
130 return len;
131}
132
133
134
135inline int operator != (<T>Vec& a, <T>Vec& b)
136{
137 return !(a == b);
138}
139
140
141#endif
142#endif