From 451fe4309af96089c50e914d4bb6252791451303 Mon Sep 17 00:00:00 2001 From: Paul Weng Date: Tue, 7 Jul 2020 09:13:35 +0800 Subject: [PATCH] L18 demos --- L18-Deep-Copy/1-Dangling-Pointer/IntSet.cpp | 52 +++++++++++++ L18-Deep-Copy/1-Dangling-Pointer/IntSet.h | 40 ++++++++++ L18-Deep-Copy/1-Dangling-Pointer/Makefile | 34 +++++++++ L18-Deep-Copy/1-Dangling-Pointer/main.cpp | 24 ++++++ .../IntSet.cpp | 76 +++++++++++++++++++ .../IntSet.h | 49 ++++++++++++ .../Makefile | 34 +++++++++ .../main.cpp | 28 +++++++ 8 files changed, 337 insertions(+) create mode 100644 L18-Deep-Copy/1-Dangling-Pointer/IntSet.cpp create mode 100644 L18-Deep-Copy/1-Dangling-Pointer/IntSet.h create mode 100644 L18-Deep-Copy/1-Dangling-Pointer/Makefile create mode 100644 L18-Deep-Copy/1-Dangling-Pointer/main.cpp create mode 100644 L18-Deep-Copy/2-Copy-Constructor-and-Assignment-Operator/IntSet.cpp create mode 100644 L18-Deep-Copy/2-Copy-Constructor-and-Assignment-Operator/IntSet.h create mode 100644 L18-Deep-Copy/2-Copy-Constructor-and-Assignment-Operator/Makefile create mode 100644 L18-Deep-Copy/2-Copy-Constructor-and-Assignment-Operator/main.cpp diff --git a/L18-Deep-Copy/1-Dangling-Pointer/IntSet.cpp b/L18-Deep-Copy/1-Dangling-Pointer/IntSet.cpp new file mode 100644 index 0000000..f8bd1e5 --- /dev/null +++ b/L18-Deep-Copy/1-Dangling-Pointer/IntSet.cpp @@ -0,0 +1,52 @@ +#include "IntSet.h" +#include + +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; +} diff --git a/L18-Deep-Copy/1-Dangling-Pointer/IntSet.h b/L18-Deep-Copy/1-Dangling-Pointer/IntSet.h new file mode 100644 index 0000000..145b95b --- /dev/null +++ b/L18-Deep-Copy/1-Dangling-Pointer/IntSet.h @@ -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 diff --git a/L18-Deep-Copy/1-Dangling-Pointer/Makefile b/L18-Deep-Copy/1-Dangling-Pointer/Makefile new file mode 100644 index 0000000..6c32488 --- /dev/null +++ b/L18-Deep-Copy/1-Dangling-Pointer/Makefile @@ -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 diff --git a/L18-Deep-Copy/1-Dangling-Pointer/main.cpp b/L18-Deep-Copy/1-Dangling-Pointer/main.cpp new file mode 100644 index 0000000..0521de7 --- /dev/null +++ b/L18-Deep-Copy/1-Dangling-Pointer/main.cpp @@ -0,0 +1,24 @@ +#include +#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; +} diff --git a/L18-Deep-Copy/2-Copy-Constructor-and-Assignment-Operator/IntSet.cpp b/L18-Deep-Copy/2-Copy-Constructor-and-Assignment-Operator/IntSet.cpp new file mode 100644 index 0000000..adbc6eb --- /dev/null +++ b/L18-Deep-Copy/2-Copy-Constructor-and-Assignment-Operator/IntSet.cpp @@ -0,0 +1,76 @@ +#include "IntSet.h" +#include + +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; +} diff --git a/L18-Deep-Copy/2-Copy-Constructor-and-Assignment-Operator/IntSet.h b/L18-Deep-Copy/2-Copy-Constructor-and-Assignment-Operator/IntSet.h new file mode 100644 index 0000000..4f8ce31 --- /dev/null +++ b/L18-Deep-Copy/2-Copy-Constructor-and-Assignment-Operator/IntSet.h @@ -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 diff --git a/L18-Deep-Copy/2-Copy-Constructor-and-Assignment-Operator/Makefile b/L18-Deep-Copy/2-Copy-Constructor-and-Assignment-Operator/Makefile new file mode 100644 index 0000000..6c32488 --- /dev/null +++ b/L18-Deep-Copy/2-Copy-Constructor-and-Assignment-Operator/Makefile @@ -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 diff --git a/L18-Deep-Copy/2-Copy-Constructor-and-Assignment-Operator/main.cpp b/L18-Deep-Copy/2-Copy-Constructor-and-Assignment-Operator/main.cpp new file mode 100644 index 0000000..ccdd4a7 --- /dev/null +++ b/L18-Deep-Copy/2-Copy-Constructor-and-Assignment-Operator/main.cpp @@ -0,0 +1,28 @@ +#include +#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; +}