Blog

Inflated

I have a 13-inch Unibody MacBook Pro purchased in 2009. This week, the trackpad became very hard to click. In fact, on this computer the trackpad has always been slightly harder to press on the left than on others of the same model, but I wasn’t really concerned. Except this week when it got worse. I opened the bottom panel to see whether I could somehow clean whatever was blocking the trackpad, but without success: the battery covers the area too tightly. But at the same time I understood why the trackpad stopped working.

The battery had inflated. Not by much, but surely enough to almost entirely block clicking on the left. Obviously, there is nothing to clean; the only possible remedy is to change the battery. I close and fasten back the bottom cover. I’m a little sad, because this battery is still in perfect working condition if you abstracts the deformity.

Today, I went to the Apple Store. The Genius had never seen it on a Unibody model. $200 later I have a new battery. It’s still costly when you consider it replaces a battery that was still working fine but likely had some kind of manufacturing defect. I’m a little disappointed.

On the brighter side, the trackpad now works better than it ever did even when the computer was new. I wonder when exactly it started to inflate…


Petition

I got an impression of déjà vu when I found this petition about MPAA bribery on the White House’s website (via Slashdot).

Here in the province of Quebec, Canada we had an official petition about a year ago asking our prime minister to resign mainly for refusing to start a public inquiry over corruption allegations. 247,379 signed the petition, which is about 8.2% of the electorate. This petition was non-binding (we don’t have a recall process and he didn’t resign), but it still had a huge impact in the media and public awareness for the whole duration while the number was blowing off the charts.

So US citizens, please keep that number increasing for a month. It will definitely have an impact once it becomes too big to be ignored.


What’s important

When drafting a law, just like when writing a computer program, we should always keep in mind how it can be abused and what are the consequences of those abuses. Because sooner or later, it will. Usually sooner than later.

Today is SOPA/PIPA protest day all over the internet. I don’t live in the United States, but I’m still concerned because this will have ripple effects all over the internet, and once passed it’ll put pressure on other countries to do the same.

Copyright raison-d’être is to promote progress of science and arts. But copyright can also stifle creation and become a censorship tool if misused and enforced too eagerly.

Maybe we need to remind ourself that everything is a remix. Which means that there’ll always be someone somewhere to claim you’re infringing on their copyright. And if you force websites to be blocked at the first copyright complain, your website better not displease anyone.

Well, it’s worse than that. You better not link to anyone who might displease someone, because those links are enough ground to shut down or block a site under these laws. Scary.


Introducing MFIndexSetForeach

Index sets can be a little annoying in Objective-C. Unlike arrays, sets and dictionaries, you can’t iterate over them using a straightforward for (a in b) loop. That’s because unlike other Objective-C containers, index sets contains integers, not objects. Now, since Mac OS X 10.6 you can use a block to iterate over an index set, but this isn’t always ideal: breaking out of the loop is more complicated, and returning from the block doesn’t return from the outer function. So what can we do better?

In an ideal world you could write this and it’d work, but alas it doesn’t:

for (NSUInteger index in indexSet)
{
    // loop body
}

What I managed to achieve using some macro wizardry is this:

MFIndexSetForeach(index, indexSet)
{
    // loop body
}

MFIndexSetForeach iterates over all indexes in a set. It uses the getIndexes:maxCount:inIndexRange: method of NSIndexSet to fill a small buffer of indexes, iterates over the buffer then refills the buffer and continue iterating until all indexes have been iterated over.

This loop behaves in every way like you’d expect a loop to behave: you can break from it, you can continue in it, and if you return it’ll truly return from the function:

MFIndexSetForeach(index, indexSet)
{
    if (index % 33) continue;
    if (index % 22) break;
    if (index % 11) return YES;
}

Oh, and the braces are optional too.

The equivalent generated code would look somewhat like this:

NSRange _indexRange = NSMakeRange(0, NSUIntegerMax);
for (NSUInteger index,
     _bufferIndex = _bufferLength-1,
     _indexCount = 0,
     _indexBuffer[_bufferLength];
     _MFIndexSetForeachNextIndex(
         &_bufferIndex, &_indexCount, indexSet, _indexBuffer,
         _bufferLength, &_indexRange, &index);
    )
{
    if (index % 33) continue;
    if (index % 22) break;
    if (index % 11) return YES;
}

where, _MFIndexSetForeachNextIndex is an inline function doing all the work.

Have fun with it: Index Set Foreach



  • © 2003–2024 Michel Fortin.