struct – setup and memory locations

When you setup a struct within c++, it is kinder like having a array of data and if you want to you can access the internal parts by using some memory pointer location fun!.

Lets say that you have a struct of

struct intvalue {
    int a;
    int b;
};

Just to say that since I am using int(eger) values so that is what I am incrementing by in the pointer arithmetic which is why I am casting the pointer to a integer value.

So lets say that we create a variable of intvalue and setup the values as below

intvalue testvalue;
testvalue.a = 4;
testvalue.b = 5;

We can then pull out the value of a or b, but using memcpy and just outputting to a int(eger) variable as below, the key is the ((int*)&testvalue)+1, this will first convert the testvalue variable to a pointer to memory location and then (int*) casts that pointer to a int pointer, because internally that is what it is, and then just add 1 to it, which points to the second value ( in this case the value of b which is 5)

    int avalue;
    // convert to a int pointer type and then add one to it (to the next array element as such).
    memcpy(&avalue, ((int*)&testvalue)+1,sizeof(int));
    cout << "a value (or is it the b value :) ) " << avalue << endl;

The output would be

a value (or is it the b value :) ) 5

because I am pointing to the second value which is b and thus 5 :).

Of course if you just wanted the value of first int (the value of a in this case) you do not add the 1 to the memory location, for example

    memcpy(&avalue, ((int*)&testvalue),sizeof(int));

this time I am just converting the testvalue (casting) to a int pointer and thus pointing to the start of the struct and that is where the int a variable is living :).

CS107 – Assignment 1 – Context Free Grammar – Random Sentence Generator

This is assignment 1 from a older CS107 course, because the latest course work is held on a UNIX server which in turn you need access to!!.. kinder does not help when you do not have access too!!.. Here is where I got the files from cs107 link, there is a zip file at the bottom of that page which has all of the assignments and handouts etc.

The basics of a context free grammar is (as taken from the PDF file attached which is the assignment 1 in more details), the tokens as such are the <..> parts of the block, so that you start at <start> and then you print out “The ” and then goto the <object> block to then print out a randomly picked object to then print out the <verb> if there is any blocks within the object/verb then do them before heading back to the last word on the <start> block which is “tonight”. That is it, it generates random text :).

The Poem grammar
{
<start>
	The <object> <verb> tonight. ;
}
 
{
<object>
	waves	;
	big yellow flowers ;
	slugs ;
}
 
{
<verb>
	sigh <adverb> ;
	portend like <object> ;
	die <adverb> ;
}
 
{
<adverb>
	warily ;
	grumpily ;
}

So the assignment is mainly to get used to compiling up programs within a Linux/Unix environment, I was using qt-creator IDE (which has placed some files within the zip file attached, but it does not effect the directory structure as such). To compile on Linux/Unix as long as there is a Makefile you can just

make

So, the assignment 1 base code reads in the textual files and all is required is to output the text within a random sentence and repeat 3 times, so the start of the code does the looping and calls the doRandomText function which requires as parameters the mapped (map) grammar and where to start ()

  for (int i =1;i <= 3; i++)
  {
      cout << "Version #" << i << endl << endl;
      doRandomText(grammar, "<start>");
      cout << endl << endl;
  }
  return 0;
}
 
// recursive loop in the text and produce randomly generated text
void doRandomText(map<string, Definition> theGrammar, string terminal)
{
    Definition def = theGrammar[terminal];
    assert(def.getNonterminal().size() !=0);
    Production prod = def.getRandomProduction();
    for (Production::iterator prodIter = prod.begin(); prodIter != prod.end(); prodIter++)
    {
        string theText = *prodIter;
        if (theText.at(0)=='<' && theText.at(theText.size()-1)== '>')
            doRandomText(theGrammar, theText);
        else
        {
            if (theText == "," || theText == ".")
                cout << theText;
            else
                cout << " " << theText;
        }
    }
}

The recursive function above basically tries to find the terminal within the grammar definitions and if there is not one, exit the code (assert), else print out the textual data whilst iterating over them, if there is any more terminals within the sentence then goto that terminal by calling this same function.

Here is the output of a run from the program.

