Pigeon's Device (2009)
4 months ago
- #sorting
- #C programming
- #optimization
- Pigeon's device is a loop optimization technique in C, similar to Duff's device but independently originated.
- It was originally used in a function for comparing date/time records in different sort orders: FORWARD, REVERSE, and REVDFWDT.
- The technique involves intertwining an if statement with a switch statement to control the flow based on the mode and conditions.
- A Gruesome Hack is used to set and read the comparison mode since the function is called from a library sort routine without a third parameter.
- The function handles different sort modes by either comparing dates forward, reverse, or a combination (reverse dates with forward times within the same day).
- A default case acts as a bug catcher to handle invalid mode values.