Coding: Fleeting Thoughts

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
Xanthir
My HERO!!!
Posts: 5410
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Sat Nov 28, 2009 2:47 pm UTC

This rage is valid and righteous.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Sat Nov 28, 2009 2:57 pm UTC

elminster wrote:

Code: Select all

if(x) {
 int y;
Yes...

Code: Select all

if(x)
{
 int y;
Yes... if you enjoy eating quiche.

Code: Select all

if(x)
{ int y;
No, just NO! *rage*


Fixed. The rage, though, is valid. Dunno what you have to be smoking to come up with something like that.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

fazzone
Posts: 186
Joined: Wed Dec 10, 2008 9:38 pm UTC
Location: A boat

Re: Coding: Fleeting Thoughts

Postby fazzone » Sat Nov 28, 2009 3:38 pm UTC

Pico.
*/

Posi
Posts: 111
Joined: Mon Jul 16, 2007 6:08 am UTC

Re: Coding: Fleeting Thoughts

Postby Posi » Sun Nov 29, 2009 7:46 am UTC

The lower the level of the language you are using, the harder it is to tell when you will be done.

User avatar
Toeofdoom
The (Male) Skeleton Guitarist
Posts: 3446
Joined: Wed Jan 10, 2007 10:06 am UTC
Location: Melbourne, Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby Toeofdoom » Sun Nov 29, 2009 9:44 am UTC

"I need a unique string identifier for each object... but I don't want to keep track of what objects I've made in some arbitrary way."
"wait a minute... the pointer to this object is unique as long as this object exists anyway..."

Code: Select all

shapeActor::shapeActor(std::string type, b2Shape *shape): DefaultActor(type), mShape(shape)
{
   //get shape scenenode
   mNode = reinterpret_cast<bodyActor*>(mShape->GetBody()->GetUserData())->getNode()->createChildSceneNode();

   std::stringstream ss;
   ss << "Shape_" << mShape; //Use the shape pointer as a unique identifier, because it needs a unique name with ogre.
      //If 2 shapes end up with the same address somehow, it will die anyway.

   Ogre::SceneManager::PrefabType ptype = Ogre::SceneManager::PT_PLANE;

   
   mEntity = OgreFramework::getSingletonPtr()->m_pSceneMgr->createEntity(ss.str(), ptype);

   mNode->attachObject(mEntity);
}

As far as I can tell, there isn't a downside to this apart from the pointer being 8 characters long (on my system)... but sticking a pointer into a string still seems strange.
Hawknc wrote:Gotta love our political choices here - you can pick the unionised socially conservative party, or the free-market even more socially conservative party. Oh who to vote for…I don't know, I think I'll just flip a coin and hope it explodes and kills me.

Website

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Sun Nov 29, 2009 12:28 pm UTC

Toeofdoom wrote:"I need a unique string identifier for each object... but I don't want to keep track of what objects I've made in some arbitrary way."
"wait a minute... the pointer to this object is unique as long as this object exists anyway..."

Spoiler:

Code: Select all

shapeActor::shapeActor(std::string type, b2Shape *shape): DefaultActor(type), mShape(shape)
{
   //get shape scenenode
   mNode = reinterpret_cast<bodyActor*>(mShape->GetBody()->GetUserData())->getNode()->createChildSceneNode();

   std::stringstream ss;
   ss << "Shape_" << mShape; //Use the shape pointer as a unique identifier, because it needs a unique name with ogre.
      //If 2 shapes end up with the same address somehow, it will die anyway.

   Ogre::SceneManager::PrefabType ptype = Ogre::SceneManager::PT_PLANE;

   
   mEntity = OgreFramework::getSingletonPtr()->m_pSceneMgr->createEntity(ss.str(), ptype);

   mNode->attachObject(mEntity);
}

As far as I can tell, there isn't a downside to this apart from the pointer being 8 characters long (on my system)... but sticking a pointer into a string still seems strange.


You could always base64-encode the pointer, if you want to cut down on the string length a bit. Or base32 if you want something easier to work with.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
Toeofdoom
The (Male) Skeleton Guitarist
Posts: 3446
Joined: Wed Jan 10, 2007 10:06 am UTC
Location: Melbourne, Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby Toeofdoom » Sun Nov 29, 2009 1:29 pm UTC

yeah, could do that, although I'm more concerned with sensible coding at this stage than optimisation. base 32 would cut down to 7 characters and base 64 to 6, not sure it's worth the effort (or would even speed things up with the calculations needed).

Also, I think it's just used as a map key, if they're stored as stl::strings using chars (not wchars) would just converting it to 4 chars mess anything up? With endline values and strange things like that I imagine it could mess up, but as it's just a key it might not matter...
Hawknc wrote:Gotta love our political choices here - you can pick the unionised socially conservative party, or the free-market even more socially conservative party. Oh who to vote for…I don't know, I think I'll just flip a coin and hope it explodes and kills me.

Website

User avatar
phlip
Restorer of Worlds
Posts: 7572
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Sun Nov 29, 2009 2:22 pm UTC

Well, for one, having null characters in there could potentially mess something up along the line... I'm pretty sure std::strings can store strings that contain null chars, but if it ever gets converted to a cstring at some point, then that will break it.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
Toeofdoom
The (Male) Skeleton Guitarist
Posts: 3446
Joined: Wed Jan 10, 2007 10:06 am UTC
Location: Melbourne, Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby Toeofdoom » Sun Nov 29, 2009 2:59 pm UTC

I think ogre3d is pretty heavy on the "OO" everything side, so I doubt they get changed back and from what I can see it doesn't. I'll probably try that way when I can be bothered, see if it works. It runs 500 effectively random numbers through at the start, I imagine that would be a fair test to see if it breaks.
Hawknc wrote:Gotta love our political choices here - you can pick the unionised socially conservative party, or the free-market even more socially conservative party. Oh who to vote for…I don't know, I think I'll just flip a coin and hope it explodes and kills me.

Website

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Sun Nov 29, 2009 5:52 pm UTC

Toeofdoom wrote:Also, I think it's just used as a map key, if they're stored as stl::strings using chars (not wchars) would just converting it to 4 chars mess anything up? With endline values and strange things like that I imagine it could mess up, but as it's just a key it might not matter...


That's kinda dumb though. Why not use the pointers themselves as map keys? The map would undoubtedly hash the keys, but you already got a perfectly unique hash--the integer value of the pointer.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
Cosmologicon
Posts: 1806
Joined: Sat Nov 25, 2006 9:47 am UTC
Location: Cambridge MA USA
Contact:

Re: Coding: Fleeting Thoughts

Postby Cosmologicon » Mon Nov 30, 2009 12:36 am UTC

You, sir, name? wrote:That's kinda dumb though. Why not use the pointers themselves as map keys? The map would undoubtedly hash the keys, but you already got a perfectly unique hash--the integer value of the pointer.

I may be misinterpreting you, but if you use pointers as keys, they definitely need to be hashed. The range of possible pointers is larger than the maximum size of your array, pretty much by definition.

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Coding: Fleeting Thoughts

Postby Berengal » Mon Nov 30, 2009 4:02 am UTC

You seem to be forgetting how a hashmap works.

The job of the hash-algorithm is to reduce a value to integer-size (or whatever uniform size the map requires). This is done by the programmer, providing a hashing algorithm for each different type of value. The algorithm should create as uniformly distributed integer values as possible.

The hashmap then takes that algorithm, applies it to the elements of the map as required to get a hash out, and then it has to dynamically map that hash to a bucket. Dynamic because a good hashmap has a dynamic number of buckets. This algorithm depends on the implementation of the hashmap, and since there's no information lost (from the hashmap's perspective) in applying this algorithm to integer hashes rather than the actual elements of the map, it can be safely factored out of the demands of the user of the hashmap and implemented once in the map implementation.

The default hashCode implementation in Java is simply the memory location of the object (might or might not be a hard pointer, depending on the jvm implementation).
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
Cosmologicon
Posts: 1806
Joined: Sat Nov 25, 2006 9:47 am UTC
Location: Cambridge MA USA
Contact:

Re: Coding: Fleeting Thoughts

Postby Cosmologicon » Mon Nov 30, 2009 5:49 am UTC

Berengal wrote:You seem to be forgetting how a hashmap works.

The job of the hash-algorithm is to reduce a value to integer-size (or whatever uniform size the map requires). This is done by the programmer, providing a hashing algorithm for each different type of value. The algorithm should create as uniformly distributed integer values as possible.

The hashmap then takes that algorithm, applies it to the elements of the map as required to get a hash out, and then it has to dynamically map that hash to a bucket.

Well, I certainly don't know how it works in Java, but I never learned about this two-step process, first mapping an object to a hash then a hash to a bucket. The way I learned it is how Wikipedia says:
A hash function converts a large, possibly variable-sized amount of data into a small datum, usually a single integer that may serve as an index to an array.
Unless you're drawing a distinction between "hash algorithm" and "hash function", the way I understand it, the "hash function" generally refers to both steps, ie, mapping the object to the bucket. A hash function defined by simply taking the address of the object would not fit Wikipedia's definition, because the result can't be used as an array index. That's what I was saying. It sounds like I might be using the terminology a little differently than you. Sorry if I caused any confusion.

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Coding: Fleeting Thoughts

Postby Berengal » Mon Nov 30, 2009 6:58 am UTC

Wikipedia's definition leaves out the intermediate step of mapping hashes to indexes, so it's wrong by virtue of pedantry.

If you think about it, using the hash directly doesn't make much sense. Start at the smallest range possible on modern machines; [0, 255]. This means a hashmap will have to allocate an array of 256 elements right away. Not too bad, but you can't store more than 256 items in it. You could grow it dynamically as needed, but you still can't index further than the 256 first elements (using the hash as the index directly), which means you'll usually have to traverse the array doing a linear search to find the element you're looking for. You've effectively transformed your hashmap into a linear search list, a datastructure you've probably not heard of before because it's utter crap.

Okay, so increase the size of the hash to two bytes. This gives you 65536 indexes into your array. 65536 still isn't alot, and many people would be angry at not being able to put more items in it than that. I know I have. Also, you shouldn't forget the size of your array. An element in the array consists a key and a value. Assuming both are pointers (since we want a generic map) and we're on a 32-bit machine, an element comes out at 8 bytes. An 8-byte array at 65536 elements comes out at 512Kb. Not that much on modern hardware, but it's starting to become a nuicanse.

Anyway, we already figured two bytes weren't enough. Let's add another, giving us hashes of three bytes. This gives us 16777216 elements. Most people should be happy with this, and those who aren't have special needs and can use special libraries. However, the size of a hashmap is now 134217728 bytes. That's ~134Mb. For a single hashmap. Consider 64-bit machines and you can double that.

In conclusion, you need to be able to grow the underlying array dynamically, which means you need to be able to map the static range of the hash onto the dynamic size of the array. This also means you can no longer use the hash directly as an index into an array.

As a side-note, hashmaps aren't just arrays and a hash-function. They're a happy mishmash of arrays, tries, linked lists and other fun structures resulting in a final structure that can't be directly indexed anyhow.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Coding: Fleeting Thoughts

Postby Berengal » Mon Nov 30, 2009 8:10 am UTC

I wish more languages could have syntax like agda's:

Code: Select all

  assoc-* : ∀ a b c → a * (b * c) ≡ a * b * c
  assoc-* zero b c = refl
  assoc-* (succ a) b c
    = proof: succ a * (b * c)
         === b * c + a * (b * c) by refl
         === b * c + a * b * c   by applying (λ x → b * c + x) on indHyp
         === b * c + c * (a * b) by applying (λ x → b * c + x) on comm-* (a * b) c
         === c * b + c * (a * b) by applying (λ x → x + c * (a * b)) on comm-* b c
         === c * (b + a * b)     by eqsym (distr-+-* c b (a * b))
         === c * (succ a * b)    by applying (λ x → c * x) on refl
         === succ a * b * c      by comm-* c (succ a * b)
      ,QED!
   where open NatChain
         indHyp = assoc-* a b c

Here I'm using four bits of syntax defined by me; "proof:_", "_===_by_", ",QED!" and "applying_on_", which are functions where the underscores indicate holes for arguments. You can define a function "if_then_else_", which is then used as "if foo then bar else baz". You can use almost every symbol and letter in any identifier, mixfix operators, whatever you like. "not a or a and not b nand c" can be a valid expression meaning "(not a) or ((a and (not b)) nand c)". I wish every language had syntax this extensible this easily.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
Cosmologicon
Posts: 1806
Joined: Sat Nov 25, 2006 9:47 am UTC
Location: Cambridge MA USA
Contact:

Re: Coding: Fleeting Thoughts

Postby Cosmologicon » Mon Nov 30, 2009 2:54 pm UTC

Berengal wrote:Wikipedia's definition leaves out the intermediate step of mapping hashes to indexes, so it's wrong by virtue of pedantry.

[explanation snipped]

Well, I fully admit that there are several things I didn't know about how hash tables are implemented in practice, but the differences between "your" definition of a hash function and "my" definition are just a matter of semantics. The issues you wrote about (the only available array sizes being 256, 65536, 16777216, etc.) are non-issues.

When you add an object to a hash table, you do some computations on it, and the final step is generally you modulo by the array size (the number of buckets). The only difference between "your" definition and "mine" is whether you consider this last step to be part of the hash function. "My" definition has no restriction on array size any more than "yours" does. The range of a hash function under "my" definition can be anything. There's also no reason you can't grow the array. You just have to rehash all your keys.

The reason I keep putting "my" and "your" in quotes is that I don't want to give the impression that they're equally valid. Having done some reading, I strongly suspect that "my" definition is completely wrong. If it weren't for Wikipedia, I'd be sure of it.

OOPS: Also I meant to add, at first I thought that taking the address of an object would be a really bad hash function because it has too many patterns, for instance if your objects are 64 bytes and your array size is a factor of 64, you're only hitting 1/64 of the buckets. But reading the python implementation, I understand now that's okay if you deal with collisions well.

User avatar
Cosmologicon
Posts: 1806
Joined: Sat Nov 25, 2006 9:47 am UTC
Location: Cambridge MA USA
Contact:

Re: Coding: Fleeting Thoughts

Postby Cosmologicon » Mon Nov 30, 2009 6:48 pm UTC

Writing a class hierarchy for a game in python, I picked up a technique that felt so wrong at first but now feels so right. I started with a base class with a default value for one of the arguments. But this ran into an issue when I had a derived class that (a) overrides the __init__ method without calling the base class's __init__, and (b) doesn't care about the variable in question. The following will crash when think is called on a Plant object.

Code: Select all

class Enemy:
    defaultvel = Vector3(0, 0, 0)
    def __init__(self, pos, vel = None):
        self.pos = pos
        self.vel = vel or self.defaultvel
    def think(self, dt):
        self.pos += dt * self.vel

class Plant (Enemy):   # doesn't move
     def __init__(self, pos):
          self.pos = pos
This is an extremely simplified example, of course. In reality, it was several different values that took defaults, not just vel, and there were good reasons for not calling the base class's __init__. So I came up with a solution I rather liked, changing the beginning of Enemy to this:

Code: Select all

class Enemy:
    vel = Vector3(0, 0, 0)
    def __init__(self, pos, vel = None):
        self.pos = pos
        if vel: self.vel = vel
Now all Plant objects also have a default vel without ever having to specify it. This struck me as weird, coming from a C++ background where a member variable is either static or it's not. But now I like it.

Random832
Posts: 2525
Joined: Wed Oct 10, 2007 4:38 pm UTC

Re: Coding: Fleeting Thoughts

Postby Random832 » Mon Nov 30, 2009 6:53 pm UTC

Cosmologicon wrote:good reasons for not calling the base class's __init__.


I cannot think of one of these that is not also a good reason for not deriving from that class.

User avatar
Xanthir
My HERO!!!
Posts: 5410
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Mon Nov 30, 2009 6:54 pm UTC

Interestingly, the approach you came up with feels like the obvious default way of doing it. It's how I handle things in, frex, PHP. Doing it your old way where you held onto a defaultVel object and used it to initialize just seems… strange.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
Cosmologicon
Posts: 1806
Joined: Sat Nov 25, 2006 9:47 am UTC
Location: Cambridge MA USA
Contact:

Re: Coding: Fleeting Thoughts

Postby Cosmologicon » Mon Nov 30, 2009 7:01 pm UTC

Random832 wrote:
Cosmologicon wrote:good reasons for not calling the base class's __init__.


I cannot think of one of these that is not also a good reason for not deriving from that class.

Mmmm... what about the diamond problem?

User avatar
Xanthir
My HERO!!!
Posts: 5410
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Mon Nov 30, 2009 7:03 pm UTC

What about it? The language can provide a reasonable way of resolving diamond-based inheritance conflicts. Common Lisp does this very well, for example.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
Cosmologicon
Posts: 1806
Joined: Sat Nov 25, 2006 9:47 am UTC
Location: Cambridge MA USA
Contact:

Re: Coding: Fleeting Thoughts

Postby Cosmologicon » Mon Nov 30, 2009 7:24 pm UTC

Yeah? I guess we're having a teachable moment. How should this be written so that A.__init__ is called only once, and every __init__ method calls the __init__ method of its parent class(es)?

Code: Select all

class A:
  def __init__(self):
    print "A"
class B (A):
  pass
class C (A):
  pass
class D (B, C):
  def __init__(self):
    print "D"
    B.__init__(self)
    C.__init__(self)

x = D()
And don't tell me to do it in Lisp. :-P

User avatar
Xanthir
My HERO!!!
Posts: 5410
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Mon Nov 30, 2009 7:54 pm UTC

Unless Python provides the equivalent of CALL-NEXT-METHOD, I'm gonna have to go with "do it in lisp". ^_^

To do it right you need access to the class graph and an ordering of such so that you can just walk up the ordering, hitting each class once, until you reach A.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

troyp
Posts: 557
Joined: Thu May 22, 2008 9:20 pm UTC
Location: Lismore, NSW

Re: Coding: Fleeting Thoughts

Postby troyp » Mon Nov 30, 2009 8:33 pm UTC

Python uses the super() function to handle this. It's in the docs - basically, every time you'd call the parent's __init__() directly, instead you use super(classname, self).__init__() and the super() call will make sure you don't double up. (Here, classname is the name of the class *containing* the super call, ie. self.__class__)

edit: you can also access the base classes of a class, so if for some odd reason, you wanted to resolve things in a way other than the default, you could write a function to construct and traverse the call graph and handle it however you wanted.

User avatar
Xanthir
My HERO!!!
Posts: 5410
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Mon Nov 30, 2009 8:46 pm UTC

Ah, yes. super() does seem to be the python equivalent of Lisp's CALL-NEXT-METHOD. Take a look at this article, though, for some pitfalls you can encounter in using it.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
Cosmologicon
Posts: 1806
Joined: Sat Nov 25, 2006 9:47 am UTC
Location: Cambridge MA USA
Contact:

Re: Coding: Fleeting Thoughts

Postby Cosmologicon » Mon Nov 30, 2009 9:22 pm UTC

Yes, good call, super is the way to go. Anyway, calling the superclass's init or else not inheriting from it is the *right* thing to do, I don't want to say otherwise. But... I don't consider it sacrosanct. In this particular case I was under a contest deadline. Sometimes a class has 90% of the functionality you want but makes use of something here or there that seemed like a good idea at the time but turned out not to be as general as you thought (like an enemy moving). So you have three options: spend hours refactoring, duplicate the code and make a near-identical class with one missing attribute, or do something that's maybe not quite *right*. Who among us has not been driven to desperation in just such a situation? it's not the first time I've bent the rules and it won't be the last.

User avatar
Toeofdoom
The (Male) Skeleton Guitarist
Posts: 3446
Joined: Wed Jan 10, 2007 10:06 am UTC
Location: Melbourne, Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby Toeofdoom » Tue Dec 01, 2009 7:41 am UTC

You, sir, name? wrote:
Toeofdoom wrote:Also, I think it's just used as a map key, if they're stored as stl::strings using chars (not wchars) would just converting it to 4 chars mess anything up? With endline values and strange things like that I imagine it could mess up, but as it's just a key it might not matter...


That's kinda dumb though. Why not use the pointers themselves as map keys? The map would undoubtedly hash the keys, but you already got a perfectly unique hash--the integer value of the pointer.

Well the point here is that the map is not one I wrote, and there may be objects in it other than the things I put in. Ogre3d has a map for strings to all renderable objects and to create a renderable object you need a string to identify it.

Most likely I'll never have to access the map by that key anyway, I just need it to be unique.
Hawknc wrote:Gotta love our political choices here - you can pick the unionised socially conservative party, or the free-market even more socially conservative party. Oh who to vote for…I don't know, I think I'll just flip a coin and hope it explodes and kills me.

Website

elminster
Posts: 1560
Joined: Mon Feb 26, 2007 1:56 pm UTC
Location: London, UK, Dimensions 1 to 42.
Contact:

Re: Coding: Fleeting Thoughts

Postby elminster » Tue Dec 01, 2009 10:18 am UTC

You, sir, name? wrote:
Spoiler:
elminster wrote:

Code: Select all

if(x) {
 int y;
Yes...

Code: Select all

if(x)
{
 int y;
Yes... if you enjoy eating quiche.

Code: Select all

if(x)
{ int y;
No, just NO! *rage*
Fixed. The rage, though, is valid. Dunno what you have to be smoking to come up with something like that.
Someone on the project I'm working on picked up that style from someone who previously coded on the project (Who I've not worked with) and I've been fixing it all, but this other guy continues to put it in. I've never seen anyone do that except these 2 people. For one thing, it screws up the auto-formatting and it can make the indentation on the first line incorrect.
I believe there's a thread around on this, so I'll leave it at that.
Image

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Tue Dec 01, 2009 1:48 pm UTC

elminster wrote:
You, sir, name? wrote:
Spoiler:
elminster wrote:

Code: Select all

if(x) {
 int y;
Yes...

Code: Select all

if(x)
{
 int y;
Yes... if you enjoy eating quiche.

Code: Select all

if(x)
{ int y;
No, just NO! *rage*
Fixed. The rage, though, is valid. Dunno what you have to be smoking to come up with something like that.
Someone on the project I'm working on picked up that style from someone who previously coded on the project (Who I've not worked with) and I've been fixing it all, but this other guy continues to put it in. I've never seen anyone do that except these 2 people. For one thing, it screws up the auto-formatting and it can make the indentation on the first line incorrect.
I believe there's a thread around on this, so I'll leave it at that.


And that is why God invented the "indent" tool.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
lulzfish
Posts: 1214
Joined: Tue Dec 16, 2008 8:17 am UTC

Re: Coding: Fleeting Thoughts

Postby lulzfish » Wed Dec 02, 2009 1:28 am UTC

Less interesting post bitching about how I don't understand something that I clearly DO understand:
Spoiler:
I don't know if this technically belongs in Coding or Computer Science, but scene graphs are causing all kinds of deadlocks in my mind.

I understand them, and I've used the Irrlicht implementation, but I want to write a much better one for my own graphics engine, and it has to cooperate with Blender and OpenGL.

Do I need a class for a model or should I just drop the model's subobjects into the scene hierarchy and treat it as a normal part of the tree? Then how do I manage geometry data? Reference counting? Where should I set the limits for adding and removing objects dynamically?

I think I'm going to make some use cases and then try to work backwards from there. But every time I think about this, I just feel like I have NO idea where to start implementing it, because it's quite complicated and appears to require a lot of thought. Maybe I should make a prototype in Lua, far away from my actual graphics engine.

Here's a better post:

My program needs to access some files that are in, let's say a subfolder "foo". So in my C++ program, I'll load "foo/bar.png" at some point.
But if I execute this program from anywhere besides its own directory, "foo/bar.png" resolves to the wrong path, and it can't load the file.
What I need is a way to load resources relative to the executable path.
I could just put the directory in a config file or shell script somewhere, but that shouldn't be necessary. Is there some kind of Linux trick to figure out where I'm executing from?

User avatar
headprogrammingczar
Posts: 3072
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

Re: Coding: Fleeting Thoughts

Postby headprogrammingczar » Wed Dec 02, 2009 3:34 pm UTC

Hey, scene graphs are very interesting. What I would do is make a node that represents the whole model, then place the model itself under that node. I don't know if that is the best way, performance-wise, but it definitely gives you the most control over your model.
<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Weeks> You're the goddamn headprogrammingspock!
<Cheese> I love you

Random832
Posts: 2525
Joined: Wed Oct 10, 2007 4:38 pm UTC

Re: Coding: Fleeting Thoughts

Postby Random832 » Wed Dec 02, 2009 3:49 pm UTC

lulzfish wrote:My program needs to access some files that are in, let's say a subfolder "foo". So in my C++ program, I'll load "foo/bar.png" at some point.
But if I execute this program from anywhere besides its own directory, "foo/bar.png" resolves to the wrong path, and it can't load the file.
What I need is a way to load resources relative to the executable path.
I could just put the directory in a config file or shell script somewhere, but that shouldn't be necessary. Is there some kind of Linux trick to figure out where I'm executing from?


No. Generally you would put the subfolder "foo" in something like /usr/share/yourprogram and set the /usr/share/yourprogram in config.h with autoconf, rather than putting it in the directory the program is in. (normally on linux a program executable file goes in a directory _with other programs_, rather than _with other stuff related to the same program_)

User avatar
lulzfish
Posts: 1214
Joined: Tue Dec 16, 2008 8:17 am UTC

Re: Coding: Fleeting Thoughts

Postby lulzfish » Wed Dec 02, 2009 4:12 pm UTC

Yes, that's what I heard from the Google results I got. I also heard that you can use the "readlink" function to follow a symbolic link from "/proc/self/exe" to your executable, so I'm going to use that.

I mean, I'm still writing this program. I want to have it in one directory, where I can manage git easily and modify all my data, not spread out across root-only directories like /usr/local/bin and /usr/local/share. I'm not freaking going in to /usr just to modify a config file or re-compile my progrma or something. And that is the only thing I hate about Linux, the directory system.

Random832
Posts: 2525
Joined: Wed Oct 10, 2007 4:38 pm UTC

Re: Coding: Fleeting Thoughts

Postby Random832 » Wed Dec 02, 2009 4:14 pm UTC

lulzfish wrote:I want to have it in one directory, where I can manage git easily and modify all my data, not spread out across root-only directories like /usr/local/bin and /usr/local/share. I'm not freaking going in to /usr just to modify a config file or re-compile my progrma or something. And that is the only thing I hate about Linux, the directory system.


Huh?

If you want stuff that's per-user, store everything in ~/.yourprogram

I thought you were making a program that was intended to be installed for people that wouldn't be changing the data

User avatar
PM 2Ring
Posts: 3713
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: Coding: Fleeting Thoughts

Postby PM 2Ring » Thu Dec 03, 2009 3:24 am UTC

Is there some kind of Linux trick to figure out where I'm executing from?
You can always inspect the path in argv[0]. Of course, that won't help much if the executable is in your $PATH.

User avatar
Emu*
Posts: 689
Joined: Mon Apr 28, 2008 9:47 am UTC
Location: Cardiff, UK
Contact:

Re: Coding: Fleeting Thoughts

Postby Emu* » Thu Dec 03, 2009 9:21 am UTC

PM 2Ring wrote:
Is there some kind of Linux trick to figure out where I'm executing from?
You can always inspect the path in argv[0]. Of course, that won't help much if the executable is in your $PATH.


If it was run without specifying the folder, you could exec "which <executablename>".
Cosmologicon wrote:Emu* implemented a naive east-first strategy and ran it for an hour, producing results that rivaled many sophisticated strategies, visiting 614 cells. For this, Emu* is awarded Best Deterministic Algorithm!

achilleas.k
Posts: 75
Joined: Wed Nov 18, 2009 3:21 pm UTC

Re: Coding: Fleeting Thoughts

Postby achilleas.k » Thu Dec 03, 2009 10:13 am UTC

Writing a proper, general use, object oriented GA is much harder than it seems. The general use bit is what gets me every time.
The most exciting phrase to hear in science -the one that heralds new discoveries- is not "Eureka!" but "That's funny...". - Isaac Asimov

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Thu Dec 03, 2009 8:18 pm UTC

I looked over PHP and went "Hmm. You could actually write readable and maintainable code with this language. I wonder why every single line of PHP I've ever seen was a pile of 100% bona fide industrial strength crap?"

It's amusing how PHP, after a decade or so of being flaunted as suitable for newbie programmers, has turned into a festering cesspool of appalling coders and their equally appalling coding practices.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
Xanthir
My HERO!!!
Posts: 5410
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Thu Dec 03, 2009 8:41 pm UTC

Ah, that's easy. Most coders are shit at their jobs. Simple enough. And when they look for a language to use, they see PHP as being easy, so they use it. The conclusion is obvious.

I was shit at PHP for nearly a year before I finally nailed the correct combination of practices to make my code maintainable. That's not saying anything against PHP, it's just saying that I was shit at doing large projects, and had to learn everything on my own. I'm competent enough to actually learn, though; most people aren't.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Sun Dec 06, 2009 2:50 pm UTC

I'm building web 2.0 applications in uncool and buzzword-free technologies such as CGI shellscripts for no reason other than to pimpslap of the ruby on rails crowd.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 7 guests