Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arrays swap #36

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cpp/Algorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void countingSort(array<int> &a, int k) {
array<int> b(a.length);
for (int i = a.length-1; i >= 0; i--)
b[--c[a[i]]] = a[i];
a = b;
a.swap(b);
}

void radixSort(array<int> &a) {
Expand All @@ -162,7 +162,7 @@ void radixSort(array<int> &a) {
c[i] += c[i-1];
for (int i = a.length-1; i >= 0; i--)
b[--c[(a[i] >> d*p)&((1<<d)-1)]] = a[i];
a = b;
a.swap(b);
}
}

Expand Down
4 changes: 2 additions & 2 deletions cpp/ArrayDeque.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void ArrayDeque<T>::clear() {
n = 0;
j = 0;
array<T> b(1);
a = b;
a.swap(b);
}

template<class T>
Expand All @@ -65,7 +65,7 @@ void ArrayDeque<T>::resize() {
array<T> b(max(1, 2*n));
for (int k = 0; k < n; k++)
b[k] = a[(j+k)%a.length];
a = b;
a.swap(b);
j = 0;
}

Expand Down
2 changes: 1 addition & 1 deletion cpp/ArrayQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void ArrayQueue<T>::resize() {
array<T> b(max(1, 2*n));
for (int k = 0; k < n; k++)
b[k] = a[(j+k)%a.length];
a = b;
a.swap(b);
j = 0;
}

Expand Down
4 changes: 2 additions & 2 deletions cpp/ArrayStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ template<class T>
void ArrayStack<T>::clear() {
n = 0;
array<T> b(1);
a = b;
a.swap(b);
}

template <class T>
Expand All @@ -72,7 +72,7 @@ void ArrayStack<T>::resize() {
array<T> b(max(2 * n, 1));
for (int i = 0; i < n; i++)
b[i] = a[i];
a = b;
a.swap(b);
}

template<class T>
Expand Down
6 changes: 3 additions & 3 deletions cpp/BinaryHeap.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ template<class T>
void BinaryHeap<T>::resize() {
array<T> b(max(2*n, 1));
std::copy(a+0, a+n, b+0);
a = b;
a.swap(b);
}

template<class T>
Expand All @@ -62,7 +62,7 @@ void BinaryHeap<T>::sort(array<T> &b) {
h.a.swap(--h.n, 0);
h.trickleDown(0);
}
b = h.a;
b.swap(h.a);
b.reverse();
}

Expand Down Expand Up @@ -134,7 +134,7 @@ BinaryHeap<T>::BinaryHeap() : a(1) {

template<class T>
BinaryHeap<T>::BinaryHeap(array<T> &b) : a(0) {
a = b;
a.swap(b);
n = a.length;
for (int i = n/2-1; i >= 0; i--) {
trickleDown(i);
Expand Down
4 changes: 2 additions & 2 deletions cpp/ChainedHashTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void ChainedHashTable<T>::resize() {
newTable[hash(x)].add(x);
}
}
t = newTable;
t.swap(newTable);
}

template<class T>
Expand Down Expand Up @@ -114,7 +114,7 @@ void ChainedHashTable<T>::clear() {
n = 0;
d = 1;
array<List> b(2);
t = b;
t.swap(b);
}

} /* namespace ods */
Expand Down
4 changes: 2 additions & 2 deletions cpp/DualArrayDeque.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ void DualArrayDeque<T>::balance() {
for (int i = 0; i < nb; i++) {
ab[i] = get(nf+i);
}
front.a = af;
front.a.swap(af);
front.n = nf;
back.a = ab;
back.a.swap(ab);
back.n = nb;
}
}
Expand Down
2 changes: 1 addition & 1 deletion cpp/FastArrayStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ template<class T>
void FastArrayStack<T>::resize() {
array<T> b(max(1, 2*n));
std::copy(a+0, a+n, b+0);
a = b;
a.swap(b);
}

template<class T>
Expand Down
4 changes: 2 additions & 2 deletions cpp/LinearHashTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void LinearHashTable<T>::resize() {
tnew[i] = t[k];
}
}
t = tnew;
t.swap(tnew);
}

template<class T>
Expand All @@ -121,7 +121,7 @@ void LinearHashTable<T>::clear() {
q = 0;
d = 1;
array<T> tnew(2, null);
t = tnew;
t.swap(tnew);
}

template<class T>
Expand Down
2 changes: 1 addition & 1 deletion cpp/SEList.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SEList {
n = 0;
j = 0;
array<int> z(b+1);
a = z;
a.swap(z);
}
virtual ~BDeque() { }
// C++ Question: Why is this necessary?
Expand Down
10 changes: 6 additions & 4 deletions cpp/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ class array {
void fill(T x);
virtual ~array();

array<T>& operator=(array<T> &b) {
if (a != NULL) delete[] a;
void swap(array<T> &b) {
T *ta = a;
a = b.a;
b.a = NULL;
b.a = ta;
int tl = length;
length = b.length;
b.length = tl;
return *this;
}

Expand Down Expand Up @@ -88,7 +90,7 @@ template<class T>
void array<T>::copyOfRange(array<T> &a0, array<T> &a, int i, int j) {
array<T> b(j-i);
std::copy(a.a, a.a+j-i, b.a);
a0 = b;
a0.swap(b);
}

template<class T>
Expand Down
6 changes: 3 additions & 3 deletions latex/arrays.tex
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ \chapter{Array-Based Lists}
\cppimport{ods/array.array(len)}
\cpponly{The elements of an array can be indexed:}
\cppimport{ods/array.operator[]}
\cpponly{Finally, when one array is assigned to another, this is just
\cpponly{Finally, when one array is swapped with another, this is just
a pointer manipulation that takes constant time:}
\cppimport{ods/array.operator=}
\cppimport{ods/array.swap(b)}

\section{#ArrayStack#: Fast Stack Operations Using an Array}
\seclabel{arraystack}
Expand Down Expand Up @@ -132,7 +132,7 @@ \subsection{Growing and Shrinking}

The #resize()# method is fairly straightforward; it allocates a new
array #b# whose size is $2#n#$ and copies the #n# elements of #a# into
the first #n# positions in #b#, and then sets #a# to #b#. Thus, after a call to #resize()#, $#a.length# = 2#n#$.
the first #n# positions in #b#, and then \javaonly{sets #a# to #b#}\cpponly{swaps #a# with #b#}. Thus, after a call to #resize()#, $#a.length# = 2#n#$.

\codeimport{ods/ArrayStack.resize()}

Expand Down