Skip to content

Commit 5d6de72

Browse files
committed
Implement array
1 parent eb58534 commit 5d6de72

File tree

5 files changed

+432
-14
lines changed

5 files changed

+432
-14
lines changed

bvt/bvt28.cc

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// This file is part of the uSTL library, an STL implementation.
2+
//
3+
// Copyright (c) 2005 by Mike Sharov <msharov@users.sourceforge.net>
4+
// This file is free software, distributed under the MIT License.
5+
6+
#include "stdtest.h"
7+
8+
template <typename T, size_t N>
9+
void TestArray (const char* ctrType)
10+
{
11+
cout << "================================================" << endl;
12+
cout << "Testing " << ctrType << endl;
13+
cout << "================================================" << endl;
14+
assert (N <= 8);
15+
#if HAVE_CPP11
16+
array<T,N> pt1 ({1,2,3,4,5,6,7,8});
17+
array<T,N> pt2;
18+
pt2 = {4,4,4,4};
19+
pt2 += {1,2,3,4};
20+
#else
21+
T pt1v[12] = { 1, 2, 3, 4, 5, 6, 7, 8 };
22+
array<T,N> pt1 (pt1v);
23+
array<T,N> pt2 (&pt1v[4]);
24+
#endif
25+
26+
cout << "pt1:\t\t\tsize = " << pt1.size() << ", value = " << pt1 << endl;
27+
cout << "pt2:\t\t\t" << pt2 << endl;
28+
iota (pt2.begin(), pt2.end(), 10);
29+
cout << "pt2:\t\t\t" << pt2 << endl;
30+
31+
pt1 *= 3;
32+
cout << "pt1 *= 3:\t\t" << pt1 << endl;
33+
pt1 /= 3;
34+
cout << "pt1 /= 3:\t\t" << pt1 << endl;
35+
pt1 += 3;
36+
cout << "pt1 += 3:\t\t" << pt1 << endl;
37+
pt1 -= 3;
38+
cout << "pt1 -= 3:\t\t" << pt1 << endl;
39+
40+
pt1 *= pt2;
41+
cout << "pt1 *= pt2:\t\t" << pt1 << endl;
42+
pt1 /= pt2;
43+
cout << "pt1 /= pt2:\t\t" << pt1 << endl;
44+
pt1 += pt2;
45+
cout << "pt1 += pt2:\t\t" << pt1 << endl;
46+
pt1 -= pt2;
47+
cout << "pt1 -= pt2:\t\t" << pt1 << endl;
48+
49+
pt1 = pt1 * pt2;
50+
cout << "pt1 = pt1 * pt2:\t" << pt1 << endl;
51+
pt1 = pt1 / pt2;
52+
cout << "pt1 = pt1 / pt2:\t" << pt1 << endl;
53+
pt1 = pt1 + pt2;
54+
cout << "pt1 = pt1 + pt2:\t" << pt1 << endl;
55+
pt1 = pt1 - pt2;
56+
cout << "pt1 = pt1 - pt2:\t" << pt1 << endl;
57+
}
58+
59+
void TestIntegralArrays (void)
60+
{
61+
TestArray<float,4> ("array<float,4>");
62+
TestArray<float,2> ("array<float,2>");
63+
TestArray<int32_t,4> ("array<int32_t,4>");
64+
TestArray<uint32_t,4> ("array<uint32_t,4>");
65+
TestArray<int32_t,2> ("array<int32_t,2>");
66+
TestArray<uint32_t,2> ("array<uint32_t,2>");
67+
TestArray<int16_t,4> ("array<int16_t,4>");
68+
TestArray<uint16_t,4> ("array<uint16_t,4>");
69+
TestArray<int8_t,8> ("array<int8_t,8>");
70+
TestArray<uint8_t,8> ("array<uint8_t,8>");
71+
72+
cout << "================================================" << endl;
73+
cout << "Testing array<string,3>" << endl;
74+
cout << "================================================" << endl;
75+
array<string,3> strv;
76+
strv[0] = "str0";
77+
strv[1] = "str1";
78+
strv[2] = "str2";
79+
cout << "str: " << strv << endl;
80+
}
81+
82+
StdBvtMain (TestIntegralArrays)

