From 077f0cd67295aca6aa0dd0cc80a78237e693095d Mon Sep 17 00:00:00 2001 From: Paul Weng Date: Thu, 9 Jul 2020 09:52:21 +0800 Subject: [PATCH] dynamic resizing demo --- L19-Dynamic-Resizing/1-Inc/IntSet.cpp | 86 ++++++++++++++++++++++++ L19-Dynamic-Resizing/1-Inc/IntSet.h | 52 ++++++++++++++ L19-Dynamic-Resizing/1-Inc/Makefile | 34 ++++++++++ L19-Dynamic-Resizing/1-Inc/main.cpp | 22 ++++++ L19-Dynamic-Resizing/2-Double/IntSet.cpp | 86 ++++++++++++++++++++++++ L19-Dynamic-Resizing/2-Double/IntSet.h | 52 ++++++++++++++ L19-Dynamic-Resizing/2-Double/Makefile | 34 ++++++++++ L19-Dynamic-Resizing/2-Double/main.cpp | 22 ++++++ 8 files changed, 388 insertions(+) create mode 100644 L19-Dynamic-Resizing/1-Inc/IntSet.cpp create mode 100644 L19-Dynamic-Resizing/1-Inc/IntSet.h create mode 100644 L19-Dynamic-Resizing/1-Inc/Makefile create mode 100644 L19-Dynamic-Resizing/1-Inc/main.cpp create mode 100644 L19-Dynamic-Resizing/2-Double/IntSet.cpp create mode 100644 L19-Dynamic-Resizing/2-Double/IntSet.h create mode 100644 L19-Dynamic-Resizing/2-Double/Makefile create mode 100644 L19-Dynamic-Resizing/2-Double/main.cpp diff --git a/L19-Dynamic-Resizing/1-Inc/IntSet.cpp b/L19-Dynamic-Resizing/1-Inc/IntSet.cpp new file mode 100644 index 0000000..d7978bc --- /dev/null +++ b/L19-Dynamic-Resizing/1-Inc/IntSet.cpp @@ -0,0 +1,86 @@ +#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; +} + +void IntSet::grow(){ + int *tmp = new int[sizeElts + 1]; + for (int i = 0; i < numElts; i++) { + tmp[i] = elts[i]; + } + delete [] elts; + elts = tmp; + sizeElts += 1; +} + +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) + grow(); + 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/L19-Dynamic-Resizing/1-Inc/IntSet.h b/L19-Dynamic-Resizing/1-Inc/IntSet.h new file mode 100644 index 0000000..bf45e16 --- /dev/null +++ b/L19-Dynamic-Resizing/1-Inc/IntSet.h @@ -0,0 +1,52 @@ +#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 + + void grow(); + // MODIFIES: this + // EFFECTS: enlarge the elts array, + // preserving current content +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} + 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/L19-Dynamic-Resizing/1-Inc/Makefile b/L19-Dynamic-Resizing/1-Inc/Makefile new file mode 100644 index 0000000..6c32488 --- /dev/null +++ b/L19-Dynamic-Resizing/1-Inc/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/L19-Dynamic-Resizing/1-Inc/main.cpp b/L19-Dynamic-Resizing/1-Inc/main.cpp new file mode 100644 index 0000000..8aedcb6 --- /dev/null +++ b/L19-Dynamic-Resizing/1-Inc/main.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +#include +#include "IntSet.h" + +using namespace std; + +int main(int argc, char *argv[]) { + int n = atoi(argv[1]); + srand(atoi(argv[2])); + IntSet s(1); + + clock_t begin = clock(); + for(int i=1; i + +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; +} + +void IntSet::grow(){ + int *tmp = new int[sizeElts * 2]; + for (int i = 0; i < numElts; i++) { + tmp[i] = elts[i]; + } + delete [] elts; + elts = tmp; + sizeElts *= 2; +} + +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) + grow(); + 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/L19-Dynamic-Resizing/2-Double/IntSet.h b/L19-Dynamic-Resizing/2-Double/IntSet.h new file mode 100644 index 0000000..bf45e16 --- /dev/null +++ b/L19-Dynamic-Resizing/2-Double/IntSet.h @@ -0,0 +1,52 @@ +#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 + + void grow(); + // MODIFIES: this + // EFFECTS: enlarge the elts array, + // preserving current content +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} + 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/L19-Dynamic-Resizing/2-Double/Makefile b/L19-Dynamic-Resizing/2-Double/Makefile new file mode 100644 index 0000000..6c32488 --- /dev/null +++ b/L19-Dynamic-Resizing/2-Double/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/L19-Dynamic-Resizing/2-Double/main.cpp b/L19-Dynamic-Resizing/2-Double/main.cpp new file mode 100644 index 0000000..8aedcb6 --- /dev/null +++ b/L19-Dynamic-Resizing/2-Double/main.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +#include +#include "IntSet.h" + +using namespace std; + +int main(int argc, char *argv[]) { + int n = atoi(argv[1]); + srand(atoi(argv[2])); + IntSet s(1); + + clock_t begin = clock(); + for(int i=1; i