<commit_before>﻿#region License
//
// LinkedHashMap.cs May 2010
//
// Copyright (C) 2010, <NAME> <<EMAIL>>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
#endregion

#region Using directives
using System;
using System.Collections.Generic;
#endregion

namespace SimpleFramework.Xml {

   /// <summary>
   /// The <c>LinkedHashMap</c> object represents an ordered map that can
   /// be used as conventional map or a least recently used cache. Using
   /// this implementation offers advantages such as the ability to order
   /// the keys by insertion. Order is reflected in the <c>Keys</c> method
   /// and can be used within a <c>foreach</c> loop to traverse the key
   /// value pairs in a predictable manner. 
   /// <p>
   /// This offers a substitute for the <c>LinkedHashMap</c> available in
   /// the Java collections framework. It is used to aid the porting of
   /// Java projects to .NET where the linked hash map is heavily used.
   /// Operations on this map are constant time regardless of size.
   /// </summary>
   public class LinkedHashMap<K, V> : Map<K, V> {

      /// <summary>
      /// This is the map that maintains the mappings for this instance.
      /// </summary>
      private readonly Map<K, Entry> map;

      /// <summary>
      /// This is the linked list that is used to bind the entries.
      /// </summary>
      private readonly EntryList list;

      /// <summary>
      /// This determines if this is a least recently used cache map.
      /// </summary>
      private readonly bool cache;

      /// <summary>
      /// Constructor for the <c>LinkedHashMap</c> object. This is used 
      /// to create a map that will provide the keys and values in the 
      /// same order as insertion. Providing ordered keys in this manner
      /// ensures the entries can be traversed in a predictable way.
      /// </summary>
      public LinkedHashMap() : this(false) {
      }

      /// <summary>
      /// Constructor for the <c>LinkedHashMap</c> object. Here a bool 
      /// can be specified to determine whether the map should be used as
      /// a least recently used cache. When the bool is set to true then
      /// the <c>RemoveEldest</c> method is called each time a new entry
      /// is added to the map. This alows various cache strategies.
      /// </summary>
      /// <param name="cache">
      /// If this is true then this map is to be used as a cache map
      /// </param>
      public LinkedHashMap(bool cache) {
         this.map = new HashMap<K, Entry>();
         this.list = new EntryList(map);
         this.cache = cache;
      }

      public override int Size {
         get {
            return map.Size;
         }
      }

      public override K[] Keys {
         get {
            return list.Keys();
         }
      }

      public override V[] Values {
         get {
            return list.Values();
         }
      }

      public override V Get(K key) {
         Entry entry = map.Get(key);

         if(entry == null) {
            return default(V);
         }
         if(cache == true) {
            entry.First();
         }
         return entry.Value;
      }

      public override V Put(K key, V value) {
         Entry entry = list.Insert(key, value);
         Entry item = map.Put(key, entry);
         Entry tail = list.Tail;

         if(item == null) {
            bool trim = RemoveEldest(tail);

            if(trim == true) {
               Remove(tail.Key);
            }
            return default(V);
         }
         return item.Delete();
      }

      public override V Remove(K key) {
         Entry entry = map.Remove(key);

         if(entry != null) {
            return entry.Delete();
         }
         return default(V);
      }

      public override bool Contains(K key) {
         return map.Contains(key);
      }
      public override void Clear() {
         list.Clear();
         map.Clear();
      }

      /// <summary>
      /// This is used to determine if the eldest entry is to be removed<commit_msg>Complete the hash map documentation<commit_after>﻿#region License
//
// LinkedHashMap.cs May 2010
//
// Copyright (C) 2010, <NAME> <<EMAIL>>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
#endregion

#region Using directives
using System;
using System.Collections.Generic;
#endregion

namespace SimpleFramework.Xml {

   /// <summary>
   /// The <c>LinkedHashMap</c> object represents an ordered map that can
   /// be used as conventional map or a least recently used cache. Using
   /// this implementation offers advantages such as the ability to order
   /// the keys by insertion. Order is reflected in the <c>Keys</c> method
   /// and can be used within a <c>foreach</c> loop to traverse the key
   /// value pairs in a predictable manner. 
   /// <p>
   /// This offers a substitute for the <c>LinkedHashMap</c> available in
   /// the Java collections framework. It is used to aid the porting of
   /// Java projects to .NET where the linked hash map is heavily used.
   /// Operations on this map are constant time regardless of size.
   /// </summary>
   public class LinkedHashMap<K, V> : Map<K, V> {

      /// <summary>
      /// This is the map that maintains the mappings for this instance.
      /// </summary>
      private readonly Map<K, Entry> map;

      /// <summary>
      /// This is the linked list that is used to bind the entries.
      /// </summary>
      private readonly EntryList list;

      /// <summary>
      /// This determines if this is a least recently used cache map.
      /// </summary>
      private readonly bool cache;

      /// <summary>
      /// Constructor for the <c>LinkedHashMap</c> object. This is used 
      /// to create a map that will provide the keys and values in the 
      /// same order as insertion. Providing ordered keys in this manner
      /// ensures the entries can be traversed in a predictable way.
      /// </summary>
      public LinkedHashMap() : this(false) {
      }