bvt/bvt28.std

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
================================================
2+
Testing array<float,4>
3+
================================================
4+
pt1: size = 4, value = (1.00,2.00,3.00,4.00)
5+
pt2: (5.00,6.00,7.00,8.00)
6+
pt2: (10.00,11.00,12.00,13.00)
7+
pt1 *= 3: (3.00,6.00,9.00,12.00)
8+
pt1 /= 3: (1.00,2.00,3.00,4.00)
9+
pt1 += 3: (4.00,5.00,6.00,7.00)
10+
pt1 -= 3: (1.00,2.00,3.00,4.00)
11+
pt1 *= pt2: (10.00,22.00,36.00,52.00)
12+
pt1 /= pt2: (1.00,2.00,3.00,4.00)
13+
pt1 += pt2: (11.00,13.00,15.00,17.00)
14+
pt1 -= pt2: (1.00,2.00,3.00,4.00)
15+
pt1 = pt1 * pt2: (10.00,22.00,36.00,52.00)
16+
pt1 = pt1 / pt2: (1.00,2.00,3.00,4.00)
17+
pt1 = pt1 + pt2: (11.00,13.00,15.00,17.00)
18+
pt1 = pt1 - pt2: (1.00,2.00,3.00,4.00)
19+
================================================
20+
Testing array<float,2>
21+
================================================
22+
pt1: size = 2, value = (1.00,2.00)
23+
pt2: (5.00,6.00)
24+
pt2: (10.00,11.00)
25+
pt1 *= 3: (3.00,6.00)
26+
pt1 /= 3: (1.00,2.00)
27+
pt1 += 3: (4.00,5.00)
28+
pt1 -= 3: (1.00,2.00)
29+
pt1 *= pt2: (10.00,22.00)
30+
pt1 /= pt2: (1.00,2.00)
31+
pt1 += pt2: (11.00,13.00)
32+
pt1 -= pt2: (1.00,2.00)
33+
pt1 = pt1 * pt2: (10.00,22.00)
34+
pt1 = pt1 / pt2: (1.00,2.00)
35+
pt1 = pt1 + pt2: (11.00,13.00)
36+
pt1 = pt1 - pt2: (1.00,2.00)
37+
================================================
38+
Testing array<int32_t,4>
39+
================================================
40+
pt1: size = 4, value = (1,2,3,4)
41+
pt2: (5,6,7,8)
42+
pt2: (10,11,12,13)
43+
pt1 *= 3: (3,6,9,12)
44+
pt1 /= 3: (1,2,3,4)
45+
pt1 += 3: (4,5,6,7)
46+
pt1 -= 3: (1,2,3,4)
47+
pt1 *= pt2: (10,22,36,52)
48+
pt1 /= pt2: (1,2,3,4)
49+
pt1 += pt2: (11,13,15,17)
50+
pt1 -= pt2: (1,2,3,4)
51+
pt1 = pt1 * pt2: (10,22,36,52)
52+
pt1 = pt1 / pt2: (1,2,3,4)
53+
pt1 = pt1 + pt2: (11,13,15,17)
54+
pt1 = pt1 - pt2: (1,2,3,4)
55+
================================================
56+
Testing array<uint32_t,4>
57+
================================================
58+
pt1: size = 4, value = (1,2,3,4)
59+
pt2: (5,6,7,8)
60+
pt2: (10,11,12,13)
61+
pt1 *= 3: (3,6,9,12)
62+
pt1 /= 3: (1,2,3,4)
63+
pt1 += 3: (4,5,6,7)
64+
pt1 -= 3: (1,2,3,4)
65+
pt1 *= pt2: (10,22,36,52)
66+
pt1 /= pt2: (1,2,3,4)
67+
pt1 += pt2: (11,13,15,17)
68+
pt1 -= pt2: (1,2,3,4)
69+
pt1 = pt1 * pt2: (10,22,36,52)
70+
pt1 = pt1 / pt2: (1,2,3,4)
71+
pt1 = pt1 + pt2: (11,13,15,17)
72+
pt1 = pt1 - pt2: (1,2,3,4)
73+
================================================
74+
Testing array<int32_t,2>
75+
================================================
76+
pt1: size = 2, value = (1,2)
77+
pt2: (5,6)
78+
pt2: (10,11)
79+
pt1 *= 3: (3,6)
80+
pt1 /= 3: (1,2)
81+
pt1 += 3: (4,5)
82+
pt1 -= 3: (1,2)
83+
pt1 *= pt2: (10,22)
84+
pt1 /= pt2: (1,2)
85+
pt1 += pt2: (11,13)
86+
pt1 -= pt2: (1,2)
87+
pt1 = pt1 * pt2: (10,22)
88+
pt1 = pt1 / pt2: (1,2)
89+
pt1 = pt1 + pt2: (11,13)
90+
pt1 = pt1 - pt2: (1,2)
91+
================================================
92+
Testing array<uint32_t,2>
93+
================================================
94+
pt1: size = 2, value = (1,2)
95+
pt2: (5,6)
96+
pt2: (10,11)
97+
pt1 *= 3: (3,6)
98+
pt1 /= 3: (1,2)
99+
pt1 += 3: (4,5)
100+
pt1 -= 3: (1,2)
101+
pt1 *= pt2: (10,22)
102+
pt1 /= pt2: (1,2)
103+
pt1 += pt2: (11,13)
104+
pt1 -= pt2: (1,2)
105+
pt1 = pt1 * pt2: (10,22)
106+
pt1 = pt1 / pt2: (1,2)
107+
pt1 = pt1 + pt2: (11,13)
108+
pt1 = pt1 - pt2: (1,2)
109+
================================================
110+
Testing array<int16_t,4>
111+
================================================
112+
pt1: size = 4, value = (1,2,3,4)
113+
pt2: (5,6,7,8)
114+
pt2: (10,11,12,13)
115+
pt1 *= 3: (3,6,9,12)
116+
pt1 /= 3: (1,2,3,4)
117+
pt1 += 3: (4,5,6,7)
118+
pt1 -= 3: (1,2,3,4)
119+
pt1 *= pt2: (10,22,36,52)
120+
pt1 /= pt2: (1,2,3,4)
121+
pt1 += pt2: (11,13,15,17)
122+
pt1 -= pt2: (1,2,3,4)
123+
pt1 = pt1 * pt2: (10,22,36,52)
124+
pt1 = pt1 / pt2: (1,2,3,4)
125+
pt1 = pt1 + pt2: (11,13,15,17)
126+
pt1 = pt1 - pt2: (1,2,3,4)
127+
================================================
128+
Testing array<uint16_t,4>
129+
================================================
130+
pt1: size = 4, value = (1,2,3,4)
131+
pt2: (5,6,7,8)
132+
pt2: (10,11,12,13)
133+
pt1 *= 3: (3,6,9,12)
134+
pt1 /= 3: (1,2,3,4)
135+
pt1 += 3: (4,5,6,7)
136+
pt1 -= 3: (1,2,3,4)
137+
pt1 *= pt2: (10,22,36,52)
138+
pt1 /= pt2: (1,2,3,4)
139+
pt1 += pt2: (11,13,15,17)
140+
pt1 -= pt2: (1,2,3,4)
141+
pt1 = pt1 * pt2: (10,22,36,52)
142+
pt1 = pt1 / pt2: (1,2,3,4)
143+
pt1 = pt1 + pt2: (11,13,15,17)
144+
pt1 = pt1 - pt2: (1,2,3,4)
145+
================================================
146+
Testing array<int8_t,8>
147+
================================================
148+
pt1: size = 8, value = (1,2,3,4,5,6,7,8)
149+
pt2: (5,6,7,8,5,6,7,8)
150+
pt2: (10,11,12,13,14,15,16,17)
151+
pt1 *= 3: (3,6,9,12,15,18,21,24)
152+
pt1 /= 3: (1,2,3,4,5,6,7,8)
153+
pt1 += 3: (4,5,6,7,8,9,10,11)
154+
pt1 -= 3: (1,2,3,4,5,6,7,8)
155+
pt1 *= pt2: (10,22,'$','4','F','Z','p',-120)
156+
pt1 /= pt2: (1,2,3,4,5,6,7,-7)
157+
pt1 += pt2: (11,13,15,17,19,21,23,10)
158+
pt1 -= pt2: (1,2,3,4,5,6,7,-7)
159+
pt1 = pt1 * pt2: (10,22,'$','4','F','Z','p',-119)
160+
pt1 = pt1 / pt2: (1,2,3,4,5,6,7,-7)
161+
pt1 = pt1 + pt2: (11,13,15,17,19,21,23,10)
162+
pt1 = pt1 - pt2: (1,2,3,4,5,6,7,-7)
163+
================================================
164+
Testing array<uint8_t,8>
165+
================================================
166+
pt1: size = 8, value = (1,2,3,4,5,6,7,8)
167+
pt2: (5,6,7,8,5,6,7,8)
168+
pt2: (10,11,12,13,14,15,16,17)
169+
pt1 *= 3: (3,6,9,12,15,18,21,24)
170+
pt1 /= 3: (1,2,3,4,5,6,7,8)
171+
pt1 += 3: (4,5,6,7,8,9,10,11)
172+
pt1 -= 3: (1,2,3,4,5,6,7,8)
173+
pt1 *= pt2: (10,22,'$','4','F','Z','p',136)
174+
pt1 /= pt2: (1,2,3,4,5,6,7,8)
175+
pt1 += pt2: (11,13,15,17,19,21,23,25)
176+
pt1 -= pt2: (1,2,3,4,5,6,7,8)
177+
pt1 = pt1 * pt2: (10,22,'$','4','F','Z','p',136)
178+
pt1 = pt1 / pt2: (1,2,3,4,5,6,7,8)
179+
pt1 = pt1 + pt2: (11,13,15,17,19,21,23,25)
180+
pt1 = pt1 - pt2: (1,2,3,4,5,6,7,8)
181+
================================================
182+
Testing array<string,3>
183+
================================================
184+
str: (str0,str1,str2)

