1

Sorry if this sounds a bit too specific, but an answer to my problem would be an answer to a much broader problem. So here's my problem's context:

In an Abstract Data Types excersize, I'm constructing a "Glue Layer" between two ADTs - Scheduler and Heap - called Container. Scheduler provides it with a compare function, and it should pass into Heap a function that returns the negative result of that compare function for any input. The reason for this is that Heap is a maximum heap, but Scheduler requires the drawing of a minimum element. I am not allowed to change Heap into a minimum heap or AT ALL. Likely, I am not allowed to change Scheduler's compare function to return the negative of what it normally returns, or again, AT ALL. It is up to Container to invert the function and feed it to Heap. The question is how the hell can it do it?!

The types i'm using are:

typedef void* Data;
typedef int (*DataCompareFunc)(const Data data1,const Data data2);

Scheduler calls the function:

Container* ContainerCreate(DataCompareFunc compFunc)

And HeapCreate's signature is:

Heap* HeapCreate(DataCompareFunc compFunc);

In what way can ContainerCreate transfer a negated function into HeapCreate?

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
Tom Yaish
  • 11
  • 3
  • Can you wrap the Scheduler's comparison function in another function and negate the result? – The-Q Jul 17 '15 at 18:30
  • You should look at lambda functions and closures, however implementing that in C you may be limited to [GCC](https://en.wikipedia.org/wiki/Anonymous_function) – Soren Jul 17 '15 at 18:33
  • I must be missing something, The way your question, right up until the last sentence, is written, you want something literally polar-opposite of what you're currently allowed to use, but can't change *anything*. Can we try this again. What *are* you allowed to change? – WhozCraig Jul 17 '15 at 18:48
  • It would make more sense as `Heap* HeapCreate (CoontainerCreate (DataCompareFunc (compFunc));` – David C. Rankin Jul 17 '15 at 18:50

1 Answers1

0

Typically such functionality is created using Lambda and Closure functions.

Lambda & Closures are supported in C++11, however even in old-style C++, you could write Functors

For C it is harder, however GCC has some support

Writing a negated function with an anonymous function in a closure is (in pseudo code)

function negate(f) { 
   return function(){ not f(); }
}

where negate(somefunction) would return a function which would return true when somefunction returns false etc.

Community
  • 1
  • 1
Soren
  • 14,402
  • 4
  • 41
  • 67
  • 1
    " I am not allowed to change Heap into a minimum heap or AT ALL." + "Likely, I am not allowed to change Scheduler's compare function to return the negative of what it normally returns, or again, AT ALL. " - I would have to assume that includes whatever flavor of implementing that function you care to dream up. Either that or the question was written incorrectly. – WhozCraig Jul 17 '15 at 18:42
  • Humm.. I was reading the question as asking for a factory-pattern for functions, but I do agree that the wording of the question is confusing – Soren Jul 17 '15 at 18:45
  • I tried creating a function the way Soren suggests, with: 'DataCompFunc negate(DataCompFunc compFunc){ return -compFunc;}' but gcc claims this is an "invalid input for unary operator". btw, i'm sorry for the confusing rules involved in my question, but those are the demands. originally i solved it by adding a minus in Scheduler's compare function's return line, but they our teacher specifically said it's not allowed. right now, the only solution i see is upgrading my heap into a min OR max heap with another binary parameter, but that's not what requested. – Tom Yaish Jul 18 '15 at 12:46
  • Your example does not follow the syntax given in the [example on the wikipedia](https://en.wikipedia.org/wiki/Anonymous_function#GCC) page, i.e. `( { return_type anonymous_functions_name (parameters) { function_body } anonymous_functions_name; } )` -- however you would have to play around with it as it ONLY works as a macro, as true closures are not supported so you cannot return a new function in C – Soren Jul 18 '15 at 18:54
  • I've come to a conclusion my teacher didn't think this through on the C89 standard we're bound to. He confirmed functors as the obvious solution, and we'll work something out. In any case, thank you for putting thought into it :) – Tom Yaish Jul 19 '15 at 18:23