luni, 30 ianuarie 2012

JMultiLocGMap - joomla module that I created

I created a new Joomla module.
It's purpose is to display a Google map with multiple locations on it.
Locations can be grouped into categories.
All configuration is done through an xml inside it.
The module was born out of necessity. I needed it for a website I was working on for my father.
The requirement was to have a select box and a Google map and upon changing the selected item in the selection box a new set of Google markers would appear on the map.
For example the selection box could have "schools" and "kintergardens" and when modifying the selection to "schools" the schools on the map would be displayed and when choosing "kintergardens" the kindergartens on the map would be displayed.
The xml handles all configuration for this.
This is the first version(0.0.1). The project is still in it's infancy of course.
Hope it helps someone. Everything is open-source.
The project is hosted here.

sâmbătă, 5 martie 2011

Merging 2 word 2007(open xml) documents

I had to merge 2 word documents once.
They were both in word 2007(open xml) format.
I searched online everywhere for a way to merge them.
All the links were about using the altChunk element. I have a problem with that. The entire document gets put in there and that is a lot of unneeded data(like header and footer etc.).

So I did it another way:
1. cloned the second document's body
2. cloned all it's children and added them to the first document's main part


Radix tree implementation in C#

Update: you can find the entire source code here 

This is my implementation of a radix tree data structure. This is a data structure very similar to a trie. Basically instead of having each one of your nodes hold a char you have each one of your nodes store a string or as wikipedia puts it, it is a trie where "each node with only one child is merged with its child".
As I understand it the names radix tree and crit bit tree are only applied to trees storing integers and Patricia trie is retained for more general inputs.
As usual I am outputting the code for each class.

First, the node:

Then the actual tree
And the test program:

sâmbătă, 26 februarie 2011

PHP sitemap using a tree data structure

I was once asked to create a sitemap given a specific table structure.
This was the structure(the table):




The resulting sitemap as I understood it was supposed to look something like this:



The parenthesis after the titles actually hold the value of the id of the node so they weren't in the original design but whatever.

So I implemented this in PHP using a tree data structure where each node held 3 pieces of information:
id - the current node's id as read from the database
parent_id - the current node's parent id as read from the database
title - the title

A real-world implementation would also have to include a link and print out elements but this is good enough for educational purposes.






