From 26c490f404731d053a6205719b6246502c07b449 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Sat, 14 Jun 2014 16:46:27 +0200 Subject: init --- ccc/Makefile | 3 ++ ccc/animal.cc | 37 +++++++++++++++++++ ccc/cast.cc | 27 ++++++++++++++ ccc/cat_char.cc | 16 +++++++++ ccc/class.cc | 31 ++++++++++++++++ ccc/class_member.cc | 33 +++++++++++++++++ ccc/compression_test.cc | 45 +++++++++++++++++++++++ ccc/filelib.c | 87 +++++++++++++++++++++++++++++++++++++++++++++ ccc/filelib.h | 25 +++++++++++++ ccc/float_int.cc | 26 ++++++++++++++ ccc/gz.c | 16 +++++++++ ccc/ooc.c | 78 ++++++++++++++++++++++++++++++++++++++++ ccc/ooc.h | 6 ++++ ccc/precedence.cc | 16 +++++++++ ccc/sort.cc | 47 ++++++++++++++++++++++++ ccc/split.cc | 55 ++++++++++++++++++++++++++++ ccc/stringstream.cc | 26 ++++++++++++++ ccc/stringutil.c | 22 ++++++++++++ ccc/stringutil.h | 13 +++++++ ccc/stringutil_test.c | 53 +++++++++++++++++++++++++++ ccc/substr.cc | 23 ++++++++++++ ccc/to_hex.cc | 14 ++++++++ ccc/vector_addition.c | 29 +++++++++++++++ ccc/vector_addition.cu | 61 +++++++++++++++++++++++++++++++ ccc/vector_addition1.cu | 48 +++++++++++++++++++++++++ ccc/vector_addition_make.sh | 5 +++ 26 files changed, 842 insertions(+) create mode 100644 ccc/Makefile create mode 100644 ccc/animal.cc create mode 100644 ccc/cast.cc create mode 100644 ccc/cat_char.cc create mode 100644 ccc/class.cc create mode 100644 ccc/class_member.cc create mode 100644 ccc/compression_test.cc create mode 100644 ccc/filelib.c create mode 100644 ccc/filelib.h create mode 100644 ccc/float_int.cc create mode 100644 ccc/gz.c create mode 100644 ccc/ooc.c create mode 100644 ccc/ooc.h create mode 100644 ccc/precedence.cc create mode 100644 ccc/sort.cc create mode 100644 ccc/split.cc create mode 100644 ccc/stringstream.cc create mode 100644 ccc/stringutil.c create mode 100644 ccc/stringutil.h create mode 100644 ccc/stringutil_test.c create mode 100644 ccc/substr.cc create mode 100644 ccc/to_hex.cc create mode 100644 ccc/vector_addition.c create mode 100644 ccc/vector_addition.cu create mode 100644 ccc/vector_addition1.cu create mode 100755 ccc/vector_addition_make.sh (limited to 'ccc') diff --git a/ccc/Makefile b/ccc/Makefile new file mode 100644 index 0000000..6fb5006 --- /dev/null +++ b/ccc/Makefile @@ -0,0 +1,3 @@ +compression_test: compression_test.cc + g++ compression_test.cc -lboost_filesystem -lboost_iostreams -o compression_test + diff --git a/ccc/animal.cc b/ccc/animal.cc new file mode 100644 index 0000000..3bad1b1 --- /dev/null +++ b/ccc/animal.cc @@ -0,0 +1,37 @@ +#include + +using namespace std; + + +class Animal +{ + public: + Animal() { // Konstruktor + cout << "Der Konstruktor wird aufgerufen." << endl; + m_alive = true; + } + + ~Animal() {cout << "Der Destruktor wird aufgerufen." << endl;} + + bool isAlive() { // Methode + return m_alive; + } + + void kill() { // Methode + m_alive=false; + } + + private: + bool m_alive; // Member-Variable +}; + + +int main(void) +{ + int i; + bool alive; + + Animal a; // Instanz von Animal + Animal b; // noch eine ^ +} + diff --git a/ccc/cast.cc b/ccc/cast.cc new file mode 100644 index 0000000..2e84a64 --- /dev/null +++ b/ccc/cast.cc @@ -0,0 +1,27 @@ +#include + +using namespace std; + + +struct A { + virtual void p() { cout << "A" << endl; } + void q() { cout << "q" << endl; } +}; + +struct Ba : public A { + void p() { cout << "Ba" << endl; } +}; + +struct Bb : public A { + void p() { cout << "Bb" << endl; } +}; + +int main(void) +{ + A* x = new A; + x = dynamic_cast(new Ba); + cout << "-" << endl; + x->q(); + x->p(); +} + diff --git a/ccc/cat_char.cc b/ccc/cat_char.cc new file mode 100644 index 0000000..3daf564 --- /dev/null +++ b/ccc/cat_char.cc @@ -0,0 +1,16 @@ +#include +#include + +using namespace std; + + +int main(void) +{ + char c[] = "c"; + char x[1024]; + string s("s"); + strcpy(x, s.c_str()); + strcat(x, c); + cout << x << endl; +} + diff --git a/ccc/class.cc b/ccc/class.cc new file mode 100644 index 0000000..b493b51 --- /dev/null +++ b/ccc/class.cc @@ -0,0 +1,31 @@ +#include + +using namespace std; + + +class A { + public: + A(int a):_a(a) {} + int geta() { + return _a; + } + static int _b; + private: + int _a; +}; + +int A::_b = 0; + +int main(void){ + A a(2); + A *b = new A(2); + cout << b->geta() << endl; + cout << b->_b << endl; + cout << a._b << endl; + A::_b = 42; + cout << b->geta() << endl; + cout << b->_b << endl; + cout << a._b << endl; + return 0; +} + diff --git a/ccc/class_member.cc b/ccc/class_member.cc new file mode 100644 index 0000000..f78ca64 --- /dev/null +++ b/ccc/class_member.cc @@ -0,0 +1,33 @@ +#include + +using namespace std; + + +struct A +{ +}; + +struct B : public A +{ + +}; + +struct C : public A +{ + +}; + +struct D +{ + A* a; +}; + +int main(void) +{ + B b; + D d; + A a; + + d.a = &b; +} + diff --git a/ccc/compression_test.cc b/ccc/compression_test.cc new file mode 100644 index 0000000..21bfafc --- /dev/null +++ b/ccc/compression_test.cc @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace boost::iostreams; +using namespace std; + + +int main(void) +{ + //ofstream raw("out-raw"); + filtering_ostream out_z; + out_z.push(gzip_compressor()); + //out_gz.push(gzip_compressor()); + out_z.push(file_sink("out-z", std::ios::binary)); + //out_gz.push(file_sink("out-gz", std::ios::binary)); + for (size_t i = 0; i < 10; i++) { + out_z << "line #" << i << endl; + //out_gz << "line #" << i << endl; + //out_bz << "line #" << i << endl; + //raw << "line #" << i << endl; + } + // flush(out); + close(out_z); + //close(out_gz); + //close(out_bz); + //raw.close(); + + for (size_t i = 0; i < 5; i++) { + ifstream file("out-z", ios_base::in | ios_base::binary); + filtering_istream in; + in.push(gzip_decompressor()); + in.push(file); + string s; + while (getline(in, s)) { + cout << s << endl; + } + file.close(); + } +} + diff --git a/ccc/filelib.c b/ccc/filelib.c new file mode 100644 index 0000000..57a27c3 --- /dev/null +++ b/ccc/filelib.c @@ -0,0 +1,87 @@ +#include "filelib.h" + + +typedef struct +{ + hFile b_; + FILE* fp; +} hFile_; + +typedef struct +{ + hFile b_; + gzFile* fp; +} hgzFile; + +gzmode(const char* mode) +{ + // TODO + return true; +} + +hgzFile* +gzmakef(char* fname, const char* mode) +{ + hgzFile* hgz = malloc(sizeof(hgzFile)); + hgz->b_.name = fname; + hgz->b_.gz = true; + gzFile* fp = malloc(sizeof(gzFile)); + *fp = gzopen(fname, mode); + hgz->fp = fp; + return hgz; +} + +hFile* +makef(char* fname, const char* mode) +{ + if (endswith(fname, GZIP_EXT)) + return gzmakef(fname, mode); + hFile_* f = malloc(sizeof(hFile_)); + if (!f) return NULL; + f->b_.name = fname; + f->b_.gz = false; + if (!strcmp(fname, "-")) { + if (!strcmp(mode, "r") && !strcmp(mode, "r+")) f->fp = stdin; + else f->fp = stdout; + } else { + f->fp = fopen(fname, mode); + } + return f; +} + +bool +gzwriteln(const char* s, hFile* f) +{ + unsigned len = strlen(s); + gzwrite(*((hgzFile*)f)->fp, s, len); + gzwrite(*((hgzFile*)f)->fp, "\n", 1); + return gzflush(*((hgzFile*)f)->fp, Z_FINISH) == Z_OK; +} + +bool +writeln(const char* s, hFile* f) +{ + if (f->gz) + return gzwriteln(s, f); + return fputs(s, ((hFile_*)f)->fp)&&fputs("\n", ((hFile_*)f)->fp); +} + +bool +gzclosef(hFile* f) +{ + bool ret = gzclose(*((hgzFile*)f)->fp); + free(((hgzFile*)f)->fp); + free(f); + return ret; +} + +bool +closef(hFile* f) +{ + if (f->gz) + return gzclosef(f); + bool ret = fclose(((hFile_*)f)->fp); + free(f); + return ret; +} + diff --git a/ccc/filelib.h b/ccc/filelib.h new file mode 100644 index 0000000..587cddf --- /dev/null +++ b/ccc/filelib.h @@ -0,0 +1,25 @@ +#ifndef FILELIB_H +#define FILELIB_H + +#include +#include +#include +#include +#include +#include "util.h" + +#define GZIP_EXT ".gz" + + +typedef struct +{ + char* name; // relative + bool gz; +} hFile; + +hFile* makef(char* fname, const char* mode); +bool writeln(const char* s, hFile* f); + + +#endif + diff --git a/ccc/float_int.cc b/ccc/float_int.cc new file mode 100644 index 0000000..9d0ece4 --- /dev/null +++ b/ccc/float_int.cc @@ -0,0 +1,26 @@ +#include + +using namespace std; + + +int main(void) +{ + int a = 1; + int b = 2; + double q = (double)a/b; + cout << q << endl; + + cout << 1 - (float)a/b << endl; + + float x = 0.1; + unsigned y = 105; + unsigned z = x*y; + cout << z << endl; + + float u = -1; + if (u) cout << "XXXXXX" << endl; + + float j = 0.0; + if (j) cout << "j" << endl; +} + diff --git a/ccc/gz.c b/ccc/gz.c new file mode 100644 index 0000000..a0dcd90 --- /dev/null +++ b/ccc/gz.c @@ -0,0 +1,16 @@ +#include +#include + +int +main(void) +{ + gzFile gz = gzopen("asdf.gz", "wb"); + int len = 0; + gzwrite(gz, "asdf\n", len); + char *s = "asdf\n"; + gzwrite(gz, "asdf\n", strlen(s)); + gzclose(gz); + + return 0; +} + diff --git a/ccc/ooc.c b/ccc/ooc.c new file mode 100644 index 0000000..aa20488 --- /dev/null +++ b/ccc/ooc.c @@ -0,0 +1,78 @@ +#include "ooc.h" +#include "stdio.h" +#include "string.h" + + +struct van { + struct vehicle base; + int cubic_size; +}; + +struct bus { + struct vehicle base; + int seats; +}; + +struct van* +make_van() +{ + struct van* v = malloc(sizeof(struct van)); + v->base.type = "van"; + v->cubic_size = 12; + return v; +} + +struct bus* +make_bus() +{ + struct bus* v = malloc(sizeof(struct bus)); + v->base.type = "bus"; + v->seats=112; + return v; +} + +struct vehicle* +make_vehicle(const char* type) +{ + if(!strcmp(type, "van")) return make_van(); + if(!strcmp(type, "bus")) return make_bus(); + return NULL; +} + +void +do_something_with_a_bus(struct vehicle* v) +{ + ((struct bus*)v)->seats = 13; +} + +void +do_something_with_a_van(struct vehicle* v) +{ + ((struct van*)v)->cubic_size = 11; +} + +void +do_something(struct vehicle* v) +{ + if(!strcmp(v->type, "van")) return do_something_with_a_van(v); + if(!strcmp(v->type, "bus")) return do_something_with_a_bus(v); +} + +int +main(void) { + struct van my_van; + struct vehicle *something = &my_van; + my_van.cubic_size = 100; + my_van.base.power = 99; + printf("%d\n", something->power); + printf("%d\n", my_van.base.power); + + struct bus* bus = make_vehicle("bus"); + printf("%s\n", bus->base.type); + printf("%d\n", bus->seats); + do_something(bus); + printf("%d\n", bus->seats); + + return 0; +} + diff --git a/ccc/ooc.h b/ccc/ooc.h new file mode 100644 index 0000000..0585d95 --- /dev/null +++ b/ccc/ooc.h @@ -0,0 +1,6 @@ +struct vehicle { + int power; + int weight; + char* type; +}; + diff --git a/ccc/precedence.cc b/ccc/precedence.cc new file mode 100644 index 0000000..08e29b6 --- /dev/null +++ b/ccc/precedence.cc @@ -0,0 +1,16 @@ +#include + +using namespace std; + + +int main(void) +{ + bool a = false; + bool b = true; + + cout << (!a && b) << endl; + + double c = 0; + if (c) cout << "xx" << endl; +} + diff --git a/ccc/sort.cc b/ccc/sort.cc new file mode 100644 index 0000000..165b086 --- /dev/null +++ b/ccc/sort.cc @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +using namespace std; + + +struct X +{ + pair p; +}; + +bool myf(X a, X b) +{ + return a.p.second > b.p.second; +} + +int main(void) +{ + X a; + a.p.first = "a"; + a.p.second = 1; + + X b; + b.p.first = "b"; + b.p.second = 2; + + X c; + c.p.first = "c"; + c.p.second = 3; + + vector v; + v.push_back(a); + v.push_back(b); + v.push_back(c); + + for (unsigned i = 0; i < v.size(); i++) { + cout << v[i].p.first << endl; + } + sort(v.begin(), v.end(), myf); + cout << endl; + for (unsigned i = 0; i < v.size(); i++) { + cout << v[i].p.first << endl; + } +} + diff --git a/ccc/split.cc b/ccc/split.cc new file mode 100644 index 0000000..e01ad64 --- /dev/null +++ b/ccc/split.cc @@ -0,0 +1,55 @@ +#include +#include +#include +#include + +using namespace std; + + +int main(void) +{ + string s("a\tb\tc\td"); + string::iterator it = s.begin(); + char d = '\t'; + string tmp; + size_t parts = 4; + size_t c = 0; + while(true) { + if (parts > 0 && c == parts-1) { + while(it != s.end()) { + tmp += *it; + it++; + } + cout << tmp << endl; + break; + } + if (it == s.end()) { cout << tmp << endl; break; } + if (*it != d) tmp += *it; + else { + cout << tmp << endl; + tmp.clear(); + c++; + } + it++; + } + + cout << "---" << endl; + + stringstream ss(s); + string si; + parts = 0; + c = 0; + + while(true) + { + if (parts > 0 && c == parts-1) { + getline(ss, si); + cout << si << endl; + break; + } + if(!getline(ss, si, '\t')) break; + cout << si << endl; + c++; + } +} + diff --git a/ccc/stringstream.cc b/ccc/stringstream.cc new file mode 100644 index 0000000..3d68db3 --- /dev/null +++ b/ccc/stringstream.cc @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + + +int main(void) { + string s = "1 2 3 4"; + int i; + vector j; + stringstream ss(s); + while (ss >> i) { + j.push_back(i); + cout << i << endl; + } + + printf("%4d %4d %4d %4d\n", j[0], j[1], j[2], j[3]); + + cout.width(100); + cout << setw(100) << j.size() << endl; +} + diff --git a/ccc/stringutil.c b/ccc/stringutil.c new file mode 100644 index 0000000..aab4f03 --- /dev/null +++ b/ccc/stringutil.c @@ -0,0 +1,22 @@ +#include "stringutil.h" + + +char* +strend(char* s) +{ + if (strlen(s) == 0) return '\0'; + while(*s != '\0') + ++s; + return --s; +} + +bool +endswith(char* s, char* suff) +{ + if (strlen(s) < strlen(suff)) return false; + char* a = strend(s)-(strlen(suff)-1); + if (!a) return false; + if (!strcmp(a, suff)) return true; + return false; +} + diff --git a/ccc/stringutil.h b/ccc/stringutil.h new file mode 100644 index 0000000..153068a --- /dev/null +++ b/ccc/stringutil.h @@ -0,0 +1,13 @@ +#ifndef STRINGUTIL_H +#define STRINGUTIL_H + +#include +#include + + +char* strend(char* s); +bool endswith(char* s, char* suff); + + +#endif + diff --git a/ccc/stringutil_test.c b/ccc/stringutil_test.c new file mode 100644 index 0000000..798bccd --- /dev/null +++ b/ccc/stringutil_test.c @@ -0,0 +1,53 @@ +#include "filelib.h" +#include "stringutil.h" + + +int +main(void) +{ + /* + * strend + * + */ + char* s= ""; + char* end = strend(s); + if (end) + printf("should not output: %s\n", end); + + s = "asdf.gz"; + end = strend(s); + if (end) + printf("expect 'z': %s\n", end); + + /* + * endswith + * + */ + if (endswith(s, ".gz")) + printf("%s ends with %s\n", s, ".gz"); + + if (endswith(".gz", ".gz")) + printf(".gz ends with .gz\n"); + + if(!endswith("gz", ".gz")) + printf("gz does not end with .gz\n"); + + /* + * filelib + * + */ + hFile* f = makef("-", "a"); + if (!f) + printf("Error creating file '%s'\n", "-"); + writeln("this goes to stdout", f); + closef(f); + + hFile* g = makef("gzfile.gz", "a"); + if (!g) + printf("Error creating file '%s'\n", "-"); + writeln("this should be compressed", g); + closef(g); + + return 0; +} + diff --git a/ccc/substr.cc b/ccc/substr.cc new file mode 100644 index 0000000..9a91adc --- /dev/null +++ b/ccc/substr.cc @@ -0,0 +1,23 @@ +#include +#include + +using namespace std; + + +int main(void) +{ + // 0 12 34 56 + string s("aaax\tbx\tcxasd\tdx"); + unsigned f = 0; + for(unsigned i = 0; i < 3; i++) { + unsigned e = f; + f = s.find("\t", f+1); + cout << "e:" << e << " f:" << f << endl; + if (e !=0) cout << "'" << s.substr(e+1, f-e-1) << "'" << endl; + else cout << "'" << s.substr(0, f) << "'" << endl; + } + cout << "---" << endl; + s.erase(0, f+1); + cout << "'" << s << "'" < + +using namespace std; + + +int main(void) +{ + int r = 116; + int g = 10; + int b = 10; + + cout << hex << r << " " << g << " " << b << endl; +} + diff --git a/ccc/vector_addition.c b/ccc/vector_addition.c new file mode 100644 index 0000000..decb3ff --- /dev/null +++ b/ccc/vector_addition.c @@ -0,0 +1,29 @@ +#include "stdio.h" +#include "stdlib.h" + + +int main(void) { + int N = 10000, M=100000; + float **x = malloc(N*sizeof(float*)); + float *y = malloc(M*sizeof(float*)); + int i,j; + for (i = 0; i < N; i++) { + x[i] = malloc(M*sizeof(float)); + for (j = 0; j < M; j++) { + x[i][j] = (float) j; + } + } + + for (i = 0; i < N ; i++) { + for (j = 0; j < M; j++) { + y[i] += x[i][j]; + } + } + + printf("%f\n", y[100]); + + free(x); free(y); + + return 0; +} + diff --git a/ccc/vector_addition.cu b/ccc/vector_addition.cu new file mode 100644 index 0000000..4f16bc3 --- /dev/null +++ b/ccc/vector_addition.cu @@ -0,0 +1,61 @@ +#include "stdio.h" + + +__global__ void add_arrays_gpu( float *in1, float *in2, float *out, int Ntot) +{ + int idx=blockIdx.x*blockDim.x+threadIdx.x; + if (idx < Ntot) + out[idx]=in1[idx]+in2[idx]; +} + + +int main(void) +{ + /* pointers to host memory */ + float *a, *b, *c; + /* pointers to device memory */ + float *a_d, *b_d, *c_d; + int N=100000000; + int i; + + /* Allocate arrays a, b and c on host*/ + a = (float*) malloc(N*sizeof(float)); + b = (float*) malloc(N*sizeof(float)); + c = (float*) malloc(N*sizeof(float)); + + /* Allocate arrays a_d, b_d and c_d on device*/ + cudaMalloc ((void **) &a_d, sizeof(float)*N); + cudaMalloc ((void **) &b_d, sizeof(float)*N); + cudaMalloc ((void **) &c_d, sizeof(float)*N); + + /* Initialize arrays a and b */ + for (i=0; i>>(a_d, b_d, c_d, N); + + /* Copy data from deveice memory to host memory */ + //cudaMemcpy(c, c_d, sizeof(float)*N, cudaMemcpyDeviceToHost); + + /* Print c */ +/*for(i=0; i>>(a_d, b_d, c_d, N); + + /* Copy data from deveice memory to host memory */ + //cudaMemcpy(c, c_d, sizeof(float)*N, cudaMemcpyDeviceToHost); + + /* Print c */ +/*for(i=0; i