RB_JSFiddle_A10

Indexes / Hashing / sdbm

by Ryan Brown

HTML

<form id="formski">
<div id="CreatedList"> 
</div>
<br/>
<div id="Hashed">
</div>
<br/>
<div id="Sorted">
</div>
</form>

CSS

#formski
  {
  font-family: courier new;
  }

JavaScript

function Node(content, contentB)
	{
  	this.content = content;
  	this.contentB = contentB;
  	this.next = null;
  	this.previous = null;
	}

function IndexNode(source, element)
	{
 		this.source = source;
 		this.content = element;
	}

function List()
	{
  	this._length = 0;
  	this._head = null;
  	this._tail = null;
	}

List.prototype.pushFront = function(content, contentB)
	{
		var node = new Node(content, contentB, source)
    if (this._length == 0)
    	{
       	this._head = node;
        this._tail = node;
       } 
    else
     	{
        this._tail.next = node;
        node.previous = this._tail;
        this._tail = node;
      }        
        this._length++;
 		return node;
	};
	
List.prototype.hash = function(key)
	{
  	key += store.toString().charCodeAt(store[i+1]);
    var  hash = key;
    hash = key + (hash << 6) + (hash << 16) - hash;
    if (hash < 0 )
    	{
        hash = 10 + hash;
    	} 
  		hash = hash & hash;
    return hash;
  };
  
/* This previous hashing algorithm was created for sdbm (a public-domain reimplementation of ndbm) database library. See comments at the end of this program. */
   
   
List.prototype.print = function()
	{
  	var string = ' ';
  	var current = this._head;
  	while (current)
    	{
    		string += current.content + " | ";
    		current = current.next;
  		}
  	return string;
	}
  
var list = new List();
var store = [];
var key = 0;
var count = 0;
var object = new IndexNode(source);
for (var i = 0; i<10; i++)
	{
  	store[i] = Math.floor(Math.random()*100);
  	count++;
  	var source = 3+"ab" + count;
  	var element = list.hash(store[i]) ;
		list.pushFront(store[i], element);
   	object[source] = element;
  	store[i] = element + " > " + store[i] + "<br/>";
  store.sort();
}

document.getElementById("Hashed").innerHTML = "These are the objects in comparison to their original values<br/>" + list.print() + "<br>" + JSON.stringify(object);
document.getElementById("Sorted").innerHTML = "These are the sorted array's...