Mobile Apps 2023W Lecture 16

From Soma-notes
Jump to navigation Jump to search

Notes

March 10
--------

I have a hostname, want to insert it into the current HNTree
 - split the hostname into a bunch of components
    (www, carleton, ca)

First, initially, we'll create a root node representing . (top of the DNS hierarchy).  So that should get its own dedicated constructor.

Then we'll have an add operation which will take a hostname, and it will add child hostnames

add will split the hostname into its components, see if the last-most suffix 

www.carleton.ca is the same as www.carleton.ca.

Code

package carleton.comp2601.historyviewer

class HNTree {
    var hostComponent: String
    var hostSuffix: String
    var parent: HNTree
    var children: MutableList<HNTree>

    constructor() {
        /* Creates the "root" node by default, rest are created using
           different constructor
         */
        hostComponent = ""
        hostSuffix = "."
        parent = this
        children = mutableListOf()
    }

    fun add(hostPrefix: String) {
        var allComponents: List<String>
        var component: String
        var child: HNTree? = null

        /* still have to deal with getting to the end */

        allComponents = hostPrefix.split(".")
        component = allComponents.last()

        if (component == hostPrefix) {
            return
        }

        for (c in this.children) {
                 if (c.hostComponent == component) {
                     child = c
                     break
                 }
        }

        if (child == null) {
            child = HNTree()
            child.parent = this
            child.hostComponent = component
            child.hostSuffix = "$component.$hostSuffix"
            children.add(child)
        }

        child.add(allComponents.dropLast(1).joinToString())
    }
}