Skip to content

Commit 98cac42

Browse files
change fun name + linting + improve logic
1 parent 8b4ca9e commit 98cac42

File tree

3 files changed

+23
-14
lines changed
  • core/src
    • main/kotlin/org/jetbrains/kotlinx/dataframe
    • test/kotlin/org/jetbrains/kotlinx/dataframe/api

3 files changed

+23
-14
lines changed

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/move.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.jetbrains.kotlinx.dataframe.documentation.SelectingColumns
2222
import org.jetbrains.kotlinx.dataframe.impl.api.afterOrBefore
2323
import org.jetbrains.kotlinx.dataframe.impl.api.moveImpl
2424
import org.jetbrains.kotlinx.dataframe.impl.api.moveTo
25+
import org.jetbrains.kotlinx.dataframe.impl.api.moveToImpl
2526
import org.jetbrains.kotlinx.dataframe.ncol
2627
import org.jetbrains.kotlinx.dataframe.util.DEPRECATED_ACCESS_API
2728
import org.jetbrains.kotlinx.dataframe.util.MOVE_TO_LEFT
@@ -560,7 +561,8 @@ public fun <T, C> MoveClause<T, C>.to(columnIndex: Int): DataFrame<T> = moveTo(c
560561
*/
561562
@Refine
562563
@Interpretable("MoveTo")
563-
public fun <T, C> MoveClause<T, C>.to(columnIndex: Int, insideGroup: Boolean): DataFrame<T> = moveTo(columnIndex, insideGroup)
564+
public fun <T, C> MoveClause<T, C>.to(columnIndex: Int, insideGroup: Boolean): DataFrame<T> =
565+
moveToImpl(columnIndex, insideGroup)
564566

565567
/**
566568
* Moves columns, previously selected with [move] to the top-level within the [DataFrame].

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/move.kt

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,11 @@ internal fun <T, C> MoveClause<T, C>.moveTo(columnIndex: Int): DataFrame<T> {
129129
return newColumnList.toDataFrame().cast()
130130
}
131131

132-
internal fun <T, C> MoveClause<T, C>.moveTo(columnIndex: Int, insideGroup: Boolean): DataFrame<T> {
132+
internal fun <T, C> MoveClause<T, C>.moveToImpl(columnIndex: Int, insideGroup: Boolean): DataFrame<T> {
133+
if (!insideGroup) {
134+
return moveTo(columnIndex)
135+
}
136+
133137
val columnsToMove = df.getColumns(columns)
134138

135139
// check if columns to move have the same parent
@@ -142,7 +146,7 @@ internal fun <T, C> MoveClause<T, C>.moveTo(columnIndex: Int, insideGroup: Boole
142146
}
143147

144148
// if columns will be moved to top level or columns to move are at top level
145-
if (!insideGroup || parentOfFirst.isEmpty()) {
149+
if (parentOfFirst.isEmpty()) {
146150
return moveTo(columnIndex)
147151
}
148152

@@ -151,10 +155,10 @@ internal fun <T, C> MoveClause<T, C>.moveTo(columnIndex: Int, insideGroup: Boole
151155
val parentPath = df[parentOfFirst].path
152156
val referenceAndSiblings = df[parentOfFirst].asColumnGroup().columns()
153157
val referenceAndSiblingsPaths = referenceAndSiblings.map { parentPath + it.path }
154-
val reference = if (columnIndex >= referenceAndSiblingsPaths.size){
158+
val reference = if (columnIndex >= referenceAndSiblingsPaths.size) {
155159
referenceAndSiblingsPaths.last()
156160
} else {
157-
referenceAndSiblingsPaths.get(columnIndex)
161+
referenceAndSiblingsPaths[columnIndex]
158162
}
159163

160164
// two cases : columns to move are before, or after, the reference
@@ -163,14 +167,15 @@ internal fun <T, C> MoveClause<T, C>.moveTo(columnIndex: Int, insideGroup: Boole
163167
val columnsToMovePath = columnsToMove.map { it.path }
164168
var referenceWasIterated = false
165169
referenceAndSiblingsPaths.forEach {
166-
if (it.path.last() == reference.path.last())
170+
if (it.path.last() == reference.path.last()) {
167171
referenceWasIterated = true
168-
else {
169-
if (columnsToMovePath.contains(it)){
170-
if (referenceWasIterated)
172+
} else {
173+
if (columnsToMovePath.contains(it)) {
174+
if (referenceWasIterated) {
171175
columnsAfterReferenceToMove.add(it)
172-
else
176+
} else {
173177
columnsBeforeReferenceToMove.add(it)
178+
}
174179
}
175180
}
176181
}
@@ -182,10 +187,12 @@ internal fun <T, C> MoveClause<T, C>.moveTo(columnIndex: Int, insideGroup: Boole
182187
val finalDf = intermediateDf.move { columnsAfterReferenceToMove.toColumnSet() }.after { newReference }
183188
return finalDf
184189
}
185-
if (columnsBeforeReferenceToMove.isNotEmpty())
190+
if (columnsBeforeReferenceToMove.isNotEmpty()) {
186191
return df.move { columnsBeforeReferenceToMove.toColumnSet() }.after { reference }
187-
if (columnsAfterReferenceToMove.isNotEmpty())
192+
}
193+
if (columnsAfterReferenceToMove.isNotEmpty()) {
188194
return df.move { columnsAfterReferenceToMove.toColumnSet() }.before { reference }
195+
}
189196

190197
// if it is not needed to move any of the nested columns
191198
return df

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/move.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ class MoveTests {
235235
df["b"].asColumnGroup().columnNames() shouldBe listOf("d", "c")
236236
}
237237

238-
//not working
238+
// not working
239239
@Test
240240
fun `move single nested column between columns remaining inside the group`() {
241241
// creating an appropriate df for the test
@@ -275,7 +275,7 @@ class MoveTests {
275275
@Test
276276
fun `move multiple non bordering nested columns`() {
277277
// creating an appropriate df for the test
278-
val groupedModified = grouped.move("r" , "q").before { "b"["c"] }
278+
val groupedModified = grouped.move("r", "q").before { "b"["c"] }
279279
groupedModified["b"].asColumnGroup().columnNames() shouldBe listOf("r", "q", "c", "d")
280280
// test itself
281281
val df = groupedModified.move { "b"["r"] and "b"["d"] }.to(1, true)

0 commit comments

Comments
 (0)