Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Collections] [Hooks] API enhancment #29

Open
lounres opened this issue Oct 25, 2022 · 0 comments
Open

[Collections] [Hooks] API enhancment #29

lounres opened this issue Oct 25, 2022 · 0 comments
Labels
enhancement New feature or request module:main:collections Concerns the [Collections] module module:main:hooks Concerns the [Hooks] module to discuss Topic to be discussed and/or thought over

Comments

@lounres
Copy link
Owner

lounres commented Oct 25, 2022

  1. Fix temporary collections API and come up with a new one (like from Java API but better?).

  2. Hooks. A part of abstract algorithms works like (or precisely are) dynamic algorithms. It means there is an input data that is changing and the output should be changed along. It also means that there should be an API to simplify representation of such relationship.

    Suggestion is that there should be a Hook interface which declares API for hooking on changes viewers. In that case all the algorithms can be represented as a simple object which state depends on its arguments and, hence, can be called "views". For example,

    /**
     * @param C type that is representing data change
     * @param I type of intermediate data representation
     */
    interface Hook<C, I> {
        fun hookOn(callback: I.(change: C) -> Unit)
    }
    interface View<E> {
        val value: E
    } 
    
    // ...
    
    sealed interface ListChange<E> {
        data class Assign<E>(val index: Int, val oldElement: E, val newElement: E): ListChange<E>
        data class Insert<E>(val index: Int, val newElement: E): ListChange<E>
        data class Remove<E>(val index: Int, val oldElement: E): ListChange<E>
    }
    interface MutableListHook<E>: MutableList<E>, Hook<ListChange<E>, List<E>>
    class MutableListHookImpl<E>(private val list: MutableList<E>): MutableList<E> by list, MutableListHook<E> {
        private val hooks = mutableListOf<List<E>.(ListChange<E>) -> Unit>()
        override fun hookOn(callback: List<E>.(change: ListChange<E>) -> Unit) { hooks.add(callback) }
        private fun trigger(change: ListChange<E>) { hooks.forEach { list.it(change) } }
    
        override fun add(element: E): Boolean {
            trigger(ListChange.Insert(list.size, element))
            return list.add(element)
        }
        // ...
    }
    
    // ...
    
    class DynamicMaximumValueView<E>(hook: MutableListHook<E>, comparator: Comparator<E>): View<E> {
        private val heap = MaxHeap(hook, comparator)
        init {
            hook.hookOn {
                when (it) {
                    is ListChange.Assign -> {
                        heap.remove(it.oldElement)
                        heap.add(it.newElement)
                    }
                    is ListChange.Insert -> {
                        heap.add(it.newElement)
                    }
                    is ListChange.Remove -> {
                        heap.remove(it.oldElement)
                    }
                }
            }
        }
        override val value: E get() = heap.head
    }
@lounres lounres changed the title Collections: Improve API Collections: API enhancment Oct 25, 2022
@lounres lounres added enhancement New feature or request to discuss Topic to be discussed and/or thought over labels Oct 25, 2022
@lounres lounres changed the title Collections: API enhancment [Collections] [Hooks] API enhancment Mar 24, 2023
@lounres lounres added module:main:collections Concerns the [Collections] module module:main:hooks Concerns the [Hooks] module labels Mar 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request module:main:collections Concerns the [Collections] module module:main:hooks Concerns the [Hooks] module to discuss Topic to be discussed and/or thought over
Projects
None yet
Development

No branches or pull requests

1 participant