![]() |
| [[ Home | Forums | 3D Engines Database | Wiki | Articles/Tutorials | Game Dev Jobs | IRC Chat Network | Contact Us ]] |
|
|
#1 |
|
Senior Member
Join Date: Sep 2005
Location: Jönköping, Sweden
Posts: 546
|
I spent the better part of the last 2 days implementing my own int class, Int. Its slow, poorly written and I would'nt ask my worst enimy to use it in production code.
BUT! It handles "infinitely" large numbers (the bits are stored in a vector<bool>) and it supports all the important operators defined for int. It can also be converted to a string for display.At least now I have proof that I can do it, and I learned a lot, so I guess it was not totally useless. EDIT: I'm putting the code up here Code:
Last edited by Reedbeta : 01-12-2006 at 03:14 PM. Reason: added the code :) |
|
|
|
|
|
#2 |
|
DevMaster Staff
Join Date: Sep 2005
Location: The Netherlands
Posts: 1,442
|
What good is this topic if you don't post the code?
![]()
___________________________________________
C++ addict - Currently working on: the 3D engine for Tomb Raider: Underworld and Deus Ex 3. |
|
|
|
|
|
#3 |
|
DevMaster Staff
Join Date: Sep 2003
Location: Hell
Posts: 1,109
|
*directs geon to the code gem submission page*
|
|
|
|
|
|
#4 |
|
DevMaster Staff
Join Date: Jan 2003
Location: Mars
Posts: 1,141
|
I'm guessing if you're using vector<bool> that each bool is treated like a single bit. Would it not be better to use a vector<unsigned char>, and treat each one as a byte (which it almost always is). That way you won't waste bits, as I very much doubt a bool is 1 bit in size
.You could even do something clever with a vector<int> and sizeof() so that it works on all platforms, and reduces the number of elements in the vector.
___________________________________________
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. |
|
|
|
|
|
#5 |
|
Senior Member
Join Date: Oct 2005
Location: Pensacola, FL
Posts: 1,028
|
Correct me if I'm wrong, but I thought vector<bool> was meant as an example of template specialization, and it uses 1 bit per bool value. Of course, this breaks a lot of things that people take for granted with regular vectors. For example, assuming that dereferencing the first index of the vector points to a contiguous array in memory of the value type.
|
|
|
|
|
|
#6 |
|
DevMaster Staff
Join Date: Sep 2005
Location: The Netherlands
Posts: 1,442
|
monjardin: you are correct
. std::vector<bool> is specialized to be stored as actual bits. When referencing a single element in the vector, you won't get a bool& but instead a type that typically holds a reference to the int where that bit is stored, and a bit index in that int. Retrieving or assigning a bool value to this type will read/write the actual bit in the array.
___________________________________________
C++ addict - Currently working on: the 3D engine for Tomb Raider: Underworld and Deus Ex 3. |
|
|
|
|
|
#7 |
|
Valued Member
Join Date: Sep 2005
Location: Germany
Posts: 119
|
I would prefer std::bitset for such things, but if it works...
AFAIK the standard committee will likely remove that std::vector<bool> template specialisation because it has too many drawbacks. |
|
|
|
|
|
#8 |
|
Valued Member
Join Date: Aug 2005
Posts: 189
|
IIRC bitset has a fixed compile time size (by a template argument). So that's not very usefull if you want to expand the "wordsize" of your int to hold arbitrary large values.
Though it might be interesting to have a std::vector<int> anyway. That way you can process 32 bits (or whatever) at once. of course you'll probably need a bit of assembly to get the carries right =) Bramz
___________________________________________
hi, i'm a signature viruz, plz set me as your signature and help me spread :) Bramz' warehouse | LiAR isn't a raytracer |
|
|
|
|
|
#9 | |
|
DevMaster Staff
Join Date: Sep 2005
Location: The Netherlands
Posts: 1,442
|
Quote:
Nah Code:
Of course, an 'add' followed by a series of 'adc' instructions is much more performant ![]()
___________________________________________
C++ addict - Currently working on: the 3D engine for Tomb Raider: Underworld and Deus Ex 3. |
|
|
|
|
|
|
#10 | |
|
Senior Member
Join Date: Sep 2005
Location: Jönköping, Sweden
Posts: 546
|
Quote:
I thought about that too, and it will possibly be the next step. However, the greatest bottleneck is my division algorithm. I simply have no idea about how to implement division effectively with "chunks" of bits (like an int). If anyone has ideas or links on that, feel free to share them. I'm all ears... |
|
|
|
|
|
|
#11 |
|
Senior Member
Join Date: Sep 2005
Location: Jönköping, Sweden
Posts: 546
|
Added as a Code Gem.
Could some moderator join the threads, please? |
|
|
|
|
|
#12 |
|
DevMaster Staff
Join Date: Oct 2004
Location: Seattle, WA
Posts: 3,707
|
We can join the threads as soon as the new code gem becomes visible (it updates at 10AM EST, so soon).
___________________________________________
Currently working at Sucker Punch reedbeta.com - OpenGL demos and other projects Luabridge - a lightweight, dependency-free C++/Lua binding library. CD Lite - an unobtrusive, minimal CD player application for Windows. |
|
|
|
|
|
#13 |
|
DevMaster Staff
Join Date: Jan 2003
Location: Mars
Posts: 1,141
|
I stand corrected, I didn't know that about vectors. Yet another thing the SGI documentation is lacking I guess.
___________________________________________
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 |
|
DevMaster Staff
Join Date: Sep 2005
Location: The Netherlands
Posts: 1,442
|
http://www.dinkumware.com/refxcpp.html (click that image with footprints)
http://www.kuzbass.ru/docs/isocpp/ ![]()
___________________________________________
C++ addict - Currently working on: the 3D engine for Tomb Raider: Underworld and Deus Ex 3. |
|
|
|
|
|
#15 |
|
Senior Member
Join Date: Sep 2005
Location: Jönköping, Sweden
Posts: 546
|
I wrote some stupid code to handle "infinitely" large ints. Just felt I had to do it...
Anyway, here it is. I make no claims about it's usefullness, but it works and is pretty readable, so someone might find it "educational". Code:
|
|
|
|
|
|
#16 | |
|
Valued Member
Join Date: Aug 2005
Location: Seoul
Posts: 272
|
Quote:
That was a really crazy thing for them to include it in the std in the first place ![]() |
|
|
|
|
|
|
#17 |
|
Senior Member
Join Date: Oct 2005
Location: Pensacola, FL
Posts: 1,028
|
FYI, you can use the std::max and std::min template functions from the STL instead of defining your own macros.
|
|
|
|
|
|
#18 |
|
Senior Member
Join Date: Oct 2005
Location: Waterville, MN
Posts: 424
|
Great! Now C++ can do this without Python's help. I wonder, though, if there are routines for this in the Python24.dll .
|
|
|
|
|
|
#19 |
|
Valued Member
Join Date: Aug 2005
Location: Seoul
Posts: 272
|
Could you edit the code and remove the MIN and MAX macros as they promote really bad coding habits - almost as bad as defining "min" and "max" as what windows headers do. The std::min and std::max functions should be enough as Monjardin noted. If you really want to do the min and max operations yourself, then why not making them as templated functions instead of macros. I'd also use explicit std:: prefix when needed and not do "using namespace std;"
![]() edit: Ok.. now when I started this nitpicking. this code also gives some warnings due to conversion between types "for(int i=0; i<Bits.size(); i++)" could be replaced by "for (size_t i=0; i<Bits.size(); ++i). ![]() However, thanks for posting this and keeping the code snapshot alive ![]() Last edited by juhnu : 01-13-2006 at 06:40 AM. |
|
|
|
|
|
#20 |
|
Valued Member
Join Date: Aug 2005
Posts: 189
|
Your code looks interesting =) But I have a few suggestions that might improve its quality:
- as mojardin and juhnu already said: use std::min and std::max - as juhn already said: don't do using namespace std in header files (i assume this code goes in a header file), because otherwise, everyone who includes your header will have using namespace std, and that might lead to unpleasant effects! Unless ... maybe ... if you do it inside your own private namespace. - No need for a custom copy constructor, the default one will do just fine (as std::vector<bool> will be copied correctly). A rule of thumb: if you need either a custom copy constructor, assignment operator or destructor, you probably need all three of them. Since you don't have a custom destructor nor assignment operator (which is correct because you don't need them), you probably won't need a copy constructor either (which in this case is indeed true). - std::string has a method 'append' which allows to add one character to the end of the string. So there's not really a need to go around with a std::vector<char>. - avoid C-style casts, they're ugly and evil. Instead, use static_cast, const_cast and reinterpret_cast to express your intent more clearly. - and of course, working on single bits is horribly slow, but that one you already know ![]() BTW, have you checked out the GMP library? It has something that might seem very familiar to you http://swox.com/gmp/Bramz
___________________________________________
hi, i'm a signature viruz, plz set me as your signature and help me spread :) Bramz' warehouse | LiAR isn't a raytracer |
|
|
|
|
|
#21 | |
|
Member
Join Date: Sep 2005
Location: Somewhere in Poland
Posts: 87
|
Quote:
err , are u sure? in libraries im using there is no special case for std::vector<bool>. from that what u're saying it looks like single element of std::vector<bool> would consist of two values: 1) reference to where int is stored and 2) bit position in that int. im sure it isnt done that way, bcos its better to store just actual bool value (1 value) instead two. it is case of compiler how it stores bool in memory, i.e. old MS Visual-C used int as typedef for bool, while in Visual-C since v6, single byte is used to store boolean value (just use sizeof(bool) to find out how its stored). of course, difference in size of bool value would cause serious compatibility problems, especially with dynamic libraries, and it causes. thats why i prefer to use some more reliable ways to store true-false states. furthermore, if one thinks he can conserve some memory by using bool's, think again. |
|
|
|
|
|
|
#22 | ||||
|
DevMaster Staff
Join Date: Sep 2005
Location: The Netherlands
Posts: 1,442
|
Quote:
![]() http://www.kuzbass.ru/docs/isocpp/li...ib.vector.bool Quote:
Quote:
Quote:
___________________________________________
C++ addict - Currently working on: the 3D engine for Tomb Raider: Underworld and Deus Ex 3. |
||||
|
|
|
|
|
#23 |
|
Member
Join Date: Sep 2005
Location: Somewhere in Poland
Posts: 87
|
hmm, so there really are libraries that has special case for vector<bool>.
i mean, as u're saying, its standard now. but, i dont think that this is advantageous over libs that doesnt has this. bits are most elementary data structures thus creating special functions to access them is nothing good, and i dont like when additional code is packed into my executables. |
|
|
|
|
|
#24 | |
|
Valued Member
Join Date: Aug 2005
Location: Seoul
Posts: 272
|
Quote:
![]() |
|
|
|
|
|
|
#25 | ||
|
DevMaster Staff
Join Date: Sep 2005
Location: The Netherlands
Posts: 1,442
|
Quote:
Well, it's better in terms of memory usage. A non-specialized vector<bool> uses CHAR_BIT (usually 8) times more memory than the specialized vector<bool>. But more importantly, the specialized vector<bool> has a flip method to flip all the bits, and the vector<bool>::reference also has a flip to flip a single bit. So this is correct code: Code:
Obviously, this won't compile when you're using a library that doesn't contain a specialized vector<bool> ![]() Quote:
___________________________________________
C++ addict - Currently working on: the 3D engine for Tomb Raider: Underworld and Deus Ex 3. |
||
|
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|