| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- package modules
-
- import (
- "errors"
- "fmt"
- )
-
- // Json Type의 sub Info
- type mytype interface{}
-
- var (
- list LList
- count_sub int // Subscription #
- emptylist []int // To administrate sub #
- num2sub = make(map[int]mytype) // num2sub[sub#] = subscription info
- israngesub = make(map[int]bool)
- )
-
- type LList struct {
- head *Node
- tail *Node
- size int
- }
-
- type Node struct {
- val int // Encrypt된 value
- next *Node
-
- // single //
- single2sub_s []int // val보다 작은 sub#
- single2sub_es []int // val보다 작거나 같은 sub#
- single2sub_b []int // val보다 큰 sub#
- single2sub_eb []int // val보다 크거나 같은 sub#
- single2sub_e []int // val과 같은 sub#
-
- // range //
- range2sub_s []int
- range2sub_es []int
- range2sub_b []int
- range2sub_eb []int
- }
-
- // ### To delete slice Array
- func remove(s []int, i int) []int {
- s[i] = s[len(s)-1]
- return s[:len(s)-1]
- }
-
- // ### To Convert Subinfo -> <value, op, sub#>
- // func Conv2info() mytype {
- // }
-
- // ### To Sort linkedList -> 다시 고쳐야함
- func MergeSort(head *Node, k int) *Node {
- if head == nil || head.next == nil {
- return head
- }
- cur := head
- nxt := head.next
- for nxt != nil && nxt.next != nil {
- cur = cur.next
- nxt = nxt.next.next
- }
- left := cur
- right := cur.next
- cur.next = nil
- left = MergeSort(left, k/2)
- right = MergeSort(right, k/2)
- fmt.Println(left, right)
- return Merge(left, right)
- }
-
- func Merge(l *Node, r *Node) *Node {
- if l == nil {
- return r
- }
- if r == nil {
- return l
- }
- t := new(Node)
- if l.val < r.val {
- t = l
- t.next = Merge(l.next, r)
- } else {
- t = r
- t.next = Merge(l, r.next)
- }
- return t
- }
-
- // ### To Insert sub#
- func (lk *LList) PushBack(val int) {
-
- // pseudocode
- // if value == val인 노드가 존재
- // -> node 추가 x
- // -> val < x에 sub# append
- // else
- // -> 끝에 노드추가
- // node := &Node{val: val}
- // if lk.head == nil {
- // lk.head, lk.tail = node, node
- // } else {
- // lk.tail.next = node
- // lk.tail = node
- // }
- // lk.size++
- }
-
- // ### To delete sub#
- func (lk *LList) delete(subnumber int) error {
- // pseudocode
- // if sub#가 num2sub에 있는지
- // -> 없으면 무시
- // else if rangesub에 있는지
- // if rangesub에 있으면
- // delete(range2sub_b, israngesub[sub#])
- // else 없으면
- // delete(all(single_node)) ...
-
- // -- 노드 자체가 사라진다면 --
- // for cur := &lk.head; *cur != nil; {
- // if (*cur).val == val {
- // lk.size--
- // *cur = (*cur).next
- // } else {
- // lk.tail = *cur
- // cur = &(*cur).next
- // }
- // }
- if num2sub[subnumber] == true {
- if israngesub[subnumber] == true {
- delete(israngesub, subnumber)
- ptr := lk.head
- for ptr != nil {
- for j := 0; j < len(ptr.range2sub_b); j++ {
- if ptr.range2sub_b[j] == subnumber {
- ptr.range2sub_b = remove(ptr.range2sub_b, j)
- return nil
- }
- }
-
- for j := 0; j < len(ptr.range2sub_eb); j++ {
- if ptr.range2sub_eb[j] == subnumber {
- ptr.range2sub_eb = remove(ptr.range2sub_eb, j)
- return nil
- }
- }
-
- for j := 0; j < len(ptr.range2sub_s); j++ {
- if ptr.range2sub_s[j] == subnumber {
- ptr.range2sub_s = remove(ptr.range2sub_s, j)
- return nil
- }
- }
-
- for j := 0; j < len(ptr.range2sub_es); j++ {
- if ptr.range2sub_es[j] == subnumber {
- ptr.range2sub_es = remove(ptr.range2sub_es, j)
- return nil
- }
- }
- }
- }
- } else {
- return errors.New("Don't delete this Subscription (Not exist)")
- }
- return nil
- }
|