Quellcode durchsuchen

파일 업로드 'modules'

initial project setup
secure
extra1563 vor 4 Jahren
Ursprung
Commit
5b4b505d39
2 geänderte Dateien mit 206 neuen und 0 gelöschten Zeilen
  1. 169
    0
      modules/matching.go
  2. 37
    0
      modules/matching_test.go

+ 169
- 0
modules/matching.go Datei anzeigen

@@ -0,0 +1,169 @@
1
+package modules
2
+
3
+import (
4
+	"errors"
5
+	"fmt"
6
+)
7
+
8
+// Json Type의 sub Info
9
+type mytype interface{}
10
+
11
+var (
12
+	list       LList
13
+	count_sub  int                    // Subscription #
14
+	emptylist  []int                  // To administrate sub #
15
+	num2sub    = make(map[int]mytype) // num2sub[sub#] = subscription info
16
+	israngesub = make(map[int]bool)
17
+)
18
+
19
+type LList struct {
20
+	head *Node
21
+	tail *Node
22
+	size int
23
+}
24
+
25
+type Node struct {
26
+	val  int // Encrypt된 value
27
+	next *Node
28
+
29
+	// single //
30
+	single2sub_s  []int // val보다 작은 sub#
31
+	single2sub_es []int // val보다 작거나 같은 sub#
32
+	single2sub_b  []int // val보다 큰 sub#
33
+	single2sub_eb []int // val보다 크거나 같은 sub#
34
+	single2sub_e  []int // val과 같은 sub#
35
+
36
+	// range //
37
+	range2sub_s  []int
38
+	range2sub_es []int
39
+	range2sub_b  []int
40
+	range2sub_eb []int
41
+}
42
+
43
+// ### To delete slice Array
44
+func remove(s []int, i int) []int {
45
+	s[i] = s[len(s)-1]
46
+	return s[:len(s)-1]
47
+}
48
+
49
+//  ### To Convert Subinfo -> <value, op, sub#>
50
+// func Conv2info() mytype {
51
+// }
52
+
53
+// ### To Sort linkedList -> 다시 고쳐야함
54
+func MergeSort(head *Node, k int) *Node {
55
+	if head == nil || head.next == nil {
56
+		return head
57
+	}
58
+	cur := head
59
+	nxt := head.next
60
+	for nxt != nil && nxt.next != nil {
61
+		cur = cur.next
62
+		nxt = nxt.next.next
63
+	}
64
+	left := cur
65
+	right := cur.next
66
+	cur.next = nil
67
+	left = MergeSort(left, k/2)
68
+	right = MergeSort(right, k/2)
69
+	fmt.Println(left, right)
70
+	return Merge(left, right)
71
+}
72
+
73
+func Merge(l *Node, r *Node) *Node {
74
+	if l == nil {
75
+		return r
76
+	}
77
+	if r == nil {
78
+		return l
79
+	}
80
+	t := new(Node)
81
+	if l.val < r.val {
82
+		t = l
83
+		t.next = Merge(l.next, r)
84
+	} else {
85
+		t = r
86
+		t.next = Merge(l, r.next)
87
+	}
88
+	return t
89
+}
90
+
91
+//	### To Insert sub#
92
+func (lk *LList) PushBack(val int) {
93
+
94
+	// pseudocode
95
+	// if value == val인 노드가 존재
96
+	// 	-> node 추가 x
97
+	//  -> val < x에 sub# append
98
+	// else
99
+	// 	-> 끝에 노드추가
100
+	// node := &Node{val: val}
101
+	// if lk.head == nil {
102
+	// 	lk.head, lk.tail = node, node
103
+	// } else {
104
+	// 	lk.tail.next = node
105
+	// 	lk.tail = node
106
+	// }
107
+	// lk.size++
108
+}
109
+
110
+//	### To delete sub#
111
+func (lk *LList) delete(subnumber int) error {
112
+	// pseudocode
113
+	// if sub#가 num2sub에 있는지
114
+	// 	-> 없으면 무시
115
+	// else if rangesub에 있는지
116
+	// 	 if rangesub에 있으면
117
+	// 			delete(range2sub_b, israngesub[sub#])
118
+	// 	 else 없으면
119
+	//			delete(all(single_node)) ...
120
+
121
+	//  -- 노드 자체가 사라진다면 --
122
+	// for cur := &lk.head; *cur != nil; {
123
+	// 	if (*cur).val == val {
124
+	// 		lk.size--
125
+	// 		*cur = (*cur).next
126
+	// 	} else {
127
+	// 		lk.tail = *cur
128
+	// 		cur = &(*cur).next
129
+	// 	}
130
+	// }
131
+	if num2sub[subnumber] == true {
132
+		if israngesub[subnumber] == true {
133
+			delete(israngesub, subnumber)
134
+			ptr := lk.head
135
+			for ptr != nil {
136
+				for j := 0; j < len(ptr.range2sub_b); j++ {
137
+					if ptr.range2sub_b[j] == subnumber {
138
+						ptr.range2sub_b = remove(ptr.range2sub_b, j)
139
+						return nil
140
+					}
141
+				}
142
+
143
+				for j := 0; j < len(ptr.range2sub_eb); j++ {
144
+					if ptr.range2sub_eb[j] == subnumber {
145
+						ptr.range2sub_eb = remove(ptr.range2sub_eb, j)
146
+						return nil
147
+					}
148
+				}
149
+
150
+				for j := 0; j < len(ptr.range2sub_s); j++ {
151
+					if ptr.range2sub_s[j] == subnumber {
152
+						ptr.range2sub_s = remove(ptr.range2sub_s, j)
153
+						return nil
154
+					}
155
+				}
156
+
157
+				for j := 0; j < len(ptr.range2sub_es); j++ {
158
+					if ptr.range2sub_es[j] == subnumber {
159
+						ptr.range2sub_es = remove(ptr.range2sub_es, j)
160
+						return nil
161
+					}
162
+				}
163
+			}
164
+		}
165
+	} else {
166
+		return errors.New("Don't delete this Subscription (Not exist)")
167
+	}
168
+	return nil
169
+}

+ 37
- 0
modules/matching_test.go Datei anzeigen

@@ -0,0 +1,37 @@
1
+package modules
2
+
3
+import (
4
+	"fmt"
5
+	"testing"
6
+)
7
+
8
+func Test_pbNtraverse(t *testing.T) {
9
+	mylist := new(LList)
10
+	mylist.PushBack(30)
11
+	mylist.PushBack(40)
12
+	mylist.PushBack(20)
13
+	mylist.PushBack(10)
14
+	ptr := mylist.head
15
+	for ptr != nil {
16
+		fmt.Printf("data = %d, address = %p\n", ptr.val, ptr.next)
17
+		ptr = ptr.next
18
+	}
19
+}
20
+
21
+func Test_mergeSort(t *testing.T) {
22
+	mylist := new(LList)
23
+	mylist.PushBack(30)
24
+	mylist.PushBack(40)
25
+	mylist.PushBack(20)
26
+	mylist.PushBack(10)
27
+	mylist.head = MergeSort(mylist.head, mylist.size / 2)
28
+	ptr := mylist.head
29
+	for ptr != nil {
30
+		fmt.Printf("data = %d, address = %p\n", ptr.val, ptr.next)
31
+		ptr = ptr.next
32
+	}
33
+}
34
+
35
+func Test_removeNdel(t &testing.T){
36
+
37
+}

Laden…
Abbrechen
Speichern