DoubleArrayDictionary.java

  1. /* Copyright 2002-2025 CS GROUP
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.utils;

  18. import java.io.Serializable;
  19. import java.util.ArrayList;
  20. import java.util.Arrays;
  21. import java.util.Collections;
  22. import java.util.HashMap;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.Map;

  26. /** String → double[] mapping, for small number of keys.
  27.  * <p>
  28.  * This class is a low overhead for a very small number of keys.
  29.  * It is based on simple array and string comparison. It plays
  30.  * the same role a {@code Map<String, double[]>} but with reduced
  31.  * features and not intended for large number of keys. For such
  32.  * needs the regular {@code Map<String, double[]>} should be preferred.
  33.  * </p>
  34.  *
  35.  * @see DataDictionary
  36.  * @since 11.1
  37.  */
  38. public class DoubleArrayDictionary implements Serializable {

  39.     /** Serializable UID. */
  40.     private static final long serialVersionUID = 20211121L;

  41.     /** Default capacity. */
  42.     private static final int DEFAULT_INITIAL_CAPACITY = 4;

  43.     /** Data container. */
  44.     private final List<Entry> data;

  45.     /** Constructor with {@link #DEFAULT_INITIAL_CAPACITY default initial capacity}.
  46.      */
  47.     public DoubleArrayDictionary() {
  48.         this(DEFAULT_INITIAL_CAPACITY);
  49.     }

  50.     /** Constructor with specified capacity.
  51.      * @param initialCapacity initial capacity
  52.      */
  53.     public DoubleArrayDictionary(final int initialCapacity) {
  54.         data = new ArrayList<>(initialCapacity);
  55.     }

  56.     /** Constructor from another dictionary.
  57.      * @param dictionary dictionary to use for initializing entries
  58.      */
  59.     public DoubleArrayDictionary(final DoubleArrayDictionary dictionary) {
  60.         // take care to call dictionary.getData() and not use dictionary.data,
  61.         // otherwise we get an empty dictionary when using a DoubleArrayDictionary.view
  62.         this(DEFAULT_INITIAL_CAPACITY + dictionary.getData().size());
  63.         for (final Entry entry : dictionary.getData()) {
  64.             // we don't call put(key, value) to avoid the overhead of the unneeded call to remove(key)
  65.             data.add(new Entry(entry.getKey(), entry.getValue()));
  66.         }
  67.     }

  68.     /** Constructor from a map.
  69.      * @param map map to use for initializing entries
  70.      */
  71.     public DoubleArrayDictionary(final Map<String, double[]> map) {
  72.         this(map.size());
  73.         for (final Map.Entry<String, double[]> entry : map.entrySet()) {
  74.             // we don't call put(key, value) to avoid the overhead of the unneeded call to remove(key)
  75.             data.add(new Entry(entry.getKey(), entry.getValue()));
  76.         }
  77.     }

  78.     /** Get an unmodifiable view of the dictionary entries.
  79.      * @return unmodifiable view of the dictionary entries
  80.      */
  81.     public List<Entry> getData() {
  82.         return Collections.unmodifiableList(data);
  83.     }

  84.     /** Get the number of dictionary entries.
  85.      * @return number of dictionary entries
  86.      */
  87.     public int size() {
  88.         return data.size();
  89.     }

  90.     /** Create a map from the instance.
  91.      * <p>
  92.      * The map contains a copy of the instance data
  93.      * </p>
  94.      * @return copy of the dictionary, as an independent map
  95.      */
  96.     public Map<String, double[]> toMap() {
  97.         final Map<String, double[]> map = new HashMap<>(data.size());
  98.         for (final Entry entry : data) {
  99.             map.put(entry.getKey(), entry.getValue());
  100.         }
  101.         return map;
  102.     }

  103.     /** Remove all entries.
  104.      */
  105.     public void clear() {
  106.         data.clear();
  107.     }

  108.     /** Add an entry.
  109.      * <p>
  110.      * If an entry with the same key already exists, it will be removed first.
  111.      * </p>
  112.      * <p>
  113.      * The new entry is always put at the end.
  114.      * </p>
  115.      * @param key entry key
  116.      * @param value entry value
  117.      */
  118.     public void put(final String key, final double[] value) {
  119.         remove(key);
  120.         data.add(new Entry(key, value));
  121.     }

  122.     /** Put all the entries from the map in the dictionary.
  123.      * @param map map to copy into the instance
  124.      */
  125.     public void putAll(final Map<String, double[]> map) {
  126.         for (final Map.Entry<String, double[]> entry : map.entrySet()) {
  127.             put(entry.getKey(), entry.getValue());
  128.         }
  129.     }

  130.     /** Put all the entries from another dictionary.
  131.      * @param dictionary dictionary to copy into the instance
  132.      */
  133.     public void putAll(final DoubleArrayDictionary dictionary) {
  134.         for (final Entry entry : dictionary.data) {
  135.             put(entry.getKey(), entry.getValue());
  136.         }
  137.     }

  138.     /** Get the value corresponding to a key.
  139.      * @param key entry key
  140.      * @return copy of the value corresponding to the key or null if key not present
  141.      */
  142.     public double[] get(final String key) {
  143.         final Entry entry = getEntry(key);
  144.         return entry == null ? null : entry.getValue();
  145.     }

  146.     /** Get a complete entry.
  147.      * @param key entry key
  148.      * @return entry with key if it exists, null otherwise
  149.      */
  150.     public Entry getEntry(final String key) {
  151.         for (final Entry entry : data) {
  152.             if (entry.getKey().equals(key)) {
  153.                 return entry;
  154.             }
  155.         }
  156.         return null;
  157.     }

  158.     /** Remove an entry.
  159.      * @param key key of the entry to remove
  160.      * @return true if an entry has been removed, false if the key was not present
  161.      */
  162.     public boolean remove(final String key) {
  163.         final Iterator<Entry> iterator = data.iterator();
  164.         while (iterator.hasNext()) {
  165.             if (iterator.next().getKey().equals(key)) {
  166.                 iterator.remove();
  167.                 return true;
  168.             }
  169.         }
  170.         return false;
  171.     }


  172.     /** Get a string representation of the dictionary.
  173.      * <p>
  174.      * This string representation is intended for improving displays in debuggers only.
  175.      * </p>
  176.      * @return string representation of the dictionary
  177.      */
  178.     @Override
  179.     public String toString() {
  180.         final StringBuilder builder = new StringBuilder();
  181.         builder.append('{');
  182.         for (int i = 0; i < data.size(); ++i) {
  183.             if (i > 0) {
  184.                 builder.append(", ");
  185.             }
  186.             builder.append(data.get(i).getKey());
  187.             builder.append('[');
  188.             builder.append(data.get(i).getValue().length);
  189.             builder.append(']');
  190.         }
  191.         builder.append('}');
  192.         return builder.toString();
  193.     }

  194.     /** Entry in a dictionary. */
  195.     public static class Entry implements Serializable {

  196.         /** Serializable UID. */
  197.         private static final long serialVersionUID = 20211121L;

  198.         /** Key. */
  199.         private final String key;

  200.         /** Value. */
  201.         private final double[] value;

  202.         /** Simple constructor.
  203.          * @param key key
  204.          * @param value value
  205.          */
  206.         Entry(final String key, final double[] value) {
  207.             this.key   = key;
  208.             this.value = value.clone();
  209.         }

  210.         /** Get the entry key.
  211.          * @return entry key
  212.          */
  213.         public String getKey() {
  214.             return key;
  215.         }

  216.         /** Get the value.
  217.          * @return a copy of the value (independent from internal array)
  218.          */
  219.         public double[] getValue() {
  220.             return value.clone();
  221.         }

  222.         /** Get the size of the value array.
  223.          * @return size of the value array
  224.          */
  225.         public int size() {
  226.             return value.length;
  227.         }

  228.         /** Increment the value.
  229.          * <p>
  230.          * For the sake of performance, no checks are done on argument.
  231.          * </p>
  232.          * @param increment increment to apply to the entry value
  233.          */
  234.         public void increment(final double[] increment) {
  235.             for (int i = 0; i < increment.length; ++i) {
  236.                 value[i] += increment[i];
  237.             }
  238.         }

  239.         /** Increment the value with another scaled entry.
  240.          * <p>
  241.          * Each component {@code value[i]} will be replaced by {@code value[i] + factor * raw.value[i]}.
  242.          * </p>
  243.          * <p>
  244.          * For the sake of performance, no checks are done on arguments.
  245.          * </p>
  246.          * @param factor multiplicative factor for increment
  247.          * @param raw raw increment to be multiplied by {@code factor} and then added
  248.          * @since 11.1.1
  249.          */
  250.         public void scaledIncrement(final double factor, final Entry raw) {
  251.             for (int i = 0; i < raw.value.length; ++i) {
  252.                 value[i] += factor * raw.value[i];
  253.             }
  254.         }

  255.         /** Reset the value to zero.
  256.          */
  257.         public void zero() {
  258.             Arrays.fill(value, 0.0);
  259.         }

  260.     }
  261. }