Question: why does whoever wants to use your stack have to pass two generic arguments? What até your Stacknode objects for?
The Big Programming Thread - Page 685
Forum Index > General Forum |
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. | ||
Acrofales
Spain17842 Posts
Question: why does whoever wants to use your stack have to pass two generic arguments? What até your Stacknode objects for? | ||
WarSame
Canada1950 Posts
| ||
Acrofales
Spain17842 Posts
Passing this by argument seems like a bad idea. The least bad, would be with a boolean or enum argument stating what type of stack should be created, but this is extremely unjavalike. EDIT: just to be clear, generics for the type of object to be stored in the Stack is exactly the type of thing that generics are good for, so Stack<String> or Stack<Jabberwocky> makes perfect sense. Just not Stack<MinstackNode<Jabberwocky>, Jabberwocky>. | ||
spinesheath
Germany8679 Posts
In C# you could specify that your generic type parameter needs to have a parameterless constructor, but it doesn't seem like Java has that functionality. So you're left with using a IStackNodeFactory interface that has a CreateStackNode method, or trying to find and call a somehow fitting constructor via reflection (which is like rolling a dice). The factory requires the user to provide yet another class just to use your stack. For a simple container type that's waaaaaaaay to complex. | ||
WarSame
Canada1950 Posts
EDIT: I'm not really sure how to replace the StackNodes with MinStackNodes after the inheritance, though. That was the original reason that I did all the weird stuff I didd. | ||
WarSame
Canada1950 Posts
On December 03 2015 05:55 spinesheath wrote: Typically a stack only has one generic argument and that's the type of the data it holds. I don't know a reason why I would want to specify a "StackNode" type. I want to put stuff on the stack and pop it off. Let the stack figure out how to hold that stuff. In C# you could specify that your generic type parameter needs to have a parameterless constructor, but it doesn't seem like Java has that functionality. So you're left with using a IStackNodeFactory interface that has a CreateStackNode method, or trying to find and call a somehow fitting constructor via reflection (which is like rolling a dice). The factory requires the user to provide yet another class just to use your stack. For a simple container type that's waaaaaaaay to complex. It seemed like a decent abstraction to me - the node needs to store data, and it needs to store the next node. I don't know if you can do those in Java without a class holding them, but I'm really not sure. Anyway, I'm trying to figure one more thing out: my Stack now just has the 1 generic type, but when I extend it in my MinStack I have problems because it assumes I want to use StackNodes. I want to use MinStackNodes, though, so that I can keep track of the min. Is it possible for me to implement this while still extending? | ||
Acrofales
Spain17842 Posts
On December 03 2015 05:56 WarSame wrote: Ahhh great point. So I should allow them to, for example, make a MinStack, but then just fill it with MinStackNodes automatically? It ended up this way because I was having problems with inheriting MinStack from Stack - I'll give your feedback a whirl and see what I come up with. Thanks! EDIT: I'm not really sure how to replace the StackNodes with MinStackNodes after the inheritance, though. That was the original reason that I did all the weird stuff I didd. But that's a different problem. What I would probably do in this case is just cast my MinStackNodes to StackNodes for the stack functionalities, but without more code it's hard to say for sure that this is the best solution. | ||
WarSame
Canada1950 Posts
Stack and StackNode MinStack and MinStackNode | ||
Acrofales
Spain17842 Posts
In your Stack:
And then in your MinStack you can use internalPush with MinStackNodes. If you don't do something like this, your Stack will always use StackNodes, and never MinStackNodes. That said, it might just be better to have your MinStack not extend Stack, but be a wrapper around it, using the Decorator pattern (https://en.wikipedia.org/wiki/Decorator_pattern). Many roads lead to Rome. I personally prefer the Decorator pattern, it is neater. But it's quite a lot of effort for just adding one extension. If you plan on decorating your stack in other ways than just min, it's worth it. Otherwise it's a load of refactoring for little gain. EDIT: nvm EDIT 2: yeah, stick with Wikipedia. That example had all kinda shit wrong with it :D | ||
WarSame
Canada1950 Posts
| ||
spinesheath
Germany8679 Posts
| ||
waffelz
Germany711 Posts
Like:
I mean on a deep down level. How exactly does the comparison work? Did the strings get compared for each bit or is there some optimisation involved (I assume so in terms of strings). Do they get compared per character? How does it work on other data-types? Just need a general direction where to go for this kind of stuff since google keeps flooding me with “how to”-articles. I have to write a small paper on the subject of searching this semester and would like to actually figure something out myself rather than acting like I did by simply comparing different search algorithms and thought that figuring out a few improved algorithms for cases where you normally would have to use linear searchalgorithms / lazysearch because your data might be unsorted / doesn’t support the usual algorithms properly. EDIT: I should also note that my results aren’t that important, focus of this is the scientific progress, so it would be fine if I came to the conclusion that my algorithms aren’t better (which I assume is most likely besides in some very special cases). I just liked to include some actual testing where the results have the potential to surprise me while also being able to justify the time I need to sink in compared to the low CP I get for it. | ||
teamamerica
United States958 Posts
On December 03 2015 15:52 waffelz wrote: Could someone give me a hint where should I look if I want to know how comparing really works? Like:
I mean on a deep down level. How exactly does the comparison work? Did the strings get compared for each bit or is there some optimisation involved (I assume so in terms of strings). Do they get compared per character? How does it work on other data-types? Just need a general direction where to go for this kind of stuff since google keeps flooding me with “how to”-articles. I have to write a small paper on the subject of searching this semester and would like to actually figure something out myself rather than acting like I did by simply comparing different search algorithms and thought that figuring out a few improved algorithms for cases where you normally would have to use linear searchalgorithms / lazysearch because your data might be unsorted / doesn’t support the usual algorithms properly. EDIT: I should also note that my results aren’t that important, focus of this is the scientific progress, so it would be fine if I came to the conclusion that my algorithms aren’t better (which I assume is most likely besides in some very special cases). I just liked to include some actual testing where the results have the potential to surprise me while also being able to justify the time I need to sink in compared to the low CP I get for it. Depends on lang I'd say for comparison like that. Java == will compare memory references which is why you're advised to use .equals (with a cavaeat about interned strings will work with ==, interned strings being either those in the source code already or those with interned called on). Actual implementation of .equals() on String class is fairly boring. I don't know of any huge secrets for actually checking equality besides well...actually checking each char. There's tradesoffs you can make in accuracy (see bloom filters) to get faster results but then you're trading off accurary (which is why Java String equals doesn't use hashCode, which would be a cached value after it's first computed). If you're interested in searching I'd review search algorithms in the context of modern processors with many cores / numa memory and real world implementations (e.g. picking good pivot for quicksorts to avoid pathological cases). | ||
teamamerica
United States958 Posts
On December 03 2015 05:16 WarSame wrote: So I've been at the library 9-6 for today and yesterday preparing for my job. I've run into a snag with Java, and I can't understand the answers I've googled. I'm creating a generic stack Stack<T extends StackNode<E>, E>, with StackNode<E>. However, I can't declare a new StackNode in my Stack properly. Using T node = new T(data);throws an error on the T, saying it can't be instantiated. Google results say this is due to erasure, but not really how to get around it. This seems to be the best answer, but I can't figure out how to apply it to my specific case. Where do I place the function mentioned? Where do I call it from? How do you call it? The comments tried to clear it up but I can't understand those well enough, either. My current attempt in Stack looks like:
and in StackNode:
However, Stack has 2 errors - it can't find CreateContents and it says that T.class is an illegal literal. Further, the "constructor" is only able to call the empty constructor. I think that can be handled by calling another function that sets the value of data, although I'm not sure how to get the value where it needs to be. If anyone can give me advice on how to do this or tell me a better way to do this I'm all ears. Thanks! I'd also mention that reflection seems unnecessary. StackNode seems like a prime candidate for a private static nested class inside stack, no need for it to be public. I see what you're going for with MinStack but I'd argue that the node knowing the minimum is confusing concerns. What is minimum in the context of a node? It doesn't even know it's part of a bigger data structure. If you're going to have that you can just keep track of min on the stack itself. Not written Java in a while but this is how I'd imagine itd look. Note I use a Comparator rather then using Comparable because a lot of classes don't implement Comparable so why force someone to wrap them to use your cool codez? See https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html as an example for how a Java collection that inherits from an unordered collection offers an ordered subclass.
| ||
Ropid
Germany3557 Posts
On December 03 2015 15:52 waffelz wrote: It depends on the language you talk about, but string is probably not a basic data type and is defined in the language itself and you have the source of that installed somewhere. You could look up the source of "==", though it's probably terrible to read. If you use an IDE like Visual Studio for your stuff, you could try to step into the "==" function call with the debugger. You could follow what's done on the two strings step-by-step. | ||
waffelz
Germany711 Posts
On December 03 2015 17:25 Ropid wrote: It depends on the language you talk about, but string is probably not a basic data type and is defined in the language itself and you have the source of that installed somewhere. You could look up the source of "==", though it's probably terrible to read. If you use an IDE like Visual Studio for your stuff, you could try to step into the "==" function call with the debugger. You could follow what's done on the two strings step-by-step. Yeah I already assumed that there isn't a general approach to this, but hoped there would be a more elegant way then just checking through IDEs. Thank you nonetheless. | ||
WarSame
Canada1950 Posts
On December 03 2015 17:04 teamamerica wrote: I'd also mention that reflection seems unnecessary. StackNode seems like a prime candidate for a private static nested class inside stack, no need for it to be public. I see what you're going for with MinStack but I'd argue that the node knowing the minimum is confusing concerns. What is minimum in the context of a node? It doesn't even know it's part of a bigger data structure. If you're going to have that you can just keep track of min on the stack itself. Not written Java in a while but this is how I'd imagine itd look. Note I use a Comparator rather then using Comparable because a lot of classes don't implement Comparable so why force someone to wrap them to use your cool codez? See https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html as an example for how a Java collection that inherits from an unordered collection offers an ordered subclass.
The problem with this is that every node needs to have its minimum in it. Say we do it where only the stack has the minimum, and from top(head) to bottom we have: "5,2,8,3". So the stack has its minimum as 2, let's say. We pop 5, it's still 2. We pop 2, what is it now? Does it search through the entire stack for it every time we pop off the min element? What if we have a 10,000 element stack with ints from 1 to 100? This is why I put the min on every node. And for this reason I believe that I do need a min node class. I'll look into a Comparator. Thanks ![]() | ||
solidbebe
Netherlands4921 Posts
| ||
WarSame
Canada1950 Posts
| ||
WarSame
Canada1950 Posts
| ||
| ||