Older blog entries for maragato (starting at number 68)

Linux Kernel Linked List Explained

I appreciate beautiful, readable code. And if someone were to ask me for an example of beautiful code, I’ve always had the answer ready: the linked list implementation in the Linux kernel.

The code is gorgeous in its simplicity, clarity, and amazing flexibility. If there’s ever a museum for code, this belongs there. It is a masterpiece of the craft.

I was just telling a friend about it while we talked about beautiful code and he found this piece that I share here: Linux Kernel Linked List Explained.

Syndicated 2013-01-04 20:13:22 from robteix dot com

Container changes in C++11

The recently approved C++11 standard brings a lot of welcome changes to C++ that modernize the language a little bit. Among the many changes, we find that containers have received some special love.

Initialization

C++ was long behind modern languages when it came to initializing containers. While you could do

int a[] = {1, 2, 3};

for simple arrays, things tended to get more verbose for more complex containers:

vector<string> v;
v.push_back("One");
v.push_back("Two");
v.push_back("Three");

C++11 has introduced an easier, simpler way to initialize this:

vector<string> v = {"One", "Two", "Three"};

The effects of the changes are even better for things like maps, which could get cumbersome quickly:

map<string, vector<string> > m;
vector<string> v1;
v1.push_back("A");
v1.push_back("B");
v1.push_back("C");

vector<string> v2;
v2.push_back("A");
v2.push_back("B");
v2.push_back("C");

m["One"] = v1;
m["Two"] = v2;

This can now be expressed as:

map<string, vector<string>> m = {{"One", {"A", "B", "C"}},
                                 {"Two", {"Z", "Y", "X"}}};

Much simpler and in line with most modern languages. As an aside, there’s another change in C++11 that would be easy to miss in the code above. The declaration

map<string, vector<string>> m;

was illegal until now due to >> always being evaluated to the right-shift operator; a space would always be required, like

map<string, vector<string> > m

No longer the case.

Iterating

Iterating through containers was also inconvenient. Iterating the simple vector v above:

for (vector<string>::iterator i = v.begin();
     i != v.end(); i++)
    cout << i << endl;

Modern languages have long had some foreach equivalent that allowed us easier ways to iterate through these structures without having to explicitly worry about iterators types. C++11 is finally catching up:

for (string s : v)
    cout << s << endl;

As well, C++11 brings in a new keyword, auto, that will evaluate to a type in compile-type. So instead of

