summaryrefslogtreecommitdiff
path: root/Chalice/src/main/scala/Chalice.cs
blob: 0b26e1f24c720cb67b09fbaa32255d31a1dd3479 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation.  All Rights Reserved.
//
//-----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace Chalice
{
  public class ImmutableList<E>
  {
    private List<E> list;

    public ImmutableList()
    {
      list = new List<E>();
    }

    public ImmutableList(IEnumerable<E> elems)
    {
      list = new List<E>(elems);
    }

    public ImmutableList<E> Append(ImmutableList<E> other)
    {
      var res = new ImmutableList<E>();
      res.list.AddRange(list);
      res.list.AddRange(other.list);
      return res;
    }

    public E At(int index)
    {
      return list[index];
    }

    public ImmutableList<E> Take(int howMany)
    {
      var res = new ImmutableList<E>(this.list.Take(howMany));
      return res;
    }

    public ImmutableList<E> Drop(int howMany)
    {
      var res = new ImmutableList<E>(this.list.Skip(howMany));
      return res;
    }

    public int Length
    {
      get
      {
        return list.Count;
      }
    }

    public static ImmutableList<int> Range(int min, int max)
    {
      ImmutableList<int> l = new ImmutableList<int>();
      for (int i = min; i < max; i++)
      {
        l.list.Add(i);
      }
      return l;
    }
  }

  public class ChannelBuffer<E>
  {
    private Queue<E> contents = new Queue<E>();

    public void Add(E e)
    {
      lock (this)
      {
        contents.Enqueue(e);
        Monitor.Pulse(this);
      }
    }

    public E Remove()
    {
      lock (this)
      {
        while (contents.Count == 0)
        {
          Monitor.Wait(this);
        }
        E e = contents.Dequeue();
        return e;
      }
    }
  }

  public class ChalicePrint
  {
    public void Int(int x)
    {
      System.Console.WriteLine(x);
    }
    public void Bool(bool x)
    {
      System.Console.WriteLine(x);
    }
  }
}