oo-70-245-1

oo-70-245-1 Commit Details


Date:2018-03-30 16:59:47 (6 years 6 months ago)
Author:Natalie Adams
Branch:master
Commit:894bb4c7b442fb56bb94c60e2e1696bb449bceb0
Parents: 4fd9a796473add12fa74c63ed2eda08a518b3a85
Message:adding example code

Changes:

File differences

Class Examples/mutex.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace mutexexample2
{
class Program
{
public static Mutex m = new Mutex();
public static readonly int LOOP = 50000;
public static long gvar = 0;
static public void produce()
{
m.WaitOne();
for (int i = 0; i < LOOP; i++)
gvar++;
m.ReleaseMutex();
}
static public void consume()
{
m.WaitOne();
for (int i = 0; i < LOOP; i++)
gvar--;
m.ReleaseMutex();
}
static void Main(string[] args)
{
Thread t1 = new Thread(new ThreadStart(produce));
t1.Start();
Thread t2 = new Thread(new ThreadStart(consume));
t2.Start();
t1.Join();
t2.Join();
Console.WriteLine(gvar);
}
}
}
Class Examples/node-generic.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace generics
{
class Program
{
class Node<T> where T : System.IComparable<T>
{
public T element;
public Node<T> left = null;
public Node<T> right = null;
Node<T> parent = null;
public Node<T> createNode(T element)
{
Node<T> n = new Node<T>() { parent = this, element = element };
return n;
}
public Node<T> addLeft(T element)
{
this.left = createNode(element);
return this.left;
}
public Node<T> addRight(T element)
{
this.right = createNode(element);
return this.right;
}
public override string ToString()
{
return "Node element => " + element.ToString();
}
}
static Node<T> dfs<T>(T val, Node<T> parent) where T : System.IComparable<T>
{
if (parent == null)
return null;
if (parent.element.CompareTo(val) == 0)
return parent;
Node<T> ret = null;
if (parent.left != null)
ret = dfs(val, parent.left);
if (ret != null)
return ret;
if (parent.right != null)
ret = dfs(val, parent.right);
if (ret != null)
return ret;
return null;
}
static void Main(string[] args)
{
Node<int> n = new Node<int>();
n.addLeft(4).addLeft(10).addLeft(7);
n.addRight(5).addRight(2);
Console.WriteLine(dfs(2, n));
}
}
}
Class Examples/node.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace generics
{
class Program
{
class Node
{
public int element;
public Node left = null;
public Node right = null;
Node parent = null;
public Node createNode(int element)
{
Node n = new Node() { parent = this, element = element };
return n;
}
public Node addLeft(int element)
{
this.left = createNode(element);
return this.left;
}
public Node addRight(int element)
{
this.right = createNode(element);
return this.right;
}
public override string ToString()
{
return "Node element => " + element.ToString();
}
}
static Node dfs(int val, Node parent)
{
if (parent == null)
return null;
if (parent.element == val)
return parent;
Node ret = null;
if (parent.left != null)
ret = dfs(val, parent.left);
if (ret != null)
return ret;
if (parent.right != null)
ret = dfs(val, parent.right);
if (ret != null)
return ret;
return null;
}
static void Main(string[] args)
{
Node n = new Node();
n.addLeft(4).addLeft(10).addLeft(7);
n.addRight(5).addRight(2);
Console.WriteLine(dfs(22, n));
}
}
}
Class Examples/produce-consume.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace mutexexample2
{
class Program
{
public static readonly int LOOP = 50000;
public static long gvar = 0;
static public void produce()
{
for (int i = 0; i < LOOP; i++)
gvar++;
}
static public void consume()
{
for (int i = 0; i < LOOP; i++)
gvar--;
}
static void Main(string[] args)
{
Thread t1 = new Thread(new ThreadStart(produce));
t1.Start();
Thread t2 = new Thread(new ThreadStart(consume));
t2.Start();
t1.Join();
t2.Join();
Console.WriteLine(gvar);
}
}
}
Class Examples/swap.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace generics
{
class Program
{
static void swap<T>(ref T lhs,ref T rhs)
{
T temp;
temp = lhs;
lhs = rhs;
rhs = temp;
}
static void Main(string[] args)
{
int a, b;
a = 10;
b = 20;
Console.WriteLine(a.ToString() + " " + b.ToString());
swap(ref a, ref b);
Console.WriteLine(a.ToString() + " " + b.ToString());
}
}
}

Archive Download the corresponding diff file

Branches

Number of commits:
Page rendered in 0.07075s using 14 queries.