./rsg assn-1-rsg-data/excuse.g 
The grammar file called "assn-1-rsg-data/excuse.g" contains 7 definitions.
Version #1
 
 I need an extension because my disk got erased, and I'm sure you've heard this before, but I had to make up a lot of documentation for the Navy in a big hurry, and I'm sure you've heard this before, but my printout was enshrowded in a mysterious fog for three days and then vanished, and I'm sure you've heard this before, but all my pencils broke, and just then I had 7 programs in like, a billion different langauges, and if you can believe it, I just didn't feel like working, and and then if I recall correctly I had to worry about the Winter Olympics, and and then if I recall correctly my disk got erased, and as if that wasn't enough I forgot how to write.
 
Version #2
 
 I need an extension because I got stuck in a blizzard at Tahoe, and then get this, my Mac was enshrowded in a mysterious fog for three days and then vanished.
 
Version #3
 
 I need an extension because I didn't know I was in this class, and then I just didn't feel like working, and I'm sure you've heard this before, but I thought I already graduated, and then, just when my mojo was getting back on its feet, the bookstore was out of erasers, and if you can believe it, I didn't know I was in this class, and and then if I recall correctly my dorm burned down.

Explicit – c++ what is it for ?

The explicit keyword in c++ is make sure that user of your class only creates and uses the class only creates it in the way that you was expecting and not via any references as such.

For example in the following code example

class mainClassWith {
public:
    // this basically will setup the internal_value equal to the value that is passed from the user
    explicit mainClassWith(int value) : internal_value(value) {} ;
 
    int internal_value;
};
 
int functionWith(const mainClassWith &theClass)
{
    cout << "the value with " << theClass.internal_value << endl;
}

The functionWith will take a parameter of mainClassWith of which has a explicit keyword on its constructor, this means that if we try and do something like

    mainClassWith mainWith(4);
 
    functionWith(mainWith);

then the compiler will compile and also the output would be printed to the screen of “the value with 4”, but since we know that the mainClassWith takes a integer value as a constructor and try to bypass any object creation of that mainClassWith and try and do this

functionWith(3);

then the compiler will complain because we cannot setup a mainClassWith on the fly like this and let the compiler pass a value of “3” to the class and hope that it works!!!, we have explicitly declared that the constructor must be initialised before using.

But if we did take off the explicit keyword as in the class example below.

class mainClassWithOut {
public:
    // this basically will setup the internal_value equal to the value that is passed from the user
    mainClassWithOut(int value) : internal_value(value) {} ;
 
    int internal_value;
};
 
int functionWithout(const mainClassWithOut &theClass)
{
    cout << "the value without  "<< theClass.internal_value << endl;
}

then we are able to call the new function (functionWithout) without actually setting up the object and allow the compiler to make up its own instance of that object and pass to the function.

functionWithout(5);

would work and the output would be “the value without 5” which is correct, but the compiler and created the mainClassWithout object and setup with the value of 5 via its constructor, which if there was a few different constructors etc then how would you be sure that the correct constructor was called and thus would the value that you want back from the class really be 5, it could be 0 and another value be that 5!!.

Static in c – hide that function

In the c language there is no such thing as a class, private/public setup as such, but if you have a allot of c files that may have allot of the same function names inside e.g. swap(void *data, void *data2). So how do you just want to call your swap function and not another one from another c file. Which in this setup it is similar to private method within a class.

So instead of having access to the private keyword, you can define a function to be a static function as below in this header file that I have called static_test.h

#ifndef STATIC_TEST_H
#define STATIC_TEST_H
 
#include <iostream>
using namespace std;
 
static void PrivateAccessable(char *sting);
 
void PublicAccessable(char *st);
 
#endif // STATIC_TEST_H

the PublicAccessable function is what is *exposed* to other parts of the main program but the static void PrivateAccesable is not.

So if you try and access it via the int main function like so

 
#include <iostream>
#include "static_test.h"
 
int main(int argc, char *argv[])
{
 
    PublicAccessable("hi there");
    // undefined reference to `PrivateAccessable(char*)'
    PrivateAccessable("should not be able to access this!!");
 
    return 0;
}

The compiler will complain with something similar to the error above the PrivateAccessable line, or

