![]() |
| [[ Home | Forums | 3D Engines Database | Wiki | Articles/Tutorials | Game Dev Jobs | IRC Chat Network | Contact Us ]] |
|
|
#1 |
|
Senior Member
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
|
Code:
a demo usage: Code:
shows that objects with increasing x-position are sitting near eachother in the array... but i still prefer Code:
![]() anyways. i just tested and it just worked so i just shared ![]()
___________________________________________
davepermen.net -Loving a Person is having the wish to see this Person happy, no matter what that means to yourself. -No matter what it means to myself.... |
|
|
|
|
|
#2 |
|
Member
Join Date: Jul 2003
Posts: 97
|
just beware of memory leaks...
alloc should call dealloc else you could end up with Code:
and why not make operator[] just return T* the proxy class doesn't add anything beside extra work for the compiler and obscures the usage pattern by actually switching the alignment from the C standard array[y][x] to array[x][y] effectivly giving us FORTRAN style arrays (column major instead of row major) |
|
|
|
|
|
#3 |
|
Senior Member
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
|
that was the plan (as i find c-array order stupid..)
thanks about the memory leak, hehe..
___________________________________________
davepermen.net -Loving a Person is having the wish to see this Person happy, no matter what that means to yourself. -No matter what it means to myself.... |
|
|
|
|
|
#4 | |
|
Member
Join Date: Jul 2003
Posts: 97
|
Quote:
that way you can be explicit about what ordering you really expect and you also lessen the confusion for new users as a rule of thumb follow the path of least surprise |
|
|
|
|
|
|
#5 |
|
Senior Member
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
|
uhm.. nobody polutes the cache in an optimizing system.. if you access by first incrementing x, then incrementing y, you get perfect cachelines..
___________________________________________
davepermen.net -Loving a Person is having the wish to see this Person happy, no matter what that means to yourself. -No matter what it means to myself.... |
|
|
|
|
|
#6 |
|
Senior Member
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
|
(the thing with the optimizing system i wanted to say is, a good optimizer should remove all overhead.. i'll see if he really does
but not today..)
___________________________________________
davepermen.net -Loving a Person is having the wish to see this Person happy, no matter what that means to yourself. -No matter what it means to myself.... |
|
|
|
|
|
#7 |
|
Senior Member
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
|
500
![]()
___________________________________________
davepermen.net -Loving a Person is having the wish to see this Person happy, no matter what that means to yourself. -No matter what it means to myself.... |
|
|
|
|
|
#8 | |
|
Member
Join Date: Jul 2003
Posts: 97
|
Quote:
And in a large code base where mixing the two could be common you would quickly loose track on when to first process rows and then columns and when the otherway was the natrual way. What would happen? Your blazing fast code would come to a halt, you would blame OOP and C++, altering the order of access effectivly creates a syntactic disparity that can only be seen by knowing the type of the array (plain C-style or wrapper) not by reading the code. Overloading operator() at least gives a clear distinction that it's not a POD type and that if Im not sure of type and behavoir I should probably look it up before touching it. |
|
|
|
|
|
|
#9 | |
|
Member
Join Date: Jul 2003
Posts: 97
|
Quote:
![]() |
|
|
|
|
|
|
#10 |
|
Member
Join Date: Jul 2003
Posts: 97
|
I would also suggest moving Line inside the Array2D class to preserve the global namespace possibly making it private even though having it publicly availble does also offer some nice opportunities.
Also you should have a look at the copy ctor using memcpy is only safe for POD types you should consider making an explicit loop assigning every elemtn instead |
|
|
|
|
|
#11 |
|
Member
Join Date: Jul 2003
Posts: 97
|
or better yet using std::copy like so
Code:
|
|
|
|
|
|
#12 |
|
Member
Join Date: Jul 2003
Posts: 97
|
You could also manually strength reduce Line by saving a T* and width instead of the array pointer and x, doing that would allow you to have
T& operator[](int y){ return ptr[ w * y];} instead of T& operator[](int y){array->access(x, y);} now this seems quite silly and as if it would buy us nothing but actually we're saving one memory access (fetching the data pointer from where ever the current array is) and todays systems are memory limited so that's really a thing to keep in mind. The drawback with doing this is that it gets risky to save Lines for prelonged periods of time, resizing the array will invalidate all Lines pointing into it, the current approach don't have that problem. But anyone that have ever dealt with STL will have no problems coping with iterators getting invalidated after resizing. |
|
|
|
|
|
#13 |
|
DevMaster Staff
Join Date: Jan 2003
Location: Mars
Posts: 1,141
|
you know, it's really a lot nicer-looking if you post everything in one post. Some might even say you're trying to get your post-count up *whistles*
.
___________________________________________
baldurk He who knows not and knows that he knows not is ignorant. Teach him. He who knows not and knows not that he knows not is a fool. Shun him. |
|
|
|
|
|
#14 | |
|
Member
Join Date: Jul 2003
Posts: 97
|
Quote:
But sure I can start keeping them togheter as one in the future... |
|
|
|
|
|
|
#15 |
|
DevMaster Staff
Join Date: Jan 2003
Location: Mars
Posts: 1,141
|
that's what paragraphs are for.
Sorry, it just annoys me. If it doesn't annoy anyone else, just ignore me.
___________________________________________
baldurk He who knows not and knows that he knows not is ignorant. Teach him. He who knows not and knows not that he knows not is a fool. Shun him. |
|
|
|
|
|
#16 | |
|
Member
Join Date: Jul 2003
Posts: 97
|
Quote:
So do you have any oppinions about the actual thread? |
|
|
|
|
|
|
#17 |
|
Senior Member
Join Date: Jan 2003
Location: East Coast, USA
Posts: 370
|
baldurk: sorry ol' chum
but paragraphs get annoying fast. by having it in separate replies, the reader can digest the little amounts of specific points given. although I have to admit... 5+ replies by the same person does seem like a shameless plug for posts ![]() ...kidding. wel... ![]()
___________________________________________
Imagine. |
|
|
|
|
|
#18 |
|
Senior Member
Join Date: Jan 2003
Location: East Coast, USA
Posts: 370
|
davepermen: got some samples of mapping [terminology?]
ex: Code:
thanks ![]()
___________________________________________
Imagine. |
|
|
|
|
|
#19 |
|
Member
Join Date: Jul 2003
Posts: 97
|
Im guessing that you're meaning associative containers right?
like std::map and hash maps? do you wan't examples of using them or making them? |
|
|
|
|
|
#20 |
|
Senior Member
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
|
hy, dave is back..
drunkencoder, uhm.. memory access cache polution is STILL not a problem. as you HAVE to use my arrays the "other way around" to get the same behaviour as the original c array anyways. there is no way around it. never said i like my code.. its merely there to be useful for newbies to see the power and the poverty of c++.. you can do everything you want, if you know how, and what. but 2d arrays in c++ are still done best by doing them 1d.. its just so simple ![]() and on a good compiler this code btw really runs optimal.. everything gets inline, and unneeded parameter pushpops get thrown out.. so its just a straight memory access..yep, line could be a subclass of array2d.. i should use std::copy, yes..
___________________________________________
davepermen.net -Loving a Person is having the wish to see this Person happy, no matter what that means to yourself. -No matter what it means to myself.... |
|
|
|
|
|
#21 | |||
|
Member
Join Date: Jul 2003
Posts: 97
|
Quote:
I never said it was a problem if you used it the way you intend I just said that it's contra intuitive and will in most cases thrash the cache for anyone trying to use it instead of a normal 2D array. Changing access order of arrays is quite much like making a numberclass where + means - and - means + sure the class author can make it compute the right things but anyone not familar with the innerworkings of it will get into trouble with seemingly faulty results. It's just one of those design guidelines don't surprise people with stuff like this if it's going to act as a drop in replacement. Quote:
I never said you said, I just pointed out some obvious flaws and something very close to abusive design. Im not attacking you or your ability to code Im merly pointing out issues with the code you posted. And about the 2D arrays are best done using 1D, yes, but wrapping them makes them easier and less error prone to use without costing you any performance. Also it makes it easier to have convinence functions to use STL algorithms, and I really hope you don't dynamicly allocate 1D arrays instead of using std::vector. Quote:
NO! It should be a member class of array2d that's probably what you ment but a subclass is a wholy diffrenty entity. |
|||
|
|
|
|
|
#22 |
|
Senior Member
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
|
memberclass, subclass, bah.. yep, its what i ment..
btw, i NEVER ever used yet a c-style 2d or 3d array! NEVER. thats why i designed it that way. its the way that sounds logically for me ![]() actually, when ever i use a dynamic sized array, but not dynamic resizable, i use it directly, no vector.. thats the lowlevel part of my heart beating ![]()
___________________________________________
davepermen.net -Loving a Person is having the wish to see this Person happy, no matter what that means to yourself. -No matter what it means to myself.... |
|
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|