FieldArrayDictionary.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.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.Collections;
  21. import java.util.HashMap;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import java.util.Map;

  25. import org.hipparchus.CalculusFieldElement;
  26. import org.hipparchus.Field;
  27. import org.hipparchus.util.MathArrays;

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

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

  42.     /** Field to which elements belong. */
  43.     private final Field<T> field;

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

  46.     /** Constructor with {@link #DEFAULT_INITIAL_CAPACITY default initial capacity}.
  47.      * @param field field to which the elements belong
  48.      */
  49.     public FieldArrayDictionary(final Field<T>  field) {
  50.         this(field, DEFAULT_INITIAL_CAPACITY);
  51.     }

  52.     /** Constructor with specified capacity.
  53.      * @param field field to which the elements belong
  54.      * @param initialCapacity initial capacity
  55.      */
  56.     public FieldArrayDictionary(final Field<T> field, final int initialCapacity) {
  57.         this.field = field;
  58.         this.data  = new ArrayList<>(initialCapacity);
  59.     }

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

  72.     /** Constructor from a map.
  73.      * @param field field to which the elements belong
  74.      * @param map map to use for initializing entries
  75.      */
  76.     public FieldArrayDictionary(final Field<T> field, final Map<String, T[]> map) {
  77.         this(field, map.size());
  78.         for (final Map.Entry<String, T[]> entry : map.entrySet()) {
  79.             // we don't call put(key, value) to avoid the overhead of the unneeded call to remove(key)
  80.             data.add(new Entry(entry.getKey(), entry.getValue()));
  81.         }
  82.     }

  83.     /** Get the field to which elements belong.
  84.      * @return field to which elements belong
  85.      */
  86.     public Field<T> getField() {
  87.         return field;
  88.     }

  89.     /** Get an unmodifiable view of the dictionary entries.
  90.      * @return unmodifiable view of the dictionary entries
  91.      */
  92.     public List<Entry> getData() {
  93.         return Collections.unmodifiableList(data);
  94.     }

  95.     /** Get the number of dictionary entries.
  96.      * @return number of dictionary entries
  97.      */
  98.     public int size() {
  99.         return data.size();
  100.     }

  101.     /** Create a map from the instance.
  102.      * <p>
  103.      * The map contains a copy of the instance data
  104.      * </p>
  105.      * @return copy of the dictionary, as an independent map
  106.      */
  107.     public Map<String, T[]> toMap() {
  108.         final Map<String, T[]> map = new HashMap<>(data.size());
  109.         for (final Entry entry : data) {
  110.             map.put(entry.getKey(), entry.getValue());
  111.         }
  112.         return map;
  113.     }

  114.     /** Remove all entries.
  115.      */
  116.     public void clear() {
  117.         data.clear();
  118.     }

  119.     /** Add an entry.
  120.      * <p>
  121.      * If an entry with the same key already exists, it will be removed first.
  122.      * </p>
  123.      * <p>
  124.      * The new entry is always put at the end.
  125.      * </p>
  126.      * @param key entry key
  127.      * @param value entry value
  128.      */
  129.     public void put(final String key, final T[] value) {
  130.         remove(key);
  131.         data.add(new Entry(key, value));
  132.     }

  133.     /** Add an entry.
  134.      * <p>
  135.      * If an entry with the same key already exists, it will be removed first.
  136.      * </p>
  137.      * <p>
  138.      * The new entry is always put at the end.
  139.      * </p>
  140.      * @param key entry key
  141.      * @param value entry value
  142.      */
  143.     public void put(final String key, final double[] value) {
  144.         final T[] converted = MathArrays.buildArray(field, value.length);
  145.         for (int i = 0; i < value.length; ++i) {
  146.             converted[i] = field.getZero().newInstance(value[i]);
  147.         }
  148.         put(key, converted);
  149.     }

  150.     /** Put all the entries from the map in the dictionary.
  151.      * @param map map to copy into the instance
  152.      */
  153.     public void putAll(final Map<String, T[]> map) {
  154.         for (final Map.Entry<String, T[]> entry : map.entrySet()) {
  155.             put(entry.getKey(), entry.getValue());
  156.         }
  157.     }

  158.     /** Put all the entries from another dictionary.
  159.      * @param dictionary dictionary to copy into the instance
  160.      */
  161.     public void putAll(final FieldArrayDictionary<T> dictionary) {
  162.         for (final Entry entry : dictionary.data) {
  163.             put(entry.getKey(), entry.getValue());
  164.         }
  165.     }

  166.     /** Get the value corresponding to a key.
  167.      * @param key entry key
  168.      * @return copy of the value corresponding to the key or null if key not present
  169.      */
  170.     public T[] get(final String key) {
  171.         final Entry entry = getEntry(key);
  172.         return entry == null ? null : entry.getValue();
  173.     }

  174.     /** Get a complete entry.
  175.      * @param key entry key
  176.      * @return entry with key if it exists, null otherwise
  177.      */
  178.     public Entry getEntry(final String key) {
  179.         for (final Entry entry : data) {
  180.             if (entry.getKey().equals(key)) {
  181.                 return entry;
  182.             }
  183.         }
  184.         return null;
  185.     }

  186.     /** remove an entry.
  187.      * @param key key of the entry to remove
  188.      * @return true if an entry has been removed, false if the key was not present
  189.      */
  190.     public boolean remove(final String key) {
  191.         final Iterator<Entry> iterator = data.iterator();
  192.         while (iterator.hasNext()) {
  193.             if (iterator.next().getKey().equals(key)) {
  194.                 iterator.remove();
  195.                 return true;
  196.             }
  197.         }
  198.         return false;
  199.     }

  200.     /** Get an unmodifiable view of the dictionary.
  201.      * <p>
  202.      * The return dictionary is backed by the original instance and offers {@code read-only}
  203.      * access to it, but all operations that modify it throw an {@link UnsupportedOperationException}.
  204.      * </p>
  205.      * @return unmodifiable view of the dictionary
  206.      */
  207.     public FieldArrayDictionary<T> unmodifiableView() {
  208.         return new View(field);
  209.     }

  210.     /** Get a string representation of the dictionary.
  211.      * <p>
  212.      * This string representation is intended for improving displays in debuggers only.
  213.      * </p>
  214.      * @return string representation of the dictionary
  215.      */
  216.     @Override
  217.     public String toString() {
  218.         final StringBuilder builder = new StringBuilder();
  219.         builder.append('{');
  220.         for (int i = 0; i < data.size(); ++i) {
  221.             if (i > 0) {
  222.                 builder.append(", ");
  223.             }
  224.             builder.append(data.get(i).getKey());
  225.             builder.append('[');
  226.             builder.append(data.get(i).getValue().length);
  227.             builder.append(']');
  228.         }
  229.         builder.append('}');
  230.         return builder.toString();
  231.     }

  232.     /** Entry in a dictionary. */
  233.     public class Entry {

  234.         /** Key. */
  235.         private final String key;

  236.         /** Value. */
  237.         private final T[] value;

  238.         /** Simple constructor.
  239.          * @param key key
  240.          * @param value value
  241.          */
  242.         Entry(final String key, final T[] value) {
  243.             this.key   = key;
  244.             this.value = value.clone();
  245.         }

  246.         /** Get the entry key.
  247.          * @return entry key
  248.          */
  249.         public String getKey() {
  250.             return key;
  251.         }

  252.         /** Get the value.
  253.          * @return a copy of the value (independent from internal array)
  254.          */
  255.         public T[] getValue() {
  256.             return value.clone();
  257.         }

  258.         /** Get the size of the value array.
  259.          * @return size of the value array
  260.          */
  261.         public int size() {
  262.             return value.length;
  263.         }

  264.         /** Increment the value.
  265.          * <p>
  266.          * For the sake of performance, no checks are done on argument.
  267.          * </p>
  268.          * @param increment increment to apply to the entry value
  269.          */
  270.         public void increment(final T[] increment) {
  271.             for (int i = 0; i < increment.length; ++i) {
  272.                 value[i] = value[i].add(increment[i]);
  273.             }
  274.         }

  275.         /** Increment the value.
  276.          * <p>
  277.          * For the sake of performance, no checks are done on argument.
  278.          * </p>
  279.          * @param increment increment to apply to the entry value
  280.          */
  281.         public void increment(final double[] increment) {
  282.             for (int i = 0; i < increment.length; ++i) {
  283.                 value[i] = value[i].add(increment[i]);
  284.             }
  285.         }

  286.         /** Increment the value with another scaled entry.
  287.          * <p>
  288.          * Each component {@code value[i]} will be replaced by {@code value[i] + factor * raw.value[i]}.
  289.          * </p>
  290.          * <p>
  291.          * For the sake of performance, no checks are done on arguments.
  292.          * </p>
  293.          * @param factor multiplicative factor for increment
  294.          * @param raw raw increment to be multiplied by {@code factor} and then added
  295.          * @since 11.1.1
  296.          */
  297.         public void scaledIncrement(final T factor, final Entry raw) {
  298.             for (int i = 0; i < raw.value.length; ++i) {
  299.                 value[i] = value[i].add(raw.value[i].multiply(factor));
  300.             }
  301.         }

  302.         /** Increment the value with another scaled entry.
  303.          * <p>
  304.          * Each component {@code value[i]} will be replaced by {@code value[i] + factor * raw.value[i]}.
  305.          * </p>
  306.          * <p>
  307.          * For the sake of performance, no checks are done on arguments.
  308.          * </p>
  309.          * @param factor multiplicative factor for increment
  310.          * @param raw raw increment to be multiplied by {@code factor} and then added
  311.          * @since 11.1.1
  312.          */
  313.         public void scaledIncrement(final double factor, final Entry raw) {
  314.             for (int i = 0; i < raw.value.length; ++i) {
  315.                 value[i] = value[i].add(raw.value[i].multiply(factor));
  316.             }
  317.         }

  318.         /** Reset the value to zero.
  319.          */
  320.         public void zero() {
  321.             Arrays.fill(value, field.getZero());
  322.         }

  323.     }
  324.     /** Unmodifiable view of the dictionary. */
  325.     private class View extends FieldArrayDictionary<T> {

  326.         /** Simple constructor.
  327.          * @param field field to which the elements belong
  328.          */
  329.         View(final Field<T> field) {
  330.             super(field);
  331.         }

  332.         /**  {@inheritDoc} */
  333.         @Override
  334.         public List<Entry> getData() {
  335.             return FieldArrayDictionary.this.getData();
  336.         }

  337.         /**  {@inheritDoc} */
  338.         @Override
  339.         public int size() {
  340.             return FieldArrayDictionary.this.size();
  341.         }

  342.         /**  {@inheritDoc} */
  343.         @Override
  344.         public Map<String, T[]> toMap() {
  345.             return FieldArrayDictionary.this.toMap();
  346.         }

  347.         /**  {@inheritDoc} */
  348.         @Override
  349.         public void clear() {
  350.             throw new UnsupportedOperationException();
  351.         }

  352.         /**  {@inheritDoc} */
  353.         @Override
  354.         public void put(final String key, final T[] value) {
  355.             throw new UnsupportedOperationException();
  356.         }

  357.         /**  {@inheritDoc} */
  358.         @Override
  359.         public void put(final String key, final double[] value) {
  360.             throw new UnsupportedOperationException();
  361.         }

  362.         /**  {@inheritDoc} */
  363.         @Override
  364.         public void putAll(final Map<String, T[]> map) {
  365.             throw new UnsupportedOperationException();
  366.         }

  367.         /**  {@inheritDoc} */
  368.         @Override
  369.         public void putAll(final FieldArrayDictionary<T> dictionary) {
  370.             throw new UnsupportedOperationException();
  371.         }

  372.         /**  {@inheritDoc} */
  373.         @Override
  374.         public T[] get(final String key) {
  375.             return FieldArrayDictionary.this.get(key);
  376.         }

  377.         /**  {@inheritDoc} */
  378.         @Override
  379.         public Entry getEntry(final String key) {
  380.             return FieldArrayDictionary.this.getEntry(key);
  381.         }

  382.         /**  {@inheritDoc} */
  383.         @Override
  384.         public boolean remove(final String key) {
  385.             throw new UnsupportedOperationException();
  386.         }

  387.     }

  388. }