Different ways of summing string lengths

::

Three different ways to sum string lengths:

int numChars(char* word)
{
   int i, j, sum = 0;

   for(i = 0; word[i][j] != '\0'; i++)
   {
      for(j = 0; word[i][j] != '\0'; j++)
      {
         sum++;
      }
   }
   return sum;
}
int numChars(char * strings[], int len) {

  int sum = 0, i;

  for (i = 0; i < len; i++) {
    sum += strlen(strings[i]);
  }

  return sum;
}
int numChars(char * strings[], int len) {

  sumOf(lengths(strings));

}

Code Snippet 2: This time in Java

:: CodeCritic, Programming Languages

/**************************************************************
 * 
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * 
 *************************************************************/



package org.openoffice.xmerge.merger;

/**
 *  This is an interface used by the {@link
 *  org.openoffice.xmerge.merger.DiffAlgorithm
 *  DiffAlgorithm} and {@link
 *  org.openoffice.xmerge.merger.MergeAlgorithm
 *  MergeAlgorithm} to access a <code>Document</code>.
 *
 *  @author smak
 */
public interface Iterator {


    /**
     *  Move to next element in the sequence.
     *
     *  @return  The <code>Object</code> of the next element in the sequence.
     *           If there is no next element, then return null.
     */
    public Object next();


    /**
     *  Move to previous element in the sequence.
     *
     *  @return  The <code>Object</code> of the previous element in the sequence.
     *           If there is no previous element, then return null.
     */
    public Object previous();


    /**
     * Move to the beginning of the sequence.
     *
     * @return  The <code>Object</code> of the first element in the sequence.
     *          If it is empty, then return null.
     */
    public Object start();


    /**
     * Move to the end of the sequence.
     *
     * @return  The <code>Object</code> of the last element in the sequence.
     *          If it is empty, then return null.
     */
    public Object end();


    /**
     * Return the current element <code>Object</code> content.
     *
     * @return  The <code>Object</code> at current position.
     */
    public Object currentElement();


    /**
     * Return the total element count in the sequence.
     *
     * @return  The total element count.
     */
    public int elementCount();


    /**
     *  A method to allow the difference algorithm to test whether the
     *  <code>obj1</code> and <code>obj2</code> in the
     *  <code>Iterator</code> are considered equal.  As not every
     *  <code>Object</code> in the <code>Iterator</code> can implement its
     *  own equal method, with this equivalent method, we can allow
     *  flexibility for the <code>Iterator</code> to choose a custom way
     *  to compare two objects.  Two objects can even be compared based on
     *  the position in the <code>Iterator</code> rather than by 
     *  the content via this option.
     *
     *  @param  obj1  The first <code>Object</code>.
     *  @param  obj2  The second <code>Object</code>.
     *
     *  @return  true if equal, false otherwise.
     */
    public boolean equivalent(Object obj1, Object obj2);


    /**
     *  <p>A method to force the <code>Iterator</code> to transverse the tree
     *  again to refresh the content.</p>
     *
     *  <p>It is used mainly for <code>Iterator</code> objects which take a snap
     *  shot instead of dynamically transversing the tree.  The current
     *  position will be set to the beginning.</p>
     */
    public void refresh();
}

Here’s another one, randomly chosen from the source for Apache OpenOffice’s 800K lines of Java (I should point out that there are something like 10M lines of C++ in the project).

A few seconds makes it clear that this is … the iterator interface. Minus the horrible “remove” call, and plus a bunch of other ones, generally fairly inoffensive.

Honestly, I can’t say too many bad things about this file: it’s clear and useful; this is the kind of file you’d like to see when you go looking for information. I had hoped for something more… complex? But the random number generator does what it wants. Darn random number generator!

One point here is that I probably shouldn’t have excluded .h files from my random C file excerpt; in Java, interface files aren’t distinguished from “source” files in the same way they are in C.

Code Snippet 1: A Random File From the Linux Source

:: CodeCritic, Programming Languages

Questions for the audience:

  • What is this code?
  • How does it work?
  • Could it be cleaner?
  • What parts of the code are easy to understand, and what parts are hard?
