/**
* 類似(Map)的功能
*/
function $Map_(dataList) {
var self = this;
this.size = 0;
// this.keys = []; // [key, key.......]
this.dataList = []; // [{k: key, v: value}, {k: key, v: value}.....]
function __constructor() {
if (Array.isArray(dataList)) {
self.dataList = dataList;
self.size = self.dataList.length;
}
};
__constructor();
};
////////////////////////////////////////////////////////////////////////////////
(function() {
/**
*
* @return {Map}
*/
this.set = function(key, value) {
var index;
var data = {
'k': key,
'v': value
};
if ((index = this.indexOfKey(key)) >= 0) {
// 若(key)已經存在
this.dataList[index] = data;
} else {
this.dataList.push(data);
this.size = this.dataList.length;
}
return this;
};
/* ====================================================================== */
this.get = function(key) {
var index, index, _k, valueObj, checkCount = 0;
if ((index = this.indexOfKey(key)) <= 0) {
// 若沒找到(key)
return result;
}
/* ---------------------------------- */
var data = this.dataList[index];
return data.v;
};
}).call($Map_.prototype);
////////////////////////////////////////////////////////////////////////////////
(function() {
/* ====================================================================== */
this.clear = function() {
this.dataList = [];
this.size = this.dataList.length;
};
/* ====================================================================== */
this.delete = function(key) {
var index, result = false;
if ((index = this.indexOfKey) >= 0) {
this.dataList.splice(index, 1);
this.size = this.dataList.length;
result = true;
}
return result;
};
})($Map_.prototype);
////////////////////////////////////////////////////////////////////////////////
(function() {
this.indexOfKey = function(key) {
var index = -1;
if (key == null) {
return index;
}
this.dataList.some(function(data, i) {
var _key = data.k || undefined;
if (_key === key) {
index = i;
return true;
}
});
return index;
};
/* ====================================================================== */
this.has = function(key) {
var index, result = false;
if ((index = this.indexOfKey(key)) >= 0) {
result = true;
}
return result;
};
}).call($Map_.prototype);
////////////////////////////////////////////////////////////////////////////////
(function() {
/**
* 返回(array)
*
* @return {array} [keys]
*/
this.keys = function() {
var keys_array = [];
this.dataList.forEach(function(data) {
var key = data.k;
keys_array.push(key);
});
return keys_array;
};
/* ====================================================================== */
/**
* 返回(array)
*
* @return {array} [values]
*/
this.values = function() {
var values_array = [];
this.dataList.forEach(function(data) {
var values = data.v;
values_array.push(values);
});
return values_array;
};
}).call($Map_.prototype);
////////////////////////////////////////////////////////////////////////////////
(function() {
this.forEach = function(callback) {
var self = this;
this.dataList.forEach(function(data, i) {
var k = data.k;
var v = data.v;
/* ------------------------ */
var copyKey = self.__clone(k);
var copyData = self.__clone(v);
/* ------------------------ */
callback(copyData, copyKey, self);
});
};
/* ====================================================================== */
this.some = function(callback) {
var self = this;
var result = false;
this.dataList.some(function(data, i) {
var k = data.k;
var v = data.v;
var copyKey = self.__clone(k);
var copyData = self.__clone(v);
/* ------------------------ */
result = callback(copyData, copyKey, self);
if (result !== true) {
result = false;
}
/* ------------------------ */
return result;
});
return result;
};
}).call($Map_.prototype);
////////////////////////////////////////////////////////////////////////////////
(function() {
this.map = function() {
};
/* ====================================================================== */
this.filter = function() {
};
}).call($Map_.prototype);
////////////////////////////////////////////////////////////////////////////////
(function() {
/**
* 依照(key)排序
*/
this.keySort = function(sortFun) {
if (typeof sortFun != 'function') {
throw new TypeError('sortFun must be function');
}
this.dataList.sort(_sortFun);
/* ---------------------------------- */
function _sortFun(a, b) {
// 排序的函式
var key_a = a.k || undefined;
var key_b = b.k || undefined;
return sortFun(key_a, key_b);
}
};
/* ====================================================================== */
this.valueSort = function(sortFun) {
if (typeof sortFun != 'function') {
throw new TypeError('sortFun must be function');
}
this.dataList.sort(_sortFun);
/* ---------------------------------- */
function _sortFun(a, b) {
// 排序的函式
var value_a = a.v || undefined;
var value_b = b.v || undefined;
var result = sortFun(value_a, value_b);
return result;
}
};
}).call($Map_.prototype);
////////////////////////////////////////////////////////////////////////////////
(function() {
/**
* 複製
*/
this.slice = function() {
'use strict';
debugger;
var arg = [].slice.call(arguments);
var copyDataList = this.dataList.slice.apply(this.dataList, arg);
var map = new $Map_(copyDataList);
return map;
};
/* ====================================================================== */
/**
* 移除,拼接(不能新增)
*/
this.splice = function(start, count) {
'use strict';
debugger;
var copyDataList;
if (arguments.length == 0) {
copyDataList = this.dataList.splice();
} else if (arguments.length == 1) {
copyDataList = this.dataList.splice(start);
} else {
// arguments.length > 1
copyDataList = this.dataList.splice(start, count);
}
var map = new $Map_(copyDataList);
/* ---------------------------------- */
this.size = this.dataList.length;
return map;
};
}).call($Map_.prototype);
////////////////////////////////////////////////////////////////////////////////
(function() {
this.__clone = function(data) {
debugger;
var result;
if (typeof $.deepCopy_ == 'function') {
result = $.deepCopy_(data);
} else {
result = data;
}
return result;
};
}).call($Map_.prototype);