The Identity of an Array

| Comments

Given two arrays of integers: a and b, what does (a == b) mean?

Most of us will say, it depends on the programming language you are using, and that is true. In the case of C, it means a and b are the pointers to the start of the same array. In Java and C# it means the arrays are Reference Equal, basically meaning that the pointer to the arrays a and b are the same.

But how is that really useful? If you passed around these two arrays and checked if the references are the same, you are really just comparing the object with itself or not. The object is never going to be equal to anything else but itself. Even another array created in a different part of the program that has exactly the same integers in it in the same order will be a different array.

One would agree that [1, 2, 3] and [1, 2, 3] are the same but if these two arrays were created at different places the == operator will return false. Why is the extra information that the arrays are made in two different places important to the == operator? It makes no sense when you look at arrays as a list of numbers, and not a pointer into memory, which is an implementation detail.

So if we were to write a better == operator for arrays, what would it be?

1
2
3
4
5
6
7
[1,2,3,3] == [1,2,3,3] -> true
[1,2,3,3] == [3,2,1,3] -> ?
[1,2,3,3] == [1,2,3,1,2,3] -> ?
[1,2,3,3] == [2,3,1] -> ?
[1,2,3,3] == [1,2,3] -> ?
[1,2,3,3] == [1233] -> ?
[1,2,3,3] == "1233" -> ?

We suddenly see that the array’s identity changes depending on what you are using it for. Let us replace those question marks with context.

1
2
3
4
5
6
7
[1,2,3,3] == [1,2,3,3] -> true if the array is an ordered list
[1,2,3,3] == [3,2,1,3] -> true if the array is an unordered list
[1,2,3,3] == [1,2,3,1,2,3] -> true if the array is a bag of numbers
[1,2,3,3] == [2,3,1] -> true if the array is an unordered set
[1,2,3,3] == [1,2,3] -> true if the array is an ordered set
[1,2,3,3] == [1233] -> true if the array is a collection of digits
[1,2,3,3] == "1233" -> true if the array is a string

The array is too general. It is actually really important that we know what the array represents. A whole bunch of things are arrays but are passed around as plain arrays. This causes maintainer programmers to wonder what exactly they represent.

Some cite performance reasons. Fair enough, passing a struct that contains an array and a set of methods to manipulate it in C is slower than just passing the struct around. C is a very low level language where implementation detail really mixes with requirements.

But why is it that so many other modern languages have this problem? Why isn’t there a language that treats a set like a set, and has a compiler that really just compiles it down into an array?

Interestingly enough, this problem does not stop at arrays. Numbers have different meanings in different contexts too. Take for example the function Math.Cos in C#. This function takes a double in radians.

Why is it that the function signature is

1
double Cos(double angle)

and not

1
ratio Cos(radians angle)

where radians is just a type of double?

If the compiler was fed this information the first place, programs like this:

1
2
degrees a = 40;
ratio r = Cos(a);

will cause the compiler to either throw a compiler error for passing degrees into a radians parameter, or generate code to convert degrees to radians.

Exceptionless Programming

| Comments

I am a big fan of writing code that generates no runtime errors. This is an implementation of a function that takes the first element of an array.

1
2
3
4
5
6
template<typename T>
T first(std::vector<T> array)
{
  if (array.size() == 0) { throw EmptyArrayException(); }
  return array[0];
}

This is the standard way we are taught to write programs. However, there is another way to write this function that makes the exception unneccessary.

1
2
3
4
5
6
7
8
9
template<typename T>
void first(std::vector<T> array, std::function<void(T)> returnCallback)
{
  if (array.size() == 0)
  {
      return;
  }
  returnCallback(array[0]);
}

Suddenly the program flow is dictated by the emptiness of the array. Besides knowing well that this function will never throw an exception, the program that uses it will be structured in such a way that you can guarantee within the scope of the returnCallback function that was passed in, the function will always have the first element of the array. You don’t even have to check for nullness.

1
2
3
4
5
6
7
8
9
10
11
12
13
int calcCheckSum(std::vector<int> sorted)
{
  int result = 0;
  first<int>(sorted, [&amp;](int a)
  {
      int& res = result;
      last<int>(sorted, [&amp;](int b)
      {
          res = a + b;
      });
  });
  return result;
}

And as a perk, if this function compiles, you know that it will run without errors.

Nice Visualization of a Cross Product

| Comments

