Javolution 6.0.0 java
FractalTableImpl.java
Go to the documentation of this file.
1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2012 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */
9 package javolution.util.internal.table;
10 
11 import javolution.lang.MathLib;
12 
19 final class FractalTableImpl {
20 
21  static final int BASE_CAPACITY_MIN = 16;
22  static final int SHIFT = 8;
23  private static final int BASE_CAPACITY_MAX = 1 << SHIFT;
24 
26  int offset;
27 
30  private Object[] data;
31 
33  private final int shift;
34 
35  public FractalTableImpl() {
36  this.shift = 0;
37  data = new Object[BASE_CAPACITY_MIN];
38  }
39 
40  public FractalTableImpl(int shift) {
41  this.shift = shift;
42  data = new Object[2];
43  }
44 
45  public FractalTableImpl(int shift, Object[] data, int offset) {
46  this.shift = shift;
47  this.data = data;
48  this.offset = offset;
49  }
50 
51  public int capacity() {
52  // Reports lower capacity to ensure that there is no fractal holding
53  // wrapping data (head and tail in the same fractal).
54  return (data.length - 1) << shift;
55  }
56 
57  public Object get(int index) {
58  Object fractal = data[((index + offset) >> shift) & (data.length - 1)];
59  return (shift == 0) ? fractal : ((FractalTableImpl) fractal).get(index
60  + offset);
61  }
62 
63  public Object set(int index, Object element) {
64  int i = ((index + offset) >> shift) & (data.length - 1);
65  if (shift != 0) return F(i).set(index + offset, element);
66  Object previous = data[i];
67  data[i] = element;
68  return previous;
69  }
70 
73  public void shiftLeft(Object inserted, int last, int length) {
74  int mask = (data.length << shift) - 1;
75  int tail = (last + offset) & mask;
76  int head = (last + offset - length) & mask;
77  if (shift == 0) {
78  int n = tail - head;
79  if (head > tail) { // Wrapping
80  System.arraycopy(data, head + 1, data, head, mask - head);
81  data[mask] = data[0];
82  n = tail;
83  }
84  System.arraycopy(data, tail - n + 1, data, tail - n, n);
85  data[tail] = inserted;
86  } else if ((head <= tail) && ((head >> shift) == (tail >> shift))) { // Shift local to inner table.
87  F(head >> shift).shiftLeft(inserted, tail, length); // (no wrapping).
88  } else {
89  int low = head >> shift;
90  int high = (low != data.length - 1) ? low + 1 : 0;
91  F(low).shiftLeft(F(high).get(0), -1, mask - head);
92  while (high != (tail >> shift)) {
93  low = high;
94  high = (low != data.length - 1) ? low + 1 : 0;
95  F(low).offset++; // Full shift left.
96  F(low).set(-1, F(high).get(0));
97  }
98  F(high).shiftLeft(inserted, tail, tail);
99  }
100  }
101 
104  public void shiftRight(Object inserted, int first, int length) {
105  int mask = (data.length << shift) - 1;
106  int head = (first + offset) & mask;
107  int tail = (first + offset + length) & mask;
108  if (shift == 0) {
109  int n = tail - head;
110  if (head > tail) { // Wrapping
111  System.arraycopy(data, 0, data, 1, tail);
112  data[0] = data[mask];
113  n = mask - head;
114  }
115  System.arraycopy(data, head, data, head + 1, n);
116  data[head] = inserted;
117  } else if ((head <= tail) && ((head >> shift) == (tail >> shift))) { // Shift local to inner table.
118  F(head >> shift).shiftRight(inserted, head, length); // (no wrapping).
119  } else {
120  int high = tail >> shift;
121  int low = (high != 0) ? high - 1 : data.length - 1;
122  F(high).shiftRight(F(low).get(-1), 0, tail);
123  while (low != (head >> shift)) {
124  high = low;
125  low = (high != 0) ? high - 1 : data.length - 1;
126  F(high).offset--; // Full shift right.
127  F(high).set(0, F(low).get(-1));
128  }
129  F(low).shiftRight(inserted, head, mask - head);
130  }
131  }
132 
134  if (data.length >= BASE_CAPACITY_MAX) { // Creates outer fractal.
136  copyTo(table.F(0));
137  return table;
138  } else {
139  FractalTableImpl table =
140  new FractalTableImpl(shift, new Object[data.length << 1], 0);
141  copyTo(table);
142  return table;
143  }
144  }
145 
146  // Copy to the specified table.
147  private void copyTo(FractalTableImpl that) {
148  int n = MathLib.min(this.data.length, that.data.length);
149  offset &= (data.length << shift) - 1; // Makes it positive.
150  int o = offset >> shift;
151  if ((o + n) > data.length) { // Wrapping.
152  int w = (o + n) - data.length;
153  n -= w;
154  System.arraycopy(data, 0, that.data, n, w);
155  }
156  System.arraycopy(data, o, that.data, 0, n);
157  that.offset = offset - (o << shift);
158  }
159 
160  private FractalTableImpl allocate(int i) {
162  new Object[1 << SHIFT], 0);
163  data[i] = fractal;
164  return fractal;
165  }
166 
167  private FractalTableImpl F(int i) {
169  return (table != null) ? table : allocate(i);
170  }
171 
172 }
javolution
javolution.util.internal.table.FractalTableImpl.set
Object set(int index, Object element)
Definition: FractalTableImpl.java:63
javolution.util.internal.table.FractalTableImpl.data
Object[] data
Definition: FractalTableImpl.java:30
javolution.util.internal.table.FractalTableImpl
Definition: FractalTableImpl.java:19
javolution.util.internal.table.FractalTableImpl.F
FractalTableImpl F(int i)
Definition: FractalTableImpl.java:167
javolution.util.internal.table.FractalTableImpl.copyTo
void copyTo(FractalTableImpl that)
Definition: FractalTableImpl.java:147
javolution.util.internal.table.FractalTableImpl.BASE_CAPACITY_MIN
static final int BASE_CAPACITY_MIN
Definition: FractalTableImpl.java:21
javolution.lang.MathLib
Definition: MathLib.java:20
javolution.util.internal.table.FractalTableImpl.FractalTableImpl
FractalTableImpl(int shift)
Definition: FractalTableImpl.java:40
javolution.util.internal.table.FractalTableImpl.get
Object get(int index)
Definition: FractalTableImpl.java:57
javolution.util.internal.table.FractalTableImpl.shift
final int shift
Definition: FractalTableImpl.java:33
javolution.lang
Definition: Configurable.java:9
javolution.util.internal.table.FractalTableImpl.FractalTableImpl
FractalTableImpl()
Definition: FractalTableImpl.java:35
javolution.util.internal.table.FractalTableImpl.BASE_CAPACITY_MAX
static final int BASE_CAPACITY_MAX
Definition: FractalTableImpl.java:23
javolution.util.internal.table.FractalTableImpl.capacity
int capacity()
Definition: FractalTableImpl.java:51
javolution.util.internal.table.FractalTableImpl.allocate
FractalTableImpl allocate(int i)
Definition: FractalTableImpl.java:160
javolution.util.internal.table.FractalTableImpl.FractalTableImpl
FractalTableImpl(int shift, Object[] data, int offset)
Definition: FractalTableImpl.java:45
javolution.util.internal.table.FractalTableImpl.upsize
FractalTableImpl upsize()
Definition: FractalTableImpl.java:133
javolution.util.internal.table.FractalTableImpl.shiftRight
void shiftRight(Object inserted, int first, int length)
Definition: FractalTableImpl.java:104
javolution.util.internal.table.FractalTableImpl.shiftLeft
void shiftLeft(Object inserted, int last, int length)
Definition: FractalTableImpl.java:73
javolution.util.internal.table.FractalTableImpl.offset
int offset
Definition: FractalTableImpl.java:26
javolution.util.internal.table.FractalTableImpl.SHIFT
static final int SHIFT
Definition: FractalTableImpl.java:22
javolution.lang.MathLib.min
static int min(int x, int y)
Definition: MathLib.java:1010