Browse code

added the Rdisop package (2nd try)

git-svn-id: https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/Rdisop@27972 bc3139a8-67e5-0310-9ffc-ced21a209358

hpages@fhcrc.org authored on 12/10/2007 19:36:30
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,168 @@
1
+// this header defines template object matrix, and also necessary row
2
+#ifndef IMS_MATRIX_H
3
+#define IMS_MATRIX_H
4
+
5
+#include <vector>
6
+
7
+namespace ims {
8
+
9
+////////////////////////////////////////////////////////////////////////////////
10
+// row type necessary for matrix
11
+
12
+template < typename T , typename A = std::allocator<T> >
13
+class row {
14
+public:
15
+	//some typedefs (see Stroustrup pp 471)
16
+	typedef T value_type;
17
+	typedef typename A::size_type size_type;
18
+	typedef std::vector<value_type, A> data_type;
19
+
20
+	//iterators
21
+	//typedef data_type::iterator iterator;
22
+	typedef typename data_type::const_iterator const_iterator;
23
+	
24
+	//references
25
+	typedef typename A::reference reference;
26
+	typedef typename A::const_reference const_reference;
27
+
28
+	//constructors
29
+	row (size_type m, value_type v = value_type()) { data = std::vector<value_type> (m,v); } 
30
+
31
+	//index access
32
+	reference			operator[] (size_type j) { return data[j]; }
33
+	const_reference		operator[] (size_type j) const { return data[j]; }
34
+
35
+	//get iterators
36
+	const_iterator	begin() const { return data.begin(); }
37
+	const_iterator	end() const { return data.end(); }
38
+
39
+	//misc
40
+	size_type		size() const { return data.size(); }
41
+private:
42
+	//data field
43
+	data_type data;
44
+};
45
+
46
+
47
+template < typename T, typename A = std::allocator<T> >
48
+class matrix {
49
+public:
50
+	//some typedefs (see Stroustrup pp 471)
51
+	typedef T value_type;
52
+	typedef typename A::size_type size_type;
53
+	typedef row<value_type> row_type;
54
+	typedef std::vector<row_type> data_type;
55
+
56
+	//row iteratorsessene
57
+//	typedef typename data_type::iterator row_iterator;
58
+	typedef typename data_type::const_iterator const_row_iterator;
59
+
60
+	//row reference types
61
+	typedef row_type& row_reference;
62
+	typedef const row_reference const_row_reference;
63
+
64
+	//constructors
65
+	matrix (size_type n, size_type m, value_type v = value_type()) { data = data_type (n, row_type (m, v)); }
66
+
67
+	//index access
68
+	//TODO: not good to give a reference to row, cause so, user is abled to change it
69
+	row_reference		operator[] (size_type i) { return data[i]; }
70
+	const_row_reference	operator[] (size_type i) const { return data[i]; }
71
+
72
+	//get row iterator (if it works, we only take const_iterator)
73
+	const_row_iterator		rows_begin() const { return data.begin(); }
74
+	const_row_iterator		rows_end() const { return data.end(); }
75
+	
76
+	//misc
77
+	size_type		rows () const { return size_type (data.size()); }
78
+	size_type		cols () const;
79
+private:
80
+	//data field
81
+	data_type data;
82
+};
83
+
84
+////////////////////////////////////////////////////////////////////////////////
85
+//functions
86
+template < typename T, typename A >
87
+typename matrix<T,A>::size_type matrix<T,A>::cols() const
88
+{
89
+	//TODO: somehow try to maintain original row_size for each row
90
+	return size_type ((data.front()).size());
91
+}
92
+
93
+////////////////////////////////////////////////////////////////////////////////
94
+//IO functions
95
+
96
+//row
97
+//I
98
+/*
99
+template <typename T, typename A>
100
+std::istream& operator>> (std::istream& s, const row<T,A>& r)
101
+{
102
+	T x;
103
+	char t;
104
+
105
+	//read in until not more
106
+	while (s >> x >> t)
107
+		r.push_back(x);
108
+
109
+	s.clear();
110
+	//ATTENTION: for the moment only unix compatible
111
+	if (t  = '\n') {
112
+		//all ok, pushback last element
113
+		r.push_back(x);
114
+	}
115
+	else {
116
+		//nothing ok
117
+		s.clear(ios_base::badbit);
118
+	}
119
+
120
+	return s;		
121
+}
122
+*/
123
+
124
+//O
125
+template <typename T, typename A>
126
+std::ostream& operator<< (std::ostream& s, const row<T,A>& r)
127
+{
128
+	typename row<T,A>::const_iterator it = r.begin();
129
+
130
+	//for now, we just write a tab as separator
131
+	for ( ; it < r.end(); ++it) {
132
+		s << *it;
133
+		if (it != (r.end()-1)) s << '\t';
134
+	}
135
+
136
+	return s;		
137
+}
138
+
139
+//matrix
140
+//I
141
+/*
142
+template <typename T, typename A>
143
+std::istream& operator>> (std::istream& s, const matrix<T,A>& m)
144
+{
145
+	typename matrix<T,A>::const_row_iterator it = m.rows_begin();
146
+
147
+	for ( ; it < m.rows_end(); ++it)
148
+		s << *it << '\n';
149
+
150
+	return s;
151
+}
152
+*/
153
+
154
+//O
155
+template <typename T, typename A>
156
+std::ostream& operator<< (std::ostream& s, const matrix<T,A>& m)
157
+{
158
+	typename matrix<T,A>::const_row_iterator it = m.rows_begin();
159
+
160
+	for ( ; it < m.rows_end(); ++it)
161
+		s << *it << '\n';
162
+
163
+	return s;
164
+}
165
+
166
+}; // namespace ims
167
+
168
+#endif