diff options
-rw-r--r-- | matheunit.pas | 123 |
1 files changed, 90 insertions, 33 deletions
diff --git a/matheunit.pas b/matheunit.pas index 5247f68..d57fb03 100644 --- a/matheunit.pas +++ b/matheunit.pas @@ -16,21 +16,23 @@ type end; tKnownValues = class private - chef: tKnownValues; - knechte: array of tKnownValues; - kvs: array of tKnownValue; - function finde(nam: string): longint; inline; + chefs,knechte: array of tKnownValues; + kvs: array of tKnownValue; + function finde(nam: string): longint; inline; public - constructor create(chf: tKnownValues); + constructor create(chef: tKnownValues); constructor createFromOriginal(original: tKnownValues); destructor destroy; override; - procedure add(val: tKnownValue); inline; overload; - procedure add(nam: string; val: extended); overload; + procedure add(val: tKnownValue; ignoriereWennBereitsVorhanden: boolean = false); inline; overload; + procedure add(nam: string; val: extended; ignoriereWennBereitsVorhanden: boolean = false); overload; function rem(nam: string): boolean; function extract(nam: string; out val: extended): boolean; inline; - procedure dump(prot: tProtokollant; prefix: string); + procedure dump(prot: tProtokollant; prefix: string); overload; + procedure dump(prefix: string; out s: string); overload; procedure neuerKnecht(knecht: tKnownValues); + procedure neuerChef(chef: tKnownValues; ignoriereWennBereitsVorhanden: boolean = false); procedure entferneKnecht(knecht: tKnownValues); + procedure entferneChef(chef: tKnownValues); end; function plus(a,b: tExtPoint): tExtPoint; @@ -63,15 +65,15 @@ implementation // tKnownValueArray ************************************************************ -constructor tKnownValues.create(chf: tKnownValues); +constructor tKnownValues.create(chef: tKnownValues); begin inherited create; fillchar(kvs,sizeOf(kvs),#0); fillchar(knechte,sizeOf(knechte),#0); add('π',pi); - chef:=chf; + fillchar(chefs,sizeOf(chefs),#0); if assigned(chef) then - chef.neuerKnecht(self); + neuerChef(chef); setLength(knechte,0); end; @@ -84,26 +86,26 @@ begin fillchar(knechte,sizeOf(knechte),#0); for i:=0 to length(original.kvs)-1 do add(original.kvs[i]); - chef:=original.chef; - if assigned(chef) then - chef.neuerKnecht(self); + for i:=0 to length(original.chefs)-1 do + neuerChef(original.chefs[i]); end; destructor tKnownValues.destroy; var - i: longint; + i,j: longint; begin + for i:=0 to length(kvs)-1 do + for j:=0 to length(knechte)-1 do + knechte[j].add(kvs[i],true); setLength(kvs,0); - for i:=0 to length(knechte)-1 do begin - knechte[i].chef:=chef; - if assigned(chef) then - chef.neuerKnecht(knechte[i]); + for i:=length(knechte)-1 downto 0 do begin + for j:=0 to length(chefs)-1 do + knechte[i].neuerChef(chefs[j],true); + knechte[i].entferneChef(self); end; setLength(knechte,0); - if assigned(chef) then begin - chef.entferneKnecht(self); - chef:=nil; - end; + for i:=length(chefs)-1 downto 0 do + entferneChef(chefs[i]); inherited destroy; end; @@ -119,12 +121,12 @@ begin end; end; -procedure tKnownValues.add(val: tKnownValue); +procedure tKnownValues.add(val: tKnownValue; ignoriereWennBereitsVorhanden: boolean = false); begin - add(val.name,val.value); + add(val.name,val.value,ignoriereWennBereitsVorhanden); end; -procedure tKnownValues.add(nam: string; val: extended); +procedure tKnownValues.add(nam: string; val: extended; ignoriereWennBereitsVorhanden: boolean = false); var i: longint; begin @@ -133,7 +135,10 @@ begin i:=length(kvs); setLength(kvs,length(kvs)+1); kvs[i].name:=nam; - end; + end + else + if ignoriereWennBereitsVorhanden then + exit; kvs[i].value:=val; end; @@ -160,10 +165,12 @@ begin i:=finde(nam); result:=i>=0; if result then - val:=kvs[i].value - else - if assigned(chef) then - result:=chef.extract(nam,val); + val:=kvs[i].value; + i:=0; + while (not result) and (i<length(chefs)) do begin + result:=chefs[i].extract(nam,val); + inc(i); + end; end; procedure tKnownValues.dump(prot: tProtokollant; prefix: string); @@ -172,8 +179,23 @@ var begin for i:=0 to length(kvs)-1 do prot.schreibe(prefix+kvs[i].name+' = '+floattostr(kvs[i].value)); - if assigned(chef) then - chef.dump(prot,prefix+' chef: '); + for i:=0 to length(chefs)-1 do + chefs[i].dump(prot,prefix+'chef['+intToStr(i)+']: '); +end; + +procedure tKnownValues.dump(prefix: string; out s: string); +var + i: longint; + t: string; +begin + s:=''; + for i:=0 to length(kvs)-1 do + s:=s+#10+prefix+kvs[i].name+' = '+floattostr(kvs[i].value); + for i:=0 to length(chefs)-1 do begin + chefs[i].dump(prefix+'chefs['+intToStr(i)+'] ',t); + s:=s+#10+t; + end; + delete(s,1,1); end; procedure tKnownValues.neuerKnecht(knecht: tKnownValues); @@ -187,6 +209,21 @@ begin knechte[length(knechte)-1]:=knecht; end; +procedure tKnownValues.neuerChef(chef: tKnownValues; ignoriereWennBereitsVorhanden: boolean = false); +var + i: longint; +begin + for i:=0 to length(chefs)-1 do + if chefs[i]=chef then begin + if ignoriereWennBereitsVorhanden then + exit; + fehler('tKnownValues soll einen Chef hinzufügen, den es schon hat!'); + end; + setLength(chefs,length(chefs)+1); + chefs[length(chefs)-1]:=chef; + chefs[length(chefs)-1].neuerKnecht(self); +end; + procedure tKnownValues.entferneKnecht(knecht: tKnownValues); var i: longint; @@ -206,6 +243,26 @@ begin fehler('tKnownValues soll einen Knecht entfernen, den es nicht hat!'); end; +procedure tKnownValues.entferneChef(chef: tKnownValues); +var + i: longint; +begin + i:=0; + while i<length(chefs) do begin + if chefs[i]=chef then begin + while i<length(chefs)-1 do begin + chefs[i]:=chefs[i+1]; + inc(i); + end; + setLength(chefs,length(chefs)-1); + chef.entferneKnecht(self); + exit; + end; + inc(i); + end; + fehler('tKnownValues soll einen Chef entfernen, den es nicht hat!'); +end; + // allgemeine Funktionen ******************************************************* function plus(a,b: tExtPoint): tExtPoint; |