1 /* 2 This file is part of BioD. 3 Copyright (C) 2012 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.bgzf.virtualoffset; 25 26 import std.conv; 27 28 /// Structure representing virtual offset in BGZF-compressed file. 29 struct VirtualOffset { 30 /// Params: 31 /// 32 /// coffset = unsigned byte offset into the BGZF file 33 /// to the beginning of a BGZF block. 34 /// Must be strictly less than 2^48. 35 /// 36 /// uoffset = unsigned byte offset into the uncompressed 37 /// data stream represented by that BGZF block 38 this(ulong coffset, ushort uoffset) nothrow @safe 39 in { 40 assert(coffset < (1UL<<48)); 41 } 42 body { 43 voffset = (coffset << 16) | uoffset; 44 } 45 46 /// Set both coffset and uoffset packed as (coffset<<16)|uoffset 47 this(ulong voffset) nothrow @safe { 48 this.voffset = voffset; 49 } 50 51 /// ditto 52 ulong coffset() @property const nothrow @safe pure { 53 return voffset >> 16; 54 } 55 56 /// ditto 57 ushort uoffset() @property const nothrow @safe pure { 58 return voffset & 0xFFFF; 59 } 60 61 int opCmp(const ref VirtualOffset other) const nothrow @safe pure { 62 if (this.voffset > other.voffset) { return 1; } 63 if (this.voffset < other.voffset) { return -1; } 64 return 0; 65 } 66 67 bool opEquals(const ref VirtualOffset other) const nothrow @safe { 68 return this.voffset == other.voffset; 69 } 70 71 bool opEquals(ulong voffset) const nothrow @safe { 72 auto vo = VirtualOffset(voffset); 73 return opEquals(vo); 74 } 75 76 ulong opCast() const nothrow @safe pure { 77 return voffset; 78 } 79 80 /// String representation in format "<coffset>/<uoffset>" 81 string toString() { 82 return to!string(coffset) ~ "/" ~ to!string(uoffset); 83 } 84 85 private: 86 ulong voffset; 87 } 88 89 unittest { 90 auto voffset = VirtualOffset(100500, 42); 91 assert(voffset.coffset == 100500); 92 assert(voffset.uoffset == 42); 93 assert(voffset == (100500UL << 16) + 42UL); 94 assert(cast(ulong)voffset == (100500UL << 16) + 42UL); 95 }