Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you define a simple "min" method in obj-c

Tags:

I want to define a min and max methods in a Utils class.

@interface Utils  int min(int a, int b); int max(int a, int b);  @end 

But I don't want to have named parameters. It would be a too heavy notation. I wanted to use the C-style definition. But then [Utils min(a, b)] as a call doesn't work. What is my problem?

Thanks in advance for any help

like image 250
Fred Polack Avatar asked Jan 23 '10 23:01

Fred Polack


2 Answers

It is already defined as a macro.

MIN(a, b)  MAX(a, b) 

You dont need to redefine these ones.

like image 107
Mongus Pong Avatar answered Nov 10 '22 12:11

Mongus Pong


There's a serious issue with the solution posted by Brandon Bodnár (which by the time of this writing is marked as a valid solution).

Issue described here: http://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Min-and-Max.html And the (valid & secure) solution to it: http://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Typeof.html

Check it out yourself:

#include <stdio.h>  #define NAIVE_MAX(a,b) (a > b ? a : b)  #define NAIVE_MIN(a,b) (a < b ? a : b)  #if !defined MAX #define MAX(a,b) \ ({ __typeof__ (a) __a = (a); \ __typeof__ (b) __b = (b); \ __a > __b ? __a : __b; }) #endif  #if !defined MIN #define MIN(a,b) \ ({ __typeof__ (a) __a = (a); \ __typeof__ (b) __b = (b); \ __a < __b ? __a : __b; }) #endif  int main (int argc, const char * argv[]) {     int a = 3;     int b = 5;  #pragma mark NON-FATAL CASES:     printf("NAIVE_MAX(%d, %d) => %d\n", a, b, NAIVE_MAX(a, b));     printf("NAIVE_MIN(%d, %d) => %d\n", a, b, NAIVE_MIN(a, b));      printf("MAX(%d, %d) => %d\n", a, b, MAX(a, b));     printf("MIN(%d, %d) => %d\n", a, b, MIN(a, b));      printf("\nEverything fine so far...\n\n");  #pragma mark FATAL CASES:     //cache:     int _a = a;     int _b = b;     printf("NAIVE_MAX(%d++, %d++) => %d\n", _a, _b, NAIVE_MAX(a++, b++));      //reset:     a = _a;     b = _b;     printf("NAIVE_MIN(%d++, %d++) => %d\n", _a, _b, NAIVE_MIN(a++, b++));      //reset:     a = _a;     b = _b;     printf("NAIVE_MAX(++%d, ++%d) => %d\n", _a, _b, NAIVE_MAX(++a, ++b));      //reset:     a = _a;     b = _b;     printf("NAIVE_MIN(++%d, ++%d) => %d\n", _a, _b, NAIVE_MIN(++a, ++b));      printf("\nOuch, this doesn't look right at all!\n\n");  #pragma mark NON-FATAL CASES:     //reset:     a = _a;     b = _b;     printf("MAX(%d++, %d++) => %d\n", _a, _b, MAX(a++, b++));      //reset:     a = _a;     b = _b;     printf("MIN(%d++, %d++) => %d\n", _a, _b, MIN(a++, b++));      //reset:     a = _a;     b = _b;     printf("MAX(++%d, ++%d) => %d\n", _a, _b, MAX(++a, ++b));      //reset:     a = _a;     b = _b;     printf("MIN(++%d, ++%d) => %d\n", _a, _b, MIN(++a, ++b));      printf("\nAh, much better now.\n\n");      return 0; } 

Console log:

NAIVE_MAX(3, 5) => 5 NAIVE_MIN(3, 5) => 3 MAX(3, 5) => 5 MIN(3, 5) => 3  Everything fine so far...  NAIVE_MAX(3++, 5++) => 6 NAIVE_MIN(3++, 5++) => 4 NAIVE_MAX(++3, ++5) => 7 NAIVE_MIN(++3, ++5) => 5  Ouch, this doesn't look right at all!  MAX(3++, 5++) => 5 MIN(3++, 5++) => 3 MAX(++3, ++5) => 6 MIN(++3, ++5) => 4  Ah, much better now. 

So never ever use the naive implementation as seen in the code above (and as suggested by Brandon Bodnár, sorry buddy ;) ) if you want to avoid worst cases like these.

like image 39
Regexident Avatar answered Nov 10 '22 12:11

Regexident