annotate SMART/Java/Python/Cpp/table.hpp @ 18:94ab73e8a190

Uploaded
author m-zytnicki
date Mon, 29 Apr 2013 03:20:15 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
1 #ifndef TABLE_HPP
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
2 #define TABLE_HPP
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
3
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
4 #include <iostream>
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
5 #include <fstream>
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
6 #include <string>
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
7 #include <stdio.h>
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
8 using namespace std;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
9
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
10 typedef unsigned int Value;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
11 static const unsigned int SENTINEL = -1;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
12
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
13 class Table {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
14 private:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
15 int sentinel;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
16 Value *values;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
17
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
18 public:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
19 string fileName;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
20 unsigned int width;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
21 unsigned int height;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
22 fstream file;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
23
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
24 Table (string fileName, unsigned int width, unsigned int height): sentinel(-1), fileName(fileName), width(width), height(height) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
25 file.open(fileName.c_str(), ios::out | ios::in | ios::binary);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
26 Value v = 0;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
27 for (unsigned int i = 0; i < width * height; i++) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
28 writeHere(v);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
29 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
30 file.flush();
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
31 values = new Value[width];
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
32 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
33
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
34 ~Table () {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
35 delete[] values;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
36 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
37
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
38 void moveTo (unsigned int col, unsigned int line) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
39 if (col == SENTINEL) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
40 sentinel = line;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
41 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
42 else {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
43 sentinel = -1;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
44 file.seekp((col * width + line) * sizeof(Value));
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
45 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
46 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
47
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
48 void write (Value v, unsigned int col, unsigned int line) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
49 moveTo(col, line);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
50 writeHere(v);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
51 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
52
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
53 void writeHere(Value v) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
54 if (sentinel >= 0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
55 values[sentinel] = v;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
56 else
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
57 file.write(reinterpret_cast<const char*>(&v), sizeof(Value));
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
58 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
59
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
60
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
61 Value read (unsigned int col, unsigned int line) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
62 moveTo(col, line);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
63 return readHere();
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
64 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
65
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
66 Value readHere () {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
67 if (sentinel >= 0) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
68 return values[sentinel];
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
69 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
70 else {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
71 Value v;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
72 file.read(reinterpret_cast<char*>(&v), sizeof(Value));
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
73 return v;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
74 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
75 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
76
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
77 void destroy () {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
78 file.close();
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
79 remove(fileName.c_str());
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
80 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
81
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
82 };
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
83
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
84 #endif