Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2012
    Location
    in the uk.
    Posts
    99
    Thanks
    2
    Thanked 6 Times in 4 Posts

    Pointerless Array Rotation without use of Indexes

    In many cases the use of an 'array pointer' is literally 'pointless' ...

    A good example would be an array of elements that is nothing more than an array for banner image url's. Using a "rotate & Return" method of working would mean that you can reduce the need to "Track" the array pointer, the most current element is returned from the array whilst being preserved in the array.

    This does not stop you adding elements to the array, should you wish a new element to be the very next used, you would unshift() the element otherwise you would just push() the element before it is called.

    Code:
    // Get the first element in the array, removes it and appends it to the array and then returns the rotated element.
    Array.prototype.returnFirstAndRotate = function(){
    	var tmp = this.shift();
    	this.push( tmp );
    	return tmp;
    }
    array = [3,1,2];
    x = array.returnFirstAndRotate();
    alert(x);
    Will return the number 3 which has now been pushed on to the end of the array, the array is still 3 elements long but now is [1,2,3] until the next call of x = array.returnFirstAndRotate();

    I do have the alternative of rolling back the array, seen here...
    Code:
    // Array roll back
    Array.prototype.rollback = function(){
    	var tmp = this.pop();
    	this.unshift( tmp );
    	return tmp;
    }
    // eg.
    array = [3,1,2];
    x = array.returnFirstAndRotate();
    alert( array.join(",") );
    x = array.rollback();
    alert(x);
    alert( array.join(",") );
    Will return the last array element value and push it on to the front of the array, x will equal 3 in this example. and we end up with an array of [3,1,2]

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Here is another way:-

    Code:
    <script type = "text/javascript">
    
    rotate = function(a, p){
    for(var l = a.length, p = (Math.abs(p) >= l && (p %= l), p < 0 && (p += l), p), i, x; p; p = (Math.ceil(l / p) - 1) * p - l + (l = p))
    for(i = l; i > p; x = a[--i], a[i] = a[i - p], a[i - p] = x);
    return a;
    }
    
    x = rotate([1,2,3,4,5,6,7,8,9,10], 2);
    alert (x);
    x = rotate(x,2);
    alert (x);
    x= rotate (x,7);
    alert (x);
    
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Jan 2012
    Location
    in the uk.
    Posts
    99
    Thanks
    2
    Thanked 6 Times in 4 Posts
    Interesting.

    I have added some more prototypes that may prove useful to someone looking to manipulate arrays and obtain information or data.
    Code:
    // remove first element and return it or return false
    Array.prototype.removeAndReturnFirst = function(){
    	return this.length ? this.shift() : false;
    }
    // remove last element and return it or return false
    Array.prototype.removeAndReturnLast = function(){
    	return this.length ? this.pop() : false;
    }
    // remove an element by ref
    Array.prototype.removeAndReturn = function(ref){
    	return this.length && this[ref] ? this.splice(ref,1) : false;
    }
    // remove and append an element by ref - you might want to move element[x] to the bottom of the pile! but still return its data.
    Array.prototype.removeAndAppend = function(ref){
    	var tmp = this.length && this[ref] ? this.splice(ref,1) : false;
    	if( tmp ) this.push( tmp );
    	return tmp;
    }
    // Rotate an array of elements by the number passed and return that value.
    Array.prototype.rotateArrayBy = function(n){
    	var tmp=false, n = n || false;
    	if( n && this.length ) 
    		while( --n >= 0){
    		 	tmp = this.shift();
    		 	this.push( tmp );
    		 } 
    	return tmp;
    }
    false is returned if the array is empty, allows for testing the return value otherwise the array element / data is returned.

    The rotateArrayBy() method will remove n elements and then return the value of the n'th element not the first element in the array, eg...

    Code:
    array = [3,4,5,6,7,8,9,10,1,2];
    x = array.rotateArrayBy(6);
    alert( x );
    alert( array.join(",") );
    will result in [9,10,1,2,3,4,5,6,7,8] and the value of the n'th element was 8.

  • #4
    New Coder
    Join Date
    Jan 2012
    Location
    in the uk.
    Posts
    99
    Thanks
    2
    Thanked 6 Times in 4 Posts
    Also here is an interesting method of either or gathering of an element.
    Code:
    // remove an element by ref or rotate array ref elements and return n'th element
    Array.prototype.removeAndReturnByOrRotateToNth = function(ref){
    	var tmp=false;
    	if( this.length && this[ref] ){
    		tmp = this.splice(ref,1);
    	}else{
    		this.rotateArrayBy(ref); // the n'th will be the last element!
    		tmp = this.pop();
    	}
    	return tmp;
    }
    
    array = [1,2,3,4,5,6,7,8,9];
    x = array.removeAndReturnByOrRotateToNth(33);
    alert( x );
    alert( array.join(",") );
    Returns [7,8,9,1,2,3,4,5] and the n'th = 6.

    The function will remove the n'th element from an array regardless of n'th existing or not. Will rotate the array by n'th elements instead and remove the last element which will be the target we want as it will be the n'th element in that array.
    Last edited by Cremator; 01-29-2012 at 11:34 AM.

  • #5
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    Interesting idea. I think that the better is to shorten names of methods in your first post like these:

    Code:
    [].rotateLeft()
    [].rotateRight()
    Look here: the returnFirstAndRotate method has well-defined name but too long and wordy. The rollback method is the inverse of the previous one. Ok -- it is shorter but we don't know what we rollback and what is the direction of rolling back.

    Anyway, starting to use one or both methods we have to read documentation of both methods so the better way is to give them names looking alike and describe what do they do and what do they return.

    -------------

    Talking about removeAndDoSomething methods' family

    false is returned if the array is empty
    Use the 'undefined' value instead. Check the result of [].pop() or [].shift() to find out the 'undefined' value returned by these methods. So removeAndReturnFirst/removeAndReturnLast can be reduced until the native methods shift/pop.

    removeAndReturn/removeAndAppend
    Consider this -- this.length && this[ref]. What about this[ref] == 0, false, '', NaN, null, undefined. What means that item is 'out of bound'?

    The last thing in my critics. Maybe this code below can be useful in the future :-) But it requires to be improved...
    Code:
    Array.prototype.rotate = function(n)
    {
    	return this.push.call(this, this.splice(0, n));
    };
    Sorry for this my wordy post. I'd like to be make your ideas slightly better if it is possible anyway.

  • #6
    New Coder
    Join Date
    Jan 2012
    Location
    in the uk.
    Posts
    99
    Thanks
    2
    Thanked 6 Times in 4 Posts
    Like it!

    The more the merrier I say.

    I do try to over simplify my examples without being too dumb if you follow. The scripts may appear rough and lacking but thats the point of a base to start from. Nothing in my coding is "Fixed" if I can get away with it.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •