|
Experience porting library regular expressionsSAGE Datastructures inthe paradigm paradigmFinding inthe internet quality article [1, 2, 3, 4, 5, 6] with algorithms forconstructing deterministic finite automata (DFA), implementing regular expressions, Istarted#&32for porting source c C++ to Active Oberon. Sources to 5th parts articles not causeddifficulties . Typical, trivial and pretty quality C++ code, with minimal interspersed OOP. Porting code attached to 6th parts articles caused some difficulties dueto active uses STLtemplates. In two small procedures used lists, lists lists and cards together taken (vector<>, set<>, set Iwantedto find interesting solution problems, with minimal bychangingthe modeof towork withthe similar constructs inC# . In ,therewere only implementations lists and sorted lists providedby Thomas Frey inthe module TFClasses.Mod. First solution in spirit OOP, which begs, - inherit new classes from ofthese sorted or unsorted lists and add desired functionality. This solution hasa mass disadvantages, since in Active Oberon no means for flexible overrides and hide parent methods. Work with such classes wasfollowedby wouldbe constant casts types extracted from lists elements, a implementation cards ingeneral was wouldbe far toelegant , not saying 32already about typesafety use ofsuch classes. But,the solution came. Idecidedto#&32#&32, that can be toimplement in this case, movingaway 32fromthe paradigm OOP tothe side component. Iwrote the empty definition class and added to him instance class list. C points view component paradigm programming i added list to as component in new class. And here me dawned :) Now wasleft towrite missing methods for operations above this instance list with needed types parameters and returned values and, voila! Alongtheway,##32wasmade decision decision write implementationof universal list with sortingfeature and checks for uniqueness inserted values. With turnedoff sorting also wouldbe wouldbe useful theability toadd elements to end#32lists or in any given position, for convenience subsequent implementation class StringList, to example. sortingfunction , presenceof or lackof sorting and checking uniqueness ofinserted elements passed in quality parameters constructor universal list. Byimplementingthe base componentlist, I quickly wrote list for elements type LONGINT and list list elements type LONGINT. Code class list for type LONGINT: TYPE LongintItem = POINTER TO RECORD value: LONGINT; END; LongintList = OBJECT VAR list: List; PROCEDURE &New(options: SET); BEGIN NEW(list, Compare, options) END New; PROCEDURE Compare(first, second: ANY): LONGINT; VAR nFirst, nSecond: LONGINT; BEGIN nFirst := first(LongintItem).value; nSecond := second(LongintItem).value; IF nFirst < nSecond THEN RETURN -1 ELSIF nFirst > nSecond THEN RETURN 1 ELSE RETURN 0 END END Compare; PROCEDURE Add(x: LONGINT); VAR item: LongintItem; BEGIN NEW(item); item.value := x; list.Add(item) END Add; PROCEDURE Insert(pos: LONGINT; x: LONGINT); VAR item: LongintItem; BEGIN NEW(item); item.value := x; list.Insert(pos, item) END Insert; PROCEDURE Remove(i: LONGINT); BEGIN list.Remove(i) END Remove; PROCEDURE IndexOf(x: LONGINT): LONGINT; VAR item: LongintItem; BEGIN NEW(item); item.value := x; RETURN list.IndexOf(item) END IndexOf; PROCEDURE GetCount(): LONGINT; BEGIN RETURN list.GetCount() END GetCount; PROCEDURE GetItem(i: LONGINT): LONGINT; VAR item: ANY; BEGIN item := list.GetItem(i); RETURN item(LongintItem).value END GetItem; END LongintList; For rooms values type LONGINT in universal list type LONGINT wraps in structure POINTER TO RECORD, for lists, elements ofwhich willbe objects, even and are not required. Adding valuesto#&32inthe list and extracting fromthe list not willrequire whenusing 32instances class list create instances entries and cast type pointer to type entries respectively, everything ishidden inside methods objects. Like free bonus weget full static (atthe stage compilation) control types with using data classes. Inasimilarwayto##32, lists areimplementedfor forany simple types and for entries. Theonly code, which willneed willbe actually write, is code procedures comparisons elements for toensure thecorrect job sort, a sort willprovidethe quick job method IndexOf() and control uniqueness inserted elements when work in mode ban duplicate values. Disablingsortingmode has meaning when works with list as with stack#32and with implementation list lines for processing texts. Then,arguing inasimilarwayto , Icreated thereareseveral types cards, required for porting source. For tocreate cards itwasrequired todescribe two entries, one for elements, describing keys by which willsort elements , and second for layout keys with matching them values. Pointer to second type entries and will actually bestored in listcomponent. Again with description cards, using as as key list elements oftype LONGINT required todescribe only one record, component key with stored value. For illustrations I’mlisting descriptions key and component entries one from cards and matching methods add, search and extract elements: TYPE Transition = RECORD iState: LONGINT; iData: LONGINT; END; TransitionMapItem = POINTER TO TransitionMapItemDesc; TransitionMapItemDesc = RECORD trans: Transition; iState: LONGINT; END; TransitionMap* = OBJECT VAR list: Lists.List; ... PROCEDURE Add(CONST trans: Transition; iState: LONGINT); VAR item: TransitionMapItem; BEGIN NEW(item); item^.trans := trans; item^.iState := iState; list.Add(item) END Add; PROCEDURE IndexOf(CONST trans: Transition): LONGINT; VAR item: TransitionMapItem; BEGIN NEW(item); item^.trans := trans; RETURN list.IndexOf(item) END IndexOf; PROCEDURE GetItem(i: LONGINT): TransitionMapItemDesc; VAR item: ANY; BEGIN item := list.GetItem(i); RETURN item(TransitionMapItem)^ END GetItem; END TransitionMap; For additional convenience forwriting code, in case ifthe key isa record with multiple fields, describethe##32constructorprocedure oftheinstance key. Thedescribed tricks demonstrateclearly demonstrate features, areprovidedby component paradigm, inthe framework programminglanguage , with design which priority wasgiven minimally -Adequate set language means. Comments to ported library regular expressions
Used sources:[1] Algorithmic Forays Part 1. [2] Algorithmic Forays Part 2. [3] Algorithmic Forays Part 3. [4] Algorithmic Forays Part 4. [5] Algorithmic Forays Part 5. [6] Algorithmic Forays Part 6.
Last update: 24-2-20 02:29:34 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Copyright © 2005-2022 SAGE. All rights reserved. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||