d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > When Are Multi Dimensional Arrays > Really Used
Prev123Next
Add Reply New Topic New Poll
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Jan 31 2014 06:25pm
Quote (KrzaQ2 @ Jan 30 2014 03:51am)
Lists are very cache-unfriendly and should be avoided unless you have reason (benchmarks) to use them. Your go-to container should be vector, it's implemented as a contiguous array of elements of your type and has a very nice and flexible interface on top of it.


This assumes your implementation of a linked-list uses pointers and structs and isn't just backed by a contiguous array ;) But that would be me nit-picking, I agree with you on the face of it.
Member
Posts: 101
Joined: Jan 19 2014
Gold: 255.00
Jan 31 2014 09:26pm
They are used in servers, or at least could be used in servers. They are good for multiple instances.

For example, in my server I have a similar game lobby system to Diablo II's. On the server side, there is a max of 100 lobbies

so for the total items on the ground for each server for example (let's say you had a 200 item max)
you could have something like

ITEM [MAXSERVERLOBBIES][200];

(implied that ITEM is a struct for whatever your info for your item would contain)

This post was edited by Pindrought on Jan 31 2014 09:26pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Jan 31 2014 10:01pm
Off topic, but why would you do it that way? Why not just have an array of Lobbys that contain a list of ground items?

Code
struct Lobby {
Item* groundItems;
};


struct Server {
Lobby* lobbies;
};

Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Feb 1 2014 01:21am
Quote (Minkomonster @ 1 Feb 2014 01:25)
This assumes your implementation of a linked-list uses pointers and structs and isn't just backed by a contiguous array ;) But that would be me nit-picking, I agree with you on the face of it.

It can't be, the standard requires constant-time insertion and deletion.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 1 2014 02:10am
Quote (KrzaQ2 @ Feb 1 2014 02:21am)
It can't be, the standard requires constant-time insertion and deletion.


This confuses me. How would you have constant time insertion on a linked list? In order to insert you need a pointer at the insertion point node. It is worst case O(n) to find this node. How do you achieve a O(1) insertion time with a linked list?
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Feb 1 2014 02:20am
You need an iterator pointing to where you want to insert/remove, of course. If you need to traverse the list to insert/remove elements, you need to rethink your design ;)
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 1 2014 02:36am
Enlighten me.
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Feb 1 2014 03:45am
Wiki entry: http://en.cppreference.com/w/cpp/container/list/erase
The important part if tl;dr:
Quote
Complexity
1) Constant.


What does The Bible say:
Quote (C++11 standard @ §23.3.5.4/5)
Complexity: Erasing a single element is a constant time operation with a single call to the destructor of T.


Usage example: http://ideone.com/YvWGeU

Do you feel enlightened? :)
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 1 2014 04:15am
No I do not feel enlightened. Because you are neglecting the indexing part. Yes, the actual removal of the node is constant, but that requires the iterator to be pointing at the correct node. That indexing is a linear search. The actual time for erasing an arbitrary element of a Linked List is O(n) + O(1).
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Feb 1 2014 05:11am
I believe your original comment was that list could be implemented as contiguous array. I cited the reason why it cannot.

List traversal is not part of the removal, and that's what we're talking about. As I said before, if you need to traverse the list just to remove some of its elements, you need to rethink your design, because list isn't the container you want.
Go Back To Programming & Development Topic List
Prev123Next
Add Reply New Topic New Poll