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.
 
 
 

122 lines
4.0 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.bam.utils.value;
  21. import bio.bam.tagvalue;
  22. import std.exception;
  23. import std.array;
  24. private {
  25. static ubyte* arrayPointer(ref Value v) {
  26. // if value contains array, as a tagged union, it has the following layout:
  27. //
  28. // Value = size_t
  29. // void*
  30. // other stuff...
  31. return cast(ubyte*)(*(cast(size_t*)(&v) + 1));
  32. }
  33. }
  34. /// Emplace value at address $(D p).
  35. /// Assumes that enough memory is allocated at that address.
  36. /// (You can find needed amount of memory with $(D sizeInBytes) function)
  37. void emplaceValue(ubyte* p, ref Value v) {
  38. enforce(!v.is_nothing, "null value can't be stored in BAM");
  39. auto tag = v.tag;
  40. auto size = tag >> 5; // element size
  41. if ((tag & 1) == 0) { // primitive type
  42. *p++ = cast(ubyte)v.bam_typeid;
  43. p[0 .. size] = (cast(ubyte*)(&v))[0 .. size];
  44. } else {
  45. auto bytes = *cast(size_t*)(&v) * (tag >> 5);
  46. if (v.is_string) {
  47. *p++ = cast(ubyte)v.bam_typeid;
  48. p[0 .. bytes] = arrayPointer(v)[0 .. bytes];
  49. p[bytes] = 0; // trailing zero
  50. } else {
  51. *p++ = cast(ubyte)'B';
  52. *p++ = cast(ubyte)v.bam_typeid;
  53. *(cast(uint*)p) = cast(uint)(bytes / size); // # of elements
  54. p += uint.sizeof;
  55. p[0 .. bytes] = arrayPointer(v)[0 .. bytes];
  56. }
  57. }
  58. }
  59. /// Put a value to an Appender!(ubyte[]) struct
  60. void emplaceValue(ref Appender!(ubyte[]) appender, ref Value v) {
  61. enforce(!v.is_nothing, "null value can't be stored in BAM");
  62. auto tag = v.tag;
  63. auto size = tag >> 5; // element size
  64. if ((tag & 1) == 0) { // primitive type
  65. appender.put(cast(ubyte)v.bam_typeid);
  66. appender.put((cast(ubyte*)(&v))[0 .. size]);
  67. } else {
  68. auto bytes = *cast(size_t*)(&v) * (tag >> 5);
  69. if (v.is_string) {
  70. appender.put(cast(ubyte)v.bam_typeid);
  71. appender.put(arrayPointer(v)[0..bytes]);
  72. appender.put(cast(ubyte)0); // trailing zero
  73. } else {
  74. appender.put(cast(ubyte)'B');
  75. appender.put(cast(ubyte)v.bam_typeid);
  76. uint number_of_elems = cast(uint)(bytes / size);
  77. appender.put((cast(ubyte*)(&number_of_elems))[0..4]);
  78. appender.put(arrayPointer(v)[0 .. bytes]);
  79. }
  80. }
  81. }
  82. /// Calculate size in bytes which value will consume in BAM file.
  83. size_t sizeInBytes(ref Value v) {
  84. enforce(!v.is_nothing, "null value can't be stored in BAM");
  85. auto tag = v.tag;
  86. if ((tag & 1) == 0) {
  87. return char.sizeof + (tag >> 5); // primitive type
  88. }
  89. auto bytes = *cast(size_t*)(&v) * (tag >> 5);
  90. if (v.is_string) {
  91. return char.sizeof + bytes + char.sizeof; // trailing zero
  92. } else {
  93. return 2 * char.sizeof + uint.sizeof + bytes;
  94. }
  95. }