Enumerable.vala 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. using Invercargill.DataStructures;
  2. using Invercargill.Modifiers;
  3. using Invercargill.Mapping;
  4. namespace Invercargill {
  5. public abstract class Enumerable<T> : Object {
  6. public abstract Tracker<T> get_tracker();
  7. public abstract int? peek_count();
  8. public abstract EnumerableInfo get_info();
  9. // Returns false if iteration was interrupted
  10. // Returns true if iteration reached natural end
  11. public virtual bool iterate_if(PredicateDelegate<T> handler) {
  12. var tracker = get_tracker();
  13. while(tracker.has_next()) {
  14. if(!handler(tracker.get_next())) {
  15. return false;
  16. }
  17. }
  18. return true;
  19. }
  20. public virtual void iterate(ItemDelegate<T>? handler = null) {
  21. if(handler != null) {
  22. iterate_if(i => {
  23. handler(i);
  24. return true;
  25. });
  26. return;
  27. }
  28. iterate_if(i => true);
  29. }
  30. public virtual Series<T> to_series() {
  31. var series = new Series<T>();
  32. iterate(i => series.add(i));
  33. return series;
  34. }
  35. public virtual Gee.Collection<T> to_gee_collection() {
  36. var collection = new Gee.LinkedList<T>();
  37. iterate(i => collection.add(i));
  38. return collection;
  39. }
  40. public virtual Tracker<T> iterator() {
  41. return get_tracker();
  42. }
  43. public virtual T[] to_array() {
  44. var array = new T[1024];
  45. var index = 0;
  46. foreach (var item in this) {
  47. if(index >= array.length) {
  48. array.resize(array.length*2);
  49. }
  50. safely_assign_to_array<T>(array, index, item);
  51. index++;
  52. }
  53. array.resize(index);
  54. return array;
  55. }
  56. public virtual int count(PredicateDelegate<T>? predicate = null) {
  57. var enumerable = this;
  58. // When no predicate is passed, see if we can peek at the count of
  59. // the enumerable before iterating.
  60. if(predicate == null) {
  61. var peek = peek_count();
  62. if(peek != null) {
  63. return peek;
  64. }
  65. }
  66. else {
  67. enumerable = this.where(predicate);
  68. }
  69. var count = 0;
  70. enumerable.iterate(i => count++);
  71. return count;
  72. }
  73. public virtual bool any(PredicateDelegate<T>? predicate = null) {
  74. var result = false;
  75. var p = resolve_nullable_predicate(predicate);
  76. iterate_if(i => {
  77. if(p(i)) {
  78. result = true;
  79. return false;
  80. }
  81. return true;
  82. });
  83. return result;
  84. }
  85. public virtual bool all(PredicateDelegate<T> predicate) {
  86. return !any(i => !predicate(i));
  87. }
  88. public virtual bool no(PredicateDelegate<T>? predicate = null) {
  89. var p = resolve_nullable_predicate(predicate);
  90. return all(i => !p(i));
  91. }
  92. public virtual Enumerable<T> where(owned PredicateDelegate<T> predicate) {
  93. return new Filter<T>(this, (owned)predicate);
  94. }
  95. public virtual Enumerable<T> from(owned PredicateDelegate<T> predicate) {
  96. return new From<T>(this, (owned)predicate);
  97. }
  98. public virtual Enumerable<T> until(owned PredicateDelegate<T> predicate) {
  99. return new Until<T>(this, (owned)predicate);
  100. }
  101. public virtual Enumerable<Tout> select_where<Tout>(owned FilterTransformDelegate<T, Tout> transform) {
  102. return new FilterTransform<T, Tout>(this, (owned)transform);
  103. }
  104. public virtual Enumerable<Tout> select<Tout>(owned TransformDelegate<T, Tout> transform) {
  105. return new Transform<T, Tout>(this, (owned)transform);
  106. }
  107. public virtual Attempts<Tout> attempt_select<Tout>(owned AttemptTransformDelegate<T, Tout> transform) {
  108. return select<Attempt<Tout>>(i => new Attempt<Tout>(() => transform(i))).assert_promotion<Attempts>();
  109. }
  110. public virtual Attempts<Tout> attempt_select_nested<Tout>(owned AttemptTransformDelegate<T, Enumerable<Attempt<Tout>>> transform) {
  111. return attempt_select<Enumerable<Attempt<Tout>>>((owned)transform)
  112. .as_enumerable()
  113. .select_many<Attempt<Tout>>(a => a.success ? a.result : Invercargill.single<Attempt<Tout>>(new Attempt<Tout>.unsuccessful(a.error)))
  114. .assert_promotion<Attempts>();
  115. }
  116. public virtual Enumerable<Pair<TFirst, TSecond>> select_pairs<TFirst, TSecond>(owned TransformDelegate<T, TFirst> transform1, owned TransformDelegate<T, TSecond> transform2) {
  117. return select<Pair<TFirst, TSecond>>(i => new Pair<TFirst, TSecond>(transform1(i), true, transform2(i), true));
  118. }
  119. public virtual Enumerable<Tout> select_many<Tout>(owned TransformDelegate<T, Enumerable<Tout>> transform) {
  120. return new MergeQuery<Tout>(select((owned)transform));
  121. }
  122. public virtual Enumerable<T> sort(owned CompareDelegate<T> compare) {
  123. return new Sort<T>(this, (owned)compare);
  124. }
  125. public virtual Enumerable<T> concat(Enumerable<T> other) {
  126. return new Concat<T>(this, other);
  127. }
  128. public virtual Enumerable<T> take(int count) {
  129. return new Take<T>(this, count);
  130. }
  131. public virtual Enumerable<T> skip(int count) {
  132. return new Skip<T>(this, count);
  133. }
  134. public virtual Enumerable<Tout> cast<Tout>() {
  135. return select<Tout>(i => (Tout)i);
  136. }
  137. public virtual Enumerable<Tout> parallel_select<Tout>(owned TransformDelegate<T, Tout> transform, uint workers = 0) {
  138. var actual_workers = workers;
  139. if(actual_workers < 1) {
  140. actual_workers = get_num_processors();
  141. }
  142. return new Parallel<T, Tout>(this, (owned)transform, (int)actual_workers);
  143. }
  144. public virtual void parallel_iterate(ItemDelegate<T> handler, uint workers = 0) {
  145. parallel_select<T>(i => {
  146. handler(i);
  147. return i;
  148. }, workers)
  149. .iterate();
  150. }
  151. public virtual Enumerable<SelectionContext<T, Tout>> contextualised_select<Tout>(owned TransformDelegate<T, Tout> transform) {
  152. return select<SelectionContext<T, Tout>>((i) => new SelectionContext<T, Tout>() {
  153. origin = i,
  154. result = transform(i)
  155. });
  156. }
  157. public virtual Tout aggregate<Tout>(Tout initial, AggregateDelegate<Tout, T> aggregate_func) {
  158. var aggregate = initial;
  159. iterate(i => {
  160. aggregate = aggregate_func(aggregate, i);
  161. });
  162. return aggregate;
  163. }
  164. public virtual T max(TransformDelegate<T, int> int_delegate) {
  165. T item = null;
  166. var first = true;
  167. var value = 0;
  168. foreach (var i in this) {
  169. if(first) {
  170. first = false;
  171. item = i;
  172. value = int_delegate(i);
  173. continue;
  174. }
  175. var item_value = int_delegate(i);
  176. if(item_value > value) {
  177. value = item_value;
  178. item = i;
  179. }
  180. }
  181. return item;
  182. }
  183. public virtual T min(TransformDelegate<T, int> int_delegate) {
  184. T item = null;
  185. var first = true;
  186. var value = 0;
  187. foreach (var i in this) {
  188. if(first) {
  189. first = false;
  190. item = i;
  191. value = int_delegate(i);
  192. continue;
  193. }
  194. var item_value = int_delegate(i);
  195. if(item_value < value) {
  196. value = item_value;
  197. item = i;
  198. }
  199. }
  200. return item;
  201. }
  202. public virtual bool contains(T item, EqualityDelegate<T>? equator = null) {
  203. var func = equator ?? Operators.equality<T>();
  204. return any(i => func(i, item));
  205. }
  206. public virtual Enumerable<TOut> zip<TOther, TOut>(Enumerable<TOther> other, owned ZipperTransformDelegate<T, TOther, TOut> transform) {
  207. return new Zip<T, TOther, TOut>(this, other, (owned)transform);
  208. }
  209. public virtual Enumerable<Pair<T, Tother>> pair_up<Tother>(Enumerable<Tother> other) {
  210. return zip<Tother, Pair<T, Tother>>(other, (t1v, t1vs, t2v, t2vs) => new Pair<T, Tother>(t1v, t1vs, t2v, t2vs));
  211. }
  212. public virtual Enumerable<T> interleave(Enumerable<T> other) {
  213. return new Interleave<T>(this, other);
  214. }
  215. public virtual Enumerable<Tout> fork<Tout>(owned TransformDelegate<T, Tout> fork1, owned TransformDelegate<T, Tout> fork2) {
  216. var seq = to_series();
  217. return seq.select<Tout>((owned)fork1).interleave(seq.select<Tout>((owned)fork2));
  218. }
  219. public virtual Enumerable<Tout> fork_many<Tout>(owned TransformDelegate<T, Enumerable<Tout>> fork1, owned TransformDelegate<T, Enumerable<Tout>> fork2) {
  220. return new MergeQuery<Tout>(fork((owned)fork1, (owned)fork2));
  221. }
  222. public virtual bool matches(Enumerable<T> other, EqualityDelegate<T> equals) {
  223. return zip<T, bool>(other, (t1v, t1vs, t2v, t2vs) => t1vs == t2vs && equals(t1v, t2v)).all(r => r);
  224. }
  225. public virtual Enumerable<T> act(ItemDelegate<T> handler) {
  226. return select<T>(i => {
  227. handler(i);
  228. return i;
  229. });
  230. }
  231. public virtual Enumerable<T> distinct(owned EqualityDelegate<T>? comparison = null) {
  232. return distinct_by<T>(i => i, (owned)comparison);
  233. }
  234. public virtual Enumerable<T> distinct_by<TProp>(owned TransformDelegate<T, TProp> property_selector, owned EqualityDelegate<TProp>? property_equality) {
  235. var func = property_equality ?? Operators.equality<T>();
  236. return new Unique<T, TProp>(this, (owned)property_selector, (owned)func);
  237. }
  238. public virtual Enumerable<Grouping<TKey, T>> group_by<TKey>(owned TransformDelegate<T, TKey> key_selector, owned EqualityDelegate<TKey>? key_equality = null) {
  239. var equality = key_equality ?? Operators.equality<TKey>();
  240. var keys = select<TKey>(i => key_selector(i)).distinct((a, b) => equality(a, b));
  241. return keys.select<Grouping<TKey, T>>(g => new Grouping<TKey, T>(g, this.where(i => equality(g, key_selector(i)))));
  242. }
  243. public virtual Enumerable<T> @with(T item, uint times = 1) {
  244. return concat(range(0, (int)times, 1).select<T>(i => item));
  245. }
  246. public virtual T first(owned PredicateDelegate<T>? predicate = null) throws SequenceError {
  247. var tracker = predicate == null ? get_tracker() : where((owned)predicate).get_tracker();
  248. if(tracker.has_next()) {
  249. return tracker.get_next();
  250. }
  251. throw new SequenceError.NO_ELEMENTS("The sequence contains no elements");
  252. }
  253. public virtual T? first_or_default(owned PredicateDelegate<T>? predicate = null) {
  254. var tracker = predicate == null ? get_tracker() : where((owned)predicate).get_tracker();
  255. if(tracker.has_next()) {
  256. return tracker.get_next();
  257. }
  258. return null;
  259. }
  260. public virtual T last(owned PredicateDelegate<T>? predicate = null) throws SequenceError {
  261. var tracker = predicate == null ? get_tracker() : where((owned)predicate).get_tracker();
  262. if(!tracker.has_next()) {
  263. throw new SequenceError.NO_ELEMENTS("The sequence contains no elements");
  264. }
  265. while(true) {
  266. T item = tracker.get_next();
  267. if(!tracker.has_next()) {
  268. return item;
  269. }
  270. }
  271. }
  272. public virtual T? last_or_default(owned PredicateDelegate<T>? predicate = null) {
  273. var tracker = predicate == null ? get_tracker() : where((owned)predicate).get_tracker();
  274. if(!tracker.has_next()) {
  275. return null;
  276. }
  277. while(true) {
  278. T item = tracker.get_next();
  279. if(!tracker.has_next()) {
  280. return item;
  281. }
  282. }
  283. }
  284. public virtual T single(owned PredicateDelegate<T>? predicate = null) throws SequenceError {
  285. var tracker = predicate == null ? get_tracker() : where((owned)predicate).get_tracker();
  286. if(tracker.has_next()) {
  287. var item = tracker.get_next();
  288. if(tracker.has_next()) {
  289. throw new SequenceError.MULTUPLE_ELEMENTS("The sequence contains more than one element");
  290. }
  291. return item;
  292. }
  293. throw new SequenceError.NO_ELEMENTS("The sequence contains no elements");
  294. }
  295. public virtual T single_or_default(owned PredicateDelegate<T>? predicate = null) throws SequenceError {
  296. var tracker = predicate == null ? get_tracker() : where((owned)predicate).get_tracker();
  297. if(tracker.has_next()) {
  298. var item = tracker.get_next();
  299. if(tracker.has_next()) {
  300. throw new SequenceError.MULTUPLE_ELEMENTS("The sequence contains more than one element");
  301. }
  302. return item;
  303. }
  304. return null;
  305. }
  306. public virtual string to_string(TransformDelegate<T, string> stringifier, string seperator = "") {
  307. bool is_first = true;
  308. return aggregate<string>("", (s, v) => {
  309. if(is_first) {
  310. is_first = false;
  311. return stringifier(v);
  312. }
  313. return s + seperator + stringifier(v);
  314. });
  315. }
  316. public virtual Object[] to_object_array() throws SequenceError {
  317. if(!typeof(T).is_object()) {
  318. throw new SequenceError.INVALID_TYPE("Can only make an object array of an Enumerable<T> where T is derrived from GLib.Object");
  319. }
  320. return select<Object>(i => (Object)i).to_array();
  321. }
  322. public virtual Vector<T> to_vector() {
  323. var vector = new Vector<T>();
  324. vector.add_all(this);
  325. return vector;
  326. }
  327. public virtual Type element_type { get {
  328. return typeof(T);
  329. }}
  330. public virtual TPromotion promote_to<TPromotion>() throws PromotionError {
  331. var type = typeof(TPromotion);
  332. if(get_type().is_a(type)) {
  333. // Don't promote if we are already target type
  334. return this;
  335. }
  336. resolve_promotion<TPromotion>(ref type);
  337. if(!type.is_instantiatable()) {
  338. throw new PromotionError.INVALID_PROMOTION_TYPE(@"Promotion type $(type.name()) is not instansiatable.");
  339. }
  340. if(!type.is_a(typeof(Promotion))) {
  341. throw new PromotionError.INVALID_PROMOTION_TYPE(@"Promotion type $(type.name()) does not implement Invercargill.Promotion.");
  342. }
  343. if(!type.is_a(typeof(Enumerable)) && typeof(TPromotion) == type) {
  344. throw new PromotionError.INVALID_PROMOTION_TYPE(@"Non-resolved promotion type $(type.name()) does not inherit from Invercargill.Enumerable.");
  345. }
  346. var promotion = Object.new(type);
  347. if(!((Promotion)promotion).can_wrap(element_type)) {
  348. throw new PromotionError.INCOMPATIBLE_ELEMENT_TYPE(@"Enumerable has an element type of $(element_type.name()) which cannot be wrapped by $(type.name())");
  349. }
  350. return ((Promotion)promotion).wrap(this);
  351. }
  352. public virtual TPromotion assert_promotion<TPromotion>() {
  353. try {
  354. return promote_to<TPromotion>();
  355. }
  356. catch (PromotionError error) {
  357. var base_type = get_type();
  358. var type = typeof(TPromotion);
  359. critical(@"Cannot promote type $(base_type.name()) to $(type.name()): $(error.message)");
  360. assert_not_reached();
  361. }
  362. }
  363. public virtual Enumerable<T> seal() {
  364. if(this.get_type().is_a(typeof(Sealed))) {
  365. return this;
  366. }
  367. return new Sealed<T>(this);
  368. }
  369. public virtual Enumerable<T> cache() {
  370. if(this.get_type().is_a(typeof(Cache))) {
  371. return this;
  372. }
  373. return new Cache<T>(this);
  374. }
  375. public virtual Dictionary<TKey, T> to_dictionary<TKey>(TransformDelegate<T, TKey> key_selecter, HashDelegate<TKey>? key_hash_func = null, EqualityDelegate<TKey>? key_equal_func = null) {
  376. var dict = new Dictionary<TKey, T>(key_hash_func, key_equal_func);
  377. iterate(i => dict.add(key_selecter(i), i));
  378. return dict;
  379. }
  380. public virtual Dictionary<TKey, TValue> select_to_dictionary<TKey, TValue>(TransformDelegate<T, TKey> key_selecter, TransformDelegate<T, TValue> value_selecter, HashDelegate<TKey>? key_hash_func = null, EqualityDelegate<TKey>? key_equal_func = null) {
  381. var dict = new Dictionary<TKey, T>(key_hash_func, key_equal_func);
  382. iterate(i => dict.add(key_selecter(i), value_selecter(i)));
  383. return dict;
  384. }
  385. public virtual HashSet<T> to_hash_set(HashDelegate<T>? hash_func = null, EqualityDelegate<T>? equal_func = null) {
  386. var @set = new HashSet<T>(hash_func, equal_func);
  387. @set.union_with(this);
  388. return @set;
  389. }
  390. public virtual Enumerable<PositionItemPair<T>> with_positions() {
  391. return new Position<T>(this);
  392. }
  393. public virtual Elements to_elements() {
  394. var series = new ElementSeries();
  395. if(typeof(T).is_a(typeof(Element))) {
  396. series.add_all((Enumerable<Element>)this);
  397. }
  398. else {
  399. series.add_all(select<Element>(i => new NativeElement<T>(i)));
  400. }
  401. return series;
  402. }
  403. public virtual Attempts<Tout> attempt_map_with<Tout>(Mapper<Tout, T> mapper) {
  404. return attempt_select<Tout>(o => mapper.materialise(o));
  405. }
  406. public virtual Enumerable<T> as_enumerable() {
  407. return this;
  408. }
  409. public virtual void debug_dump(TransformDelegate<T, string> stringifier, string additional_message = "", DebugOutputDelegate? output_func = null, bool formatting = true) {
  410. DebugPrinter.print_enumerable_dump<T>(this, stringifier, additional_message, output_func, formatting);
  411. }
  412. public virtual Enumerable<T> debug_type(string additional_message = "", DebugOutputDelegate? output_func = null, bool formatting = true) {
  413. DebugPrinter.print_enumerable_information(get_info(), additional_message, output_func, formatting);
  414. return this;
  415. }
  416. public virtual Enumerable<T> debug_trace(owned TransformDelegate<T, string> stringifier, string additional_message = "", owned DebugOutputDelegate? output_func = null, bool formatting = true) {
  417. return new TraceEnumerable<T>(this, get_info(), (owned)stringifier, additional_message, (owned)output_func, formatting);
  418. }
  419. private PredicateDelegate<T> resolve_nullable_predicate(PredicateDelegate<T>? predicate) {
  420. if(predicate == null) {
  421. return (p) => true;
  422. }
  423. return predicate;
  424. }
  425. }
  426. }