Created
March 25, 2014 12:13
-
-
Save bennadel/9760671 to your computer and use it in GitHub Desktop.
Creating A Fixed-Length Queue In JavaScript Using Arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Fixed-Length Queue Data Type In JavaScript</title> | |
<script type="text/javascript"> | |
// Create a constructor for the fixed-length queue. This is | |
// really more of a FACTORY than a construtor since an | |
// entirely tangential object is returned. | |
function FixedQueue( size, initialValues ){ | |
// If there are no initial arguments, default it to | |
// an empty value so we can call the constructor in | |
// a uniform way. | |
initialValues = (initialValues || []); | |
// Create the fixed queue array value. | |
var queue = Array.apply( null, initialValues ); | |
// Store the fixed size in the queue. | |
queue.fixedSize = size; | |
// Add the class methods to the queue. Some of these have | |
// to override the native Array methods in order to make | |
// sure the queue lenght is maintained. | |
queue.push = FixedQueue.push; | |
queue.splice = FixedQueue.splice; | |
queue.unshift = FixedQueue.unshift; | |
// Trim any initial excess from the queue. | |
FixedQueue.trimTail.call( queue ); | |
// Return the new queue. | |
return( queue ); | |
} | |
// I trim the queue down to the appropriate size, removing | |
// items from the beginning of the internal array. | |
FixedQueue.trimHead = function(){ | |
// Check to see if any trimming needs to be performed. | |
if (this.length <= this.fixedSize){ | |
// No trimming, return out. | |
return; | |
} | |
// Trim whatever is beyond the fixed size. | |
Array.prototype.splice.call( | |
this, | |
0, | |
(this.length - this.fixedSize) | |
); | |
}; | |
// I trim the queue down to the appropriate size, removing | |
// items from the end of the internal array. | |
FixedQueue.trimTail = function(){ | |
// Check to see if any trimming needs to be performed. | |
if (this.length <= this.fixedSize){ | |
// No trimming, return out. | |
return; | |
} | |
// Trim whatever is beyond the fixed size. | |
Array.prototype.splice.call( | |
this, | |
this.fixedSize, | |
(this.length - this.fixedSize) | |
); | |
}; | |
// I synthesize wrapper methods that call the native Array | |
// methods followed by a trimming method. | |
FixedQueue.wrapMethod = function( methodName, trimMethod ){ | |
// Create a wrapper that calls the given method. | |
var wrapper = function(){ | |
// Get the native Array method. | |
var method = Array.prototype[ methodName ]; | |
// Call the native method first. | |
var result = method.apply( this, arguments ); | |
// Trim the queue now that it's been augmented. | |
trimMethod.call( this ); | |
// Return the original value. | |
return( result ); | |
}; | |
// Return the wrapper method. | |
return( wrapper ); | |
}; | |
// Wrap the native methods. | |
FixedQueue.push = FixedQueue.wrapMethod( | |
"push", | |
FixedQueue.trimHead | |
); | |
FixedQueue.splice = FixedQueue.wrapMethod( | |
"splice", | |
FixedQueue.trimTail | |
); | |
FixedQueue.unshift = FixedQueue.wrapMethod( | |
"unshift", | |
FixedQueue.trimTail | |
); | |
// -------------------------------------------------- // | |
// -------------------------------------------------- // | |
// -------------------------------------------------- // | |
// -------------------------------------------------- // | |
// Create a fixed queue with some values. | |
var friends = FixedQueue( 3, [ "Sarah", "Kit" ] ); | |
console.log( "Initial Values" ); | |
console.log( friends ); | |
// Add 2 items to the head. | |
friends.unshift( "Tricia", "Anna" ); | |
console.log( "Add [ Tricia, Anna ] to head." ); | |
console.log( friends ); | |
// Add 2 items to the tail. | |
friends.push( "Kim", "Joanna" ); | |
console.log( "Add [ Kim, Joanna ] to tail." ); | |
console.log( friends ); | |
// Add explicit value outside array. | |
friends[ 5 ] = "Nancy"; | |
console.log( "Add [ Nancy ] out of bounds (explicitly)." ); | |
console.log( friends ); | |
// Add another item to the tail. | |
friends.push( "Becca" ); | |
console.log( "Add [ Becca ] to tail." ); | |
console.log( friends ); | |
</script> | |
</head> | |
<body> | |
<!-- Left intentionally blank. --> | |
</body> | |
</html> |
Thanks a lot Ben :), Works perfectly. Saved me a lot of time.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Elegant solution, thanks for sharing Ben!