CREATE TABLE IF NOT EXISTS `sitepages` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_parent` int(11) unsigned NOT NULL,
`title` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

INSERT INTO `sitepages` (`id`, `id_parent`, `title`) VALUES
(1, 0, 'Home'),
(2, 1, 'Page 1'),
(3, 0, 'Page 2'),
(4, 1, 'Page 3'),
(5, 3, 'Page 4'),
(6, 1, 'Page 5'),
(7, 4, 'Page 6'),
(8, 7, 'Page 7'),
(9, 5, 'Page 8');


I used to php classes to represent the tree. One to represent a node of the tree and one for the tree itself:

This is for the node:



//a node of a tree that is not binary, each nodes has multiple subnodes
class Node
{
public $id;
public $id_parent;
public $childnodes;
public $title;

public function __construct($id,$id_parent,$title)
{
//in the test script I use mysql_fetch_object which populates the members and then //calls the constuctor with all
//parameters as null
//the following checks are here to make sure no members are accidentally overriden

if(!isset($this--->id))
$this->id = $id;

if(!isset($this->id_parent))
$this->id_parent = $id_parent;

if(!isset($this->title))
$this->title = $title;

if(!isset($this->childnodes))
$this->childnodes = array();
}

//check if this node has subnodes or not
public function hasChildren()
{
if($this->childnodes === null)
return false;

if(!isset($this->childnodes))
return false;

if(count($this->childnodes) == 0)
return false;

return true;
}
}



This is the tree:


require("node.class.php");
class Tree
{

private $root;

//construct a tree with only one root node public function __construct()
{
$this->root = new Node(0,0);
}

//recursively traverse the tree looking for the appropiate parent id
//the node to be inserted($node) must have a parent id equal to one of the ids in the nodes of the tree
//if such an id is found in a node the node is included as a subnode of that node
private function addNodeRecursive(&$nextnode,$node)
{
if($nextnode == null)
return false;

if($nextnode->id == $node->id_parent)
{
array_push($nextnode->childnodes,$node);
return true;
}
else
{
if($nextnode->hasChildren())
{
foreach ($nextnode->childnodes as $childnode)
{
$this->addNodeRecursive($childnode,$node);
}
return false;
}
else
return false;
}
}

//add a node to the tree
//check if it is a valid node start at the root and recursively traverse the tree looking for the appropiate parent id
public function addNode($node)
{
if(!($node instanceof Node))
return;

$nextnode = $this->root;

$this->addNodeRecursive($nextnode,$node);
}

//print the tree in sitemap style
public function printSiteMap()
{
foreach($this->root->childnodes as $childnode)
{
$this->printTree($childnode,0);
}
}

//print the tree recursively
//first print the current node's value and then print the cycle through it's subnodes
public function printTree(&$nextnode,$level)
{
$this->printNodeAccordingToLevel($nextnode,$level);

if($nextnode->hasChildren())
{
foreach($nextnode->childnodes as $childnode)
{
$this->printTree($childnode,$level+1);
}
}
}

//add the appropiate number of before each node value so as to obtain a nicely formatted sitemap
//each node value is indented with respect to the node's level
private function printNodeAccordingToLevel($node,$level)
{
for($i=0;$i<$level;$i++) echo " "; echo $node->title."(".$node->id.")";

echo "
";
}

}





And this is a test just to make sure everything works:



require("tree.class.php");
$tree = new Tree();
if(!($conn = mysql_connect("localhost","root","")))
{
echo "Could not connect";
exit;
}
if(!mysql_selectdb("sitemap"))
{
echo "No such database";
exit;
}
if($result = mysql_query("SELECT id,id_parent,title FROM sitepages"))
{
while ($page = mysql_fetch_object($result,'Node'))
$tree->addNode($page);
}

$tree->printSiteMap();

miercuri, 23 februarie 2011

Prefix tree(trie) implementation in F#

Update: all the code can be downloaded from here

This is my implementation of a a prefix tree data structure in F#.
This structure is described in more detail in a previous post of mine.
I just describe the F# related stuff in the comments.
I'm sure this is not the best implementation. I am aware this is not the most functional implementation ever.
I am open to all constructive criticism of course.

luni, 31 ianuarie 2011

Autocomplete functionality using prefix trees

Update: all the source code for this article can be accessed from here

This is a follow up to my previous post on implementing a prefix tree in C#.
This expands on that idea and uses the data structure to generate a list of autocomplete matches for a given input character sequence.
These are the modifications I brought to the code:





This is the main program:

duminică, 30 ianuarie 2011

Implementation of the prefix tree(trie) data structure in C#

Update: all the source code for this article can be accessed from here

A prefix tree is a data structure with many uses. I won't go into details about this data structure Wikipedia does a better job than I could. What I will do is post a C# implementation of this data structure and in a following post I will show how to use this data structure when implementing an auto-complete solution.

These are the properties of a prefix tree:
  • it stores strings(finite sequences of chars)
  • it had nodes :) (doooooooh!)
  • each node stores a character
  • each node can have a number of subnodes(0,1,2, ... infinity)
  • the char stored in a subnode is the one following the char stored by the node in a string
  • each node has a flag which is raised if a word ends there(this doesn't mean it can't have subnodes for example a prefix tree can store the words "cat" and "catastrophy" in this case node 3 stores 't' and has a subnode that stores 'a')

And now the code:
The node:



The tree:



The main program:

The Trie: A Neglected Data Structure

From the very first days in our lives as  programmers , we’ve all dealt with data structures: Arrays, linked lists, trees, sets, stacks and...