We often need to "reindex" one array/vec/series/frame so as to produce a new one.
First, note that an array A of values of type T is actually a mapping defined as
f
[0, A.length) => {T}
That is, each integer index of the array yields a value of type T when it is de-referenced.
ReIndexer has two fields, lTake and rTake, which are array-based maps defined for some integers M and N as
g
[0, N) => [-1, M)
In other words, lTake and rTake are arrays of length N whose entries are integers between -1 and M-1. We call these "indexers".
Given a reindexer, g, a re-indexing operation on array A yields a new array B of length N which represents the following mapping:
B
[0, N) => {T}
where B(i) == f(g(i)) if g(i) != -1
== NA if g(i) == -1
For this to work, the maximum value M in the co-domain of g must be <= A.length. We also augment the mapping f to send -1 to the corresponding NA value of type T.
For example, suppose we have Index(0,1,2,4) and Index(0,1,2,3). Performing a full outer join would yield the following ReIndexer:
- lTake = {0, 1, 2, -1, 3},
- rTake = {0, 1, 2, 3, -1},
- index = Index(0, 1, 2, 3, 4).
These indexers are then amenable to using with org.saddle.array.take to select elements out of an indexed data structure.
A performance optimization is to make lTake and rTake of type Option[Array], where we make a value of None behave as if it were an array of [0, N). So, the "taking" code knows to take the original values. You will therefore see code that looks like:
val v = Vec(...)
val ixer = ReIndexer(...)
ixer.lTake.map(x => v.take(x)) getOrElse v
Attributes
- Companion
- object
- Graph
-
- Supertypes
-
class Objecttrait Matchableclass Any