Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 8834c31d29
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 131 lines (108 sloc) 3.831 kb
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
/*
Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/


using System;
using C5;
using SCG = System.Collections.Generic;

namespace MultiCollection
{
  class BasicCollectionValue<T> : CollectionValueBase<T>, ICollectionValue<T>
  {
    SCG.IEnumerable<T> enumerable;
    Func<T> chooser;
    int count;
    //TODO: add delegate for checking validity!

    public BasicCollectionValue(SCG.IEnumerable<T> e, Func<T> chooser, int c) { enumerable = e; count = c; this.chooser = chooser; }

    public override int Count { get { return count; } }

    public override Speed CountSpeed { get { return Speed.Constant; } }

    public override bool IsEmpty { get { return count == 0; } }

    public override T Choose() { return chooser(); }

    public override System.Collections.Generic.IEnumerator<T> GetEnumerator()
    {
      return enumerable.GetEnumerator();
    }
  }

  interface IMultiCollection<K, V>
  {
    SCG.IEqualityComparer<K> KeyEqualityComparer { get;}
    SCG.IEqualityComparer<V> ValueEqualityComparer { get;}
    bool Add(K k, V v);
    bool Remove(K k, V v);
    ICollectionValue<V> this[K k] { get;}
    ICollectionValue<K> Keys { get;}
    SCG.IEnumerable<V> Values { get;}

  }

  class BasicMultiCollection<K, V, W, U> : IMultiCollection<K, V>//: IDictionary<K, W>
  where W : ICollection<V>, new()
      where U : IDictionary<K, W>, new()
  {
    U dict = new U();

    public SCG.IEqualityComparer<K> KeyEqualityComparer { get { return EqualityComparer<K>.Default; } }

    public SCG.IEqualityComparer<V> ValueEqualityComparer { get { return EqualityComparer<V>.Default; } } //TODO: depends on W!

    public bool Add(K k, V v)
    {
      W w;
      if (!dict.Find(ref k, out w))
        dict.Add(k,w = new W());
      return w.Add(v);
    }

    public bool Remove(K k, V v)
    {
      W w;
      if (dict.Find(ref k, out w) && w.Remove(v))
      {
        if (w.Count == 0)
          dict.Remove(k);
        return true;
      }
      return false;
    }

    public ICollectionValue<V> this[K k] { get { return dict[k]; } }

    public ICollectionValue<K> Keys { get { return dict.Keys; } }

    public SCG.IEnumerable<V> Values
    {
      get
      {
        foreach (W w in dict.Values)
          foreach (V v in w)
            yield return v;
      }
    }
  }

  class WordIndex : BasicMultiCollection<string,int,HashSet<int>,HashDictionary<string,HashSet<int>>>
  {
  }

  class MyTest
  {
    public static void Main(String[] args)
    {
      WordIndex wi = new WordIndex();
      wi.Add("ja", 2);
      wi.Add("nej", 4);
      wi.Add("ja", 7);
      foreach (string s in wi.Keys)
      {
        Console.WriteLine(s + " -->");
        foreach (int line in wi[s])
        {
          Console.WriteLine(" " + line);
        }
      }
    }
  }
}
Something went wrong with that request. Please try again.