-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuple.h
executable file
·74 lines (57 loc) · 1.43 KB
/
tuple.h
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
#ifndef TUPLE_H
#define TUPLE_H
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
// This is eugene going a little nuts with C++
template <typename TYPE, unsigned SIZE>
class tuple
{
public:
tuple() { }
tuple(TYPE first, ...)
{
data[ 0 ] = first;
va_list args;
va_start( args, first );
for( unsigned i = 1; i < SIZE; ++i )
{
data[ i ] = va_arg( args, TYPE );
}
va_end( args );
}
tuple(const TYPE array[SIZE]) {
memcpy(data, array, SIZE * sizeof(TYPE));
}
tuple(const tuple& other) {
memcpy(this->data, &other.data, SIZE * sizeof(TYPE));
}
tuple& operator=(const tuple& other) {
if (&other != this)
memcpy(this->data, &other.data, SIZE * sizeof(TYPE));
return *this;
}
tuple& operator=(const TYPE array[SIZE]) {
memcpy(data, array, SIZE * sizeof(TYPE));
return *this;
}
inline TYPE& operator[](unsigned i) {
return data[i];
}
inline const TYPE& operator[](unsigned i) const {
return data[i];
}
private:
TYPE data[SIZE];
};
template <typename TYPE, unsigned SIZE>
std::ostream& operator<<(std::ostream &out, const tuple<TYPE,SIZE> x)
{
out << '[';
for (unsigned i=0; i<SIZE; i++)
out << ' ' << x[i];
return out << " ]" << std::flush;
}
#endif