NSUInteger hash
Hi all, I'm just getting started with Cocoa and I'm trying to implement hash and isEqual: methods according to the recommendations in the coding guidelines. To implement a hash method I would normally just hash the receiver's instance variables together and xor the result, but this only works if the instance variables are objects. However my instance variables are NSPoints, which are defined as structs, not objects. The C programmer in me wants to cast the floats into integers and hash those, but we are in a 64-bit world now, and I assume that 32-bit algorithms may not give a good result. Maybe I'm trying too hard, but it's important for what I'm doing that I don't have a lot of collisions so I want a good quality hash function. What's the standard way of hashing non-object values in Cocoa? Thanks, Steve ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: NSUInteger hash
Thanks for the useful feedback. Nick I thought about creating an object for the number but I was apprehensive about the allocation overhead. It seems strange that NSRect and NSPoint are structs and not objects, but I'm rolling with it :) Mike thanks for suggesting I use integer value as its own hash. I was looking in completely the wrong places. Steve On Sat, Aug 16, 2008 at 2:21 PM, Michael Ash <[EMAIL PROTECTED]> wrote: > On Sat, Aug 16, 2008 at 3:57 PM, Steve Wart <[EMAIL PROTECTED]> wrote: > > > What's the standard way of hashing non-object values in Cocoa? > > For primitives, the hash value can generally just be itself. > > Recall that NSUinteger is just a typedef for either int (in 32-bit) or > long (in 64-bit). So it's just an integer. > > You have a pair of floats. The simplest thing to do would be to cast > them to NSUInteger, thereby truncating off the decimal bits, and then > XOR the result together: > > - (NSUInteger)hash { return (NSUInteger)p.x ^ (NSUInteger)p.y; } > > Note that this stops making much sense if you expect your points to be > fractional values, and you want different fractional values to be > considered unequal*. In that case, I'd recommend simply using the > whole float bit-pattern as your hash: > > static NSUInteger CGFloatHash(CGFloat f) { return *(NSUInteger *)&f; } > > - (NSUInteger)hash { return CGFloatHash(p.x) ^ CGFloatHash(p.y); } > > (This works in both 32-bit and 64-bit since NSUInteger and CGFloat > happen to be the same size. If you use this in real code then I > recommend at least adding a check that the sizes are in fact equal and > failing in some loud, obvious way if they aren't so you don't end up > reading junk.) > > Note that if you ever expect to have both positive zero and negative > zero in your points then this will fall apart, as negative zero and > positive zero can compare as equal but don't have matching bit > patterns. The solution to this is left as an exercise to the reader, > with a pointer to the IEEE 754 spec. > > * Note that in general it's bad to compare floating point numbers with > non-integral parts for equality anyway. Two numbers which you think > should be equal may well not be due to rounding error in the > calculations which generated them. There's even an optional gcc > warning flag, -Wfloat-equal, which will produce a warning any time the > == operator is used on floating point numbers. Thus if your instance > variables are NSPoints, and you expect those points to have fractional > values, and you're using those variables as part of your equality > comparison, you're probably doing something wrong. (Although if you > only expect two floats to be equal when you've merely saved a copy of > one to look up later, then that can be reasonably expected to work.) > > Mike > ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: NSUInteger hash
Hi Aaron, That's always a good question to ask. I'm porting a Smalltalk/OpenGL maze application I wrote a few years ago to Cocoa. The maze is initialized by creating a 2D matrix of Room objects which are separated by Wall objects. Every room has an ordered collection of walls which I've put into an NSMutableArray. Every wall has a start point and an end point and exists in exactly one room. So the common wall separating a pair of rooms is actually represented by two walls. I go through the rooms in random order, pick a wall at random, and knock it down (also being careful to knock down the corresponding wall in any adjacent rooms). By knocking down a wall, two rooms become merged into one. When I have only one room left, it means that the maze is complete. I depend on isEqualTo: to compare the walls. They are both created from the same set of points but they are different objects, so I can't use an identity comparison. The Smalltalk code is reasonably clean but it's bloating unpleasantly in Objective C so I will probably need to take a higher-level look at what I'm trying to accomplish. The original maze algorithm was cribbed from C so it shouldn't be too hard to make it work :-) Steve On Sun, Aug 17, 2008 at 12:10 AM, Aaron Lees <[EMAIL PROTECTED]> wrote: > What are you trying to do? It's usually a bad idea to compare floating > point values for equality since you will run into subtle bugs to do with > rounding. If you want to use your objects as dictionary keys it's often > better to use a pointer equality rule instead of semantic equality. You can > do this with NSMapTable without any overrides in your class. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Mixing C++ and Objective-C
I am trying to build an application that uses some C++ classes, based on Apple's example Cocoa OpenGL application here: http://developer.apple.com/samplecode/CocoaGL/index.html#//apple_ref/doc/uid/DTS10004501 It works fine in 10.5.6 with XCode 3.1 if I change the file type of BasicOpenGLView.m to sourcecode.cpp.objcpp. But if I then add the following to the definitions prior to the @implementation: class Vector3d { double x, y, z; }; class CameraFrame { public: Vector3d origin; }; I get a number of errors linking to the C functions in GLCheck.c. Is there a subtle issue I am missing or am I going about this completely the wrong way? Steve "HaveOpenGLCapsChanged(GLCaps*, unsigned int)", referenced from: getCurrentCaps() in BasicOpenGLView.o "initCapsTexture(GLCaps*, unsigned int)", referenced from: getCurrentCaps() in BasicOpenGLView.o "drawCaps(GLCaps*, unsigned int, long, float)", referenced from: -[BasicOpenGLView drawInfo] in BasicOpenGLView.o "CheckOpenGLCaps(unsigned int, GLCaps*, unsigned int*)", referenced from: getCurrentCaps() in BasicOpenGLView.o getCurrentCaps() in BasicOpenGLView.o ld: symbol(s) not found collect2: ld returned 1 exit status ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Mixing C++ and Objective-C
Solved, thanks to the archives. #ifdef __cplusplus extern "C" { #endif extern CFunctionHere #ifdef __cplusplus } #endif Cheers, Steve On Sat, Feb 7, 2009 at 12:29 PM, Steve Wart wrote: > I am trying to build an application that uses some C++ classes, based on > Apple's example Cocoa OpenGL application here: > http://developer.apple.com/samplecode/CocoaGL/index.html#//apple_ref/doc/uid/DTS10004501 > > It works fine in 10.5.6 with XCode 3.1 if I change the file type of > BasicOpenGLView.m to sourcecode.cpp.objcpp. But if I then add the following > to the definitions prior to the @implementation: > > class Vector3d { > double x, y, z; > }; > > class CameraFrame { > public: > Vector3d origin; > }; > > I get a number of errors linking to the C functions in GLCheck.c. Is there > a subtle issue I am missing or am I going about this completely the wrong > way? > > Steve > > "HaveOpenGLCapsChanged(GLCaps*, unsigned int)", referenced from: > getCurrentCaps() in BasicOpenGLView.o > "initCapsTexture(GLCaps*, unsigned int)", referenced from: > getCurrentCaps() in BasicOpenGLView.o > "drawCaps(GLCaps*, unsigned int, long, float)", referenced from: > -[BasicOpenGLView drawInfo] in BasicOpenGLView.o > "CheckOpenGLCaps(unsigned int, GLCaps*, unsigned int*)", referenced from: > getCurrentCaps() in BasicOpenGLView.o > getCurrentCaps() in BasicOpenGLView.o > ld: symbol(s) not found > collect2: ld returned 1 exit status > > ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Mixing C++ and Objective-C
Thanks Peter. I realized after posting my question that it wasn't actually Cocoa-related, nor even Objective-C related - the error was a result of my inexperience with C++. Apologies to everyone for posting off-topic. I appreciate that this is a high-volume list and didn't intend to distract from the useful discussions that generally take place here. Steve On Sun, Feb 8, 2009 at 5:42 PM, Peter N Lewis wrote: > At 12:29 -0800 7/2/09, Steve Wart wrote: > > It works fine in 10.5.6 with XCode 3.1 if I change the file type of >> BasicOpenGLView.m to sourcecode.cpp.objcpp. >> > > You normally use the extension .mm for Objective C++ code. > > At 10:41 +0900 9/2/09, Peter N Lewis wrote: > >> At 13:12 -0800 7/2/09, Steve Wart wrote: >> >>> Solved, thanks to the archives. >>> >>> #ifdef __cplusplus >>> extern "C" { >>> #endif >>> >> > Personally, I just switch the whole project to compile everything in > Objective C++ and this eliminates the need for ifdef __cplusplus at > essentially no cost in compile time, execution speed or executable size. It > just means you are only dealing with one version of C to go with your > Objective C rather than C, C++ and Objective C. > > Enjoy, > Peter. > ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
UINavigationItem backButtonTitle property is missing?
Hi, I'm working through the iPhone UINavigationController tutorial and noticed that the class reference for UINavigationItem contains the following *backButtonTitle* The title to use when this item is represented by a back button on the navigation bar. @property(nonatomic, copy) NSString *backButtonTitle *Discussion* When this item is the back item of the navigation bar—when it is the next item below the top item—it may be represented as a back button on the navigation bar. Use this property to specify a title for the back button. The default value is the navigation item's title. *Availability* Available in iPhone OS 2.0 and later. *See Also* @property backItem @property hidesBackButton *Declared In* UINavigationBar.h However, this property does not seem to be declared in UINavigationItem.h (the private attribute _backButtonTitle is declared). I've encountered this in both the 2.0 and 2.1 versions of the SDK. Am I missing something? Is there a workaround that someone can suggest? Thanks, Steve ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: UINavigationItem backButtonTitle property is missing?
Sorry about that. I should have checked the discussion thread. On Sat, Oct 4, 2008 at 9:23 AM, Roland King <[EMAIL PROTECTED]> wrote: > no iPhone discussions allowed here yet. > > > On Oct 4, 2008, at 11:47 PM, Steve Wart wrote: > > Hi, >> >> I'm working through the iPhone UINavigationController tutorial and noticed >> that the class reference for UINavigationItem contains the following >> > > ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]