for (map<string, vector<string> >::iterator i = m.begin();
     i != m.end(); i++) {

we can now write

for (auto i = m.begin(); i != m.end(); i++) {

and auto will evaluate to map<string, vector<string>>::iterator.

Combining these changes, we move from the horrendous

for (map<string, vector<string> >::iterator i = m.begin();
     i != m.end(); i++)
    for (vector<string>::iterator j = i->second.begin();
         j != i->second.end(); j++)
        cout << i->first << ': ' << *j << endl;

to the much simpler

for (auto i : m)
    for (auto j : i.second)
        cout << i.first << ': ' << j << endl;

Not bad.

C++11 support varies a lot from compiler to compiler, but all of the changes above are already supported in the latest versions of GCC, LLVM, and MSVC compilers.

Syndicated 2012-12-15 14:41:36 from robteix dot com

FOR is evil or something

Have you ever wondered how FORs impact your code? How they are limiting your design and more important how they are transforming your code into an amount of lines without any human meaning?

How can you not want to read an article that starts like that? I had to steal the intro from the original. Seriously, I have used FOR since I learned BASIC back in the day. I never thought about how it was limiting my design. I. Must. Learn. How.

The article I am referring to is, Avoiding FORs – Anti-If Campaign. Eager learner I, I could not not read it.

After the resplendent intro, Avoiding FOR goes on to show “how to transform a simple example of a for […], to something more readable and well designed.”

It takes this unreadable piece of code — that I now recognize as unreadable:

public class Department {

    private List resources = new ArrayList();

    public void addResource(Resource resource) {
        this.resources.add(resource);
    }

    public void printSlips() {

        for (Resource resource : resources) {
            if(resource.lastContract().deadline().after(new Date())) {

                System.out.println(resource.name());
                System.out.println(resource.salary());
            }
        }
    }
}

My eyes hurt already. Thankfully the author transforms the aberration above into this clean, much more readable snippet:

public class ResourceOrderedCollection {
        private Collection<Resource> resources = new ArrayList<Resource>();



    public ResourceOrderedCollection() {
        super();
    }

    public ResourceOrderedCollection(Collection<Resource> resources) {
        this.resources = resources;
    }

    public void add(Resource resource) {
        this.resources.add(resource);
    }

    public void forEachDo(Block block) {
        Iterator<Resource> iterator = resources.iterator();

        while(iterator.hasNext()) {
            block.evaluate(iterator.next());
        }

    }

    public ResourceOrderedCollection select(Predicate predicate) {

        ResourceOrderedCollection resourceOrderedCollection = new ResourceOrderedCollection();

        Iterator<Resource> iterator = resources.iterator();

        while(iterator.hasNext()) {
            Resource resource = iterator.next();
            if(predicate.is(resource)) {
                resourceOrderedCollection.add(resource);
            }
        }

        return resourceOrderedCollection;
    }
}

public class Department {

    private List<Resource> resources = new ArrayList<Resource>();

    public void addResource(Resource resource) {
        this.resources.add(resource);
    }

    public void printSlips() {
        new ResourceOrderedCollection(this.resources).select(new InForcePredicate()).forEachDo(new PrintSlip());
    }

}

Wait, what? Is this an Onion article?

Snarky Mode Off.

I understand what the author wanted to do, but really, the example used is so off the left field that it’s not even funny.

Syndicated 2012-11-19 13:31:16 from robteix dot com

Missus Lady Wife’s bday

20121111-121454.jpg

Wife’s birthday translates into dad-daughter’s day out as my wife gets a day-off from us :)

Syndicated 2012-11-11 15:07:51 from robteix dot com

All those black people…

I had a talk with my landlady.

Prostest against the Argentine government in Buenos Aires

There had been another set of monster protests against the government in Argentina and she was talking about the situation in her country.

As usual, she scolded me for leaving Brazil and coming here.

As usual, I tried to stay out of it saying I don’t really follow the news.

“And also,” I explained, “Brazil isn’t that much better, you know?”

“Ah, but you are so much more organized,” she continued.

“We were lucky with the last few presidencies,” I conceded.

“That wasn’t luck,” she educated me. “You know how to vote over there.”

LOL

“We have too much corruption,” I continued. “And crime. We’ve got too much violence!”

And then she said something I heard before in Argentina.

“Ah, yes,” she permitted sympathetically. “You have all those black people…”

She must have seen my face because she said “well, I don’t know, of course.”

“That’s not the problem, you know,” I said. “When I was kidnapped, none of the men were blacks. That really isn’t how it works over there.”

And we changed the subject.

That’s not the first time I hear from someone here the same argument that Brazil’s problems are somehow a result of “those black people” but it stills throws me off, every time.

Syndicated 2012-11-10 21:39:16 from robteix dot com

Quote of the Day

An idiot with a PhD is still an idiot

Syndicated 2012-11-10 11:57:25 from robteix dot com

Rolling out your own Fusion Drive with the recovery partition

disk utility showing Fusion Drive

My Macbook Pro has two disks, an HDD and an SSD, each of 240GB or so. With the details of Apple’s Fusion Drive coming out I decided to do what any reasonable geek would do to their production computer: I’ve decided to implement my own untested, highly experimental and barely understood Fusion Drive.

One of the things that initially put me off doing this was that according to the 3,471,918 tutorials that have popped up in the last 10 minutes would cause me to lose my Mountain Lion recovery partition because these partitions are not supported in a Fusion drive. Turns out this is not exactly true.

Fusion Drive is just a marketing term for a what essentially is a CoreStorage logical volume spanning an SSD and an HDD. And although you cannot have the recovery partition inside a CS logical volume, it doesn’t mean you can’t have both a recovery partition and a Fusion Drive at the same time. It’s all in the diskutil man page, by the way:

Create a CoreStorage logical volume group. The disks specified will become the (initial) set of physical volumes; more than one may be specified. You can specify partitions (which will be re-typed to be Apple_CoreStorage) or whole-disks (which will be partitioned as GPT and will contain an Apple_CoreStorage partition). The resulting LVG UUID can then be used with createVolume below. All existing data on the drive(s) will be lost. Ownership of the affected disk is required.

What matters is what’s in bold above: we’re not limited to using whole disks. So here’s what I did.

I rebooted my system and held the option key so I could select my recovery partition as the start up disk. Once the OSX recovery started up, I launched a terminal to do the dirty work.

  diskutil list

From this I noted two things: (a) the main SSD partition (the one holding my OSX and that sited by my recovery partition) and (b) the disk name of my HDD. They were respectively disk0s2 and disk1 in my case, but they’ll very likely be different for you. Then the magic begins.

  diskutil cs create "Fusion Drive" disk0s2 disk1

(For crying out loud, you need to change disk0s2 and disk1 for whatever makes sense on your system!)

That created the coreStorage logical volume. Then I listed it all again to note what the new logical volume UUID was.

  diskutil list

The UUID is a long number identifier like F47AC10B-58CC-4372-A567-0E02B2C3D479. You’ll need that one next to actually create the volume where you’ll be installing your system.

  diskutil coreStorage createVolume F47AC10B-58CC-4372-A567-0E02B2C3D479 jhfs+ "Macbook FD" 100%

The command above will create a volume named “Macbook FD” using 100% of the logical volume we had created earlier.

I then restored my Time Machine backup and that’s it.

Update: Note that after this process, the Recovery partition will still be present and things that require it (such as Find My Mac) will work fine. Some people correctly pointed out, however, that you can no longer boot from the recovery partition by using the menu from holding ⌥ (option) during boot. I’m not sure why that is, but fear not, it will still boot normally from pressing R (command + R).

Syndicated 2012-11-09 11:18:34 from robteix dot com

Not a-ok

I have a little confession to make: our family is going through a rough patch. Both my wife and I are having problems. No, not between us. And both are experiencing very different sorts of problems. But the happening-at-the-same-time complicates the one-supports-the-other thing.

And to compound to that, I have been sick.

As I sat in the waiting room at the hospital the other day, waiting for some exams, I felt like this is one of the worst times of my life. I feel tired, unmotivated, unappreciated, and generally unhappy. And fucking hopeless. That’s the worst part, I suppose.

I also have been distancing myself from friends lately. Ostensibly to avoid distractions in a time where I am having to give all and a bit more to a project I do not believe in. Truth be told, I just don’t feel like small talk but I also don’t want bring others down to my Dark Hole of Misery. And so I’m trying to keep some distance.

I feel lost. I look at my friends and how they all seem to have it all figured out already. I’m still trying to figure out who the fuck I am. I was so sure when I was younger. I was so fucking good at what I did. Now? I just don’t know anymore. I feel unhappy.

I know some people who will be So. Fucking. Happy. reading the paragraph above. This one’s for free for you guys.

But not all is bad news. I actually got some good news last night. I can’t tell the details although I know some of you know exactly what this is about. Anyway, I now have a set date for it and it’s in under six months from now.

I’m actually confident that this will make it all better somehow. Just have to wait.

Sorry for the downer, but I felt like writing something.

Syndicated 2012-11-08 17:48:39 from robteix dot com

Shouldn’t be much of a surprise to anyone…

Shouldn't be much of a surprise to anyone…

Kaspersky: Mac security is '10 years behind Microsoft'

In an interview, the security firm's CEO says Apple has a lot more malware coming its way, and that it's not putting enough resources into protecting users. Read this blog post by Josh Lowensohn on Ap…

Syndicated 2012-04-26 20:33:10 from ~robteix

The article points to a huge resource library…

The article points to a huge resource library on the software used. But the really cool part is how they actually stored the software. LOL Memory FTW

Source code for Apollo and Gemini programs

An extensive collection gathered from all over the internet of the source code and documentation for NASA's Apollo and Gemini prog

Syndicated 2012-04-26 15:58:13 from ~robteix

59 older entries...

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!