mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
Core: Refactor base classes
This commit is contained in:
@@ -214,7 +214,7 @@ public class Xodb_load_mgr_txt implements Xodb_load_mgr {
|
||||
Int_obj_ref count = Int_obj_ref.New_zero();
|
||||
Xob_random_itm[] files = Build_random_itms(regy_mgr, count);
|
||||
int random_idx = RandomAdp_.new_().Next(count.Val() - 1); // get a random idx; -1 since count is super 1 (EX: count of 1 mil; random_idx of 0 - 999,999)
|
||||
int file_idx = CompareAble_.FindSlot(Xob_random_itm_comparer.Instance, files, new Xob_random_itm(-1, random_idx, -1));
|
||||
int file_idx = Xowd_regy_mgr_.FindSlot(Xob_random_itm_comparer.Instance, files, new Xob_random_itm(-1, random_idx, -1));
|
||||
Io_url file_url = fsys_mgr.Url_ns_fil(Xotdb_dir_info_.Tid_ttl, ns.Id(), file_idx);
|
||||
Load_xdat_file(Cancelable_.Never, tmp_xdat_file, file_url);
|
||||
Xob_random_itm file = files[file_idx];
|
||||
|
||||
@@ -25,7 +25,7 @@ public class Xow_html_util implements Gfo_invk {
|
||||
} private static final String Invk_if_bool = "if_bool", Invk_if_yn = "if_yn";
|
||||
String If_bool(String expr, String true_val, String false_val) {
|
||||
Object o = wiki.Appe().Gfs_mgr().Run_str(expr);
|
||||
try {return Bool_.cast(o) ? true_val : false_val;}
|
||||
try {return Bool_.Cast(o) ? true_val : false_val;}
|
||||
catch (Exception e) {Err_.Noop(e); return "expr failed: " + expr;}
|
||||
}
|
||||
String If_yn(String expr, String true_val, String false_val) {
|
||||
|
||||
@@ -27,7 +27,7 @@ public class Xowd_regy_mgr {
|
||||
public void Clear() {files_ary = Xowd_hive_regy_itm.Ary_empty;}
|
||||
public int Files_find(byte[] key) {
|
||||
if (files_ary.length == 0) return Xowd_regy_mgr.Regy_null; // NOTE: FindSlot does not accept empty ary; returning 0, b/c Find returns likely file_idx; EX: regy of 0|B|D and 1|F|H; A returns 0; Z returns 1
|
||||
return CompareAble_.FindSlot(comparer, files_ary, comparer_itm.End_(key));
|
||||
return Xowd_regy_mgr_.FindSlot(comparer, files_ary, comparer_itm.End_(key));
|
||||
} ComparerAble comparer = Xowd_ttl_file_comparer_end.Instance; Xowd_hive_regy_itm comparer_itm = Xowd_hive_regy_itm.tmp_().Count_(1);
|
||||
public Xowd_hive_regy_itm Create(byte[] key) {
|
||||
int itm_idx = files_ary.length;
|
||||
|
||||
44
400_xowa/src/gplx/xowa/wikis/tdbs/hives/Xowd_regy_mgr_.java
Normal file
44
400_xowa/src/gplx/xowa/wikis/tdbs/hives/Xowd_regy_mgr_.java
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.xowa.wikis.tdbs.hives; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*; import gplx.xowa.wikis.tdbs.*;
|
||||
import gplx.core.lists.*;
|
||||
public class Xowd_regy_mgr_ {
|
||||
public static int FindSlot(ComparerAble comparer, Object[] ary, Object itm) {if (itm == null) throw Err_.new_null();
|
||||
int aryLen = ary.length;
|
||||
switch (aryLen) {
|
||||
case 0: throw Err_.new_wo_type("ary cannot have 0 itms");
|
||||
case 1: return 0;
|
||||
}
|
||||
int lo = -1, hi = aryLen - 1; // NOTE: -1 is necessary; see test
|
||||
int curPos = (hi - lo) / 2;
|
||||
int delta = 1;
|
||||
while (true) {
|
||||
Object curSeg = ary[curPos];
|
||||
int comp = curSeg == null ? CompareAble_.More : comparer.compare(itm, curSeg); // nulls should only happen for lastAry
|
||||
// if (dbg) {
|
||||
// Tfds.Write(curPos, itm.toString(), comp, comp.toString(), curSeg.toString());
|
||||
// }
|
||||
if (comp == CompareAble_.Same) return curPos;
|
||||
else if (comp > CompareAble_.Same) {lo = curPos; delta = 1;}
|
||||
else if (comp < CompareAble_.Same) {hi = curPos; delta = -1;}
|
||||
int dif = hi - lo;
|
||||
if (dif == 1 || dif == 0) return hi; // NOTE: can be 0 when ary.length == 1 || 2; also, sometimes 0 in some situations
|
||||
else curPos += (dif / 2) * delta;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.xowa.wikis.tdbs.hives; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*; import gplx.xowa.wikis.tdbs.*;
|
||||
import org.junit.*; import gplx.core.lists.*;
|
||||
public class Xowd_regy_mgr__tst implements ComparerAble {
|
||||
@Test public void Basic() {
|
||||
String[] slotAry = new String[] {"b", "e", "h"}; // 0=b 1=e 2=h
|
||||
tst_FindSlot(slotAry, "f", "h"); // f -> 1 2 -> 2
|
||||
tst_FindSlot(slotAry, "c", "e"); // c -> -1 1 -> 0 -> 0 1 -> 1
|
||||
tst_FindSlot(slotAry, "a", "b"); // a -> -1 1 -> 0 -> -1 0 -> 0
|
||||
}
|
||||
@Test public void Null() {
|
||||
String[] slotAry = new String[] {"b", "g", "l", "q", "v", null};
|
||||
tst_FindSlot(slotAry, "a", "b");
|
||||
tst_FindSlot(slotAry, "b", "b");
|
||||
tst_FindSlot(slotAry, "c", "g");
|
||||
tst_FindSlot(slotAry, "v", "v");
|
||||
tst_FindSlot(slotAry, "w", null);
|
||||
}
|
||||
public int compare(Object lhsObj, Object rhsObj) {return CompareAble_.Compare_obj(lhsObj, rhsObj);}
|
||||
void tst_FindSlot(String[] slotAry, String s, String expd) {Tfds.Eq(expd, slotAry[gplx.xowa.wikis.tdbs.hives.Xowd_regy_mgr_.FindSlot(this, slotAry, s)]);}
|
||||
}
|
||||
Reference in New Issue
Block a user