/*
 * Copyright (C) 2008 Freescale Semiconductor, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

#include <linux/device.h>	/* devres_*(), devm_ioremap_release() */
#include <linux/gfp.h>
#include <linux/io.h>		/* ioremap_prot() */
#include <linux/export.h>	/* EXPORT_SYMBOL() */

/**
 * devm_ioremap_prot - Managed ioremap_prot()
 * @dev: Generic device to remap IO address for
 * @offset: BUS offset to map
 * @size: Size of map
 * @flags: Page flags
 *
 * Managed ioremap_prot().  Map is automatically unmapped on driver
 * detach.
 */
void __iomem *devm_ioremap_prot(struct device *dev, resource_size_t offset,
				 size_t size, unsigned long flags)
{
	void __iomem **ptr, *addr;

	ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
	if (!ptr)
		return NULL;

	addr = ioremap_prot(offset, size, flags);
	if (addr) {
		*ptr = addr;
		devres_add(dev, ptr);
	} else
		devres_free(ptr);

	return addr;
}
EXPORT_SYMBOL(devm_ioremap_prot);

Granite Mon 2011

:: granitemon

In 2011, Charlotte Clews organized either the Maine Mountain challenge or the Granite Womon challenge or the Long Island challenge or whatever the heck we’re going to call it. It was a resounding success, as evidenced by this picture that Charlotte took:

Granite Woman Swimmers 2011

Granite Woman Swimmers 2011

The swimmers, from left to right, possibly out of order?:

  • Charlotte Clews
  • Bahia Yackzan
  • Louise Bourne
  • Kim Parrot
  • Mary Clews
  • Moira McMahon

Charlotte writes:

What an awesome morning! All six women (Kim Parrot, Mary Clews, Bahia Yackzan, Louise Bourne, Moira Mahon and me) swam from Long Island to Sculpin Point in less than 2 hrs! We rock. And so do our support crew - it took a last minute fuel-line switcharoo, a last-minute boat put-in in South Blue Hill, and a last-minute kayak paddler to pull it off but we did, and the longer we waited the calmer the water got - and ultimately the current seemed just right. Let’s remember that timing for next year. (By the way, if you weren’t there this year, this email is your recruitment email for next year).

I continued the challenge with a bike ride to the base of Cadillac Mountain’s North Ridge Trail - it was 38 miles on my odometer and I stopped for a snack at John Edwards, so it took me 2.5 hrs and I only got side-swiped once - on the last mile of the Park Loop Road …. Otherwise it was a pretty nice bike ride. The (barefoot) hike up Cadillac felt super easy after the long swim and ride. It’s 2.2 miles and only took an hour with Jerome talking the whole way.

There are a whole bunch of pictures in Andy’s flickr set for this year.

Granite Mon 2010

:: granitemon

As I write this, the 2010 Granite Mon is still in progress. This morning at 6:00 AM, a hardy crew of four swimmers and a nearly-as-hardy crew of chase-boaters gathered at the KYC dock, and on a spectacularly sunny and smooth day set out on one of the warmest swims on record. Andy—the only one without a wet suit—exited the water when he got too cold, but Charlotte, Justin, and John stuck really close together for a near-simultaneous arrival at the Becton’s dock.

Andy has a bunch of pictures in his flickr set of the swim. This is one of the whole crew, before the swim:

Pre-swim 2010

Pre-swim 2010

Participants:

  • John Clements
  • Charlotte Clews
  • Justin Pollard
  • Andy Wanning

Later, Andy wrote:

As for this year - successful! JP, Charlotte and I completed the 108.7 mile trek to Medway, Maine, arriving at 11:15pm. In a slight twist, we elected to do the last part along the route where Ben got lost all those years ago - a little longer, but flatter. Unfortunately, pretty bumpy, which was a quite literal pain in the ass in the dark, but still not bad. A big shout out to Charlotte’s mom Retta, who met us twice for food drop offs etc. during the ride, and Charlotte’s husband Jerome, who met us at the hotel with all our gear and supplied transportation from then on.

