forked from rdotnet/rdotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogicalVector.cs
111 lines (104 loc) · 4.13 KB
/
LogicalVector.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using RDotNet.Internals;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Permissions;
namespace RDotNet
{
/// <summary>
/// A collection of Boolean values.
/// </summary>
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public class LogicalVector : Vector<bool>
{
/// <summary>
/// Creates a new empty LogicalVector with the specified length.
/// </summary>
/// <param name="engine">The <see cref="REngine"/> handling this instance.</param>
/// <param name="length">The length.</param>
/// <seealso cref="REngineExtension.CreateLogicalVector(REngine, int)"/>
public LogicalVector(REngine engine, int length)
: base(engine, SymbolicExpressionType.LogicalVector, length)
{ }
/// <summary>
/// Creates a new LogicalVector with the specified values.
/// </summary>
/// <param name="engine">The <see cref="REngine"/> handling this instance.</param>
/// <param name="vector">The values.</param>
/// <seealso cref="REngineExtension.CreateLogicalVector(REngine, IEnumerable{bool})"/>
public LogicalVector(REngine engine, IEnumerable<bool> vector)
: base(engine, SymbolicExpressionType.LogicalVector, vector)
{ }
/// <summary>
/// Creates a new instance for a Boolean vector.
/// </summary>
/// <param name="engine">The <see cref="REngine"/> handling this instance.</param>
/// <param name="coerced">The pointer to a Boolean vector.</param>
protected internal LogicalVector(REngine engine, IntPtr coerced)
: base(engine, coerced)
{ }
/// <summary>
/// Gets or sets the element at the specified index.
/// </summary>
/// <param name="index">The zero-based index of the element to get or set.</param>
/// <returns>The element at the specified index.</returns>
public override bool this[int index]
{
get
{
if (index < 0 || Length <= index)
{
throw new ArgumentOutOfRangeException();
}
using (new ProtectedPointer(this))
{
int offset = GetOffset(index);
int data = Marshal.ReadInt32(DataPointer, offset);
return Convert.ToBoolean(data);
}
}
set
{
if (index < 0 || Length <= index)
{
throw new ArgumentOutOfRangeException();
}
using (new ProtectedPointer(this))
{
int offset = GetOffset(index);
int data = Convert.ToInt32(value);
Marshal.WriteInt32(DataPointer, offset, data);
}
}
}
/// <summary>
/// Efficient conversion from R vector representation to the array equivalent in the CLR
/// </summary>
/// <returns>Array equivalent</returns>
protected override bool[] GetArrayFast()
{
int[] intValues = new int[this.Length];
Marshal.Copy(DataPointer, intValues, 0, intValues.Length);
return Array.ConvertAll(intValues, Convert.ToBoolean);
}
/// <summary>
/// Efficient initialisation of R vector values from an array representation in the CLR
/// </summary>
protected override void SetVectorDirect(bool[] values)
{
var intValues = Array.ConvertAll(values, Convert.ToInt32);
Marshal.Copy(intValues, 0, DataPointer, values.Length);
}
/// <summary>
/// Gets the size of a Boolean value in byte.
/// </summary>
protected override int DataSize
{
get
{
// Boolean is int internally.
return sizeof(int);
}
}
}
}