Now, there are lots of ways to do this. I wanted something that was simple and fast and, probably because I'm not particularly good with tricky algorithms, I was hoping for something kind of cool so that I could just sort of mention this awesome new technique in casual conversation with some of the Masters of Computer Science at the upcoming ColdFusion conferences, waiting for them to recognize me as a member of The Fraternity, worthy of being taught the Secret Handshake.
I played around with some stuff and came up with something I thought might fit the bill! Now, it so happens that I work with a fellow programmer who has a Masters degree in Computer Science, so I tried my technique on him.
"Sure," he said, but without the note of awe I was hoping for. "That's a good idea," but it was obvious that my idea was no great revelation to him.
Great minds, it's often been said, must deal with such petty jealousies. So, heedless of his lackluster response, I present it to you...
For any given month, the unavailable dates (either blacked out or already booked) are stored as a single number. That's possible if we assign all dates of the month with a unique number from the powers of 2.
For example...
the 1stof the month = 1 (or 2^0)
the 2d of the month = 2 (or 2^1)
the 3d of the month = 4 (or 2^2)
the 4th of the month = 8 (or 2^3)
...and so forth.
To come up with a single integer describing all unavailable dates, I just add up the "powers of 2" values for all dates on which the property can't be rented. So, if the first through the third are unavailable, I add 1 + 2 + 4, giving me 7. I can now store this number somewhere handy (a database, for instance).
In my calendar, I can use the first of the CF functions mentioned in the title of this post: BitAnd. This function takes two numbers and checks to see if both bits in each location are "on".
In my example, the number for unavailable dates for this month is 7. Now, let's see if the 2d of the month (corresponding with 2^1) is unavailable: BitAnd(7, 2). If so, I will get back a non-negative, non-zero number. (In fact, I'll get back 2, corresponding to the bit the two numbers share in common.)
Further, if I want to see if a range of dates is available, I can add the desired numbers up and BitAnd them against the unavailable dates integer. If all the dates are available, I'll get back a 0. That will provide me with a quick way of testing for availability searches.
That works pretty well! But, I need to keep track of the next 12 months worth of dates. Maybe an array would work well here, a one-dimensional array with 12 elements in it, each element having a struct that holds both the actual month number (e.g. 2 for February, 3 for March, etc.) and that number representing unavailable dates.
I could try something like this:
But that runs me into trouble. If I start with, say, month 7, September, and add 11 to it, I get month 18 and, while enlightened people call this month "Haltober", alas, the wider world has not caught on: I need to do something different.
A little code that says "If the lastMonth is greater than 12, subtract 12 from it" will work fine.
I await immortality...
No comments:
Post a Comment