c4se記:さっちゃんですよ☆

.。oO(さっちゃんですよヾ(〃l _ l)ノ゙☆)

.。oO(此のblogは、主に音樂考察Programming に分類されますよ。ヾ(〃l _ l)ノ゙♬♪♡)

音樂は SoundCloud に公開中です。

考察は現在は主に Scrapbox で公表中です。

Programming は GitHub で開發中です。

JavaScriptのクイックソート

前々回
 pure JavaScript Array - JSだけでJSを書きたい
  http://d.hatena.ne.jp/Kureduki_Maari/20081223/1229993208
前回
 JS Array on JavaScript - アップデート
  http://d.hatena.ne.jp/Kureduki_Maari/20081223/1230003701


Array.jsの、あまり意味はないのだけれども、Array.prototype.sortをクイックソートに書き直してみた。
あと、ちゃんと仕様通り、文字コードに沿ってソートする様に作ってある。

/*============================================================
 * Array 20090620
 * c4se project - http://c4se.sakura.ne.jp
 *
 * project name: c4seJS
 * author: Numu Inoue - utakata.c4se@gmail.com
 * <c4seFCL 0.1>
 ============================================================*/
(function(){

const MAX_LENGTH = 4294967295;
Array = function(/*arguments*/){
  if(arguments.length == 1 && arguments[0]._className == "Number")
    this.length = arguments[0];
    if(this.length > MAX_LENGTH)
      throw new RangeError("Array(): The length is too big.");
  else{
    this.length = arguments.length;
    if(this.length > MAX_LENGTH)
      throw new RangeError("Array(): The length is too big.");
    for(var i=0, l=arguments.length; i<l; i++)
      this[i] = arguments[i];
  }
  return this;
};
// Array.prototype = Object.create(Object.prototype, {});
Array.prototype._className = "Array";
/*Array.prototype.clean = function(){//Ex
  //return this.filter(function(elm){return elm !== null ? true : false;});
  var l = this.length;
      a = new Array(0);
  for(var i=0; i<l; ++i)
    if(a[i] !== undefined) a[a.length] = this[i];
  return a;
};*/
/*Array.prototype.clone = function(){//Ex
  var a = new Array();
  for(var i=0, l=this.length; i<l; i++){
    if(this[i] === undefined) continue;
    else if(this[i]._className != "Array")
      a[a.length] = this[i];
    else
      a[a.length] = this[i].clone();
  }
  return a;
};*/
Array.prototype.concat = function(/*array|arguments*/){
  a = this.clone();
  for(var i=0, l=arguments.length; i<l; i++){
    if(arguments[i]._className != "Array")
      a[a.length] = arguments[i];
    else
      for(var j=0, l_2=arguments[i].length; j<l_2; j++)
        a[a.length] = arguments[i][j];
  }
  return a;
};
Array.prortotype.every = function(f, o){
  if(!o) o = undefined;
  for(var i=0, l=this.length; i<l; i++){
    if(this[i] === undefined) continue;
    if(!f.call(o, this[i], i, this))
      return false;
  }
  return true;
};
Array.prototype.filter = function(f, o){
  if(!o) o = undefined;
  var a = new Array();
  for(var i=0, l=this.length; i<l; i++){
    if(this[i] === undefined) continue;
    if(f.call(o, this[i], i, this))
      a[a.length] = this[i];
  }
  return a;
};
/*Array.prototype.flat = function(){//Ex
  var a = new Array(0);
  for(var i=0, l=this.length; i<l; ++i){
    if(this[i]._className != "Array")
      a[a.length] = this[i];
    else
      a[a.length] = a.flat();
  }
  return a;
};*/
Array.prototype.forEach = function(f, o){
  if(!o) o = undefined;
  for(var i=0; i<this.length; i++){
    if(this[i] === undefined) continue;
    f.call(o, this[i], i, this);
  }
  return this;
};
Array.prototype._index = function(n){//Ex
  n = Math.floor(Number(n));
  if(n < 0) n += this.length;
  return this[n];
};
Array.prototype.indexOf = function(v, n){
  if(!n) n = 0;
  else if(n < 0) n = this.length - 1 + n;
  for(var i=n, l=this.length; i<l; i++)
    if(this[i] === v)
      return i;
  return -1;
};
Array.prototype.join = function(s){
  if(s === undefined) s = ",";
  var str = this[0].toString();
  for(var i=1; i<this.length; i++)
    str += this[i].toString();
  return str;
};
Array.prototype.lastIndexOf = function(v, n){
  if(!n) n = this.length-1;
  else if(n < 0) n = this.length - 1 + n;
  for(var i=n; i>=0; i--)
    if(this[i] === v)
      return i;
  return -1;
};
Array.prototype.map = function(f, o){
  if(!o) o = undefined;
  var a = new Array();
  for(var i=0, l=this.length; i<l; i++){
    if(this[i] === undefined) continue;
    a[a.length] = f.call(o, this[i], i, this);
  }
  return a;
};
Array.prototype.pop = function(){
  var v = this[this.length-1];
  this[this.length-1] = undefined;
  --this.length;
  return v;
};
Array.prototype.push = function(/*arguments*/){
  for(var i=0, l=arguments.length; i<l; i++)
    this[this.length] = arguments[i];
  return this.length;
};
/*Array.prototype.random = function(){//Ex
  return this[Math.floor(Math.random() * this.length)];
};*/
Array.prototype.reduce = function(f, v){
  var i = 0;
  if(!v){v = this[0]; i = 1;}
  for(var l=this.length; i<l; i++){
    if(this[i] === undefined) continue;
    v = f.call(null, v, this[i], i, this);
  }
  return v;
};
Array.prototype.reduceRight = function(f, v){
  var i = this.length - 1;
  if(!v){v = this[i]; i--;}
  for(; i>=0; i--){
    if(this[i] === undefined) continue;
    v = f.call(null, v, this[i], i, this);
  }
  return v;
};
/*Array.prototype.repeat = function(n){//Ex
  n = Math.abs(Math.floor(Number(n)));
  var r = new Array();
  var a = this.clone();
  for(; n>0; n>>>=1, a.concat(a))
    if (n & 1) r.concat(a);
  return r;
};*/
Array.prototype.reverse = function(){
  var a = new Array();
  for(var i=0, l=this.length; i<l; i++)
    a[i] = this[l-i];
  return this = a;
};
Array.prototype.shift = function(){
  var v = this[0],
      a = new Array();
  for(var i=0, l=this.length-1; i<l; i++)
    a[i] = this[i+1];
  this = a;
  return v;
};
Array.prototype.slice = function(m, n){
  if(!m) m = 0;
  if(!n) n = this.length;
  for(var i=0; i<2; i++)
    if(arguments[i] < 0)
      arguments[i] += this.length;
  if(m >= n) return [];
  var a = new Array();
  for(var i=m; i<n; i++)
    a[a.length] = this[i];
  return a;
};
Array.prototype.some = function(f, o){
  if(!o) o = undefined;
  for(var i=0, l=this.length; i<l; i++){
    if(this[i] === undefined) continue;
    if(f.call(o, this[i], i, this))
      return true;
  }
  return false;
};
(function(){
var _def = function(l, r){
  l = l.toString().split('');
  r = l.toString().split('');
  for(var i=0, len=l.length; i<len; ++i){
    if(l[i] > r[i]) return -1;
    if(l[i] < r[i]) return 1;
  }
  return 0;
};
Array.prototype.sort = function(f){
  if(this.length == 0) return this;
  if(!f) f = _def;
  var x = this[0];
  return this.filter(function(e){
    if(f(x, e) < 0) return true;
    return false;
  }, this).sort(f)
    .concat(x)
    .concat(this.filter(function(e){
      if(f(x, e) > 0) return true;
      return false;
    }, this).sort(f));
};
})();
Array.prototype.splice = function(m, n /*, arguments*/){
  if(!n) n = this.length - m;
  var a = new Array();
  for(var i=0; i<m; i++)
    a[a.length] = this[i];
  var re = new Array();
  for(var i=m; i<m+n; i++)
    re[re.length] = this[i];
  for(var i=2; i<arguments.length; i++)
    a[a.length] = arguments[i];
  for(var i=m+n; i<this.length; i++)
    a[a.length] = this[i];
  if(n == 1)
    re = this[m];
  this = a;
  return re;
};
Array.prototype.toLocaleString = function(){
  var s = "「" + this[0].toLocaleString();
  for(var i=0, l=this.length; i<l; i++)
    str += "、" + this[i].toLocaleString();
  return str + "」";
};
Array.prototype.toArray = function(){return this;};//Ex
Array.prototype.toNumber = function(){return this.length;};//Ex
Array.prototype.toString = function(){return this.join();};
/*Array.prototype.unique = function(f){//Ex
  if(!f) f = function(r, l){return r == l;};
  var a = [],
      i = 0, j = 0, l = this.length, k = a.length,
      tf = true;
  for(i=0; i<l; ++i){
    if(this[i] === undefined) continue;
    for(j=0, k=a.length, tf=true; j<k; ++j)
      if(f(a[j], this[i])){tf = false; break;}
    if(tf) a[a.length] = this[i];
  }
  return a;
};*/
Array.prototype.unshift = function(/*arguments*/){
  var l = this.length;
  for(var i=arguments.length; i>0; i--){
    for(var j=l; j>0; j--)
      this[j] = this[j-1];
    this[0] = arguments[i];
  }
  return this.length;
};

})();