Skip to content

Commit

Permalink
L18 demos
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Weng authored and Paul Weng committed Jul 7, 2020
1 parent c77be57 commit 451fe43
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 0 deletions.
52 changes: 52 additions & 0 deletions L18-Deep-Copy/1-Dangling-Pointer/IntSet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "IntSet.h"
#include <iostream>

using namespace std;

IntSet::IntSet(int size): elts(new int[size]),
sizeElts(size), numElts(0){
}

IntSet::~IntSet(){
cout << "Destructor" << endl;
delete[] elts;
}

int IntSet::indexOf(int v){
for (int i = 0; i < numElts; i++){
if (elts[i] == v)
return i;
}
return sizeElts;
}

int IntSet::size(){
return numElts;
}

bool IntSet::query(int v){
return (indexOf(v) != sizeElts);
}

void IntSet::insert(int v){
if (indexOf(v) == sizeElts){
if (numElts == sizeElts)
throw sizeElts;
elts[numElts++] = v;
}
}

void IntSet::remove(int v){
int victim = indexOf(v);
if (victim != sizeElts){
elts[victim] = elts[numElts-1];
numElts--;
}
}

void IntSet::print(){
for (int i = 0; i < numElts; i++){
cout << elts[i] << " " << flush;
}
cout << endl;
}
40 changes: 40 additions & 0 deletions L18-Deep-Copy/1-Dangling-Pointer/IntSet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef INTSET_H
#define INTSET_H

const int MAXELTS = 100;

class IntSet{
// OVERVIEW: a mutable set of integers, |set| <= sizeElts
int *elts; // pointer to dynamic array
int sizeElts; // capacity of array
int numElts; // current occupancy

int indexOf(int v);
// EFFECTS: returns the index of v if it exists in the
// array, sizeElts otherwise.

public:
IntSet(int size = MAXELTS);
// EFFECTS: create a set with specified capacity.
// It defaults to MAXELTS if not supplied.
~IntSet(); // Destroy this IntSet

void insert(int v);
// MODIFIES: this
// EFFECTS: this = this + {v} if room,
// throws int sizeElts otherwise.
void remove(int v);
// MODIFIES: this
// EFFECTS: this = this - {v} if v is in this
// throws int v otherwise.
bool query(int v);
// EFFECTS: returns true if v is in this, false otherwise.
int size();
// EFFECTS: returns |this|.
void print();
// MODIFIES: cout
// EFFECTS: print out the integers contained in the set in
// sequence.
};

#endif
34 changes: 34 additions & 0 deletions L18-Deep-Copy/1-Dangling-Pointer/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# variable definition

CC = g++

DEFS =
LIBS =
INCLUDES = #-I.
HEADERS = #int_t.h int_impl.h
MAINSRCS = main.cpp
OTHSRCS = IntSet.cpp
SRCS = $(MAINSRCS) $(OTHSRCS)
OBJS = $(SRCS:.cpp=.o)
TARGETS = $(MAINSRCS:.cpp=)

CFLAGS = -g -Wall $(INCLUDES) $(DEFS)

%.o: %.cpp
$(CC) $(CFLAGS) -o $@ -c $<

all: $(TARGETS)

$(TARGETS): $(OBJS)
$(CC) $(CFLAGS) -o $(TARGETS) $(OBJS) $(LIBS)

depend:
makedepend -Y $(INCLUDES) $(SRCS)

memcheck: $(TARGETS)
valgrind --leak-check=full ./$(TARGETS)

clean:
rm -f $(OBJS) $(TARGETS)

.PHONY: all depend memcheck clean
24 changes: 24 additions & 0 deletions L18-Deep-Copy/1-Dangling-Pointer/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <iostream>
#include "IntSet.h"

using namespace std;

void foo(IntSet x){
}

int main(){
IntSet s;
s.insert(5);
s.print();

// foo(s);
// s.print();

{
IntSet x;
x = s;
}
s.print();

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "IntSet.h"
#include <iostream>

using namespace std;

IntSet::IntSet(int size): elts(new int[size]),
sizeElts(size), numElts(0){
}

IntSet::IntSet(const IntSet &is): elts(NULL), sizeElts(0), numElts(0){
cout << "Copy constructor\n";
copyFrom(is);
}

IntSet& IntSet::operator= (const IntSet &is){
if(this != &is)
copyFrom(is);
return *this;
}

IntSet::~IntSet(){
delete[] elts;
}

int IntSet::indexOf(int v){
for (int i = 0; i < numElts; i++){
if (elts[i] == v)
return i;
}
return sizeElts;
}

void IntSet::copyFrom(const IntSet &is){
if (is.sizeElts != sizeElts){ // Resize array
delete[] elts;
sizeElts = is.sizeElts;
elts = new int[sizeElts];
}
// Copy array
for (int i = 0; i < is.sizeElts; i++){
elts[i] = is.elts[i];
}
// Establish numElts invariant
numElts = is.numElts;
}

int IntSet::size(){
return numElts;
}

bool IntSet::query(int v){
return (indexOf(v) != sizeElts);
}

void IntSet::insert(int v){
if (indexOf(v) == sizeElts){
if (numElts == sizeElts)
throw sizeElts;
elts[numElts++] = v;
}
}

void IntSet::remove(int v){
int victim = indexOf(v);
if (victim != sizeElts){
elts[victim] = elts[numElts-1];
numElts--;
}
}

void IntSet::print(){
for (int i = 0; i < numElts; i++){
cout << elts[i] << " " << flush;
}
cout << endl;
}
49 changes: 49 additions & 0 deletions L18-Deep-Copy/2-Copy-Constructor-and-Assignment-Operator/IntSet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#ifndef INTSET_H
#define INTSET_H

const int MAXELTS = 100;

class IntSet{
// OVERVIEW: a mutable set of integers, |set| <= sizeElts
int *elts; // pointer to dynamic array
int sizeElts; // capacity of array
int numElts; // current occupancy

int indexOf(int v);
// EFFECTS: returns the index of v if it exists in the
// array, sizeElts otherwise.

void copyFrom(const IntSet &is);
// MODIFIES: this
// EFFECTS: copies is contents to this

public:
IntSet(int size = MAXELTS);
// EFFECTS: create a set with specified capacity.
// It defaults to MAXELTS if not supplied.

IntSet(const IntSet &is); // copy constructor

IntSet& operator= (const IntSet& is); // assigment operator

~IntSet(); // Destroy this IntSet

void insert(int v);
// MODIFIES: this
// EFFECTS: this = this + {v} if room,
// throws int sizeElts otherwise.
void remove(int v);
// MODIFIES: this
// EFFECTS: this = this - {v} if v is in this
// throws int v otherwise.
bool query(int v);
// EFFECTS: returns true if v is in this, false otherwise.
int size();
// EFFECTS: returns |this|.
void print();
// MODIFIES: cout
// EFFECTS: print out the integers contained in the set in
// sequence.
};

#endif
34 changes: 34 additions & 0 deletions L18-Deep-Copy/2-Copy-Constructor-and-Assignment-Operator/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# variable definition

CC = g++

DEFS =
LIBS =
INCLUDES = #-I.
HEADERS = #int_t.h int_impl.h
MAINSRCS = main.cpp
OTHSRCS = IntSet.cpp
SRCS = $(MAINSRCS) $(OTHSRCS)
OBJS = $(SRCS:.cpp=.o)
TARGETS = $(MAINSRCS:.cpp=)

CFLAGS = -g -Wall $(INCLUDES) $(DEFS)

%.o: %.cpp
$(CC) $(CFLAGS) -o $@ -c $<

all: $(TARGETS)

$(TARGETS): $(OBJS)
$(CC) $(CFLAGS) -o $(TARGETS) $(OBJS) $(LIBS)

depend:
makedepend -Y $(INCLUDES) $(SRCS)

memcheck: $(TARGETS)
valgrind --leak-check=full ./$(TARGETS)

clean:
rm -f $(OBJS) $(TARGETS)

.PHONY: all depend memcheck clean
28 changes: 28 additions & 0 deletions L18-Deep-Copy/2-Copy-Constructor-and-Assignment-Operator/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <iostream>
#include "IntSet.h"

using namespace std;

void foo(IntSet x){
}

int main() {

IntSet s(10);
s.insert(5);

foo(s);
s.query(5);
s.print();

{
IntSet x;
x = s;
x.print();
}

s.query(5);
s.print();

return 0;
}

0 comments on commit 451fe43

Please sign in to comment.