Skip to content

Commit 0d03cf4

Browse files
committed
Update package names and enhance graph utilities
Renamed annotation package from `sschr15.aoc.annotations` to `com.sschr15.aoc.annotations` for better namespace organization. Added new utilities to Graph class for managing incoming and outgoing edges and introduced `indexOfMin` and `indexOfMax` functions for list processing.
1 parent 934da0a commit 0d03cf4

File tree

7 files changed

+164
-25
lines changed

7 files changed

+164
-25
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
package sschr15.aoc.annotations
1+
package com.sschr15.aoc.annotations
22

3+
@Retention(AnnotationRetention.SOURCE)
34
annotation class ExportIr
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
package sschr15.aoc.annotations
1+
package com.sschr15.aoc.annotations
22

33
/**
44
* Marks a function to be memoized by the compiler plugin.
55
*/
66
@Target(AnnotationTarget.FUNCTION)
7+
@Retention(AnnotationRetention.SOURCE)
78
annotation class Memoize

compiler-plugin/runtime-components/src/main/kotlin/OverflowUnderflowCheck.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@file:Suppress("NOTHING_TO_INLINE")
22

3-
package sschr15.aoc.annotations
3+
package com.sschr15.aoc.annotations
44

55
inline fun plus(a: Int, b: Int): Int = Math.addExact(a, b)
66
inline fun minus(a: Int, b: Int): Int = Math.subtractExact(a, b)

