-
Notifications
You must be signed in to change notification settings - Fork 247
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
ArrayStack looks bogus #97
Comments
If you read Chapter 1, you'll find that there are only four interfaces
implemented in the book: List, USet, SSet, and (Priority) Queue.
You'll also find this text:
Although we will normally not discuss the Stack, Deque and FIFO Queue
interfaces in subsequent chapters, the terms Stack and Deque are
sometimes used in the names of data structures that implement the List
interface. When this happens, it highlights the fact that these data
structures can be used to implement the Stack or Deque interface very
efficiently. For example, the ArrayDeque class is an implementation of
the List interface that implements all the Deque operations in
constant time per operation.
…On Fri, Apr 29, 2022 at 2:03 AM StéphaneDucasse ***@***.***> wrote:
I started the book and the first datastructure feels strange and it gave a strange view on the rest of the book.
See
https://github.com/Ducasse/Containers-XP/blob/master/Containers-XP/CTArrayStack.class.st
and copied here.
Started to implement ArrayStack from this book https://opendatastructures.org/
But the first datastructure feels strange and it gave a strange view on the rest of the book.
The book defines the state of ArrayStack as an array and a number representing the number of elements.
T[] a;
int n;
int size() {
return n; }
For example add is defined as follows:
void add(int i, T x) {
if (n + 1 > a.length) resize();
for (int j = n; j > i; j--)
a[j] = a[j-1];
a[i] = x;
n++;
}
Set is defined as follows:
T set(int i, T x) {
T y = a[i];
a[i] = x;
return y;
}
To me these definition are bogus.
Indeed
first set does not update the number of elements, so using set break the invariant that n is the number of elements stored.
second set should not return the previous value because it propagates null value
third why set and get are needed (I renamed them as at: at:put:) but they are not needed in a Stack API.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
Thanks ok for the names. I will continue to read the book but code breaks invariants and this is a pity because it could be a great book. Here is my full log of analysis of the first ArrayStack implementation. Started to implement ArrayStack from this book https://opendatastructures.org/ The book defines the state of ArrayStack as an array and a number representing the number of elements.
AnalysisTo me these definitions are bogus. The name is particularly not good because this is not a stack but a list. So a better name is SimpleArrayList or NaiveArrayList. About set and get
API problemsThe java implementation is better than the algo in the book but still some of them are bogus. Let us have a look at add
Imagine that we have a ArrayStack with 5 of capacity and with 5 elements, the user can use add(3000, anObject). This example raises two problems:
Unclear pointsIt is unclear why elements are shifted in the remove or add. It looks like add is in fact an insertAt a given index. The fact that we can add an element at a given index is mixing lot of concerns. |
I started the book and the first datastructure feels strange and it gave a strange view on the rest of the book.
See
https://github.com/Ducasse/Containers-XP/blob/master/Containers-XP/CTArrayStack.class.st
and copied here.
Started to implement ArrayStack from this book https://opendatastructures.org/
But the first datastructure feels strange and it gave a strange view on the rest of the book.
The book defines the state of ArrayStack as an array and a number representing the number of elements.
For example add is defined as follows:
Set is defined as follows:
To me these definition are bogus.
Indeed
set
does not update the number of elements, so using set break the invariant that n is the number of elements stored.set
should not return the previous value because it propagates null valueset
andget
are needed (I renamed them as at: at:put:) but they are not needed in a Stack API.The text was updated successfully, but these errors were encountered: