001/*
002        AUTOMATICALLY GENERATED BY jTemp FROM
003        /Users/jsh2/Work/openimaj/target/checkout/core/core/src/main/jtemp/org/openimaj/util/array/#T#ArrayView.jtemp
004*/
005/**
006 * Copyright (c) 2011, The University of Southampton and the individual contributors.
007 * All rights reserved.
008 *
009 * Redistribution and use in source and binary forms, with or without modification,
010 * are permitted provided that the following conditions are met:
011 *
012 *   *  Redistributions of source code must retain the above copyright notice,
013 *      this list of conditions and the following disclaimer.
014 *
015 *   *  Redistributions in binary form must reproduce the above copyright notice,
016 *      this list of conditions and the following disclaimer in the documentation
017 *      and/or other materials provided with the distribution.
018 *
019 *   *  Neither the name of the University of Southampton nor the names of its
020 *      contributors may be used to endorse or promote products derived from this
021 *      software without specific prior written permission.
022 *
023 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
024 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
025 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
026 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
027 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
028 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
030 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
031 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
032 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
033 */
034package org.openimaj.util.array;
035
036/**
037 * A wrapper around a java double array that allows
038 * views onto the array to be created without the 
039 * overhead of maintaining copies of the data.
040 * 
041 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
042 *
043 */
044public class DoubleArrayView implements Cloneable {
045        private double [] array;
046        private int l;
047        private int r;
048        
049        /**
050         * Create a view on the given array from l (inclusive) to 
051         * r (exclusive).
052         * 
053         * @param array the array
054         * @param l start (left) index
055         * @param r end (right) index
056         */
057        public DoubleArrayView(double [] array, int l, int r) {
058                this.array = array;
059                this.l = l;
060                this.r = r;
061        }
062        
063        /**
064         * Create a view on the entirety of the given array.
065         * 
066         * @param array the array.
067         */
068        public DoubleArrayView(double [] array) {
069                this(array, 0, array.length);
070        }
071        
072        /**
073         * Create a view on an array of length n. The underlying
074         * array is initialised automatically. 
075         * 
076         * @param n the length of the view and underlying array.
077         */
078        public DoubleArrayView(int n) {
079                this(new double[n], 0, n);
080        }
081        
082        /**
083         * Get a subview of this view from l1 (inclusive) to r1 (exclusive).
084         * @param l1 start (left) index 
085         * @param r1 end (right) index
086         * @return subview
087         */
088        public DoubleArrayView subView(int l1, int r1) {
089                if ((l+l1) < 0 || (l+r1)>r)
090                        throw new ArrayIndexOutOfBoundsException();
091                
092                return new DoubleArrayView(array, l+l1, l+r1);
093        }
094        
095        /**
096         * Get the ith element of this array view.
097         *  
098         * @param i the index to get
099         * @return the value at the ith index
100         */
101        public double get(int i) {
102                int idx = i + l;
103                
104                if (idx < 0 || idx>=r)
105                        throw new ArrayIndexOutOfBoundsException();
106                
107                return array[idx];
108        }
109        
110        /**
111         * Get the ith element of this array view omitting any
112         * bounds checks.
113         *  
114         * @param i the index to get
115         * @return the value at the ith index
116         */
117        public double getFast(int i) {
118                return array[i + l];
119        }
120        
121        /**
122         * Set the ith element of this array view to the value v.
123         *  
124         * @param i the index to set
125         * @param v the value to set
126         */
127        public void set(int i, double v) {
128                int idx = i + l;
129                
130                if (idx < 0 || idx>=r)
131                        throw new ArrayIndexOutOfBoundsException();
132                
133                array[idx] = v;
134        }
135        
136        /**
137         * Set the ith element of this array view to the value v.
138         * Bounds checks are omitted.
139         *  
140         * @param i the index to set
141         * @param v the value to set
142         */
143        public void setFast(int i, double v) {
144                array[i + l] = v;
145        }
146        
147        /**
148         * Get access to the underlying array of this view.
149         * 
150         * @return the underlying array.
151         */
152        public double [] getUnderlyingStorage() {
153                return array;
154        }
155        
156        /**
157         * Get the size (number of elements) in the view.
158         * @return the size
159         */
160        public int size() {
161                return r-l;
162        }
163        
164        /**
165         * Convert this view to an array.
166         * @return a copy of the view's data as an array
167         */
168        public double [] toArray() {
169                double [] a = new double[r-l];
170                System.arraycopy(array, l, a, 0, r-l);
171                return a;
172        }
173        
174        /* (non-Javadoc)
175         * @see java.lang.Object#toString()
176         */
177        @Override
178        public String toString() {
179                String s = "[";
180                
181                for (int i=l; i<r-1; i++) {
182                        s += array[i] + ",";
183                }
184                s += array[r-1] + "]";
185                return s;
186        }
187        
188        /* (non-Javadoc)
189         * @see java.lang.Object#clone()
190         */
191        @Override
192        public DoubleArrayView clone() {
193                return new DoubleArrayView(this.toArray());
194        }
195}