So this year was nigh-luxurious (though mitigated by the late bike arrival) in that one can now register online for a parking permit - so we just had to arrive in the parking lot by 7:30am to claim our space. So we got up at 5:30 rather than 3:45 or 4:00, and made our way to the Roaring Brook parking lot. Then we made a rather quick ascent & descent, since we had started later (8:00) and had to return by 5:00 so that Charlotte & Jerome could get back to their kids. We were definitely feeling the ol’ legs and knees (hell, I’m still feeling my thighs) after that bike ride, but all in all it wasn’t so bad and we conquered that darn mountain.

So…huge congrats to Charlotte for being the first Granite Maiden/Lady/Beef (Jamaican term) to do the whole thing. I almost did it too, but as you pointed out I had to bail halfway through the swim for cold / lack of a wetsuit. Oh well. And JP did the whole thing for the 3rd or 4th time (?)

Here’s a picture from his flickr set of the climb:

Climbers at top of Katahdin

Climbers at top of Katahdin

Granite Mon 2009

:: granitemon

John Clements swam early and short with Erin Taylor from Jim Point (on Long Island) across to High Head (on the South Blue Hill shore).

Wing took pictures. Here’s one of them:

Erin and John swimming

Erin and John swimming

About his Granite Mon experience, Andy writes:

Hurricane Billy was threatening to hit shore on the morning of August 29, but nevertheless Justin and Andy roused themselves and met chaseboater Steve Neuhauser at the KYC in the early morn. Unfortunately, Billy started making his presence known quite stridently as they arrived, silencing even JP’s indomitable will to swim, so they gave up and went back to bed. The end.

Andy and Justin, victorious

Andy and Justin, victorious

Granite Mon 2008

:: granitemon

I think this was the year of the gorgeous fogged-in tour of the harbor, wherein it was deemed far too dangerous to try to make it to Long Island, much less back again. The water was incredibly warm, though, so we settled for a swim out to the nun, then over to Lappahanink, then to one of the inner-harbor cans, then back to the KYC.

Participants:

  • Mark Read
  • Ben Walker
  • John Clements
  • Andy Wanning (Andy, can you confirm this?)
  • Justin, were you there?

Andy Writes:

For ’08, yes I was there, but I was just a chaseboater, along with Lena (Kulikova) and Mark’s friend Will Etundi. There was no other biking/climbing that time, ’cuz it was Matt’s wedding day. JP might have done that stuff later on in the year though.

Ed. Note: Andy’s notes and my notes don’t exactly line up. It’s probably me that’s wrong.

Granite Mon 2007

:: granitemon

Pictures taken by Andy, in the dining room at East Egg:

One at East Egg

One at East Egg

Another one at East Egg

Another one at East Egg

There’s more, in this Flickr set.

Andy writes:

For ’07, we had a successful swim in the rain. Swimmers: me (Andy Wanning), Mark Read, Oliver Grantham, JP, Ben Walker, I think Jerry (Millay). Chaseboaters/others: Oliver’s wife, his friend Sean or something, Sean(?)’s wife, and David who made the brunch. I’m not sure if the others did the bike ride, because Lena & I stayed in BH and then saw mom’s play, and then arrived in Medway at 1:30 am, got 2 hours of sleep, and then climbed Katahdin with everyone. So yeah, successful Katahdin climb too that year.

Granite Mon 2006

:: granitemon

Post-swim 2006

Post-swim 2006

Swimmers, from left:

  • Andy Wanning
  • Mark Read
  • Ben Walker
  • Justin Pollard
During the Hike up Katahdin

During the Hike up Katahdin

It looks like Jerry Millay was also there for the hike, and probably took this picture.

There are a whole bunch more photos, all taken by Andy, in this Flickr set

Granite Mon 2005

:: granitemon

swimmers 2005

swimmers 2005

Swimmers, from left to right:

  • Oliver Grantham
  • Andy Wanning
  • Ben Walker
  • Justin Pollard
swimmers and crew 2005

swimmers and crew 2005

More pictures from Andy in this flickr set.

