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