You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

96 lines
3.1 KiB

  1. /*
  2. This file is part of BioD.
  3. Copyright (C) 2012 Artem Tarasov <lomereiter@gmail.com>
  4. Permission is hereby granted, free of charge, to any person obtaining a
  5. copy of this software and associated documentation files (the "Software"),
  6. to deal in the Software without restriction, including without limitation
  7. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. and/or sell copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  17. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  18. DEALINGS IN THE SOFTWARE.
  19. */
  20. module bio.core.bgzf.virtualoffset;
  21. import std.conv;
  22. /// Structure representing virtual offset in BGZF-compressed file.
  23. struct VirtualOffset {
  24. /// Params:
  25. ///
  26. /// coffset = unsigned byte offset into the BGZF file
  27. /// to the beginning of a BGZF block.
  28. /// Must be strictly less than 2^48.
  29. ///
  30. /// uoffset = unsigned byte offset into the uncompressed
  31. /// data stream represented by that BGZF block
  32. this(ulong coffset, ushort uoffset) nothrow
  33. in {
  34. assert(coffset < (1UL<<48));
  35. }
  36. body {
  37. voffset = (coffset << 16) | uoffset;
  38. }
  39. /// Set both coffset and uoffset packed as (coffset<<16)|uoffset
  40. this(ulong voffset) nothrow {
  41. this.voffset = voffset;
  42. }
  43. /// ditto
  44. ulong coffset() @property const nothrow {
  45. return voffset >> 16;
  46. }
  47. /// ditto
  48. ushort uoffset() @property const nothrow {
  49. return voffset & 0xFFFF;
  50. }
  51. int opCmp(const ref VirtualOffset other) const nothrow {
  52. if (this.voffset > other.voffset) { return 1; }
  53. if (this.voffset < other.voffset) { return -1; }
  54. return 0;
  55. }
  56. bool opEquals(const ref VirtualOffset other) const nothrow {
  57. return this.voffset == other.voffset;
  58. }
  59. bool opEquals(ulong voffset) const nothrow {
  60. auto vo = VirtualOffset(voffset);
  61. return opEquals(vo);
  62. }
  63. ulong opCast() const nothrow {
  64. return voffset;
  65. }
  66. /// String representation in format "<coffset>/<uoffset>"
  67. string toString() {
  68. return to!string(coffset) ~ "/" ~ to!string(uoffset);
  69. }
  70. private:
  71. ulong voffset;
  72. }
  73. unittest {
  74. auto voffset = VirtualOffset(100500, 42);
  75. assert(voffset.coffset == 100500);
  76. assert(voffset.uoffset == 42);
  77. assert(voffset == (100500UL << 16) + 42UL);
  78. assert(cast(ulong)voffset == (100500UL << 16) + 42UL);
  79. }