-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generalise
SSubsetStruct
to views on SBaseStruct
s
- Loading branch information
Showing
11 changed files
with
231 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
hail/src/main/scala/is/hail/types/physical/stypes/concrete/SStructView.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package is.hail.types.physical.stypes.concrete | ||
|
||
import is.hail.annotations.Region | ||
import is.hail.asm4s.{Settable, TypeInfo, Value} | ||
import is.hail.expr.ir.{EmitCodeBuilder, IEmitCode} | ||
import is.hail.types.physical.{PCanonicalStruct, PType} | ||
import is.hail.types.physical.stypes.{EmitType, SType, SValue} | ||
import is.hail.types.physical.stypes.interfaces.{SBaseStruct, SBaseStructSettable, SBaseStructValue} | ||
import is.hail.types.virtual.{TBaseStruct, TStruct, Type} | ||
|
||
object SStructView { | ||
def subset(fieldnames: IndexedSeq[String], struct: SBaseStruct): SStructView = | ||
struct match { | ||
case s @ SStructView(parent, restrict, rename) => | ||
SStructView( | ||
parent, | ||
restrict.typeAfterSelect(fieldnames.map(s.fieldIdx)), | ||
rename.typeAfterSelectNames(fieldnames), | ||
) | ||
|
||
case s => | ||
val restrict = s.virtualType.asInstanceOf[TStruct].typeAfterSelectNames(fieldnames) | ||
SStructView(s, restrict, restrict) | ||
} | ||
} | ||
|
||
// A 'view' on `SBaseStruct`s, ie one that presents an upcast and/or renamed facade on another | ||
final case class SStructView(parent: SBaseStruct, restrict: TStruct, rename: TStruct) | ||
extends SBaseStruct { | ||
|
||
assert( | ||
restrict.canCastTo(rename), | ||
f"""Upcast operations are not isomorphic | ||
| restrict: '${restrict._toPretty}' | ||
| rename: '${rename._toPretty}' | ||
|""".stripMargin, | ||
) | ||
|
||
override val size: Int = | ||
restrict.fields.length | ||
|
||
lazy val newToOldFieldMapping: Map[Int, Int] = | ||
restrict.fields.view.map(f => f.index -> parent.fieldIdx(f.name)).toMap | ||
|
||
override lazy val fieldTypes: IndexedSeq[SType] = | ||
Array.tabulate(size) { i => | ||
parent | ||
.fieldTypes(newToOldFieldMapping(i)) | ||
.castRename(rename.fields(i).typ) | ||
} | ||
|
||
override lazy val fieldEmitTypes: IndexedSeq[EmitType] = | ||
Array.tabulate(size) { i => | ||
parent | ||
.fieldEmitTypes(newToOldFieldMapping(i)) | ||
.copy(st = fieldTypes(i)) | ||
} | ||
|
||
override def virtualType: TBaseStruct = | ||
rename | ||
|
||
override def fieldIdx(fieldName: String): Int = | ||
rename.fieldIdx(fieldName) | ||
|
||
override def castRename(t: Type): SType = | ||
SStructView(parent, restrict, rename = t.asInstanceOf[TStruct]) | ||
|
||
override def _coerceOrCopy( | ||
cb: EmitCodeBuilder, | ||
region: Value[Region], | ||
value: SValue, | ||
deepCopy: Boolean, | ||
): SValue = { | ||
if (deepCopy) | ||
throw new NotImplementedError("Deep copy on struct view") | ||
|
||
value.st match { | ||
case s: SStructView if this == s && !deepCopy => | ||
value | ||
} | ||
} | ||
|
||
override def settableTupleTypes(): IndexedSeq[TypeInfo[_]] = | ||
parent.settableTupleTypes() | ||
|
||
override def fromSettables(settables: IndexedSeq[Settable[_]]): SStructViewSettable = | ||
new SStructViewSettable( | ||
this, | ||
parent.fromSettables(settables).asInstanceOf[SBaseStructSettable], | ||
) | ||
|
||
override def fromValues(values: IndexedSeq[Value[_]]): SStructViewValue = | ||
new SStructViewValue(this, parent.fromValues(values).asInstanceOf[SBaseStructValue]) | ||
|
||
override def copiedType: SType = | ||
if (virtualType.size < 64) | ||
SStackStruct(virtualType, fieldEmitTypes.map(_.copiedType)) | ||
else { | ||
val ct = SBaseStructPointer(storageType().asInstanceOf[PCanonicalStruct]) | ||
assert(ct.virtualType == virtualType, s"ct=$ct, this=$this") | ||
ct | ||
} | ||
|
||
def storageType(): PType = { | ||
val pt = PCanonicalStruct( | ||
required = false, | ||
args = rename.fieldNames.zip(fieldEmitTypes.map(_.copiedType.storageType)): _*, | ||
) | ||
assert(pt.virtualType == virtualType, s"pt=$pt, this=$this") | ||
pt | ||
} | ||
|
||
// aspirational implementation | ||
// def storageType(): PType = StoredSTypePType(this, false) | ||
|
||
override def containsPointers: Boolean = | ||
parent.containsPointers | ||
|
||
override def equals(obj: Any): Boolean = | ||
obj match { | ||
case s: SStructView => | ||
rename == s.rename && | ||
newToOldFieldMapping == s.newToOldFieldMapping && | ||
parent == s.parent // todo test isIsomorphicTo | ||
case _ => | ||
false | ||
} | ||
} | ||
|
||
class SStructViewValue(val st: SStructView, val prev: SBaseStructValue) extends SBaseStructValue { | ||
|
||
override lazy val valueTuple: IndexedSeq[Value[_]] = | ||
prev.valueTuple | ||
|
||
override def subset(fieldNames: String*): SBaseStructValue = | ||
new SStructViewValue(SStructView.subset(fieldNames.toIndexedSeq, st), prev) | ||
|
||
override def loadField(cb: EmitCodeBuilder, fieldIdx: Int): IEmitCode = | ||
prev | ||
.loadField(cb, st.newToOldFieldMapping(fieldIdx)) | ||
.map(cb)(_.castRename(st.virtualType.fields(fieldIdx).typ)) | ||
|
||
override def isFieldMissing(cb: EmitCodeBuilder, fieldIdx: Int): Value[Boolean] = | ||
prev.isFieldMissing(cb, st.newToOldFieldMapping(fieldIdx)) | ||
} | ||
|
||
final class SStructViewSettable(st: SStructView, prev: SBaseStructSettable) | ||
extends SStructViewValue(st, prev) with SBaseStructSettable { | ||
override def subset(fieldNames: String*): SBaseStructValue = | ||
new SStructViewSettable(SStructView.subset(fieldNames.toIndexedSeq, st), prev) | ||
|
||
override def settableTuple(): IndexedSeq[Settable[_]] = | ||
prev.settableTuple() | ||
|
||
override def store(cb: EmitCodeBuilder, pv: SValue): Unit = | ||
prev.store(cb, pv.asInstanceOf[SStructViewValue].prev) | ||
} |
144 changes: 0 additions & 144 deletions
144
hail/src/main/scala/is/hail/types/physical/stypes/concrete/SSubsetStruct.scala
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.