修订版 | 9 (tree) |
---|---|
时间 | 2010-04-19 20:50:30 |
作者 | (del#42041) |
introducing template
@@ -5,6 +5,7 @@ | ||
5 | 5 | |
6 | 6 | namespace test_particle |
7 | 7 | { |
8 | + | |
8 | 9 | void cut_setup() |
9 | 10 | { |
10 | 11 | } |
@@ -15,7 +16,7 @@ | ||
15 | 16 | void test_construction(void) |
16 | 17 | { |
17 | 18 | // Constructor |
18 | - Particle a(5); | |
19 | + Particle<int> a(5); | |
19 | 20 | vector<int> p = a.get_particle(); |
20 | 21 | cppcut_assert_equal(5, (int)p.size()); |
21 | 22 |
@@ -24,6 +25,10 @@ | ||
24 | 25 | for(int i=1;i<5;++i) b.at(i-1)=i; |
25 | 26 | a = b; |
26 | 27 | for(int i=1;i<5;++i) cut_assert_equal_int(i, a[i-1]); |
28 | + Particle<int> c(5); | |
29 | + cut_assert_equal_int(5,c.size()); | |
30 | + cut_assert_equal_string("(0,0,0,0,0)",c.toString().c_str()); | |
31 | + for(int i=1;i<5;++i) cut_assert_equal_int(i, a[i-1]); | |
27 | 32 | } |
28 | 33 | void test_MersennneTwister(void) |
29 | 34 | { |
@@ -32,7 +37,7 @@ | ||
32 | 37 | } |
33 | 38 | void test_dumping(void) |
34 | 39 | { |
35 | - Particle a(5); | |
36 | - cout<<endl; cppcut_assert_equal(0,a.dump_particle()); | |
40 | + Particle<int> a(5); | |
41 | + cut_assert_equal_string("(0,0,0,0,0)",a.toString().c_str()); | |
37 | 42 | } |
38 | 43 | } |
@@ -4,7 +4,7 @@ | ||
4 | 4 | #include <filter.h> |
5 | 5 | using namespace std; |
6 | 6 | |
7 | -Particles func(Particles &p) | |
7 | +Particles<int> func(Particles<int> &p) | |
8 | 8 | { |
9 | 9 | // Particles a(5,3); |
10 | 10 | // Particles a; |
@@ -30,7 +30,12 @@ | ||
30 | 30 | a.createInitialParticles(); |
31 | 31 | a.dump_predict_particles(); |
32 | 32 | a.set_state_func(&func); |
33 | - Particles b(5,3); | |
33 | + Particles<int> b(5,3); | |
34 | 34 | a.get_next_state(b); |
35 | 35 | } |
36 | 36 | } |
37 | +int main() | |
38 | +{ | |
39 | + test_filter::test_construction(); | |
40 | + return(0); | |
41 | +} | |
\ No newline at end of file |
@@ -14,13 +14,38 @@ | ||
14 | 14 | |
15 | 15 | void test_construction(void) |
16 | 16 | { |
17 | - Particles a(3); | |
18 | - vector<Particle> p = a.get_particles(); | |
17 | + Particles<int> a(3); | |
18 | + vector<Particle<int> > p = a.get_particles(); | |
19 | 19 | cppcut_assert_equal(3, (int)p.size()); |
20 | + cppcut_assert_equal(3, a.size()); | |
21 | + Particles<int> b(3,2); | |
22 | + for(int i=0;i<3;++i) | |
23 | + cut_assert_equal_string("(0,0)", b[i].toString().c_str()); | |
20 | 24 | } |
25 | + void test_manipulate(void) | |
26 | + { | |
27 | + Particles<int> b(3,2); | |
28 | + Particle<int> c(2); | |
29 | + c[0]=1; c[1]=2; | |
30 | + cut_assert_equal_string("(1,2)", c.toString().c_str()); | |
31 | + b[0] = c; | |
32 | + cut_assert_equal_string("(1,2)", b[0].toString().c_str()); | |
33 | + int n=10; | |
34 | + for(int i=0;i<3;++i) | |
35 | + { | |
36 | + for(int j=0;j<2;++j) | |
37 | + { | |
38 | + b[i][j] = n++; | |
39 | + } | |
40 | + } | |
41 | + cut_assert_equal_string("(10,11)", b[0].toString().c_str()); | |
42 | + cut_assert_equal_string("(12,13)", b[1].toString().c_str()); | |
43 | + cut_assert_equal_string("(14,15)", b[2].toString().c_str()); | |
44 | +// cppcut_assert_equal(0,b.dump_particles()); | |
45 | + } | |
21 | 46 | void test_dumping(void) |
22 | 47 | { |
23 | - Particles a(3,2); | |
48 | + Particles<int> a(3,2); | |
24 | 49 | cppcut_assert_equal(0,a.dump_particles()); |
25 | 50 | } |
26 | 51 |
@@ -1,48 +1,82 @@ | ||
1 | 1 | #include <particle.h> |
2 | 2 | |
3 | -Particle::Particle() | |
3 | + | |
4 | +template<class C> Particle<C>::Particle() | |
4 | 5 | { |
6 | + dimension = -1; | |
5 | 7 | } |
6 | -Particle::Particle(int dimension) | |
8 | +template<class C> Particle<C>::Particle(int dimension) | |
7 | 9 | { |
8 | 10 | resize(dimension); |
9 | 11 | } |
10 | -Particle::~Particle() | |
12 | +template<class C> Particle<C>::~Particle() | |
11 | 13 | { |
12 | 14 | } |
13 | -Particle& Particle::operator=(vector<int> &f) | |
15 | +template<class C> Particle<C>::Particle(const Particle &f) | |
14 | 16 | { |
17 | + p = f.p; | |
18 | + dimension = f.dimension; | |
19 | +} | |
20 | +template<class C> Particle<C>& Particle<C>::operator=(const Particle &f) | |
21 | +{ | |
22 | + if(this != &f) | |
23 | + { | |
24 | + if(dimension<0) | |
25 | + { | |
26 | + dimension = f.dimension; | |
27 | + resize(dimension); | |
28 | + } | |
29 | + p = f.p; | |
30 | + } | |
31 | + return(*this); | |
32 | +} | |
33 | +template<class C> Particle<C>& Particle<C>::operator=(vector<C> &f) | |
34 | +{ | |
15 | 35 | p = f; |
16 | 36 | return(*this); |
17 | 37 | } |
18 | -int Particle::operator[](unsigned int i) | |
38 | +template<class C> C &Particle<C>::operator[](unsigned int i) | |
19 | 39 | { |
20 | - return(p.at(i)); | |
40 | + return(p[i]); | |
21 | 41 | } |
22 | -int Particle::resize(int n) | |
42 | +template<class C> C Particle<C>::operator[](unsigned int i) const | |
23 | 43 | { |
44 | + return(p[i]); | |
45 | +} | |
46 | +template<class C> int Particle<C>::resize(int n) | |
47 | +{ | |
24 | 48 | p.resize(n); |
25 | 49 | this->dimension = n; |
26 | 50 | return(0); |
27 | 51 | } |
28 | -vector<int> Particle::get_particle() | |
52 | +template<class C> int Particle<C>::size() | |
29 | 53 | { |
54 | + return(dimension); | |
55 | +} | |
56 | +template<class C> vector<C> Particle<C>::get_particle() | |
57 | +{ | |
30 | 58 | return(p); |
31 | 59 | } |
32 | -int Particle::dump(vector<int> q) | |
60 | +template<class C> string Particle<C>::toString() | |
33 | 61 | { |
34 | - cout<<"("; | |
35 | - for(vector<int>::iterator i=q.begin(); | |
36 | - i!=q.end();++i) | |
62 | + ostringstream s; | |
63 | + s<<"("; | |
64 | +// for(vector<C>::iterator i=p.begin(); | |
65 | +// i!=p.end();++i) | |
66 | + for(int i=0;i<p.size();++i) | |
37 | 67 | { |
38 | - if(i!=q.begin()) cout<<","; | |
39 | - cout<<*i; | |
68 | +// if(i!=p.begin()) s<<","; | |
69 | + if(i!=0) s<<","; | |
70 | +// s<<dec<<*i; | |
71 | + s<<dec<<p[i]; | |
40 | 72 | } |
41 | - cout<<")"; | |
42 | - return(0); | |
73 | + s<<")"; | |
74 | + return(s.str()); | |
43 | 75 | } |
44 | -int Particle::dump_particle() | |
76 | +template<class C> int Particle<C>::dump() | |
45 | 77 | { |
46 | - dump(p); | |
78 | + cout<<toString()<<endl; | |
47 | 79 | return(0); |
48 | 80 | } |
81 | + | |
82 | +template class Particle<int>; |
@@ -5,11 +5,14 @@ | ||
5 | 5 | number = -1; |
6 | 6 | dimension = -1; |
7 | 7 | state_func = NULL; |
8 | - x = NULL; | |
8 | +// x = NULL; | |
9 | +// v = NULL; | |
10 | + mtrand.seed(time(NULL)); | |
9 | 11 | } |
10 | 12 | Filter::~Filter() |
11 | 13 | { |
12 | - if(x) delete x; | |
14 | +// if(x) delete x; | |
15 | +// if(v) delete v; | |
13 | 16 | } |
14 | 17 | |
15 | 18 | bool Filter::setNumber(int n) |
@@ -25,20 +28,34 @@ | ||
25 | 28 | bool Filter::createInitialParticles() |
26 | 29 | { |
27 | 30 | if(number<0||dimension<0) return(false); |
28 | - x = new Particles(number, dimension); | |
31 | +// x = new Particles(number, dimension); | |
32 | +// v = new Particles(number, dimension); | |
33 | + x.resize(number, dimension); | |
34 | + v.resize(number, dimension); | |
29 | 35 | return(true); |
30 | 36 | } |
37 | +bool Filter::createSystemNoise() | |
38 | +{ | |
39 | + for(int i=0;i<v.size();++i) | |
40 | + { | |
41 | + for(int j=0;j<v[i].size();++j) | |
42 | + { | |
43 | +// v[i][j] = | |
44 | + } | |
45 | + } | |
46 | + | |
47 | +} | |
31 | 48 | int Filter::dump_predict_particles() |
32 | 49 | { |
33 | - x->dump_particles(); | |
50 | + x.dump_particles(); | |
34 | 51 | return(0); |
35 | 52 | } |
36 | -bool Filter::set_state_func(Particles (*func)(Particles &p)) | |
53 | +bool Filter::set_state_func(Particles<int> (*func)(Particles<int> &p)) | |
37 | 54 | { |
38 | 55 | state_func = func; |
39 | 56 | return(true); |
40 | 57 | } |
41 | -Filter & Filter::get_next_state(Particles &p) | |
58 | +Filter & Filter::get_next_state(Particles<int> &p) | |
42 | 59 | { |
43 | 60 | cout<<"go..."<<endl; |
44 | 61 | (*state_func)(p); |
@@ -2,23 +2,29 @@ | ||
2 | 2 | #define __PARTICLE |
3 | 3 | #include <iostream> |
4 | 4 | #include <vector> |
5 | +#include <string> | |
6 | +#include <sstream> | |
5 | 7 | #include <MersenneTwister.h> |
6 | 8 | using namespace std; |
7 | 9 | |
8 | -class Particle | |
10 | +template<class C> class Particle | |
9 | 11 | { |
10 | - vector<int> p; | |
12 | + vector<C> p; | |
11 | 13 | int dimension; |
12 | 14 | public: |
13 | 15 | Particle(); |
14 | 16 | Particle(int dimension); |
15 | 17 | ~Particle(); |
18 | + Particle(const Particle &f); | |
19 | + Particle& operator=(const Particle &f); | |
16 | 20 | int resize(int n); |
17 | - vector<int> get_particle(); | |
18 | - int dump(vector<int> q); | |
19 | - int dump_particle(); | |
20 | - Particle& operator=(vector<int> &f); | |
21 | - int operator[](unsigned int i); | |
21 | + int size(); | |
22 | + vector<C> get_particle(); | |
23 | + string toString(); | |
24 | + int dump(); | |
25 | + Particle& operator=(vector<C> &f); | |
26 | + C &operator[](unsigned int i); | |
27 | + C operator[](unsigned int i) const; | |
22 | 28 | }; |
23 | 29 | |
24 | 30 |
@@ -1,37 +1,58 @@ | ||
1 | 1 | #include <particles.h> |
2 | 2 | |
3 | -Particles::Particles() | |
3 | +template<class C> Particles<C>::Particles() | |
4 | 4 | { |
5 | 5 | } |
6 | -Particles::Particles(int number) | |
6 | +template<class C> Particles<C>::Particles(int number) | |
7 | 7 | { |
8 | 8 | p.resize(number); |
9 | 9 | this->number = number; |
10 | 10 | } |
11 | -Particles::Particles(int number, int dimension) | |
11 | +template<class C> Particles<C>::Particles(int number, int dimension) | |
12 | 12 | { |
13 | + resize(number, dimension); | |
14 | +} | |
15 | +template<class C> Particles<C>::~Particles() | |
16 | +{ | |
17 | +} | |
18 | +template<class C> bool Particles<C>::resize(int number, int dimension) | |
19 | +{ | |
13 | 20 | p.resize(number); |
14 | 21 | this->number = number; |
15 | - for(vector<Particle>::iterator i= p.begin(); | |
16 | - i!=p.end();++i) | |
22 | + typedef Particle<C> ParticleC; | |
23 | +// for(vector<ParticleC>::iterator i= p.begin(); | |
24 | +// i!=p.end();++i) | |
25 | + for(int i=0;i<p.size();++i) | |
17 | 26 | { |
18 | - i->resize(dimension); | |
27 | +// i->resize(dimension); | |
28 | + p[i].resize(dimension); | |
19 | 29 | } |
30 | + return(true); | |
20 | 31 | } |
21 | -Particles::~Particles() | |
32 | +template<class C> int Particles<C>::size() | |
22 | 33 | { |
34 | + return(number); | |
23 | 35 | } |
24 | -vector<Particle> Particles::get_particles() | |
36 | +template<class C> Particle<C> &Particles<C>::operator[](unsigned int i) | |
25 | 37 | { |
38 | + return(p.at(i)); | |
39 | +} | |
40 | +template<class C> Particle<C> Particles<C>::operator[](unsigned int i) const | |
41 | +{ | |
42 | + return(p.at(i)); | |
43 | +} | |
44 | +template<class C> vector<Particle<C> > Particles<C>::get_particles() | |
45 | +{ | |
26 | 46 | return(p); |
27 | 47 | } |
28 | -int Particles::dump_particles() | |
48 | +template<class C> int Particles<C>::dump_particles() | |
29 | 49 | { |
30 | - for(vector<Particle>::iterator i=p.begin(); | |
31 | - i!=p.end();++i) | |
50 | +// for(vector<Particle<C> >::iterator i=p.begin(); | |
51 | +// i!=p.end();++i) | |
52 | + for(int i=0;i<p.size();++i) | |
32 | 53 | { |
33 | - if(i!=p.begin()) cout<<endl; | |
34 | - i->dump_particle(); | |
54 | +// i->dump(); | |
55 | + p[i].dump(); | |
35 | 56 | } |
36 | 57 | return(0); |
37 | 58 | } |
@@ -4,14 +4,17 @@ | ||
4 | 4 | #include <vector> |
5 | 5 | #include <particles.h> |
6 | 6 | #include <MersenneTwister.h> |
7 | +#include <ctime> | |
7 | 8 | using namespace std; |
8 | 9 | |
9 | 10 | class Filter |
10 | 11 | { |
11 | - Particles *x; | |
12 | + Particles<int> x, v; | |
13 | + double w; | |
14 | + MTRand mtrand; | |
12 | 15 | vector<int> y; |
13 | 16 | int number, dimension; |
14 | - Particles (*state_func)(Particles &p); | |
17 | + Particles<int> (*state_func)(Particles<int> &p); | |
15 | 18 | public: |
16 | 19 | Filter(); |
17 | 20 | ~Filter(); |
@@ -18,9 +21,10 @@ | ||
18 | 21 | bool setNumber(int n); |
19 | 22 | bool setDimension(int d); |
20 | 23 | bool createInitialParticles(); |
24 | + bool createSystemNoise(); | |
21 | 25 | int dump_predict_particles(); |
22 | - bool set_state_func(Particles (*func)(Particles &p)); | |
23 | - Filter & get_next_state(Particles &p); | |
26 | + bool set_state_func(Particles<int> (*func)(Particles<int> &p)); | |
27 | + Filter & get_next_state(Particles<int> &p); | |
24 | 28 | }; |
25 | 29 | |
26 | 30 | #endif |
@@ -5,9 +5,9 @@ | ||
5 | 5 | #include <particle.h> |
6 | 6 | using namespace std; |
7 | 7 | |
8 | -class Particles | |
8 | +template<class C> class Particles | |
9 | 9 | { |
10 | - vector<Particle> p; | |
10 | + vector<Particle<C> > p; | |
11 | 11 | int number; |
12 | 12 | public: |
13 | 13 | Particles(); |
@@ -14,7 +14,11 @@ | ||
14 | 14 | Particles(int number); |
15 | 15 | Particles(int number, int dimension); |
16 | 16 | ~Particles(); |
17 | - vector<Particle> get_particles(); | |
17 | + bool resize(int number, int dimension); | |
18 | + int size(); | |
19 | + Particle<C> &operator[](unsigned int i); | |
20 | + Particle<C> operator[](unsigned int i) const; | |
21 | + vector<Particle<C> > get_particles(); | |
18 | 22 | int dump_particles(); |
19 | 23 | }; |
20 | 24 |