|
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. |
again, thanks for all the help, I was able properly finish my program. I do however have some questions regarding lists/linked lists. If i understand lists correctly, you take an object/structure/template that stores some stuff. You then organize them by having each object/structure/template having a pointer that leads you to the next element of the list(next). If the object/structure/template also has an pointer that leads you to the previous element(head), it is a linked list. In contrast to other datastructures/containers you cant access random elements of the list, without following the chain untill you find the one you want. If you insert/delete an object/structure/template from the list, you have to make sure that head/next get changed accordingly, otherwise the chain would be broken and parts of the list inaccessible. They work faster, a s long as you dont have to access random elements.
In contrast to that, stuff like <vector> (simplified spoken) takes care of the head/next part for you so you dont have to worry about it and grants methods to access random elements in a faster fashion.
So it seems, that lists/linked lists are the predecessor/more specialized version of stuff like vector. If I had a big database of adresses, to which I regularly send some newsletters, I would have organized them in a list/linked list since I usually just have to go through the whole thing/maybe have pointer to where the names with A/B/C etc. start. If I have an database like bankaccoutn data, where sometimes certain parts get requested, without asking for the ata of just everyone in the database, I would use something else then linked lists. So both ways of storing stuff are equally viable in their own scenario, even though we now have incredible computingpower at our disposal?
And on the behalf of my former problem, linked list wouldnt be the thing I want to use, since I want to randomly access parts of the database.
|
On May 01 2015 02:18 Artesimo wrote: again, thanks for all the help, I was able properly finish my program. I do however have some questions regarding lists/linked lists. If i understand lists correctly, you take an object/structure/template that stores some stuff. You then organize them by having each object/structure/template having a pointer that leads you to the next element of the list(next). If the object/structure/template also has an pointer that leads you to the previous element(head), it is a linked list. In contrast to other datastructures/containers you cant access random elements of the list, without following the chain untill you find the one you want. If you insert/delete an object/structure/template from the list, you have to make sure that head/next get changed accordingly, otherwise the chain would be broken and parts of the list inaccessible. They work faster, a s long as you dont have to access random elements.
In contrast to that, stuff like <vector> (simplified spoken) takes care of the head/next part for you so you dont have to worry about it and grants methods to access random elements in a faster fashion.
So it seems, that lists/linked lists are the predecessor/more specialized version of stuff like vector. If I had a big database of adresses, to which I regularly send some newsletters, I would have organized them in a list/linked list since I usually just have to go through the whole thing/maybe have pointer to where the names with A/B/C etc. start. If I have an database like bankaccoutn data, where sometimes certain parts get requested, without asking for the ata of just everyone in the database, I would use something else then linked lists. So both ways of storing stuff are equally viable in their own scenario, even though we now have incredible computingpower at our disposal?
And on the behalf of my former problem, linked list wouldnt be the thing I want to use, since I want to randomly access parts of the database. I don't know anything about vector, but everything else you've said is spot on.
|
On April 30 2015 23:26 netherh wrote: I'm not sure there's any "good" way to read in a file format like that. Can you persuade your friend to use xml or json or lua... some actual data description language? (It'll be easier for him in the long run too).
XML is crap. It has a ton of overhead for even the simplest of data structures. JSON is fine but kind of meh. If you want readability you should go with YAML, which works very well with most languages (even Haskell if you're that hardcore). You could also go a step further and create a simple SQLite database to store the data.
The big plus of YAML is that its first and foremost goal is to be human readable. It also supports more complex data structures than JSON (while being easier to read).
|
@Artesimo:
For the list, while you can only do things at the position you currently look at, at that spot you can insert and remove elements without having to change all of the list. You only need to change the pointers that are in the surrounding elements. It's just the one pointer in the previous element for a singly-linked list, or also a second one in the following element for a doubly-linked list. It's also straight forward to think about how you'd go about inserting a second list into the spot where you are in your first list.
With the vector, if you remove or insert, you will have to copy the rest of the list to move it around. If the block where your vector is positioned in memory can't resize because something is following it, you will have to get a new block of memory and copy the whole vector.
Now, for some reason, the vector data structure actually beats the list on a real machine. I don't know if that's just most of the time or always. It has to do with things like caches in the CPU and latency of that vs. main memory.
When you use those data structures to deal with some sort of objects, what's saved in them is really just pointers to those objects. The vector will then just be an array of those pointers to the objects. A list will instead be multiple times the size of that array.
When you approach something like it's Math, try to reason about your problem so that you'll have proof that your solution is correct, a list is nice if the problem can be split into rules for (a) empty list, (b) single element, (c) rest of the list. You can then afterwards still hide the details of what the machine is actually doing in C++, make it so using a vector looks exactly like using a list. You can use those "iterators". So you can still use lists if it helps while thinking about your problem, but then the machine actually does something else.
|
On May 01 2015 02:18 Artesimo wrote: again, thanks for all the help, I was able properly finish my program. I do however have some questions regarding lists/linked lists. If i understand lists correctly, you take an object/structure/template that stores some stuff. You then organize them by having each object/structure/template having a pointer that leads you to the next element of the list(next). If the object/structure/template also has an pointer that leads you to the previous element(head), it is a linked list. In contrast to other datastructures/containers you cant access random elements of the list, without following the chain untill you find the one you want. If you insert/delete an object/structure/template from the list, you have to make sure that head/next get changed accordingly, otherwise the chain would be broken and parts of the list inaccessible. They work faster, a s long as you dont have to access random elements.
In contrast to that, stuff like <vector> (simplified spoken) takes care of the head/next part for you so you dont have to worry about it and grants methods to access random elements in a faster fashion.
So it seems, that lists/linked lists are the predecessor/more specialized version of stuff like vector. If I had a big database of adresses, to which I regularly send some newsletters, I would have organized them in a list/linked list since I usually just have to go through the whole thing/maybe have pointer to where the names with A/B/C etc. start. If I have an database like bankaccoutn data, where sometimes certain parts get requested, without asking for the ata of just everyone in the database, I would use something else then linked lists. So both ways of storing stuff are equally viable in their own scenario, even though we now have incredible computingpower at our disposal?
And on the behalf of my former problem, linked list wouldnt be the thing I want to use, since I want to randomly access parts of the database.
Linked list doesn't have to point to the previous element. If so, it is singly linked list. Otherwise, doubly linked list.
If vector is like ArrayList in Java (and I think it's similar), then it uses array under the cover, so its implementation is completely different from list. Because it uses arrays, it can access them by index efficiently. That's why you have random access advantage with vectors. If you ask how a vector/ArrayList holds 'unlimited' data even though arrays are fixed, it is because they create a new array, its size is twice larger or something and they copy all previous elements if it needs more space.
I think databases don't use either. What they usually use is binary tree or tree of some sort as far as I remember.
|
On May 01 2015 03:25 darkness wrote:
Linked list doesn't have to point to the previous element. If so, it is singly linked list. Otherwise, doubly linked list.
If vector is like ArrayList in Java (and I think it's similar), then it uses array under the cover, so its implementation is completely different from list. Because it uses arrays, it can access them by index efficiently. That's why you have random access advantage with vectors.
I think databases don't use either. What they usually use is binary tree or tree of some sort as far as I remember.
From what I read, binary trees mainly get implemented using linked lists. Could be wrong though.
@Ropid: thanks for the explanation 
|
On May 01 2015 03:28 Artesimo wrote:Show nested quote +On May 01 2015 03:25 darkness wrote:
Linked list doesn't have to point to the previous element. If so, it is singly linked list. Otherwise, doubly linked list.
If vector is like ArrayList in Java (and I think it's similar), then it uses array under the cover, so its implementation is completely different from list. Because it uses arrays, it can access them by index efficiently. That's why you have random access advantage with vectors.
I think databases don't use either. What they usually use is binary tree or tree of some sort as far as I remember. From what I read, binary trees mainly get implemented using linked lists. Could be wrong though. @Ropid: thanks for the explanation 
A binary tree is essentially a linked list that has two next pointers instead of only one so the comparison is pretty natural. It is possible to implement a binary tree as an array though.
|
On April 30 2015 23:33 Artesimo wrote:Show nested quote +On April 30 2015 23:26 netherh wrote: I'm not sure there's any "good" way to read in a file format like that. Can you persuade your friend to use xml or json or lua... some actual data description language? (It'll be easier for him in the long run too).
Unfortunately he only codes in java since its the only thing he knows. He also does it as a hobby since his work isnt related to IT in any form (even though he still is better than me at coding... for now  ).
Have your friend consider protocol buffers. The java support for it is quite good (though it is an odd to get used to at first).
|
On May 01 2015 09:43 phar wrote:Have your friend consider protocol buffers. The java support for it is quite good (though it is an odd to get used to at first).
I will redirect this to him. I dont think he will really read it though, he just likes to fondle around with his java-skills without really expanding them as far as I can tell.
|
Anybody has some experiance with c# unit testing?
Im trying to test if if a function returns an object of type Grass (which is a subclass of type Tile), I used AreEqual and AreEqual<T> but keep getting errors like this
Result Message: Assert.AreEqual failed. Expected:<ConsiGame.Grass>. Actual:<ConsiGame.Grass>.
|
On May 02 2015 05:45 sabas123 wrote:Anybody has some experiance with c# unit testing? Im trying to test if if a function returns an object of type Grass (which is a subclass of type Tile), I used AreEqual and AreEqual<T> but keep getting errors like this Result Message: Assert.AreEqual failed. Expected:<ConsiGame.Grass>. Actual:<ConsiGame.Grass>.
Are you checking for equality or equality of types? I don't know your exact code, but I would guess you're checking for the first (so, what you see printed is probably the default toString value, which you didn't override if I'm correct), while you want the second. You should do this: Assert.AreEqual(obj.GetType(), typeof(MyObject)); Is that what you do? Note that this passes if obj's exact type is MyObject, it fails if it is from a derived class I believe. Alternatively you could use Assert.IsInstanceOfType(obj, typeof(MyObject)); , which does pass if obj is of a class derived from MyObject. I would probably use that in your case, so if you have subclasses of Grass, like GreenerGrass, it still passes your test
|
On May 01 2015 20:29 Artesimo wrote:Show nested quote +On May 01 2015 09:43 phar wrote:Have your friend consider protocol buffers. The java support for it is quite good (though it is an odd to get used to at first). I will redirect this to him. I dont think he will really read it though, he just likes to fondle around with his java-skills without really expanding them as far as I can tell. Ah ha well, then props to you for trying to help, but maybe abandon all hope
|
On May 01 2015 03:31 Blitzkrieg0 wrote:Show nested quote +On May 01 2015 03:28 Artesimo wrote:On May 01 2015 03:25 darkness wrote:
Linked list doesn't have to point to the previous element. If so, it is singly linked list. Otherwise, doubly linked list.
If vector is like ArrayList in Java (and I think it's similar), then it uses array under the cover, so its implementation is completely different from list. Because it uses arrays, it can access them by index efficiently. That's why you have random access advantage with vectors.
I think databases don't use either. What they usually use is binary tree or tree of some sort as far as I remember. From what I read, binary trees mainly get implemented using linked lists. Could be wrong though. @Ropid: thanks for the explanation  A binary tree is essentially a linked list that has two next pointers instead of only one so the comparison is pretty natural. It is possible to implement a binary tree as an array though.
If you have a fairly balanced binary tree, it is really common to implement it as an array. Root node is at index 1 (or 0, but that makes the math a bit less nice). A node with index n's children are at indexes n*2 and n*2+1, and it's parent is at floor(n/2).
For example, the node at index 7 has children in positions 14 and 15, and parent at position 3.
Since n*2 and n/2 are just one bit shift operation, this is a rather fast way of implementing binary trees.
In addition, the binary representation of a number is directly correlated to it's position in the binary tree. (the series of 1's and 0's is the path to get to that node.)
|
On May 02 2015 05:56 ZenithM wrote:Show nested quote +On May 02 2015 05:45 sabas123 wrote:Anybody has some experiance with c# unit testing? Im trying to test if if a function returns an object of type Grass (which is a subclass of type Tile), I used AreEqual and AreEqual<T> but keep getting errors like this Result Message: Assert.AreEqual failed. Expected:<ConsiGame.Grass>. Actual:<ConsiGame.Grass>.
Are you checking for equality or equality of types? I don't know your exact code, but I would guess you're checking for the first (so, what you see printed is probably the default toString value, which you didn't override if I'm correct), while you want the second. You should do this: Is that what you do? Note that this passes if obj's exact type is MyObject, it fails if it is from a derived class I believe. Alternatively you could use , which does pass if obj is of a class derived from MyObject. I would probably use that in your case, so if you have subclasses of Grass, like GreenerGrass, it still passes your test  Yes, that was exactly I was looking for.
thank you<3
|
I have just started looking Javascript and I'm wondering if there's an OS agnostic constant for linebreaks, like phps PHP_EOL.
At the moment I'm doing something along these lines:
str.split("\r\n"); str.split("\n"); However I'm wondering if theres a way to do it in a single pass.
|
On May 03 2015 00:00 Propelled wrote:I have just started looking Javascript and I'm wondering if there's an OS agnostic constant for linebreaks, like phps PHP_EOL. At the moment I'm doing something along these lines: str.split("\r\n"); str.split("\n"); However I'm wondering if theres a way to do it in a single pass. Im not sure if the break line tag works entirely the same as EOL, but they both make a new line.
str.split("<br>");
|
On May 03 2015 02:55 sabas123 wrote:Show nested quote +On May 03 2015 00:00 Propelled wrote:I have just started looking Javascript and I'm wondering if there's an OS agnostic constant for linebreaks, like phps PHP_EOL. At the moment I'm doing something along these lines: str.split("\r\n"); str.split("\n"); However I'm wondering if theres a way to do it in a single pass. Im not sure if the break line tag works entirely the same as EOL, but they both make a new line. str.split("<br>");
The html <br /> code is not the same thing at all. I don´t think there is an OS agnostic version (there's probably a library to do that, though, depending on your framework). Of course, if you just split on "\n" you will get all the ones with a carriage return in there as well, just remember to parse them out when you use your string.
|
Not sure if this is what you mean by 'single pass', but I think Javascript's str.split() accepts regex... so it might be doable with one call to str.split().
Then again regex might be introducing needless complexity, so eh D:
|
Well, a regex like /[\n\r]+/ is not too terribly complex. But you are right: It does not feel right, there should be a better solution to this.
"a\n\rb\nc\rd".split(/[ \n\r]+/) => Array [ "a", "b", "c", "d" ] ^ This space is not supposed to be there, but whithout, the backslash was not displayed in this post.
|
Now I don't know much JS, but that shouldn't matter here. A quick google search indicates that you probably should use a regex. There are 2 approaches: Use string.match with a regex that matches non-linebreak characters, or string.split with a regex that matches linebreaks. Don't know how accurate it really is, but this seems to be the best one:
string.split(\r\n|[ \n\v\f\r\x85\u2028\u2029]) See Alan Moore's comment a bit down the page. (same deal as above, remove the space in front of the \n)
This should match \r\n, \r, \n and a number of other obscure newline variations. Shove it into a dedicated function, put a comment on it and never think about it again.
|
|
|
|