worbooks以及workbook初次实现
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
Type Collection = class
|
||||
|
||||
public
|
||||
Function Create();
|
||||
Function SetIgnoreCase(value);
|
||||
Function SetActivate(value);
|
||||
Function GetActivate();
|
||||
Function CalNewName(prefix);
|
||||
Function CompareFunction(index, data);
|
||||
|
||||
Function AddCollection(obj, name);
|
||||
Function RemoveCollection(index);
|
||||
Function Operator[](index);
|
||||
Function GetCount();
|
||||
|
||||
public
|
||||
class Function GetInstance(value);
|
||||
|
||||
private
|
||||
ignorecase_; // 忽略大小写
|
||||
container_; // 容器对象
|
||||
fp_; // 函数指针 -- 用来比较
|
||||
activate_; // 激活的对象
|
||||
|
||||
private
|
||||
static collection_;
|
||||
|
||||
End;
|
||||
|
||||
// ============== 实现 ================= //
|
||||
Function Collection.Create();
|
||||
Begin
|
||||
container_ := ContainerList();
|
||||
ignorecase_ := false;
|
||||
fp_ := FindFunction("CompareFunction", self);
|
||||
End;
|
||||
|
||||
Function Collection.SetIgnoreCase(value);
|
||||
Begin
|
||||
ignorecase_ := value;
|
||||
End;
|
||||
|
||||
Function Collection.SetActivate(value);
|
||||
Begin
|
||||
activate_ := self[value];
|
||||
End;
|
||||
|
||||
Function Collection.GetActivate();
|
||||
Begin
|
||||
return activate_;
|
||||
End;
|
||||
|
||||
Function Collection.CompareFunction(index, data);
|
||||
Begin
|
||||
return (ifnumber(index) and data.index = index)
|
||||
or data.name = index
|
||||
or (ignorecase_ and lowercase(data.name) = lowercase(index));
|
||||
End;
|
||||
|
||||
Function Collection.CalNewName(prefix);
|
||||
Begin
|
||||
len := GetCount() + 1;
|
||||
name := prefix $ len;
|
||||
while container_.Get(name, fp_) do
|
||||
name := prefix $ ++len;
|
||||
return name;
|
||||
End;
|
||||
|
||||
class Function Collection.GetInstance(value);
|
||||
Begin
|
||||
if not istable(collection_) then collection_ := array();
|
||||
if not collection_[value] then
|
||||
collection_[value] := new Collection();
|
||||
return collection_[value];
|
||||
End;
|
||||
|
||||
Function Collection.AddCollection(obj, name);
|
||||
Begin
|
||||
data := new Data();
|
||||
data.obj := obj;
|
||||
data.name := name;
|
||||
data.index := GetCount() + 1;
|
||||
return container_.Add(data);
|
||||
End;
|
||||
|
||||
Function Collection.RemoveCollection(index);
|
||||
Begin
|
||||
return container_.Remove(index, fp_);
|
||||
End;
|
||||
|
||||
Function Collection.GetCount();
|
||||
Begin
|
||||
return container_.Size();
|
||||
End;
|
||||
|
||||
Function Operator Collection.[](index);
|
||||
Begin
|
||||
ret := container_.Get(index, fp_);
|
||||
return ret ? ret.obj : nil;
|
||||
End;
|
||||
|
||||
// 集合中需要记录的数据
|
||||
Type Data = class
|
||||
obj;
|
||||
name;
|
||||
index;
|
||||
End;
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
Function ContainerList();
|
||||
Begin
|
||||
return new LinkList();
|
||||
End;
|
||||
|
||||
Type LinkList = class
|
||||
|
||||
private
|
||||
count_;
|
||||
head_;
|
||||
tail_;
|
||||
|
||||
public
|
||||
Function Create();
|
||||
Function Add(data);
|
||||
Function Remove(index, fp);
|
||||
Function Size();
|
||||
Function Get(index, fp);
|
||||
Function Find(index, fp);
|
||||
|
||||
End;
|
||||
|
||||
Function LinkList.Create();
|
||||
Begin
|
||||
head_ := new List(nil);
|
||||
tail_ := head_;
|
||||
count_ := 0;
|
||||
End;
|
||||
|
||||
Function LinkList.Add(data);
|
||||
Begin
|
||||
list := new List(data);
|
||||
tail_.next := list;
|
||||
list.prev := tail_;
|
||||
tail_ := list;
|
||||
count_ ++;
|
||||
End;
|
||||
|
||||
Function LinkList.Remove(index, fp);
|
||||
Begin
|
||||
node := Find(index, fp);
|
||||
if ifnil(node) then return;
|
||||
if node = tail_ then tail_ := node.prev;
|
||||
else begin
|
||||
node.prev.next := node.next;
|
||||
node.next.prev := node.prev;
|
||||
node := nil;
|
||||
end
|
||||
count_--;
|
||||
End;
|
||||
|
||||
Function LinkList.Size();
|
||||
Begin
|
||||
return count_;
|
||||
End;
|
||||
|
||||
Function LinkList.Find(index, fp);
|
||||
Begin
|
||||
node := head_.next;
|
||||
while (node) do
|
||||
begin
|
||||
if (fp and fp.do(index, node.data)) or node.data = index then
|
||||
return node;
|
||||
node := node.next;
|
||||
end
|
||||
return nil;
|
||||
End;
|
||||
|
||||
Function LinkList.Get(index, fp);
|
||||
Begin
|
||||
node := Find(index, fp);
|
||||
return ifnil(node) ? nil : node.data;
|
||||
End;
|
||||
|
||||
|
||||
Type List = class
|
||||
|
||||
public
|
||||
data;
|
||||
prev;
|
||||
next;
|
||||
|
||||
Function Create(value);
|
||||
Begin
|
||||
data := value;
|
||||
prev := nil;
|
||||
next := nil;
|
||||
End;
|
||||
|
||||
End;
|
||||
|
||||
Reference in New Issue
Block a user