~seh999/jcog/proto3

« back to all changes in this revision

Viewing changes to spacetime/src/opencog/spacetime/scene/data/IntArray.java

  • Committer: SeH
  • Date: 2009-09-19 22:59:48 UTC
  • Revision ID: seh999@gmail.com-20090919225948-q3ab80xa57i74mm6
start of major jReality refactoring

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 *
3
 
 * This file is part of jReality. jReality is open source software, made
4
 
 * available under a BSD license:
5
 
 *
6
 
 * Copyright (c) 2003-2006, jReality Group: Charles Gunn, Tim Hoffmann, Markus
7
 
 * Schmies, Steffen Weissmann.
8
 
 *
9
 
 * All rights reserved.
10
 
 *
11
 
 * Redistribution and use in source and binary forms, with or without
12
 
 * modification, are permitted provided that the following conditions are met:
13
 
 *
14
 
 * - Redistributions of source code must retain the above copyright notice, this
15
 
 *   list of conditions and the following disclaimer.
16
 
 *
17
 
 * - Redistributions in binary form must reproduce the above copyright notice,
18
 
 *   this list of conditions and the following disclaimer in the documentation
19
 
 *   and/or other materials provided with the distribution.
20
 
 *
21
 
 * - Neither the name of jReality nor the names of its contributors nor the
22
 
 *   names of their associated organizations may be used to endorse or promote
23
 
 *   products derived from this software without specific prior written
24
 
 *   permission.
25
 
 *
26
 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27
 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28
 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29
 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30
 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31
 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32
 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33
 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34
 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35
 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36
 
 * POSSIBILITY OF SUCH DAMAGE.
37
 
 *
38
 
 */
39
 
 
40
 
 
41
 
package opencog.spacetime.scene.data;
42
 
 
43
 
import java.nio.ByteBuffer;
44
 
import java.nio.ByteOrder;
45
 
import java.nio.IntBuffer;
46
 
 
47
 
/**
48
 
 * An immutable array. A good JIT compiler optimizes this such that access it is not slower
49
 
 * as for the pure array. The advantage is, that only the creator who provided the array is allowed to 
50
 
 * change it.
51
 
 * 
52
 
 * @version 1.0
53
 
 * @author <a href="mailto:hoffmann@math.tu-berlin.de">Tim Hoffmann</a>
54
 
 *
55
 
 */
56
 
public class IntArray extends DataList {
57
 
  transient final int[] data;
58
 
  transient final int offset, length;
59
 
  /**
60
 
   * 
61
 
   */
62
 
  public IntArray(int[] data) {
63
 
    this(data, 0, data.length);
64
 
  }
65
 
  public IntArray(int[] data, int offset, int length) {
66
 
    super(StorageModel.INT_ARRAY, data, offset, length);
67
 
    this.data= data;
68
 
    this.offset=offset;
69
 
    this.length=length;
70
 
  }
71
 
  public IntArray toIntArray() {
72
 
    return this;
73
 
  }
74
 
  public final int[] toIntArray(int[] target) {
75
 
    if(target==null) target=new int[length];
76
 
    for(int src=offset, dst=0, n=length; dst<n; src++, dst++)
77
 
      target[dst]=data[src];
78
 
    return target;
79
 
  }
80
 
  /**
81
 
   * copies the containing data into a given or native ByteBuffer<br>
82
 
   * JUST FOR TESTING
83
 
   * @param bb
84
 
   * @return bb
85
 
   */
86
 
  public final ByteBuffer toNativeByteBuffer(ByteBuffer bb) {
87
 
      if(bb==null) {
88
 
          bb = (ByteBuffer)ByteBuffer.allocateDirect(length*4).order(ByteOrder.nativeOrder());      
89
 
      }
90
 
      IntBuffer target = bb.asIntBuffer();
91
 
      target.put(data, offset, length);
92
 
      return bb;
93
 
  }
94
 
  /**
95
 
   * Copies all entries of the underlying array into the <code>target</code>
96
 
   * parameter or into a new array using widening conversion for each entry.
97
 
   * Return the target array.
98
 
   */
99
 
  public final double[] toDoubleArray(double[] target) {
100
 
    if(target==null) target=new double[length];
101
 
    for(int src=offset, dst=0, n=length; dst<n; src++, dst++)
102
 
      target[dst]=data[src];
103
 
    return target;
104
 
  }
105
 
  public final int getValueAt(final int n) {
106
 
    if(n>=length) throw new ArrayIndexOutOfBoundsException();
107
 
    return data[n+offset];
108
 
  }
109
 
  public final int getLength() {
110
 
    return length;
111
 
  }
112
 
}