/* Ċ Ġ* ĠP _ Tuple To Set . kt Ċ Ġ* ĠCopyright ĠÂ© Ġ 1 9 9 3 - 2 0 2 0 , ĠThe ĠAv ail ĠFoundation , ĠLLC . Ċ Ġ* ĠAll Ġrights Ġreserved . Ċ Ġ* Ċ Ġ* ĠRedistribution Ġand Ġuse Ġin Ġsource Ġand Ġbinary Ġforms , Ġwith Ġor Ġwithout Ċ Ġ* Ġmodification , Ġare Ġpermitted Ġprovided Ġthat Ġthe Ġfollowing Ġconditions Ġare Ġmet : Ċ Ġ* Ċ Ġ* Ġ* ĠRedistributions Ġof Ġsource Ġcode Ġmust Ġretain Ġthe Ġabove Ġcopyright Ġnotice , Ġthis Ċ Ġ* ĠĠ Ġlist Ġof Ġconditions Ġand Ġthe Ġfollowing Ġdisclaimer . Ċ Ġ* Ċ Ġ* Ġ* ĠRedistributions Ġin Ġbinary Ġform Ġmust Ġreproduce Ġthe Ġabove Ġcopyright Ġnotice , Ċ Ġ* ĠĠ Ġthis Ġlist Ġof Ġconditions Ġand Ġthe Ġfollowing Ġdisclaimer Ġin Ġthe Ġdocumentation Ċ Ġ* ĠĠ Ġand / or Ġother Ġmaterials Ġprovided Ġwith Ġthe Ġdistribution . Ċ Ġ* Ċ Ġ* Ġ* ĠNeither Ġthe Ġname Ġof Ġthe Ġcopyright Ġholder Ġnor Ġthe Ġnames Ġof Ġthe Ġcontributors Ċ Ġ* ĠĠ Ġmay Ġbe Ġused Ġto Ġendorse Ġor Ġpromote Ġproducts Ġderived Ġfrom Ġthis Ġsoftware Ċ Ġ* ĠĠ Ġwithout Ġspecific Ġprior Ġwritten Ġpermission . Ċ Ġ* Ċ Ġ* ĠTHIS ĠSOFTWARE ĠIS ĠPROVIDED ĠBY ĠTHE ĠCOPYRIGHT ĠHOLDERS ĠAND ĠCONTRIBUTORS Ġ" AS ĠIS " Ċ Ġ* ĠAND ĠANY ĠEXPRESS ĠOR ĠIMPLIED ĠWARRANTIES , ĠINCLUDING , ĠBUT ĠNOT ĠLIMITED ĠTO , ĠTHE Ċ Ġ* ĠIMPLIED ĠWARRANTIES ĠOF ĠMERCHANTABILITY ĠAND ĠFITNESS ĠFOR ĠA ĠPARTICULAR ĠPURPOSE Ċ Ġ* ĠARE ĠDISCLAIMED . ĠIN ĠNO ĠEVENT ĠSHALL ĠTHE ĠCOPYRIGHT ĠHOLDER ĠOR ĠCONTRIBUTORS ĠBE Ċ Ġ* ĠLIABLE ĠFOR ĠANY ĠDIRECT , ĠINDIRECT , ĠINCIDENTAL , ĠSPECIAL , ĠEXEMPLARY , ĠOR Ċ Ġ* ĠCONSEQUENTIAL ĠDAMAGES Ġ( INCLUDING , ĠBUT ĠNOT ĠLIMITED ĠTO , ĠPROCUREMENT ĠOF Ċ Ġ* ĠSUBSTITUTE ĠGOODS ĠOR ĠSERVICES ; ĠLOSS ĠOF ĠUSE , ĠDATA , ĠOR ĠPROFITS ; ĠOR ĠBUSINESS Ċ Ġ* ĠINTERRUPTION ) ĠHOWEVER ĠCAUSED ĠAND ĠON ĠANY ĠTHEORY ĠOF ĠLIABILITY , ĠWHETHER ĠIN Ċ Ġ* ĠCONTRACT , ĠSTRICT ĠLIABILITY , ĠOR ĠTORT Ġ( INCLUDING ĠNEGLIGENCE ĠOR ĠOTHERWISE ) Ċ Ġ* ĠARISING ĠIN ĠANY ĠWAY ĠOUT ĠOF ĠTHE ĠUSE ĠOF ĠTHIS ĠSOFTWARE , ĠEVEN ĠIF ĠADVISED ĠOF ĠTHE Ċ Ġ* ĠPOSSIBILITY ĠOF ĠSUCH ĠDAMAGE . Ċ Ġ*/ Ċ package Ġcom . avail . interpreter . primitive . sets Ċ Ċ import Ġcom . avail . descriptor . functions . A _ Raw Function Ċ import Ġcom . avail . descriptor . numbers . A _ Number . Companion . equals Int Ċ import Ġcom . avail . descriptor . numbers . A _ Number . Companion . extract Int Ċ import Ġcom . avail . descriptor . numbers . Integer Descriptor . Companion . one Ċ import Ġcom . avail . descriptor . numbers . Integer Descriptor . Companion . zero Ċ import Ġcom . avail . descriptor . sets . Set Descriptor Ċ import Ġcom . avail . descriptor . tuples . A _ Tuple . Companion . as Set Ċ import Ġcom . avail . descriptor . tuples . Object Tuple Descriptor . Companion . tuple Ċ import Ġcom . avail . descriptor . tuples . Tuple Descriptor Ċ import Ġcom . avail . descriptor . types . A _ Type Ċ import Ġcom . avail . descriptor . types . A _ Type . Companion . lower Bound Ċ import Ġcom . avail . descriptor . types . A _ Type . Companion . size Range Ċ import Ġcom . avail . descriptor . types . A _ Type . Companion . tuple Of Types From To Ċ import Ġcom . avail . descriptor . types . A _ Type . Companion . union Of Types At Through Ċ import Ġcom . avail . descriptor . types . A _ Type . Companion . upper Bound Ċ import Ġcom . avail . descriptor . types . A _ Type . Companion . upper Inclusive Ċ import Ġcom . avail . descriptor . types . FunctionType Descriptor . Companion . function Type Ċ import Ġcom . avail . descriptor . types . Integer Range TypeDescriptor . Companion . integer Range Type Ċ import Ġcom . avail . descriptor . types . Set TypeDescriptor . Companion . most General Set Type Ċ import Ġcom . avail . descriptor . types . Set TypeDescriptor . Companion . setType For Sizes ContentType Ċ import Ġcom . avail . descriptor . types . Tuple TypeDescriptor . Companion . most General Tuple Type Ċ import Ġcom . avail . interpreter . Primitive Ċ import Ġcom . avail . interpreter . Primitive . Flag . Can Fold Ċ import Ġcom . avail . interpreter . Primitive . Flag . Can Inline Ċ import Ġcom . avail . interpreter . Primitive . Flag . Cannot Fail Ċ import Ġcom . avail . interpreter . execution . Interpreter Ċ import Ġcom . avail . interpreter . level Two . operand . L 2 Read Boxed Operand Ċ import Ġcom . avail . interpreter . level Two . operand . L 2 Read Boxed Vector Operand Ċ import Ġcom . avail . interpreter . level Two . operand . Type Restriction . Companion . restriction ForType Ċ import Ġcom . avail . interpreter . level Two . operand . Type Restriction . Restriction Flag Encoding Ċ import Ġcom . avail . interpreter . level Two . operation . L 2 _ CREATE _ SET Ċ import Ġcom . avail . optimizer . L 1 Translator Ċ Ċ /** Ċ Ġ* Ġ** Primitive :** ĠConvert Ġa Ġ[ tuple ][ Tuple Descriptor ] Ġinto Ġa Ċ Ġ* Ġ[ set ][ Set Descriptor ]. Ċ Ġ*/ Ċ @ Suppress (" unused ") Ċ object ĠP _ Tuple To Set Ġ: ĠPrimitive ( 1 , ĠCannot Fail , ĠCan Fold , ĠCan Inline ) Ċ { Ċ ĉ override Ġfun Ġattempt ( interpreter : ĠInterpreter ): ĠResult Ċ ĉ { Ċĉ ĉ interpreter . check Argument Count ( 1 ) Ċĉ ĉ val Ġtuple Ġ= Ġinterpreter . argument ( 0 ) Ċĉ ĉ return Ġinterpreter . primitive Success ( tuple . as Set ()) Ċ ĉ } ĊĊ ĉ override Ġfun Ġprivate BlockType Restriction (): ĠA _ Type Ġ= Ċĉ ĉ function Type ( Ċĉĉ ĉ tuple ( Ċĉĉĉ ĉ most General Tuple Type ()), Ċĉĉ ĉ most General Set Type ()) ĊĊ ĉ override Ġfun ĠreturnType Gu arante edBy VM ( Ċĉ ĉ raw Function : ĠA _ Raw Function , Ċĉ ĉ argument Types : ĠList < A _ Type >): ĠA _ Type Ċ ĉ { Ċĉ ĉ val Ġtuple Type Ġ= Ġargument Types [ 0 ] ĊĊĉ ĉ val Ġunion Type Ġ= Ġtuple Type . union Of Types At Through ( 1 , ĠInteger . MAX _ VALUE ) Ċĉ ĉ union Type . make Immutable () Ċĉ ĉ val Ġtuple Sizes Ġ= Ġtuple Type . size Range () Ċĉ ĉ // ĠTech n ically , Ġif Ġtwo Ġtuple Ġentries Ġhave Ġdis joint Ġtypes Ġthen Ġthe Ċĉ ĉ // Ġminimum Ġset Ġsize Ġis Ġtwo . Ġ ĠGeneral izing Ġthis Ġleads Ġto Ġcomputing Ġthe Ċĉ ĉ // ĠBir kh off Ġchrom atic Ġpolynomial Ġof Ġthe Ġgraph Ġwhose Ġvertices Ġare Ġthe Ċĉ ĉ // Ġtuple Ġsub scripts Ġand Ġwhich Ġhas Ġedges Ġwhen Ġtwo Ġtuple Ġsub script Ġtypes Ċĉ ĉ // Ġare Ġdis joint Ġ( their Ġintersection Ġis Ġbottom ). Ġ ĠThis Ġis Ġthe Ġopt imum Ċĉ ĉ // Ġbound Ġfor Ġthe Ġminimum Ġsize Ġof Ġthe Ġresulting Ġset . Ġ ĠThe Ġmaximum Ġcan Ġbe Ċĉ ĉ // Ġimproved Ġby Ġa Ġnot Ġyet Ġworked Ġout Ġpi geon Ġhole Ġprinciple Ġwhen Ġthere Ġare Ċĉ ĉ // Ġelement Ġtypes Ġwith Ġa Ġsmall Ġnumber Ġof Ġpossible Ġinstances Ġ( e . g ., Ċĉ ĉ // Ġenum er ations ) Ġand Ġthose Ġsets Ġof Ġinstances Ġoverlap Ġbetween Ġmany Ġtuple Ċĉ ĉ // Ġelements . Ġ ĠWe Ġdo Ġneither Ġoptimization Ġhere , Ġbut Ġwe Ġdo Ġnote Ġthat Ġonly Ċĉ ĉ // Ġthe Ġempty Ġtuple Ġcan Ġproduce Ġthe Ġempty Ġset , Ġand Ġthe Ġset Ġsize Ġis Ġnever Ċĉ ĉ // Ġgreater Ġthan Ġthe Ġtuple Ġsize . Ċĉ ĉ val Ġmin Size Ġ= Ċĉĉ ĉ if Ġ( tuple Sizes . lower Bound (). equals Int ( 0 )) Ġzero () Ċĉĉ ĉ else Ġone () Ċĉ ĉ val Ġset Sizes Ġ= Ġinteger Range Type ( Ċĉĉ ĉ min Size , Ċĉĉ ĉ true , Ċĉĉ ĉ tuple Sizes . upper Bound (), Ċĉĉ ĉ tuple Sizes . upper Inclusive ()) Ċĉ ĉ return Ġset Type For Sizes ContentType ( set Sizes , Ġunion Type ) Ċ ĉ } ĊĊ ĉ override Ġfun Ġtry To Generate Special Primitive Invocation ( Ċĉ ĉ function To Call Reg : ĠL 2 Read Boxed Operand , Ċĉ ĉ raw Function : ĠA _ Raw Function , Ċĉ ĉ arguments : ĠList < L 2 Read Boxed Operand >, Ċĉ ĉ argument Types : ĠList < A _ Type >, Ċĉ ĉ translator : ĠL 1 Translator , Ċĉ ĉ call Site Helper : ĠL 1 Translator . Call Site Helper ): ĠBoolean Ċ ĉ { Ċĉ ĉ val Ġtuple Reg Ġ= Ġarguments [ 0 ] ĊĊĉ ĉ val Ġgenerator Ġ= Ġtranslator . generator Ċĉ ĉ if Ġ(! generator . currently Reach able ()) Ċĉ ĉ { Ċĉĉ ĉ // ĠGenerator Ġis Ġnot Ġat Ġa Ġlive Ġposition , Ġso Ġpret end Ġwe Ġgenerated Ġthe Ċĉĉ ĉ // Ġcode Ġfor Ġthis Ġprimitive Ġinvocation . Ċĉĉ ĉ return Ġtrue Ċĉ ĉ } ĊĊĉ ĉ val Ġsize Range Ġ= Ġtuple Reg . type (). size Range () Ċĉ ĉ val Ġsize Ġ= Ġsize Range . lower Bound () Ċĉ ĉ if Ġ(! size . is Int Ġ|| Ġ! size Range . upper Bound (). equals ( size )) Ċĉĉ ĉ return Ġfalse Ċĉ ĉ val Ġsize Int Ġ= Ġsize . extract Int () Ċĉ ĉ val Ġelement Regs Ġ= Ġgenerator . explode Tuple If Possible ( Ċĉĉ ĉ tuple Reg , Ċĉĉ ĉ tuple Reg . type (). tuple Of Types From To ( 1 , Ġsize Int ). toList ()) Ċĉ ĉ element Regs Ġ?: Ġreturn Ġfalse ĊĊĉ ĉ // ĠCreate Ġthe Ġset Ġdirectly Ġfrom Ġthe Ġvalues . Ġ ĠThis Ġmay Ġturn Ġthe Ġtuple Ċĉ ĉ // Ġcreation Ġinstruction Ġinto Ġdead Ġcode . Ċĉ ĉ val Ġrestriction Ġ= ĠreturnType Gu arante edBy VM ( raw Function , Ġargument Types ) Ċĉ ĉ val Ġsemantic Result Ġ= Ġgenerator . primitive Invocation ( this , Ġarguments ) Ċĉ ĉ val Ġwrite Ġ= Ġgenerator . boxed Write ( Ċĉĉ ĉ semantic Result , Ċĉĉ ĉ restriction ForType ( restriction , ĠRe striction Flag Encoding . BOX ED )) Ċĉ ĉ generator . add Instruction ( Ċĉĉ ĉ L 2 _ CREATE _ SET , Ċĉĉ ĉ L 2 Read Boxed Vector Operand ( element Regs ), Ċĉĉ ĉ write ) Ċĉ ĉ call Site Helper . use Answer ( translator . read Boxed ( write )) Ċĉ ĉ return Ġtrue Ċ ĉ } Ċ } Ċ