Skip to main content

Our first list.

For our list of primes we need a list (naturally).  The best way to understand data structures is to implement them in a low level language.  Since I don't feel like inflicting ASM on everyone (although I do like assembler) we will use the next best thing.  C

Out list needs a data element (the found prime) and a pointer to the next element.
In C that looks like this.

struct prime_elem_s {
  uint64_t prime;
  struct prime_elem_s *next;
};
typedef struct prime_elem_s prime_elem_t;

We define a structure containing the current prime and a pointer to the next element.  Then we define a type prime_elem_t to refer to that structure.

In this case we want to add elements to the end of the list so we will also need a tail pointer.  We will create a list header which will keep a head and tail pointer as well as the number of elements in the list.

typedef struct {
  uint64_t length;
  prime_elem_t *head;
  prime_elem_t *tail;
} prime_list_t;

Now we can write a function which appends a new prime to the list.

void push_prime(prime_list_t *prime_list, uint64_t prime)
{
  prime_elem_t *elem = malloc(sizeof(prime_elem_t));

  elem->prime = prime;
  elem->next = NULL;

  if( prime_list->tail )
    {
      prime_list->tail->next = elem;
      prime_list->tail = elem;
    }
  else
    {
      prime_list->head = prime_list->tail = elem;
    }
  prime_list->length++;
}

Here we:
  1. allocate a new element
  2. initialize its fields
  3. if this isn't the first element then append it to the list's tail then update the tail pointrewolf
  4. otherwise set both the head and tail to the new element
  5. increment the list's length

Comments

Popular posts from this blog

Duck typing considered harmful.

I've had a chance to work on a fairly large chunk of Python at this point and have decided that Python (like Perl) is completely untenable at scale.  <rant><rave><drool>  but wait!  I have reasons! Programmers spend most of their time reading code.  We read massive amounts of code.  After we read massive amounts of code we write one... or change one... To make a change to a piece of code you first have to understand what it does and how it interacts with the system around it.  Then we start reading massive amounts of code again.  Anything you can do to minimize the amount of code a programmer has to understand to make a change becomes a huge gain in productivity. Duck typing causes the amount of code you need to read to make a change to grow very large. For example lets look at two functions, one in C++ and one in Python. First in C++ int function(type1 arg1, type2 arg2) {   return arg1->method(arg2); } In this fun...

How big are primes?

So, If we were to calculate the first billion primes how much ram would we need? At the end the primes won't fit in a 32bit integer.  If we store them all in 64bit integers we'd need 8GB. Even if they all fit in 32bit integers we'd still need 4GB of storage just to store the list. Seeing at my little machine has 4GB ram that isn't going to work either. How about just storing the intervals between primes? For the first primes (2, 3, 5, 7, 11, 13, 17) we would store (2, 1, 2, 2, 4, 2, 4) Using a byte for every interval less than 128 and a short for the rest lets us store the first billion primes in just about 1GB.  Now that we can do! Maybe we can do even better though...
C Craft  is a great article about C including discussion on the good, the bad, and why you can't eat just one. Quote from the preface: Let me open with a disclaimer. What follows is an unabashedly opinionated diatribe certain to offend many programmers. Craftsmen inevitably grow defensive of their favourite tools and practices. Rather than respect this natural sensitivity, I will exploit it, shamelessly intensifying the vitriol for attention. I care little whether one applauds or deplores my views; I mainly want my audience to feel compelled to read on. However, I try to be informative as well as incendiary. Whether one finds my statements profound or unsound, whether one abhors or adores my examples, and whether one is amused or bemused by my observations, I hope one leaves wiser.