Skip to content

Commit

Permalink
dynamic resizing demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Weng authored and Paul Weng committed Jul 9, 2020
1 parent f40fd36 commit 077f0cd
Show file tree
Hide file tree
Showing 8 changed files with 388 additions and 0 deletions.
86 changes: 86 additions & 0 deletions L19-Dynamic-Resizing/1-Inc/IntSet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#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;
}

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;
}
52 changes: 52 additions & 0 deletions L19-Dynamic-Resizing/1-Inc/IntSet.h
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions L19-Dynamic-Resizing/1-Inc/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
22 changes: 22 additions & 0 deletions L19-Dynamic-Resizing/1-Inc/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <iostream>
#include <stdlib.h>
#include <cstdlib>
#include <ctime>
#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<n; i++){
s.insert(i);
}
clock_t end = clock();
cout << double(end - begin) / CLOCKS_PER_SEC << endl;

return 0;
}
86 changes: 86 additions & 0 deletions L19-Dynamic-Resizing/2-Double/IntSet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#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;
}

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;
}
52 changes: 52 additions & 0 deletions L19-Dynamic-Resizing/2-Double/IntSet.h
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions L19-Dynamic-Resizing/2-Double/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
22 changes: 22 additions & 0 deletions L19-Dynamic-Resizing/2-Double/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <iostream>
#include <stdlib.h>
#include <cstdlib>
#include <ctime>
#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<n; i++){
s.insert(i);
}
clock_t end = clock();
cout << double(end - begin) / CLOCKS_PER_SEC << endl;

return 0;
}

0 comments on commit 077f0cd

Please sign in to comment.