Skip to content

Commit

Permalink
commit test
Browse files Browse the repository at this point in the history
  • Loading branch information
realfirst committed Jun 6, 2011
1 parent 162bf68 commit 0a9f2fe
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 22 deletions.
19 changes: 0 additions & 19 deletions array.cc (Case Conflict 1)

This file was deleted.

Binary file added ch22/IntegerMapping
Binary file not shown.
3 changes: 2 additions & 1 deletion ch22/IntegerMapping.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Negate : public IntegerMapping {

class Compose : public IntegerMapping {
public:
Compose(IntegerMapping &n1, IntegerMapping &n2) : m1(&n1), m2(&n2) {}
Compose(IntegerMapping *n1, IntegerMapping *n2) : m1(n1), m2(n2) {}
int maps(int x) const {
return m1->maps(m2->maps(x));
}
Expand All @@ -47,3 +47,4 @@ int main(int argc, char **argv) {
cout << weird(&c, 15, 16) << endl;
return 0;
}

4 changes: 2 additions & 2 deletions ch23/reference_virtual.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SecondClass : public FirstClass {
void print1() {
cout << "SecondClass::print1()" << endl;
}
virtual void print2() {
void print2() {
cout << "SecondClass::print2()" << endl;
}
};
Expand All @@ -32,7 +32,7 @@ class ThirdClass : public SecondClass {
void print1() {
cout << "ThirdClass::print1()" << endl;
}
virtual void print2() {
void print2() {
cout << "ThirdClass::print2()" << endl;
}
};
Expand Down
Binary file added ch25/Internal_vector
Binary file not shown.
140 changes: 140 additions & 0 deletions ch25/Internal_vector.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#include <iostream>

using namespace std;

class InternalVector {
public:
double *data;
int size;
int nbref;

InternalVector(double *d, int s) : data(new double[s]),
size(s), nbref(0) {
cout << " + Expensive allocation and copy" << endl;
for (int k = 0; k < s; k++) {
data[k] = d[k];
}
}

~InternalVector() {
cout << " + Destruction" << endl;
delete[] data;
}

void release() {
if (--nbref == 0) {
delete this;
}
}

InternalVector *grab() {
nbref++;
return this;
}

InternalVector *own() {
if (nbref == 1) {
return this;
} else {
nbref--;
InternalVector *result = new InternalVector(data, size);
result->nbref++;
return result;
}
}
};

class Vector {
public:
Vector();
Vector(double *d, int s);
Vector(const Vector &v);
~Vector();
Vector & operator = (const Vector &v);
inline double get(int k) const;
inline void set(int k, double v);
inline int size() const;
private:
InternalVector *internal;
};


Vector::Vector() {
cout << " * Creating empty Vector" << endl;
internal = 0;
}

Vector::Vector(double *d, int s) {
cout << " * Creating Vector" << endl;
internal = new InternalVector(d, s);
internal->grab();
}

Vector::Vector(const Vector &v) {
cout << " * Coping Vector" << endl;
if (v.internal) {
internal = v.internal->grab();
} else {
internal = 0;
}
}

Vector::~Vector() {
cout << " * Destorying Vector" << endl;
if (internal) {
internal->release();
}
}

Vector & Vector::operator = (const Vector &v) {
cout << " * Assigning Vector from Vector" << endl;
if (this != &v) {
if (internal) {
internal->release();
internal = v.internal->grab();
}
}
}

inline double Vector::get(int k) const {
return internal->data[k];
}

inline void Vector::set(int k, double v) {
if (v != internal->data[k]) {
internal = internal->own();
internal->data[k] = v;
}
}

inline int Vector::size() const {
return internal->size;
}

double sum(Vector v) {
cout << "Entering sum()" << endl;
double s = 0;
for (int i = 0; i < v.size(); i++) {
s += v.get(i);
}
cout << "Leaving sum()" << endl;
return s;
}

int main(int argc, char **argv) {
cout << "Doing double a[] = {1, 2, 3, 4, 5, 6, 7};" << endl;
double a[] = {1, 2, 3, 4, 5, 6, 7};
cout << "DOING Vector v(a, sizeof(a)/sizeof(double));" << endl;
Vector v(a, sizeof(a)/sizeof(double));
cout << "DING Vector w;" << endl;
Vector w;
cout << "DOING w = v;" << endl;
w = v;
cout << "DOING cout << sum(v) << \"\\n\";" << endl;
cout << sum(v) << endl;
cout << "DOING w.set(3, 2.1);" << endl;
w.set(3, 2.1);
cout << "FINISHED" << endl;

return 0;
}
Binary file added ch26/e1
Binary file not shown.
13 changes: 13 additions & 0 deletions ch26/e1.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char **argv) {
string s = "What a beautiful weather!!!";
string t;
t = s;
std::cout << t << std::endl;

return 0;
}
Binary file added ch26/e2
Binary file not shown.
25 changes: 25 additions & 0 deletions ch26/e2.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <iostream>
#include <string>

using namespace std;

void something(string s) {
cout << "s = [" << s << "]" << endl;
s[0] = 'X';
cout << "s = [" << s << "]" << endl;
}

int main(int argc, char **argv) {
string s1 = "University";
string s2 = " of ";
string s3(" Chicago");
string s4;
s4 = s1 + s2;
s4 += s3;
cout << s4 << endl;
s1 = s4.substr(11, 2);
cout << s1 << endl;
something(s1);
cout << s1 << endl;
return 0;
}
25 changes: 25 additions & 0 deletions ch26/e3.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class IntIterator {
public:
virtual bool hasNext() = 0;
virtual int next() = 0;
};

class SmallIterator : public IntIterator {
public:
SmallIterator() : n(0) {}
SmallIterator(int aa) : a(aa), n(1) {}
SmallIterator(int bb, int aa) :a(aa), b(bb), n(2) {}
bool hasNext() {
return n > 0;
}
int next() {
n--;
if (n == 1) {
return b;
} else if (n == 0) {
return a;
}
}
private:
int a, b, n;
};

0 comments on commit 0a9f2fe

Please sign in to comment.