Andy writes:

Essentially, it was mostly a wash, primarily because of the 70% chance of thunderstorms we heard about for Katahdin after finishing the swim. We realized that with those odds, as JP put it “this would create a 99% chance that the park rangers wouldn’t allow us up Katahdin tomorrow”. So that would’ve been a lot of effort (and expense, a la hotel & food) for naught.

Then it was also raining sporadically as we sat around JP’s pondering what to do next, and after the swim, which I will explain below, we were pretty beat of course and not exactly psyched to ride bikes in the rain. However, we were still sort of psyched to bike around the peninsula. At the same time, Hillary was going to go hiking in Acadia and was encouraging us to come with her instead, so this was the other option. Unfortunately for the bike option, one of Oliver’s bikes, the one I was going to ride, had a hole in the inner tube - so he patched that one, and found another hole, so he patched that one, and found another hole, and so on, and after 4 holes it was getting late and that basically broke the ol’ camel’s back. So it was off to hiking in Acadia.

Which was actually quite nice of course - we hiked around Jordan Pond, up a mountain in the fog, and back down and around to the Jordan Pond restaurant, forget the name, and had a really nice meal there. So that all worked out pretty well.

But of course I digress. The swim was, in two words, f*cking freezing. I would have given my left nut (and very nearly did to frostbite) to have that nifty little wetsuit of yours. Just getting out of the boat at Long Island and picking around the rocks & seaweed while applying generous amounts of Crisco was pretty miserable, though fun in a we’re-totally-nuts kind of way. And wading into the water was more or less hellacious, and then diving in the water was ridiculous, especially the freezing-the-face part. I thought eventually my face would numb up like the rest of my body, but it never really did, so I would just do a few crawl strokes and then do the side- and backstrokes and try to keep my face out of the water. And that was after I had summoned over Sara to give me the can of Crisco so I could re-apply the stuff on my face.

So I lasted maybe half an hour, as did Justin, as the other skinny guy. I got into Sara’s boat, and my teeth started chattering a few minutes later, and didn’t stop until we got to the Becton’s dock another hour or whatever later. So, just in case you were wondering, we were F*CKING COLD. When we caught up to Oliver, who was churning along at about 2 knots I suppose and leading the pack of 2, he yelled to us “how much further ‘til the dock?”, and after we told him, he added “My goodness it’s cold”.

So he finally dragged his numb body onto the dock and sat there shivering for a while, but was very pleased of course to have actually finished. Maybe 15 or 20 minutes later, Ben calmly ascended the ladder, and when asked about the temperature, said “Actually I got used to it pretty fast”, not kidding a smidgeon. This provided much mirth and shocking disbelief for our band of swimming, well, supporters, which at this point consisted of Nate, Hillary, Howie, Vicki, Cuzco the dog, and Hillary’s dog as well as the aforementioned Sara and JP and me.

So we called it a job well done and proceeded to our respective showers, and then onto JP’s house for an absolutely delectable brunch consisting of scrambled eggs, toast with pesto spread, blueberries, and some other stuff which I can’t remember. Abby and The Judge joined us at JP’s, the latter positing the whacky idea of initiating a bike ride that mere mortals might actually be interested in, such as biking around the peninsula and then hiking up Blue Hill, making it a peninsula event that area people & businesses might like to join and/or sponsor. It was also suggested that we start a non-profit associated with the event called “Bibles for Guantanamo” and donate any proceeds to a fund of that purpose in order to show our compassion for the prisoners being abused in our name there. However, some may not exactly see the sarcastic nature of such an endeavor, so it may be shelved in favor of a more broadly inclusive goal such as “Weed for the Fatigued” or something like that (actually this just popped into my head, so should not be considered an official proposal of any sort). No, actually there were a few more suggestions, but I don’t remember them, so we’ll need some help remembering or fresh inspiration if we are to come up with a theme for any fundraiser we may want to endeavor next year.

Anyway, then we proceeded to the 3-hour council meeting in the driveway, an experience perhaps more painful than diving into the Blue Hill Bay water, and you know the rest.