Mercurial > hg > Database > jungle-sharp
diff src/main/csharp/jp.ac.u-ryukyu.ie.cr/jungle/util/IterableConverter.cs @ 0:dec15de2c6ff
first commit
author | Kazuma |
---|---|
date | Tue, 21 Jun 2016 17:11:12 +0900 |
parents | |
children | 79da77797f7e |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/csharp/jp.ac.u-ryukyu.ie.cr/jungle/util/IterableConverter.cs Tue Jun 21 17:11:12 2016 +0900 @@ -0,0 +1,80 @@ +using UnityEngine; +using System.Collections.Generic; +using System.Collections; +using System; + +public class IterableConverter<A,B> : IEnumerable<A> { + private IEnumerable<B> iterable; + private Converter<A,B> converter; + + + IEnumerator IEnumerable.GetEnumerator() + { + // call the generic version of the method + return this.GetEnumerator(); + } + + public IEnumerator<A> GetEnumerator() + { + return iterator (); + } + + public IterableConverter(IEnumerable<B> _iterable,Converter<A,B> _converter) + { + iterable = _iterable; + converter = _converter; + } + + public IEnumerator<A> iterator() + { + return new IteratorConverter<A,B>(iterable.GetEnumerator(),converter); + } + + private class IteratorConverter<A,B> : IEnumerator<A> + { + public List<A> appLines { get; set; } + private int position; + + private IEnumerator<B> iterator; + private Converter<A,B> converter; + + public IteratorConverter(IEnumerator<B> _iterator,Converter<A,B> _converter) + { + iterator = _iterator; + converter = _converter; + } + + public bool MoveNext() + { + return iterator.MoveNext(); + } + + public A Current + { + get{ + return converter.conv (iterator.Current); + } + } + + public void Reset() + { + // ホントはremove? + iterator.Reset(); + } + + object IEnumerator.Current + { + get { return (appLines.ToArray())[position] ; } + } + + public void Dispose() { + ((IEnumerator<A>)this.appLines).Dispose (); + } + } + + + public interface Converter<A,B>{ + A conv (B _b); + } + +} \ No newline at end of file