compiler-plugin/src/main/kotlin/Memoizer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Memoizer(private val context: IrPluginContext) : IrElementTransformerVoid(
4545
private val pair = context.referenceClass(ClassId(FqName("kotlin"), FqName("Pair"), false))!!
4646
private val triple = context.referenceClass(ClassId(FqName("kotlin"), FqName("Triple"), false))!!
4747

48-
private val memoizeAnnotation = FqName("sschr15.aoc.annotations.Memoize")
48+
private val memoizeAnnotation = FqName("com.sschr15.aoc.annotations.Memoize")
4949

5050
private fun IrPluginContext.keyFor(declaration: IrFunction): IrType = when (declaration.valueParameters.size) {
5151
1 -> declaration.valueParameters.single().type

compiler-plugin/src/main/kotlin/OverflowUnderflowChecker.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import org.jetbrains.kotlin.name.Name
2828

2929
@OptIn(UnsafeDuringIrConstructionAPI::class)
3030
class OverflowUnderflowChecker(private val context: IrPluginContext, private val config: CompilerConfiguration) : IrElementTransformerVoid() {
31-
val skipCheckAnnotation = FqName("sschr15.aoc.annotations.SkipOverflowUnderflowCheck")
31+
val skipCheckAnnotation = FqName("com.sschr15.aoc.annotations.SkipOverflowUnderflowCheck")
3232

3333
val singleTypeChecks = setOf(
3434
"plus", "minus", "times",
@@ -48,7 +48,7 @@ class OverflowUnderflowChecker(private val context: IrPluginContext, private val
4848
.irCall(
4949
context.referenceFunctions(
5050
CallableId(
51-
FqName("sschr15.aoc.annotations"),
51+
FqName("com.sschr15.aoc.annotations"),
5252
null,
5353
expression.symbol.owner.name,
5454
)
@@ -67,7 +67,7 @@ class OverflowUnderflowChecker(private val context: IrPluginContext, private val
6767
if (owner.name != absoluteValueName) return null
6868
return context.irBuiltIns.createIrBuilder(expression.symbol, expression.startOffset, expression.endOffset)
6969
.irCall(context.referenceFunctions(CallableId(
70-
FqName("sschr15.aoc.annotations"),
70+
FqName("com.sschr15.aoc.annotations"),
7171
null,
7272
Name.identifier("abs"),
7373
)).single { it.owner.valueParameters.single().type == extension.type })
@@ -80,7 +80,7 @@ class OverflowUnderflowChecker(private val context: IrPluginContext, private val
8080
if (declaration.annotations.any { it.isAnnotationWithEqualFqName(skipCheckAnnotation) })
8181
return declaration // skip checking this and all children
8282

83-
if (declaration.annotations.any { it.isAnnotationWithEqualFqName(FqName("sschr15.aoc.annotations.ExportIr")) }) {
83+
if (declaration.annotations.any { it.isAnnotationWithEqualFqName(FqName("com.sschr15.aoc.annotations.ExportIr")) }) {
8484
config.report(CompilerMessageSeverity.WARNING, declaration.dumpKotlinLike())
8585
config.report(CompilerMessageSeverity.WARNING, declaration.dump())
8686
}
@@ -119,7 +119,7 @@ class OverflowUnderflowChecker(private val context: IrPluginContext, private val
119119

120120
return context.irBuiltIns.createIrBuilder(expression.symbol, expression.startOffset, expression.endOffset)
121121
.irCall(context.referenceFunctions(CallableId(
122-
FqName("sschr15.aoc.annotations"),
122+
FqName("com.sschr15.aoc.annotations"),
123123
null,
124124
expression.symbol.owner.name,
125125
)).single { it.owner.valueParameters.first().type == expression.type }).apply {

src/main/kotlin/sschr15/aocsolutions/util/Graphs.kt

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ import kotlin.collections.ArrayDeque
1616
*/
1717
class Graph<T> {
1818
inner class Node internal constructor(val name: String? = null, val value: T) {
19-
val edges = mutableListOf<Edge>()
19+
private val _edges = mutableListOf<Edge>()
20+
private val _incoming = mutableListOf<Edge>()
21+
private val _outgoing = mutableListOf<Edge>()
22+
23+
val edges: List<Edge> get() = _edges
24+
val incomingEdges: List<Edge> get() = _incoming
25+
val outgoingEdges: List<Edge> get() = _outgoing
2026

2127
/**
2228
* Connects the current node to another node, creating a bidirectional edge between them.
@@ -26,8 +32,15 @@ class Graph<T> {
2632
* edge list are updated to include this new connection.
2733
*/
2834
fun connectTo(other: Node, weight: Int? = null) = Edge(this, other, weight).also {
29-
edges.add(it)
30-
other.edges.add(it.reversed())
35+
_edges.add(it)
36+
_incoming.add(it)
37+
other._outgoing.add(it)
38+
39+
val rev = it.reversed()
40+
_outgoing.add(rev)
41+
other._edges.add(rev)
42+
other._incoming.add(rev)
43+
3144
this@Graph.edges.add(it)
3245
}
3346

@@ -39,7 +52,9 @@ class Graph<T> {
3952
* and the parent graph's edge list.
4053
*/
4154
fun oneWayConnectTo(other: Node, weight: Int? = null) = Edge(this, other, weight).also {
42-
edges.add(it)
55+
_edges.add(it)
56+
_outgoing.add(it)
57+
other._incoming.add(it)
4358
this@Graph.edges.add(it)
4459
}
4560

@@ -79,18 +94,6 @@ class Graph<T> {
7994

8095
override fun toString() = "Graph(nodes=$nodes, edges=$edges)"
8196

82-
fun removeNode(node: Node) {
83-
nodes.remove(node)
84-
edges.removeAll(node.edges.toSet())
85-
node.edges.forEach { it.to.edges.remove(it.reversed()) }
86-
}
87-
88-
fun removeEdge(edge: Edge) {
89-
edges.remove(edge)
90-
edge.from.edges.remove(edge)
91-
edge.to.edges.remove(edge.reversed())
92-
}
93-
9497
/**
9598
* Converts the graph represented by the current instance into a Graphviz DOT format string.
9699
*

src/main/kotlin/sschr15/aocsolutions/util/IterableUtils.kt

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,137 @@ inline fun <T> Iterable<T>.noneIndexed(predicate: (Int, T) -> Boolean): Boolean
359359
}
360360
return true
361361
}
362+
363+
inline fun List<Int>.indexOfMin(): Int {
364+
if (isEmpty()) return -1
365+
if (size == 1) return 0
366+
367+
var min = Int.MAX_VALUE
368+
var minIndex = -1
369+
for ((i, value) in this.withIndex()) {
370+
if (value < min) {
371+
min = value
372+
minIndex = i
373+
}
374+
}
375+
if (minIndex == -1) return 0 // All values are Int.MAX_VALUE
376+
return minIndex
377+
}
378+
379+
@JvmName("longIndexOfMin")
380+
inline fun List<Long>.indexOfMin(): Int {
381+
if (isEmpty()) return -1
382+
if (size == 1) return 0
383+
384+
var min = Long.MAX_VALUE
385+
var minIndex = -1
386+
for ((i, value) in this.withIndex()) {
387+
if (value < min) {
388+
min = value
389+
minIndex = i
390+
}
391+
}
392+
if (minIndex == -1) return 0
393+
return minIndex
394+
}
395+
396+
@JvmName("floatIndexOfMin")
397+
inline fun List<Float>.indexOfMin(): Int {
398+
if (isEmpty()) return -1
399+
if (size == 1) return 0
400+
401+
var min = Float.POSITIVE_INFINITY
402+
var minIndex = -1
403+
for ((i, value) in this.withIndex()) {
404+
if (value < min) {
405+
min = value
406+
minIndex = i
407+
}
408+
}
409+
if (minIndex == -1) return 0
410+
return minIndex
411+
}
412+
413+
@JvmName("doubleIndexOfMin")
414+
inline fun List<Double>.indexOfMin(): Int {
415+
if (isEmpty()) return -1
416+
if (size == 1) return 0
417+
418+
var min = Double.POSITIVE_INFINITY
419+
var minIndex = -1
420+
for ((i, value) in this.withIndex()) {
421+
if (value < min) {
422+
min = value
423+
minIndex = i
424+
}
425+
}
426+
if (minIndex == -1) return 0
427+
return minIndex
428+
}
429+
430+
inline fun List<Int>.indexOfMax(): Int {
431+
if (isEmpty()) return -1
432+
if (size == 1) return 0
433+
434+
var max = Int.MIN_VALUE
435+
var maxIndex = -1
436+
for ((i, value) in this.withIndex()) {
437+
if (value > max) {
438+
max = value
439+
maxIndex = i
440+
}
441+
}
442+
if (maxIndex == -1) return 0
443+
return maxIndex
444+
}
445+
446+
@JvmName("longIndexOfMax")
447+
inline fun List<Long>.indexOfMax(): Int {
448+
if (isEmpty()) return -1
449+
if (size == 1) return 0
450+
451+
var max = Long.MIN_VALUE
452+
var maxIndex = -1
453+
for ((i, value) in this.withIndex()) {
454+
if (value > max) {
455+
max = value
456+
maxIndex = i
457+
}
458+
}
459+
if (maxIndex == -1) return 0
460+
return maxIndex
461+
}
462+
463+
@JvmName("floatIndexOfMax")
464+
inline fun List<Float>.indexOfMax(): Int {
465+
if (isEmpty()) return -1
466+
if (size == 1) return 0
467+
468+
var max = Float.NEGATIVE_INFINITY
469+
var maxIndex = -1
470+
for ((i, value) in this.withIndex()) {
471+
if (value > max) {
472+
max = value
473+
maxIndex = i
474+
}
475+
}
476+
if (maxIndex == -1) return 0
477+
return maxIndex
478+
}
479+
480+
@JvmName("doubleIndexOfMax")
481+
inline fun List<Double>.indexOfMax(): Int {
482+
if (isEmpty()) return -1
483+
if (size == 1) return 0
484+
485+
var max = Double.NEGATIVE_INFINITY
486+
var maxIndex = -1
487+
for ((i, value) in this.withIndex()) {
488+
if (value > max) {
489+
max = value
490+
maxIndex = i
491+
}
492+
}
493+
if (maxIndex == -1) return 0
494+
return maxIndex
495+
}

0 commit comments

Comments
 (0)