[cc lang="php"]
// 二维数组排序函数
function sysSortArray($ArrayData,$KeyName1,$SortOrder1 = "SORT_ASC",$SortType1 = "SORT_REGULAR")
{
if(!is_array($ArrayData))
{
return $ArrayData;
}
// Get args number.
$ArgCount = func_num_args();
// Get keys to sort by and put them to SortRule array.
for($I = 1;$I < $ArgCount;$I ++)
{
$Arg = func_get_arg($I);
if(!eregi("SORT",$Arg))
{
$KeyNameList[] = $Arg;
$SortRule[] = '$'.$Arg;
}
else
{
$SortRule[] = $Arg;
}
}
// Get the values according to the keys and put them to array.
foreach($ArrayData AS $Key => $Info)
{
foreach($KeyNameList AS $KeyName)
{
${$KeyName}[$Key] = $Info[$KeyName];
}
}
// Create the eval string and eval it.
$EvalString = 'array_multisort('.join(",",$SortRule).',$ArrayData);';
eval ($EvalString);
return $ArrayData;
} [/cc]
Saturday, November 27, 2010
PHP组合排列数组函数
[cc lang="php"]
private function combination($ar, $num) {
$control = range(0, $num-1);
$k = false;
$total = count($ar);
while($control[0] < $total-($num-1)) {
$t = array();
for($i=0; $i <$num; $i++) $t[] = $ar[$control[$i]];
$r[] = $t;
for($i=$num-1; $i>=0; $i--) {
$control[$i]++;
for($j=$i; $j <$num-1; $j++) $control[$j+1] = $control[$j]+1;
if($control[$i] < $total-($num-$i-1)) break;
}
}
return $r;
} private function combination($ar, $num) {
$control = range(0, $num-1);
$k = false;
$total = count($ar);
while($control[0] < $total-($num-1)) {
$t = array();
for($i=0; $i <$num; $i++) $t[] = $ar[$control[$i]];
$r[] = $t;
for($i=$num-1; $i>=0; $i--) {
$control[$i]++;
for($j=$i; $j <$num-1; $j++) $control[$j+1] = $control[$j]+1;
if($control[$i] < $total-($num-$i-1)) break;
}
}
return $r;
}[/cc]
private function combination($ar, $num) {
$control = range(0, $num-1);
$k = false;
$total = count($ar);
while($control[0] < $total-($num-1)) {
$t = array();
for($i=0; $i <$num; $i++) $t[] = $ar[$control[$i]];
$r[] = $t;
for($i=$num-1; $i>=0; $i--) {
$control[$i]++;
for($j=$i; $j <$num-1; $j++) $control[$j+1] = $control[$j]+1;
if($control[$i] < $total-($num-$i-1)) break;
}
}
return $r;
} private function combination($ar, $num) {
$control = range(0, $num-1);
$k = false;
$total = count($ar);
while($control[0] < $total-($num-1)) {
$t = array();
for($i=0; $i <$num; $i++) $t[] = $ar[$control[$i]];
$r[] = $t;
for($i=$num-1; $i>=0; $i--) {
$control[$i]++;
for($j=$i; $j <$num-1; $j++) $control[$j+1] = $control[$j]+1;
if($control[$i] < $total-($num-$i-1)) break;
}
}
return $r;
}[/cc]
Thursday, November 25, 2010
Vim去掉utf-8的BOM
[cc lang="text"]'去掉utf-8 BOM
:set nobomb
'保留utf-8 BOM
:set bomb[/cc]
:set nobomb
'保留utf-8 BOM
:set bomb[/cc]
用的Javascript函数var_dump(),implode()
[cc lang="javascript"]
// 检测是否为整数
function isInteger( str ){
var regu = /^[-]{0,1}[0-9]{1,}$/;
return regu.test(str);
}
function implode (glue, pieces) {
var i = '', retVal='', tGlue='';
if (arguments.length === 1) {
pieces = glue;
glue = '';
}
if (typeof(pieces) === 'object') {
if (pieces instanceof Array) {
return pieces.join(glue);
}
else {
for (i in pieces) {
retVal += tGlue + pieces[i];
tGlue = glue;
}
return retVal;
}
}
else {
return pieces;
}
}
function print_r(x, max, sep, l) {
l = l || 0;
max = max || 10;
sep = sep || ' ';
if (l > max) {
return "[WARNING: Too much recursion]\n";
}
var
i,
r = '',
t = typeof x,
tab = '';
if (x === null) {
r += "(null)\n";
} else if (t == 'object') {
l++;
for (i = 0; i < l; i++) {
tab += sep;
}
if (x && x.length) {
t = 'array';
}
r += '(' + t + ") :\n";
for (i in x) {
try {
r += tab + '[' + i + '] : ' + print_r(x[i], max, sep, (l + 1));
} catch(e) {
return "[ERROR: " + e + "]\n";
}
}
} else {
if (t == 'string') {
if (x == '') {
x = '(empty)';
}
}
r += '(' + t + ') ' + x + "\n";
}
return r;
};
var_dump = print_r;
[/cc]
// 检测是否为整数
function isInteger( str ){
var regu = /^[-]{0,1}[0-9]{1,}$/;
return regu.test(str);
}
function implode (glue, pieces) {
var i = '', retVal='', tGlue='';
if (arguments.length === 1) {
pieces = glue;
glue = '';
}
if (typeof(pieces) === 'object') {
if (pieces instanceof Array) {
return pieces.join(glue);
}
else {
for (i in pieces) {
retVal += tGlue + pieces[i];
tGlue = glue;
}
return retVal;
}
}
else {
return pieces;
}
}
function print_r(x, max, sep, l) {
l = l || 0;
max = max || 10;
sep = sep || ' ';
if (l > max) {
return "[WARNING: Too much recursion]\n";
}
var
i,
r = '',
t = typeof x,
tab = '';
if (x === null) {
r += "(null)\n";
} else if (t == 'object') {
l++;
for (i = 0; i < l; i++) {
tab += sep;
}
if (x && x.length) {
t = 'array';
}
r += '(' + t + ") :\n";
for (i in x) {
try {
r += tab + '[' + i + '] : ' + print_r(x[i], max, sep, (l + 1));
} catch(e) {
return "[ERROR: " + e + "]\n";
}
}
} else {
if (t == 'string') {
if (x == '') {
x = '(empty)';
}
}
r += '(' + t + ') ' + x + "\n";
}
return r;
};
var_dump = print_r;
[/cc]
Thursday, November 18, 2010
Javascript 组合算法
[cc lang="javascript"]
/**
* 递归排列
* 从 arr[1...n] 中任选 num(0 < num <= n) 个数的所有排列
*/
function recursion_permutate(arr, num) {
var r = [];
(function f(t, a, n) {
if (n == 0) return r.push(t);
for (var i = 0, l = a.length; i < l; i++) {
f(t.concat(a[i]), a.slice(0, i).concat(a.slice(i + 1)), n - 1);
}
})([], arr, num);
return r;
}
/**
* 递归组合
* 从 arr[1...n] 中任选 num(0 < num <= n) 个数的所有组合
*/
function recursion_combine(arr, num) {
var r = [];
(function f(t, a, n) {
if (n == 0) return r.push(t);
for (var i = 0, l = a.length; i <= l - n; i++) {
f(t.concat(a[i]), a.slice(i + 1), n - 1);
}
})([], arr, num);
return r;
}
/**
* 快速组合 - 字符串
*/
function quick_combine(n, m) {
var t = ((1 << n) - (1 << n - m)).toString(2),
r = [], s, p1, p2;
while((r.push(t), p1 = t.indexOf("10")) >= 0) {
s = t.slice(0, p1);
p2 = s.indexOf("1");
t = (p2 > 0 ? ((1 << p1) - (1 << p2)).toString(2) : s)
+ "01" + t.slice(p1 + 2);
}
return r;
}
/**
* 快速组合 - 纯位移
*/
function bit_quick_combine(n, m) {
var t = (1 << n + 1) - (1 << n - m), // 故意多留一个 1,免得补零
r = [], o, p1, p2;
while((o = t.toString(2).slice(1), r.push(o), p1 = o.indexOf("1"), p2 = o.indexOf("10")) >= 0){
t ^= ((p1 ? ((1 << p2) - (1 << p1) ^ (1 << p2 - p1) - 1) << 2 : 0) | 3) << n - p2 - 2;
}
return r;
}
/**
* 进位法
*/
function carry_combine(n, m) {
var r = [], t = [], i, p, max;
// seed
for(i = 0; i < m; i++) t.push(i);
r.push(t.concat());
while(p = m - 1, max = n - 1) {
// increase
while(t[p] < max) {
t[p]++;
r.push(t.concat());
}
// carry
while(t[--p] === --max){}
t[p]++;
for(i = p + 1; i < m; i++) t[i] = t[p] + i - p;
r.push(t.concat());
// done
if(t[0] === n - m) break;
}
return r;
}
/**
* 沙漏法
*/
function sandglass_combine(n, m) {
var p = [], r = [] , i, j;
for(i = m - 1, j = 0; i >= 0; i--, j++) {
p[j] = n - i - 1;
}
r.push(p.concat());
//5,4,3 5,4,2 5,4,1 5,4,0->5,3,2 5,3,1 5,3,0->5,2,1 5,2,0->5,1,0->5,0,0->4,3,2...
while (p[m - 1] >= m) {
if (--p[0] >= 0) {
r.push(p.concat());
}
else {
for (i = 1; i < m; i++) {
if (p[i] > i) {
p[i]--;
for (j = i - 1; j >= 0; j--) {
p[j] = p[j + 1] - 1;
}
r.push(p.concat());
break;
}
}
}
}
return r;
}
/**
* DP 组合
* @ref: http://bbs.51js.com/viewthread.php?tid=85574
* C(n, m) = C(n - 1, m) + C(n - 1, m - 1)
*/
function dp_combine(a, m) {
var t = [[]], r = [];
for (var i = 0, n = a.length; i < n; ++i) {
for (var j = 0, l = t.length; j < l; ++j) {
(t[j].length < m - 1 ? t : r).push(t[j].concat([a[i]]));
}
}
return r;
}
[/cc]
/**
* 递归排列
* 从 arr[1...n] 中任选 num(0 < num <= n) 个数的所有排列
*/
function recursion_permutate(arr, num) {
var r = [];
(function f(t, a, n) {
if (n == 0) return r.push(t);
for (var i = 0, l = a.length; i < l; i++) {
f(t.concat(a[i]), a.slice(0, i).concat(a.slice(i + 1)), n - 1);
}
})([], arr, num);
return r;
}
/**
* 递归组合
* 从 arr[1...n] 中任选 num(0 < num <= n) 个数的所有组合
*/
function recursion_combine(arr, num) {
var r = [];
(function f(t, a, n) {
if (n == 0) return r.push(t);
for (var i = 0, l = a.length; i <= l - n; i++) {
f(t.concat(a[i]), a.slice(i + 1), n - 1);
}
})([], arr, num);
return r;
}
/**
* 快速组合 - 字符串
*/
function quick_combine(n, m) {
var t = ((1 << n) - (1 << n - m)).toString(2),
r = [], s, p1, p2;
while((r.push(t), p1 = t.indexOf("10")) >= 0) {
s = t.slice(0, p1);
p2 = s.indexOf("1");
t = (p2 > 0 ? ((1 << p1) - (1 << p2)).toString(2) : s)
+ "01" + t.slice(p1 + 2);
}
return r;
}
/**
* 快速组合 - 纯位移
*/
function bit_quick_combine(n, m) {
var t = (1 << n + 1) - (1 << n - m), // 故意多留一个 1,免得补零
r = [], o, p1, p2;
while((o = t.toString(2).slice(1), r.push(o), p1 = o.indexOf("1"), p2 = o.indexOf("10")) >= 0){
t ^= ((p1 ? ((1 << p2) - (1 << p1) ^ (1 << p2 - p1) - 1) << 2 : 0) | 3) << n - p2 - 2;
}
return r;
}
/**
* 进位法
*/
function carry_combine(n, m) {
var r = [], t = [], i, p, max;
// seed
for(i = 0; i < m; i++) t.push(i);
r.push(t.concat());
while(p = m - 1, max = n - 1) {
// increase
while(t[p] < max) {
t[p]++;
r.push(t.concat());
}
// carry
while(t[--p] === --max){}
t[p]++;
for(i = p + 1; i < m; i++) t[i] = t[p] + i - p;
r.push(t.concat());
// done
if(t[0] === n - m) break;
}
return r;
}
/**
* 沙漏法
*/
function sandglass_combine(n, m) {
var p = [], r = [] , i, j;
for(i = m - 1, j = 0; i >= 0; i--, j++) {
p[j] = n - i - 1;
}
r.push(p.concat());
//5,4,3 5,4,2 5,4,1 5,4,0->5,3,2 5,3,1 5,3,0->5,2,1 5,2,0->5,1,0->5,0,0->4,3,2...
while (p[m - 1] >= m) {
if (--p[0] >= 0) {
r.push(p.concat());
}
else {
for (i = 1; i < m; i++) {
if (p[i] > i) {
p[i]--;
for (j = i - 1; j >= 0; j--) {
p[j] = p[j + 1] - 1;
}
r.push(p.concat());
break;
}
}
}
}
return r;
}
/**
* DP 组合
* @ref: http://bbs.51js.com/viewthread.php?tid=85574
* C(n, m) = C(n - 1, m) + C(n - 1, m - 1)
*/
function dp_combine(a, m) {
var t = [[]], r = [];
for (var i = 0, n = a.length; i < n; ++i) {
for (var j = 0, l = t.length; j < l; ++j) {
(t[j].length < m - 1 ? t : r).push(t[j].concat([a[i]]));
}
}
return r;
}
[/cc]
Subscribe to:
Posts (Atom)