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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
/*
This file is part of BioD.
Copyright (C) 2012 Artem Tarasov <lomereiter@gmail.com>
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.
*/
module bio.core.tinymap;
private import std.algorithm;
private import std.range;
private import std.traits;
import std.bitmanip;
/// Efficient dictionary for cases when the number of possible keys is small
/// and is known at compile-time. The data is held in a static array, no allocations occur.
///
/// Key type must:
/// have static member ValueSetSize (integer)
/// have static method fromInternalCode (returning an instance of Key type);
/// have property $(D internal_code) that maps it to integers 0, 1, ..., ValueSetSize - 1
struct TinyMap(K, V, alias TinyMapPolicy=useBitArray) {
private V[K.ValueSetSize] _dict;
private size_t _size;
private mixin TinyMapPolicy!(K, V) Policy;
private alias ReturnType!(K.internal_code) TCode;
/// Constructor
static TinyMap!(K, V, TinyMapPolicy) opCall(Args...)(Args args) {
TinyMap!(K, V, TinyMapPolicy) result;
result.Policy.init(args);
return result;
}
/// Current number of elements
size_t length() @property const {
return _size;
}
/// Indexed access
auto ref V opIndex(Key)(auto ref Key key)
if(is(Unqual!Key == K))
{
assert(key in this);
return _dict[key.internal_code];
}
/// ditto
auto ref const(V) opIndex(Key)(auto ref Key key) const
if(is(Unqual!Key == K))
{
assert(key in this);
return _dict[key.internal_code];
}
/// ditto
V opIndexAssign(V value, K key) {
if (key !in this) {
++_size;
}
_dict[key.internal_code] = value;
Policy._onInsert(key);
return value;
}
/// ditto
void opIndexOpAssign(string op)(V value, K key) {
if (key !in this) {
++_size;
_dict[key.internal_code] = V.init;
}
mixin("_dict[key.internal_code] " ~ op ~ "= value;");
Policy._onInsert(key);
}
/// Check if the key is in the dictionary
// bool opIn_r(K key) const {
auto opBinaryRight(string op)(K key) if (op == "in") {
return Policy._hasKey(key);
}
/// Removal
bool remove(K key) {
if (key in this) {
--_size;
Policy._onRemove(key);
return true;
}
return false;
}
/// Range of keys
auto keys() @property const {
// FIXME: create nice workaround for LDC bug #217
K[] _ks;
foreach (i; 0 .. K.ValueSetSize) {
if (Policy._hasKeyWithCode(i))
_ks ~= K.fromInternalCode(cast(TCode)i);
}
return _ks;
}
/// Range of values
auto values() @property const {
V[] _vs;
foreach (i; 0 .. K.ValueSetSize) {
if (Policy._hasKeyWithCode(i))
_vs ~= _dict[i];
}
return _vs;
}
/// Iteration with foreach
int opApply(scope int delegate(V value) dg) {
foreach (i; iota(K.ValueSetSize)) {
if (Policy._hasKeyWithCode(i)) {
auto ret = dg(_dict[i]);
if (ret != 0) return ret;
}
}
return 0;
}
/// ditto
int opApply(scope int delegate(K key, V value) dg) {
foreach (i; iota(K.ValueSetSize)) {
if (Policy._hasKeyWithCode(i)) {
auto ret = dg(K.fromInternalCode(cast(TCode)i), _dict[i]);
if (ret != 0) return ret;
}
}
return 0;
}
}
/// For each possible key store 0 if it's absent in the dictionary,
/// or 1 otherwise. Bit array is used for compactness.
///
/// This is the default option. In this case, size of dictionary is
/// roughly (V.sizeof + 1/8) * K.ValueSetSize
mixin template useBitArray(K, V) {
private BitArray _value_is_set;
private void init() {
_value_is_set.length = K.ValueSetSize;
}
private bool _hasKey(K key) const {
return _value_is_set[key.internal_code];
}
private bool _hasKeyWithCode(size_t code) const {
return _value_is_set[code];
}
private void _onInsert(K key) {
_value_is_set[key.internal_code] = true;
}
private void _onRemove(K key) {
_value_is_set[key.internal_code] = false;
}
}
/// Use default value specified at construction as an indicator
/// of key absence.
/// That allows to save K.ValueSetSize bits of memory.
///
/// E.g., you might want to use -1 as such indicator if non-negative
/// numbers are stored in the dictionary.
mixin template useDefaultValue(K, V) {
private V _default_value;
private void init(V value) {
_default_value = value;
if (_default_value != V.init) {
_dict[] = _default_value;
}
}
private bool _hasKey(K key) const {
return _dict[key.internal_code] != _default_value;
}
private bool _hasKeyWithCode(size_t code) const {
return _dict[code] != _default_value;
}
private void _onInsert(K key) {}
private void _onRemove(K key) {
this[key] = _default_value;
}
}
/// Allows to set up a dictionary which is always full.
mixin template fillNoRemove(K, V) {
private void init() {
_size = K.ValueSetSize;
}
private void init(V value) {
_size = K.ValueSetSize;
for (size_t i = 0; i < _size; ++i)
_dict[i] = value;
}
private bool _hasKey(K key) const {
return true;
}
private bool _hasKeyWithCode(size_t code) const {
return true;
}
private void _onInsert(K key) {}
private void _onRemove(K key) {
++_size;
}
}
unittest {
import std.array;
import bio.core.base;
void test(M)(ref M dict) {
auto b1 = Base('A');
auto b2 = Base('C');
auto b3 = Base('G');
auto b4 = Base('T');
dict[b1] = 2;
dict[b2] = 3;
assert(dict.length == 2);
assert(dict[b1] == 2);
assert(b2 in dict);
assert(b3 !in dict);
assert(b4 !in dict);
dict[b4] = 5;
assert(equal(sort(array(dict.values)), [2, 3, 5]));
dict.remove(b1);
assert(b1 !in dict);
assert(dict.length == 2);
assert(dict[b2] == 3);
foreach (k, v; dict) {
assert(k in dict);
assert(dict[k] == v);
}
}
auto dict1 = TinyMap!(Base, int)();
auto dict2 = TinyMap!(Base, int, useDefaultValue)(-1);
int[Base] dict3;
test(dict1);
test(dict2);
test(dict3);
auto dict4 = TinyMap!(Base, ulong[4])();
dict4[Base('A')] = [0, 1, 2, 3];
dict4[Base('A')][3] += 1;
assert(dict4[Base('A')] == [0, 1, 2, 4]);
}
/// Convenient mixin template for getting your struct working with TinyMap.
///
/// Creates
/// 1) private member of type T with name _code
/// 2) fromInternalCode static method
/// 3) internal_code property
/// 4) static member ValueSetSize equal to N
/// 5) invariant that _code is always less than ValueSetSize
///
/// That is, the only thing which implementation is up to you is
/// setting _code appropriately.
mixin template TinyMapInterface(uint N, T=ubyte) if (isUnsigned!T) {
private T _code;
enum ValueSetSize = N;
static assert(N <= 2 ^^ (T.sizeof * 8));
static typeof(this) fromInternalCode(T code) {
typeof(this) obj = void;
obj._code = code;
return obj;
}
T internal_code() @property const {
return _code;
}
}
|