Debian Codegraph @

src/dr/math/matrixAlgebra/WrappedVector.java

raw symbols SCIP
1 /*
2 * WrappedVector.java
3 *
4 * Copyright (c) 2002-2017 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.math.matrixAlgebra;
27
28 import dr.inference.model.Parameter;
29
30 /**
31 * Created by msuchard on 1/27/17.
32 */
33
34 public interface WrappedVector {
35
36 double get(final int i);
37
38 void set(final int i, final double x);
39
40 int getDim();
41
42 double[] getBuffer();
43
44 int getOffset();
45
46 abstract class Abstract implements WrappedVector {
47 final protected double[] buffer;
48 final protected int offset;
49 final protected int dim;
50
51 public Abstract(final double[] buffer, final int offset, final int dim) {
52 this.buffer = buffer;
53 this.offset = offset;
54 this.dim = dim;
55 }
56
57 final public double[] getBuffer() {
58 return buffer;
59 }
60
61 final public int getOffset() { return offset; }
62
63 final public int getDim() {
64 return dim;
65 }
66
67 final public String toString() {
68 StringBuilder sb = new StringBuilder("[ ");
69 if (dim > 0) {
70 sb.append(get(0));
71 }
72 for (int i = 1; i < dim; ++i) {
73 sb.append(", ").append(get(i));
74 }
75 sb.append(" ]");
76
77 return sb.toString();
78 }
79 }
80
81 final class Raw extends Abstract {
82
83 public Raw(double[] buffer, int offset, int dim) {
84 super(buffer, offset, dim);
85 }
86
87 @Override
88 final public double get(final int i) {
89 return buffer[offset + i];
90 }
91
92 @Override
93 final public void set(final int i, final double x) {
94 buffer[offset + i] = x;
95 }
96 }
97
98 final class Indexed extends Abstract {
99
100 final private int[] indices;
101
102 public Indexed(double[] buffer, int offset, int[] indices, int dim) {
103 super(buffer, offset, dim);
104 this.indices = indices;
105 }
106
107 @Override
108 final public double get(int i) { return buffer[offset + indices[i]]; }
109
110 @Override
111 final public void set(int i, double x) { buffer[offset + indices[i]] = x; }
112 }
113 }