public static IntPtr CreateProblem(DataInstances data) { problem prob = new problem(); prob.l = data.Instances.Count; prob.n = data.FeatureCount; prob.y = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * data.Instances.Count); Marshal.Copy(data.Labels.ToArray(),0,prob.y,data.Labels.Count); prob.x = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)) * data.Instances.Count); IntPtr i_ptr_x = prob.x; for (int i = 0; i < data.Instances.Count; i++) { IntPtr inst = CreateFeatureNode(data.Instances[i]); Marshal.StructureToPtr(inst, i_ptr_x, true); i_ptr_x = IntPtr.Add(i_ptr_x, Marshal.SizeOf(typeof(IntPtr))); } prob.bias = -1.0d; int size = Marshal.SizeOf(prob); IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(prob, ptr, true); return ptr; }
public static void FreeProblem(problem x) { Marshal.FreeHGlobal(x.y); x.y = IntPtr.Zero; IntPtr i_ptr_x = x.x; for (int i = 0; i < x.l; i++) { IntPtr ptr_nodes = (IntPtr)Marshal.PtrToStructure(i_ptr_x, typeof(IntPtr)); FreeNode(ptr_nodes); i_ptr_x = IntPtr.Add(i_ptr_x, Marshal.SizeOf(typeof(IntPtr))); } Marshal.FreeHGlobal(x.x); x.x = IntPtr.Zero; }