DoubleArrayDictionary.java

  1. /* Copyright 2002-2022 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.  * @since 11.1
  35.  */
  36. public class DoubleArrayDictionary implements Serializable {

  37.     /** Serializable UID. */
  38.     private static final long serialVersionUID = 20211121L;

  39.     /** Default capacity. */
  40.     private static final int DEFAULT_INITIAL_CAPACITY = 4;

  41.     /** Data container. */
  42.     private final List<Entry> data;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  170.     /** Get an unmodifiable view of the dictionary.
  171.      * <p>
  172.      * The return dictionary is backed by the original instance and offers {@code read-only}
  173.      * access to it, but all operations that modify it throw an {@link UnsupportedOperationException}.
  174.      * </p>
  175.      * @return unmodifiable view of the dictionary
  176.      */
  177.     public DoubleArrayDictionary unmodifiableView() {
  178.         return new View();
  179.     }

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

  202.     /** Entry in a dictionary. */
  203.     public static class Entry implements Serializable {

  204.         /** Serializable UID. */
  205.         private static final long serialVersionUID = 20211121L;

  206.         /** Key. */
  207.         private final String key;

  208.         /** Value. */
  209.         private final double[] value;

  210.         /** Simple constructor.
  211.          * @param key key
  212.          * @param value value
  213.          */
  214.         Entry(final String key, final double[] value) {
  215.             this.key   = key;
  216.             this.value = value.clone();
  217.         }

  218.         /** Get the entry key.
  219.          * @return entry key
  220.          */
  221.         public String getKey() {
  222.             return key;
  223.         }

  224.         /** Get the value.
  225.          * @return a copy of the value (independent from internal array)
  226.          */
  227.         public double[] getValue() {
  228.             return value.clone();
  229.         }

  230.         /** Get the size of the value array.
  231.          * @return size of the value array
  232.          */
  233.         public int size() {
  234.             return value.length;
  235.         }

  236.         /** Increment the value.
  237.          * <p>
  238.          * For the sake of performance, no checks are done on argument.
  239.          * </p>
  240.          * @param increment increment to apply to the entry value
  241.          */
  242.         public void increment(final double[] increment) {
  243.             for (int i = 0; i < increment.length; ++i) {
  244.                 value[i] += increment[i];
  245.             }
  246.         }

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

  263.         /** Reset the value to zero.
  264.          */
  265.         public void zero() {
  266.             Arrays.fill(value, 0.0);
  267.         }

  268.     }

  269.     /** Unmodifiable view of the dictionary. */
  270.     private class View extends DoubleArrayDictionary {

  271.         /** {@link Serializable} UID. */
  272.         private static final long serialVersionUID = 20211121L;

  273.         /**  {@inheritDoc} */
  274.         @Override
  275.         public List<Entry> getData() {
  276.             return DoubleArrayDictionary.this.getData();
  277.         }

  278.         /**  {@inheritDoc} */
  279.         @Override
  280.         public int size() {
  281.             return DoubleArrayDictionary.this.size();
  282.         }

  283.         /**  {@inheritDoc} */
  284.         @Override
  285.         public Map<String, double[]> toMap() {
  286.             return DoubleArrayDictionary.this.toMap();
  287.         }

  288.         /**  {@inheritDoc} */
  289.         @Override
  290.         public void clear() {
  291.             throw new UnsupportedOperationException();
  292.         }

  293.         /**  {@inheritDoc} */
  294.         @Override
  295.         public void put(final String key, final double[] value) {
  296.             throw new UnsupportedOperationException();
  297.         }

  298.         /**  {@inheritDoc} */
  299.         @Override
  300.         public void putAll(final Map<String, double[]> map) {
  301.             throw new UnsupportedOperationException();
  302.         }

  303.         /**  {@inheritDoc} */
  304.         @Override
  305.         public void putAll(final DoubleArrayDictionary dictionary) {
  306.             throw new UnsupportedOperationException();
  307.         }

  308.         /**  {@inheritDoc} */
  309.         @Override
  310.         public double[] get(final String key) {
  311.             return DoubleArrayDictionary.this.get(key);
  312.         }

  313.         /**  {@inheritDoc} */
  314.         @Override
  315.         public Entry getEntry(final String key) {
  316.             return DoubleArrayDictionary.this.getEntry(key);
  317.         }

  318.         /**  {@inheritDoc} */
  319.         @Override
  320.         public boolean remove(final String key) {
  321.             throw new UnsupportedOperationException();
  322.         }

  323.     }

  324. }