      /// <summary>
      /// Constructor for the <c>LinkedHashMap</c> object. Here a bool 
      /// can be specified to determine whether the map should be used as
      /// a least recently used cache. When the bool is set to true then
      /// the <c>RemoveEldest</c> method is called each time a new entry
      /// is added to the map. This alows various cache strategies.
      /// </summary>
      /// <param name="cache">
      /// If this is true then this map is to be used as a cache map
      /// </param>
      public LinkedHashMap(bool cache) {
         this.map = new HashMap<K, Entry>();
         this.list = new EntryList(map);
         this.cache = cache;
      }

      /// <summary>
      /// This is used to determine the number of mappings that have been
      /// inserted in to the map. If size is zero <c>Empty</c> will be
      /// true. This can be used to iterate over the key and value arrays.
      /// </summary>
      /// <returns>
      /// The name number of mappings that have been added to the map.
      /// </returns>
      public override int Size {
         get {
            return map.Size;
         }
      }

      /// <summary>
      /// This is used to acquire the keys for each mapping within the
      /// list. Acquiring the key mappings in this way ensures that the
      /// array of values will be in the order they have been added to
      /// the linked list.
      /// </summary>
      /// <returns>
      /// Provides an ordered array of keys from the mappings created.
      /// </returns>
      public override K[] Keys {
         get {
            return list.Keys();
         }
      }

      /// <summary>
      /// This is used to acquire the values for each mapping within the
      /// list. Acquiring the value mappings in this way ensures that the
      /// array of values will be in the order they have been added to
      /// the linked list.
      /// </summary>
      /// <returns>
      /// Provides an ordered array of values from the mappings created.
      /// </returns>
      public override V[] Values {
         get {
            return list.Values();
         }
      }

      /// <summary>
      /// This is used to acquire the value associated with the specified
      /// key. If there is no mapping within the <c>Map</c> this returns 
      /// null. Given that mappings can be made to a null value this can
      /// not be used to determine if a mapping exists within the instance.
      /// </summary>
      /// <param name="key">
      /// This is the key that is used to acquire the mapped value.
      /// </param>
      /// <returns>
      /// This is used to acquire the value associated with the key.
      /// </returns>
      public override V Get(K key) {
         Entry entry = map.Get(key);

         if(entry == null) {
            return default(V);
         }
         if(cache == true) {
            entry.First();
         }
         return entry.Value;
      }

      /// <summary>
      /// This is used to establish a mapping within the map. If a mapping
      /// already exists with the specified key then this will return the
      /// value that was previously associated with the key. Once mapped
      /// the <c>Get</c> method or indexer can be used to get the value.
      /// </summary>
      /// <param name="key">
      /// This is the key that is used to establish the mapping.
      /// </param>
      /// <param name="value">
      /// This is the value that will be associated with the given key.
      /// </param>
      /// <returns>
      /// Returns the value previously associated with the key, or null.
      /// </returns>
      public override V Put(K key, V value) {
         Entry entry = list.Insert(key, value);
         Entry item = map.Put(key, entry);
         Entry tail = list.Tail;

         if(item == null) {
            bool trim = RemoveEldest(tail);

            if(trim == true) {
               Remove(tail.Key);
            }
            return default(V);
         }
         return item.Delete();
      }

      /// <summary>
      /// This is used to remove a mapping from the <c>Map</c>. This acts
      /// much like a take method would for a cache, in that it will 
      /// the value of the mapping that has just been removed. If the
      /// mapping did not exist then this method will return null.
      /// </summary>
      /// <param name="key">
      /// This is the key to be searched for within this map instance.
      /// </param>
      /// <returns>
      /// This returns the value associated with the mapping if it exists.
      /// </returns>
      public override V Remove(K key) {
         Entry entry = map.Remove(key);

         if(entry != null) {
            return entry.Delete();
         }
         return default(V);
      }

      /// <summary>
      /// This is used to determine if a mapping exists within the map. If
      /// this returns true then the <c>Keys</c> property will return the
      /// specified key within the array of keys. However, if the key is 
      /// mapped to null then the <c>Get</c> method will return null.
      /// </summary>
      /// <param name="key">
      /// This is the key to be searched for within this map instance.
      /// </param>
      /// <returns>
      /// This will return true if the mapping exists within the instance.
      /// </returns>
      public override bool Contains(K key) {
         return map.Contains(key);
      }

      /// <summary>
      /// This is used to clear the <c>Map</c> instance. Clearing the map
      /// instance ensures that there are no mappings maintained. Further
      /// attempts to get via the <c>Get</c> method will return null.
      /// </summary>
      public override void Clear() {
         list.Clear();
         map.Clear();
      }

      /// <summary>
      /// This is used to determine if the eldest entry is to be removed
{'fraction_non_alphanumeric': 0.10854753793744193, 'fraction_numerical': 0.0018581604211830288, 'mean_word_length': 1.8190746398952422, 'pattern_counts': {'":': 0, '<': 133, '<?xml version=': 0, '>': 133, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6316577', 'n_tokens_mistral': 3369, 'n_tokens_neox': 3291, 'n_words': 1520}