[This] is a very nice Java applet that allows you to see what a cross product is. The applet shows A x B = C (i.e. C is the cross product of A and B) in 3 dimensions. You can drag A and B around to see the effects on C.

This UI is an example of very good interactivity. It makes it very easy for the user to see what happens and since its interactive, the feedback is instant and a little playing around with it gives the user an intuition of how cross products work (not how they are done).

Instead of a program where you enter numbers, dragging arrows around give you a much better view of whats going on, and allows the user to relate a real-life situation to it better too.

Blurred Text Effect

| Comments

I came across a strange site that blurred the text of answers and asked the reader to sign up to see it. It was strange because the text was already there, just blurred via css. I thought it was an interesting little snippet so I decided to record it.

This is how it blurs.

1
2
3
4
.blurred_text {
  color: transparent;
  text-shadow: 0 0 7px #777;
}

Regex Escapees

| Comments

Sometimes when one writes regexes, Its hard to know what needs to be escaped and what doesn’t. But I’ve solved that problem while I was writing a program that generated my regexes for me. Now I have a snippet that both humans and programs can use to write regexes!

This is for the .NET flavor of Regex.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private static string SanitizeToken(string token)
{
  return token
                  .Replace(@"\", @"\\")
                  .Replace("*", @"\*")
                  .Replace("?", @"\?")
                  .Replace("+", @"\+")
                  .Replace("{", @"\{")
                  .Replace("}", @"\}")
                  .Replace("^", @"\^")
                  .Replace("$", @"\$")
                  .Replace(".", @"\.")
                  .Replace("[", @"\[")
                  .Replace("]", @"\]")
                  .Replace("(", @"\(")
                  .Replace(")", @"\)")
                  .Replace("|", @"\|");
}

Making Color From Alpha, Hue, Saturation and Brightness

| Comments

I find myself wanting to do this sometimes, so here’s a bit of code (I’m sure I stole this code from somewhere else);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public static Color ColorFromAhsb(int a, float h, float s, float b) {

  if (0 > a || 255 < a) {
      throw new ArgumentOutOfRangeException(&quot;a&quot;);
  }
  if (0f > h || 360f < h) {
      throw new ArgumentOutOfRangeException(&quot;h&quot;);
  }
  if (0f > s || 1f < s) {
      throw new ArgumentOutOfRangeException(&quot;s&quot;);
  }
  if (0f > b || 1f < b) {
      throw new ArgumentOutOfRangeException(&quot;b&quot;);
  }

  if (0 == s) {
      return Color.FromArgb(a, Convert.ToInt32(b * 255),
          Convert.ToInt32(b * 255), Convert.ToInt32(b * 255));
  }

  float fMax, fMid, fMin;
  int iSextant, iMax, iMid, iMin;

  if (0.5 < b) {
      fMax = b - (b * s) + s;
      fMin = b + (b * s) - s;
  } else {
      fMax = b + (b * s);
      fMin = b - (b * s);
  }

  iSextant = (int)Math.Floor(h / 60f);
  if (300f <= h) {
      h -= 360f;
  }
  h /= 60f;
  h -= 2f * (float)Math.Floor(((iSextant + 1f) % 6f) / 2f);
  if (0 == iSextant % 2) {
      fMid = h * (fMax - fMin) + fMin;
  } else {
      fMid = fMin - h * (fMax - fMin);
  }

  iMax = Convert.ToInt32(fMax * 255);
  iMid = Convert.ToInt32(fMid * 255);
  iMin = Convert.ToInt32(fMin * 255);

  switch (iSextant) {
      case 1:
          return Color.FromArgb(a, iMid, iMax, iMin);
      case 2:
          return Color.FromArgb(a, iMin, iMax, iMid);
      case 3:
          return Color.FromArgb(a, iMin, iMid, iMax);
      case 4:
          return Color.FromArgb(a, iMid, iMin, iMax);
      case 5:
          return Color.FromArgb(a, iMax, iMin, iMid);
      default:
          return Color.FromArgb(a, iMax, iMid, iMin);
  }
}

OpenGL Weirdness

| Comments

Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    dispList = glGenLists(1);

    glNewList(dispList, GL_COMPILE);

    for(int x=-768; x<768; x++)
    for(int y=-768; y<768; y++)
    {
        glBegin(GL_QUADS);
            glColor3f((double)(rand() % 100) / 500,
      (double)(rand() % 100) / 100, 0);
            glVertex3i((x)  * 1, (y)  * 1, 0);
            glVertex3i((x+1)* 1, (y)  * 1, 0);
            glVertex3i((x+1)* 1, (y+1)* 1, 0);
            glVertex3i((x)  * 1, (y+1)* 1, 0);
        glEnd();
    }

    glEndList();

and render your display list. Then, try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    dispList = glGenLists(1);

    glNewList(dispList, GL_COMPILE);
    glBegin(GL_QUADS);    // <- the subtle difference is here

    for(int x=-768; x<768; x++)
    for(int y=-768; y<768; y++)
    {
            glColor3f((double)(rand() % 100) / 500,
      (double)(rand() % 100) / 100, 0);
            glVertex3i((x)  * 1, (y)  * 1, 0);
            glVertex3i((x+1)* 1, (y)  * 1, 0);
            glVertex3i((x+1)* 1, (y+1)* 1, 0);
            glVertex3i((x)  * 1, (y+1)* 1, 0);
    }
    glEnd();        // <- and here

    glEndList();

I don’t know if its my graphics drivers, or graphics card or perhaps just me, but the top code maxed out at 60 fps while the bottom code ran at ~10 fps.

Oh, and don’t ask me why I’m using display lists.

Help! GLEW Compiles but Doesn’t Link!

| Comments

When you compile the glew_static project from glew on Visual Studio and link it with your program, you may get something like this:

1
2
3
4
5
app_init.obj : error LNK2001: unresolved external symbol __imp__glewInit
worldscene.obj : error LNK2001: unresolved external symbol __imp____glewBufferSubData
worldscene.obj : error LNK2001: unresolved external symbol __imp____glewBufferData
worldscene.obj : error LNK2001: unresolved external symbol __imp____glewBindBuffer
worldscene.obj : error LNK2001: unresolved external symbol __imp____glewGenBuffers

It’s easy to fix. Just add GLEW_STATIC to the preprocessor definitions and you’re done. This is because without it the header specifies dllimport instead of just extern, which is needed for static linkage.

Why I Prefer Initialization Through Constructors

| Comments

I always prefer initializing my class through the use of a constructor like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Apple
{
  Color color;
  Taste taste;
  List<string> countries;
  public Apple(Color color, Taste taste, List<string> countries)
  {
      this.color = color;
      this.taste = taste;
      this.countries = countries;
  }
}

void DoSomething()
{
  List<string> grownInCountries = new List<string>();
  grownInCountries.Add("Japan");
  grownInCountries.Add("New Zealand");
  grownInCountries.Add("Poland");
  Apple a = new Apple(Color.Green, Taste.Sweet, grownInCountries);
  DoStuffWith(a);
}

Instead of

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Apple
{
  public Color color;
  public Taste taste;
  public List<string> countries;
}

void DoSomething()
{
  Apple a = new Apple();
  a.Color = Color.Green;
  a.Taste = Taste.Sweet;
  a.GrownInCountries = new List<string>();

  a.GrownInCountries.Add("Japan");
  a.GrownInCountries.Add("New Zealand");
  a.GrownInCountries.Add("Poland");
  DoStuffWith(a);
}

To some, the answer seems obvious: constructors enforce filling in all the fields. To others, it may look like a waste of time and increased complexity to apply the boilerplate: less code = less bugs. Either that or the argument is the class becomes less serialization-friendly.

The C# compiler (or C++ or Java compilers for that matter) provide us with Constructors as a facility to make sure that our fields are all initialized upon construction, besides being able to imply the size required for the data structure to the new operator.

The second example also means that encapsulation has been broken and other classes are free to access the variables within Apple (causing coupling). We could argue that the readonly qualifier could be applied to them, but the “countries” field is a reference type and still modifiable via its own methods.

This way you can avoid forgetting to initialize a certain set of fields. Whenever a field is added, the constructor should also get a parameter, forcing the implementor to ensure that all places that create instances of this class will have to initialize the new field, eliminating a whole class of bugs related to uninitialized variables.

First Test Post From BunnyBlogger

| Comments

Several moons ago I have been working on a special blogging client, that is capable of wysiwyg blogging and allows you to take snapshots from movie files and add images and other images to your post.

I have only shown this to a few people. But now, I’ve been working to bring it up to user-standard so everyone can benefit from a way to blog quickly, without the waiting for image upload messing with your writing flow.

This is a test post from that very program, entering its beta.