KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > util > DoubleArray


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.util;
30
31 public class DoubleArray {
32   double []data;
33   int size;
34
35   public void clear() {
36     size = 0;
37   }
38   
39   private void expand(int max) {
40     while (max > data.length) {
41       double []next = new double[data.length * 2];
42
43       for (int i = 0; i < data.length; i++)
44     next[i] = data[i];
45
46       data = next;
47     }
48   }
49
50   public int size() { return size; }
51
52   public void append(double i) {
53     expand(size + 1);
54
55     data[size++] = i;
56   }
57
58   public void append(DoubleArray array) {
59     expand(size + array.size);
60
61     for (int i = 0; i < array.size; i++)
62       data[size++] = array.data[i];
63   }
64
65   public void insert(int i, double value) {
66     expand(size + 1);
67
68     System.arraycopy(data, i, data, i + 1, size - i);
69     data[i] = value;
70     size++;
71   }
72
73   public double pop() {
74     return data[--size];
75   }
76
77   public void setLength(int size)
78   {
79     expand(size);
80
81     for (int i = this.size; i < size; i++)
82       data[i] = 0;
83
84     this.size = size;
85   }
86
87   public double get(int i) {
88     return data[i];
89   }
90
91   public double last() {
92     return data[size - 1];
93   }
94
95   public void set(int i, double value) {
96     if (i + 1 > size)
97       setLength(i + 1);
98
99     data[i] = value;
100   }
101
102   public boolean contains(double test)
103   {
104     for (int i = 0; i < size; i++)
105       if (data[i] == test)
106     return true;
107
108     return false;
109   }
110
111   public boolean isSubset(DoubleArray subset)
112   {
113     for (int i = 0; i < subset.size; i++)
114       if (! contains(subset.data[i]))
115     return false;
116
117     return true;
118   }
119
120   public void union(DoubleArray newArray)
121   {
122     for (int i = 0; i < newArray.size; i++) {
123       if (! contains(newArray.data[i]))
124     append(newArray.data[i]);
125     }
126   }
127
128   public DoubleArray() {
129     data = new double[16];
130     size = 0;
131   }
132 }
133
Popular Tags