Listing 12 A "fast" implementation for the protocol-based lns defined in Listing 11

//
// lns4.cpp - line number sequence
// implementation
//
#include <stdio.h>

#include "lns.h"

class lns_fast : public lns::protocol
   {
public:
   lns_fast(unsigned n);
   ~lns_fast();
   void add(unsigned n);
   void print();
private:
   struct node;
   node *first, *last;
   };

struct lns_fast::node
   {
   node(unsigned n);
   unsigned number;
   node *next;
   };

inline lns_fast::node::node(unsigned n)
   : number(n), next(0)
   {
   }

inline lns_fast::lns_fast(unsigned n)
   {
   first = last = new node(n);
   }

lns_fast::~lns_fast()
   {
   node *p;
   while ((p = first) != 0)
      {
      first = first->next;
      delete p;
      }
   }

void lns_fast::add(unsigned n)
   {
   if (last->number != n)
      last = last->next = new node(n);
   }

void lns_fast::print()
   {
   node *p;
   for (p = first; p != 0; p = p->next)
      printf(" %4d", p->number);
   }

lns::lns(unsigned n)
   {
   pp = new lns_fast(n);
   }
lns::protocol::~protocol()
{}
// End of File