1 /*
2     This file is part of BioD.
3     Copyright (C) 2012-2014    Artem Tarasov <lomereiter@gmail.com>
4 
5     Permission is hereby granted, free of charge, to any person obtaining a
6     copy of this software and associated documentation files (the "Software"),
7     to deal in the Software without restriction, including without limitation
8     the rights to use, copy, modify, merge, publish, distribute, sublicense,
9     and/or sell copies of the Software, and to permit persons to whom the
10     Software is furnished to do so, subject to the following conditions:
11     
12     The above copyright notice and this permission notice shall be included in
13     all copies or substantial portions of the Software.
14     
15     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21     DEALINGS IN THE SOFTWARE.
22 
23 */
24 module bio.core.utils.algo;
25 
26 import std.traits;
27 import std.range;
28 import std.algorithm;
29 import std.array;
30 
31 
32 /**
33   This function is supposed to be used on a small amount of objects,
34   typically tags of the same alignment. Therefore it runs in O(N^2)
35   but doesn't require any memory allocations.
36 */
37 bool allDistinct(Range)(Range r) {
38     uint sz = 0;
39     uint eq = 0;
40     foreach (e1; r) {
41         ++sz;
42         foreach (e2; r) {
43             if (e1 == e2) {
44                 eq += 1;
45             }
46         }
47     }
48     return sz == eq;
49 }
50 
51 private import std.algorithm;
52 static if (!__traits(compiles, any!"a == 2"([1,2,3]))) {
53 
54     import std.functional;
55 
56     /// check if all elements satisfy the condition
57     bool all(alias F, R)(R r) {
58         foreach (e; r) {
59             if (!unaryFun!F(e)) {
60                 return false;
61             }
62         }
63         return true;
64     }
65 
66     /// check if any element satisfies the condition
67     bool any(alias F, R)(R r) {
68         foreach (e; r) {
69             if (unaryFun!F(e)) {
70                 return true;
71             }
72         }
73         return false;
74     }
75 }
76 
77 import std.functional;
78 
79 auto argmax(alias func, S)(S set) {
80     auto best_elem = set.front;
81     auto best_value = unaryFun!func(best_elem);
82 
83     set.popFront();
84     foreach (elem; set) {
85         auto value = unaryFun!func(elem);
86         if (value > best_value) {
87             best_value = value;
88             best_elem = elem;
89         }
90     }
91 
92     return best_elem;
93 }
94 
95 struct NonOverlappingChunks(R, alias begFunc, alias endFunc) {
96 
97     this(R r) {
98         _range = r;
99         popFront();
100     }
101 
102     bool empty() @property {
103         return _empty;
104     }
105 
106     auto front() @property {
107         return _front;
108     }
109 
110     void popFront() {
111         if (!_range.empty()) {
112             _front = _range.front;
113             tryToJoinWithNextChunks();
114         } else {
115             _empty = true;
116         }
117     }
118 
119 private:
120 
121     R _range;
122 
123     void tryToJoinWithNextChunks() {
124         _range.popFront();
125         while (!_range.empty()) {
126             /// Get next element
127             auto next = _range.front;
128             /// It's presumed that chunks are sorted
129             assert(next >= _front);
130             /// Check if the chunk can be joined with the previous one
131             if (endFunc(_front) >= begFunc(next)) {
132                 /// update end of _front
133                 endFunc(_front) = max(endFunc(_front), endFunc(next));
134                 _range.popFront(); /// next is consumed
135             } else {
136                 /// can't join
137                 break;
138             }
139         }
140     }
141 
142     Unqual!(ElementType!R) _front;
143     bool _empty = false;
144 }
145 
146 /// $(D begFunc) and $(D endFunc) must take a reference to the object
147 /// and return references to the field.
148 /// FIXME: the design is ugly.
149 /// Params: 
150 ///      r - range of chunks, sorted by leftmost coordinate
151 /// Returns:
152 ///      range of non-overlapping chunks, covering the same subset
153 ///      as original chunks
154 NonOverlappingChunks!(R, begFunc, endFunc)
155 nonOverlapping(alias begFunc, alias endFunc, R)(R r) 
156     if (__traits(compiles, {
157 	if (begFunc(r.front) == endFunc(r.front))
158 	    endFunc(r.front) = begFunc(r.front);
159     }))
160 {
161     return NonOverlappingChunks!(R, begFunc, endFunc)(r);
162 }