undefined reference to `PrivateAccessable(char*)'

because the linker does not know where that function is, since it is “hidden” within the static_test namespace as such.

Just to complete the code example here is the static_test.c

#include "static_test.h"
 
static void PrivateAccessable(char *sting)
{
    cout << sting << endl;
}
 
void PublicAccessable(char *st)
{
    PrivateAccessable(st);
}

Graphs – Part 2

The graphs use two algorithms to either find the shortest path (Dijkstra algorithm) or the minimal spanning tree (Krushal’s method).

This is the second post of the assignment of the Path class and also the Dijkstra / Krushal methods. So to start with the Path class, below is the header of the class with the different methods inside the class, which basically has a Add method to add the arc to the Path, TotalPathDistance which will return the total distance taken in O(1) in big O notation, a toString method which returns a string version of the internal data structure of the arcs added, size method which returns the size of the arcs added to the arcs internal vector array and lastly the GetElementAt method that will return a arc of a element at any given point in the vector array.

class Path {
 
// [TODO: complete the class definition]
 
public:
	Path();
	~Path();
 
/*
 * Function: Add
 * Usage: Add(arcT *arc);
 * ----------------------------------
 * Adds a arc to the internal array of arc(s)
 */
	void Add(arcT *arc);
 
/*
 * Function: TotalPathDistance
 * Usage: TotalPathDistance();
 * ----------------------------------
 * returns the total distance in a O(1) time instead of cycling through the list of arcs
 */
	double TotalPathDistance();
 
 
/*
 * Function: toString
 * Usage: toString();
 * ----------------------------------
 * converts the array of arcs to a string repenstation
 */
	string toString();
 
/*
 * Function: size
 * Usage: size();
 * ----------------------------------
 * returns the size of the internal arcs implemetation e.g. the total amount of arcs already stored.
 */
	int size();
/*
 * Function: GetElementAt
 * Usage: GetElementAt(4);
 * ----------------------------------
 * returns a arcT* at any given element index, as passed by the parameter
 */
	arcT* GetElementAt(int i);
 
private:
	Vector<arcT *> arcs;
	double totalCost;
};

and here is the functions implementation, to start with the Path class is constructed with a total distance of 0 and then every time that a new arc is added the total distance is increased by the value of the cost associated with that arc. The tostring method just loops over the vector array of arcs and builds up a string of there details.

Path::Path()
{
	totalCost = 0;
}
 
Path::~Path()
{
	arcs.clear();
}
 
void Path::Add(arcT *arc)
{
	arcs.add(arc);
	totalCost += arc->cost;
}
 
double Path::TotalPathDistance()
{
	return totalCost;
}
 
string Path::toString()
{
	string returnStr;
	foreach(arcT *arc in arcs)
	{
		returnStr = returnStr + arc->start->name + "->" + arc->finish->name + "(" + RealToString(arc->cost) + ")" +"\n";
	}
	return returnStr;
}
 
int Path::size()
{
	return arcs.size();
}
 
arcT* Path::GetElementAt(int i)
{
	return arcs[i];
}

Because the Dijkstra method was already coded from the course reader handout, here is how I have altered it to use the new Path class to have a big O notation of O(1), I have just basically altered it where it was a Vector types to be a Path type instead and called the relevant methods on that new Path type instead of using the TotalPathDistance function, also with a couple of changes to the bottom part of the function below because there was no = operator, so instead changed to GetElementAt method.

Path FindShortestPath(nodeT *start, nodeT *finish) {
	Path path;
	PQueue< Path> queue;
	Map<double> fixed;
	while (start != finish) {
		if (!fixed.containsKey(start->name)) {
			fixed.put(start->name, path.TotalPathDistance());  
			foreach (arcT *arc in start->arcs) {
				if (!fixed.containsKey(arc->finish->name)) {
					Path newPath = path;
					newPath.Add(arc);
					queue.add(newPath, newPath.TotalPathDistance());
				}
			}
		}
		if (queue.isEmpty()) return Path();
		path = queue.extractMin();
		start = path.GetElementAt(path.size() -1)->finish; 
	}
	return path;
}

and here is the Kruskal function, to start with I only need to store the set of nodes within a array (so using a vector array) with also the just needing to store the arcs that are joining the nodes together (I am using a Set here instead of a vector, but does not really matter as such). So to start with I am first building up a list of nodes (Vector array of Sets of strings) of each node within the graph, whilst also adding to a priority queue of all of the arcs, I could have used a foreach loop and gone over the getNodesSet from thePath variable, but the arcs should have all of the nodes inside it :), and since if the nodes are not attached to the arcs then how could they form a path!. And then the main loop is just going through all of the arcs in order of cost (lowest first to highest) seeing if the start and finish nodes are within the same Set within the vector array, if so they have already been added to the minimal spanning tree (Kruskal) else if they have not been added merge (union) the two sets together and also add the arc to the list of arcs to display to the screen once this function has completed.

void KruskalMethod(PathfinderGraph &thePath)
{
	Set<arcT *> arcs = thePath.getArcSet();
	PQueue<arcT *> path;
	Vector< Set<string> > nodes;
	Set<arcT *> theJoiningArcs;
 
	CleanScreen(thePath);
	// place into a pqueue and also build up a vector of sets of the different node names
	foreach (arcT *arc in arcs)
	{
		path.add(arc, arc->cost);
		bool inAlready = false;
		for (int i = 0; i < nodes.size(); i++)
		{
			if (nodes[i].contains(arc->start->name))
			{
				inAlready = true;
				break;
			}
		}
		if (!inAlready)
		{
			Set<string> newNode;
			newNode.add(arc->start->name);
			nodes.add(newNode);
		}
	}
	while (!path.isEmpty())
	{
		arcT * arc = path.extractMin();
		// start node and end nodes set id
		int startN, endN;
		startN = endN = -1;
		for (int i =0; i < nodes.size(); i++)
		{
			if (nodes[i].contains(arc->start->name))
				startN = i;
			if (nodes[i].contains(arc->finish->name))
				endN = i;
		}
		// if in different sets then
		if (startN != endN)
		{
			nodes[startN].unionWith(nodes[endN]);
			nodes.removeAt(endN);
			theJoiningArcs.add(arc);
//			cout << "Cost : " << arc->cost << " : Start : " << arc->start->name << " : Finish : " << arc->finish->name << endl;
		} /*else
			cout << "Cost : " << arc->cost << " : Start : " << arc->start->name << " : Finish : " << arc->finish->name << " ( not needed) " << endl;*/
	}
	// draw out all of the arcs on the grid in a dim color
	DrawNodes(thePath.getNodeSet(), DIM_COLOR);	
	// draw the minimum arcs in a highlight color
	DrawArcs(theJoiningArcs, HIGHLIGHT_COLOR);
}

Graphs

The graphs use two algorithms to either find the shortest path (Dijkstra algorithm) or the minimal spanning tree (Krushal’s method).

I am going to split up the assignment into 2 posts so that can try and covert as much of the assignment without spanning a massive post, so in this post going to talk about loading in the data from the textual files and also displaying the data on screen (graphics), and how I altered the sublcass to allow for more data to be stored for this assignment. In the next post it will be about the Path class implementation with the Dijkstra and Krushal algorithms.

So to start off with, I altered the type definition for the main/parent class of Graph to be called PathfinderGraphMain so that when I created the subclass I could call it the PathfinderGraph, just liked the look of that :). and since the graph types, nodeT / arcT are capable of holding all of the other data (have included them in the code below) I just need to store the graph image which is what the subclass below is doing for me, was trying to avoid using any global variables, so that is all that my subclass is additional storing and accessing using the get/set functions.

typedef Graph<nodeT, arcT> PathfinderGraphMain;
class PathfinderGraph : public PathfinderGraphMain
{
public:
	string returnJpg() { return jpgimage;}
	void setJpg(string jpg) { jpgimage = jpg;}
 
private:
	string jpgimage;
};
 
// as taken from the graphtypes.h
struct nodeT {
	string name;
	Set<arcT *> arcs;
	pointT loc;
};
struct arcT {
	nodeT *start, *finish;
	double cost;
};

here is the sub part of the method that will read in the data from the textual file, I am basically reading in the nodes to start with and then calling the AddNode function (below the code) and also the AddArc (again further down in the code below) which basically just call the main Graph class addNode / addArc functions.

if (contents=="NODES")
	{
		// end at EOF or ARCS
		while (contents != "ARCS" && !inFile.eof())
		{
			contents = ReadLineFromFile(inFile);
			// add the node into 
			if (contents != "ARCS")
			{
				// need to split the contents string
				pointT location;
				int nameI = contents.find_first_of(" ");
				location.x = StringToInteger(contents.substr(nameI, contents.find_last_of(" ")- nameI));
				location.y = StringToInteger(contents.substr(contents.find_last_of(" ")));
				AddNode(contents.substr(0,nameI), location, thePath);
			}
		}
	}
	// end point is the word ARCS
	if (contents == "ARCS")
	{
		// read till the end
		while (!inFile.eof())
		{
			contents = ReadLineFromFile(inFile);
			if (!inFile.eof())
			{
				string startP, endP;
				int nameI = contents.find_first_of(" ");
				startP = contents.substr(0, nameI);
				nameI++;
				endP = contents.substr(nameI, contents.find_last_of(" ")-nameI);
				double cost = StringToReal(contents.substr(contents.find_last_of(" ")));
				AddArc(startP, endP, cost, thePath);
			}
		}
	}
 
void AddNode(string name, pointT location, PathfinderGraph &thePath)
{
	nodeT *node = new nodeT();
	node->name = name;
	node->loc = location;
	thePath.addNode(node);
}
 
void AddArc(string startP, string endP, double cost, PathfinderGraph &thePath)
{
	nodeT *start = thePath.getNode(startP);
	nodeT *end = thePath.getNode(endP);
 
	arcT *arc = new arcT();
	arc->cost = cost;
	arc->start = start;
	arc->finish = end;
	thePath.addArc(arc);
 
	arcT *arc2 = new arcT();
	arc2->cost = cost;
	arc2->start = end;
	arc2->finish = start;
	thePath.addArc(arc2);
}

To draw to the screen, I have created two methods that will take a set of nodes (nodeT structures) and also the colour that is required to display. Since the Set class has a Iterator I can use the foreach (marco from the CS106B libraries) to loop over the Set class to then just display the nodes/arcs via the CS106B graphics libraries again.

void DrawNodes(Set<nodeT *> nodes, string color)
{	
	foreach (nodeT *node in nodes)
	{
		DrawPathfinderNode(node->loc, color, node->name);
		DrawArcs(node->arcs, color);
	}
	UpdatePathfinderDisplay();
}
 
void DrawArcs(Set<arcT *> arcs, string color)
{
	foreach (arcT *arc in arcs)
		DrawPathfinderArc(arc->start->loc, arc->finish->loc, color);
}

The last part of the graphical aspects is getting the mouse inputs to link to a node on the graphical display, once again using the CS106B libraries for obtaining a point location (x/y coordinates) and then testing to see if this coordinates have clicked on the node location (kinder putting a square around the node of the node radius in size, this NODE_RADIUS is defined in the CS106B libraries and that is what the node will have a circle radius off)

// find the nodes near the mouse cords
// NULL if not found a node, else return a nodeT
nodeT * GetNodeFromMouseCords(PathfinderGraph &thePath, pointT mouseInput)
{
	nodeT * returnNode;
	foreach (nodeT *node in thePath.getNodeSet())
	{
		pointT location = node->loc;
		if (mouseInput.x >(location.x - NODE_RADIUS) && 
			mouseInput.x <(location.x + NODE_RADIUS))
			if (mouseInput.y > (location.y - NODE_RADIUS) &&
				mouseInput.y < (location.y + NODE_RADIUS))
				return node;
	}
	return NULL;
}
 
// need to pull out two places, start and end.
void GetTwoLocationsFromMouse(PathfinderGraph &thePath, nodeT * &startN, nodeT * &finishN)
{
	pointT mouseClick = pointT();
	cout << "Please choice your starting location "<< endl;
	while ((startN = GetNodeFromMouseCords(thePath, mouseClick)) == NULL)
		mouseClick = GetMouseClick();
	cout << "Start : " << startN->name << endl;
	DrawPathfinderNode(startN->loc, HIGHLIGHT_COLOR, startN->name);
	while (true)
	{
		cout << "Please choice your ending location "<< endl;
		mouseClick = pointT();
		while ((finishN = GetNodeFromMouseCords(thePath, mouseClick)) == NULL)
			mouseClick = GetMouseClick();
		if (startN->name == finishN->name)
			cout << "That is silly, you are already there!"<< endl;
		else
			break;
	}
	cout << "Finish : " << finishN->name << endl;
	DrawPathfinderNode(finishN->loc, HIGHLIGHT_COLOR, finishN->name);
}

after the user has selected two nodes it will highlight the nodes with the HIGHLIGHT_COLOR from the CS106B libraries again (which was the colour red). I shall post next on the Path class and also the two breath-search and minimal spanning trees, (Dijkstra and Krushal’s algorithms).

Huffman coding – part 2

The huffman coding is mainly used to compress files that are not already compressed already ( the reason why I say this for is because if you are trying to compress a already compressed file then the assignment 5 will add on more header details onto the file for decompressing the compressed file.

This is a follow on from the part 1 of the assignment 5 this is the decompression of the compressed file.

So to start with, I need to read in the header file of the compressed file. For example if I was going to compress the file

hello there

then the compressed file will be

104=2|101=3|108=2|111=1|32=1|116=1|114=1|-1=1|;u