• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GENERATED SOURCE. DO NOT MODIFY. */
2 // © 2016 and later: Unicode, Inc. and others.
3 // License & terms of use: http://www.unicode.org/copyright.html#License
4 /*
5  *******************************************************************************
6  * Copyright (C) 1996-2011, International Business Machines Corporation and    *
7  * others. All Rights Reserved.                                                *
8  *******************************************************************************
9  */
10 package ohos.global.icu.impl;
11 
12 /**
13  * @hide exposed on OHOS
14  * @hide draft / provisional / internal are hidden on OHOS
15  */
16 public class CalendarCache
17 {
18     /**
19      * @hide draft / provisional / internal are hidden on OHOS
20      */
CalendarCache()21     public CalendarCache() {
22         makeArrays(arraySize);
23     }
24 
makeArrays(int newSize)25     private void makeArrays(int newSize) {
26         keys    = new long[newSize];
27         values  = new long[newSize];
28 
29         for (int i = 0; i < newSize; i++) {
30             values[i] = EMPTY;
31         }
32         arraySize = newSize;
33         threshold = (int)(arraySize * 0.75);
34         size = 0;
35     }
36 
37     /**
38      * @hide draft / provisional / internal are hidden on OHOS
39      */
get(long key)40     public synchronized long get(long key) {
41         return values[findIndex(key)];
42     }
43 
44     /**
45      * @hide draft / provisional / internal are hidden on OHOS
46      */
put(long key, long value)47     public synchronized void put(long key, long value)
48     {
49         if (size >= threshold) {
50             rehash();
51         }
52         int index = findIndex(key);
53 
54         keys[index] = key;
55         values[index] = value;
56         size++;
57     }
58 
findIndex(long key)59     private final int findIndex(long key) {
60         int index = hash(key);
61         int delta = 0;
62 
63         while (values[index] != EMPTY && keys[index] != key)
64         {
65             if (delta == 0) {
66                 delta = hash2(key);
67             }
68             index = (index + delta) % arraySize;
69         }
70         return index;
71     }
72 
rehash()73     private void rehash()
74     {
75         int oldSize = arraySize;
76         long[] oldKeys = keys;
77         long[] oldValues = values;
78 
79         if (pIndex < primes.length - 1) {
80             arraySize = primes[++pIndex];
81         } else {
82             arraySize = arraySize * 2 + 1;
83         }
84         size = 0;
85 
86         makeArrays(arraySize);
87         for (int i = 0; i < oldSize; i++) {
88             if (oldValues[i] != EMPTY) {
89                 put(oldKeys[i], oldValues[i]);
90             }
91         }
92     }
93 
94 
95     /**
96      * Produce a uniformly-distributed hash value from an integer key.
97      * This is essentially a linear congruential random number generator
98      * that uses the key as its seed value.
99      */
hash(long key)100     private final int hash(long key)
101     {
102         int h = (int)((key * 15821 + 1) % arraySize);
103         if (h < 0) {
104             h += arraySize;
105         }
106         return h;
107     }
108 
hash2(long key)109     private final int hash2(long key) {
110         return arraySize - 2 - (int)(key % (arraySize-2) );
111     }
112 
113     static private final int primes[] = {  // 5, 17, 31, 47, // for testing
114         61, 127, 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521,
115         131071, 262139,
116     };
117 
118     private int     pIndex      = 0;
119     private int     size        = 0;
120     private int     arraySize   = primes[pIndex];
121     private int     threshold   = (arraySize * 3) / 4;
122 
123     private long[]  keys        = new long[arraySize];
124     private long[]  values      = new long[arraySize];
125 
126     /**
127      * @hide draft / provisional / internal are hidden on OHOS
128      */
129     static public  long EMPTY   = Long.MIN_VALUE;
130 }
131