summaryrefslogtreecommitdiff
path: root/Chalice/src/Chalice.cs
blob: 4865db69609e2c79fc360dd1dbe478c8b6e5d7a7 (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
//-----------------------------------------------------------------------------
//
// 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 LinkedList<E> contents = new LinkedList<E>();
    
    public void Add(E e) {
      lock(this) {
        contents.AddFirst(e);
        Monitor.Pulse(this);
      }
    }
    
    public E Remove() {
      lock(this) {
        while(contents.Count == 0){
          Monitor.Wait(this);
        }
        E e = contents.Last.Value; 
        contents.RemoveLast();
        return e;
      }
    }
  }

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