docs/index.html

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ <h2>Contents</h2>
5252
<li><a href="#Algorithms">Algorithms</a></li>
5353
<li><a href="#Memblocks">Memblock and Memlink</a></li>
5454
<li><a href="#Streams">Streams</a></li>
55-
<li><a href="#Tuples">Tuples</a></li>
55+
<li><a href="#Arrays">Arrays</a></li>
5656
<li><a href="#Exceptions">Exceptions</a></li>
5757
<li><a href="#Savings">Template Bloat Be Gone</a></li>
5858
<li><a href="#Contact">Bug reporting</a></li>
@@ -514,23 +514,23 @@ <h2><a name="Streams">Streams</a></h2>
514514

515515
} // namespace myns
516516
</pre>
517-
<h2><a name="Tuples">Tuples</a></h2>
517+
<h2><a name="Arrays">Arrays</a></h2>
518518
<p>
519-
One last container I'll mention is a <code>tuple</code>, which is a
520-
fixed-size array of identical elements. No, it's not the same as the tuple
521-
in boost, which is more like a template-defined struct. This one should
522-
have been named "array", which is what it will be called in the next STL
523-
standard, but I guess I'm stuck with the name now. What are they good
524-
for? Graphical objects. Points, sizes, rectangles, triangles, etc. As a
525-
bonus, operations on tuples can automatically use SIMD instructions if
526-
they are available. Any fixed size-array also works better as a tuple,
527-
since it becomes a standard STL container, which you can use with any
528-
algorithm, copy by assignment, initialize in the constructor, etc.
519+
One last container I'll mention is <code>array</code>, which is
520+
a fixed-size array of identical elements. There is also the older
521+
version of this called <code>tuple</code>, with template parameters
522+
in reversed order, written for uSTL before C++11 came out with
523+
<code>array</code>. These classes are good for graphical objects like
524+
points, sizes, rectangles, triangles, etc. tuples have SIMD instruction
525+
optimizations, which may or may not be a benefit. Any C-style fixed
526+
size-array works better as one of these, since it becomes a standard
527+
STL container, which you can use with any algorithm, copy by assignment,
528+
initialize in the constructor, etc.
529529
</p><pre>
530-
typedef int32_t coord_t;
530+
typedef short int coord_t;
531531
typedef tuple&lt;2, coord_t&gt; Point2d;
532532
typedef tuple&lt;2, coord_t&gt; Size2d;
533-
typedef tuple&lt;2, Point2d&gt; Rect;
533+
typedef array&lt;Point2d, 2&gt; Rect;
534534

535535
Rect r (Point2d (1,2), Point2d (3,4));
536536
r += Size2d (4, 4);

0 commit comments

Comments
 (0)