// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

package com.intellij.psi.impl.cache.impl.id;

import com.intellij.util.text.CharArrayCharSequence;
import gnu.trove.THashMap;
import org.jetbrains.annotations.NotNull;

import java.util.Map;

public class IdDataConsumer {
  @NotNull
  private final Map<IdIndexEntry, Integer> myResult = new THashMap<>();

  @NotNull
  public Map<IdIndexEntry, Integer> getResult() {
    return myResult;
  }
  
  public void addOccurrence(CharSequence charSequence, int start, int end, int occurrenceMask) {
    if (end == start) return;
    final IdIndexEntry entry = new IdIndexEntry(charSequence, start, end, true);
    addOccurrence(entry, occurrenceMask);

    int hashNoCase = IdIndexEntry.getWordHash(charSequence, start, end, false);
    if (hashNoCase != entry.getWordHashCode()) {
      addOccurrence(new IdIndexEntry(hashNoCase), occurrenceMask);
    }
  }

  public void addOccurrence(char[] chars, int start, int end, int occurrenceMask) {
    addOccurrence(new CharArrayCharSequence(chars), start, end, occurrenceMask);
  }

  private void addOccurrence(@NotNull IdIndexEntry entry, int occurrenceMask) {
    if (occurrenceMask != 0) {
      final int old = myResult.getOrDefault(entry, 0);
      int v = old | occurrenceMask;
      if (v != old) {
        myResult.put(entry, v);
      }
    }
  }
}

@@ -245,19 +245,52 @@
 ort 
-gnu.trove.T
+it.unimi.dsi.fastutil.objects.Object2IntOpen
 Hash
@@ -417,16 +417,34 @@
 e final 
+Object2IntOpenHash
 Map%3CIdIn
@@ -451,25 +451,16 @@
 dexEntry
-, Integer
 %3E myResu
@@ -472,9 +472,22 @@
 new 
-T
+Object2IntOpen
 Hash
@@ -582,18 +582,16 @@
 lt;%0A  %7D%0A
-  
 %0A  publi
@@ -615,33 +615,20 @@
 nce(
-CharSequence charSequence
+char%5B%5D chars
 , in
@@ -675,82 +675,131 @@
     
-if (end == start) return;%0A    final IdIndexEntry entry = new IdIndexEntry(
+addOccurrence(new CharArrayCharSequence(chars), start, end, occurrenceMask);%0A  %7D%0A%0A  public void addOccurrence(CharSequence 
 char
@@ -811,52 +811,74 @@
 nce,
+ int
  start,
+ int
  end, 
-true);%0A    addOccurrence(entry,
+int occurrenceMask) %7B%0A    if (end == start %7C%7C
  occ
@@ -888,19 +888,30 @@
 enceMask
-);%0A
+ == 0) return;
 %0A    int
@@ -915,22 +915,16 @@
 int hash
-NoCase
  = IdInd
@@ -973,20 +973,19 @@
 nd, 
-fals
+tru
 e);%0A    
 if (
@@ -984,72 +984,25 @@
     
-if (hashNoCase != entry.getWordHashCode()) %7B%0A      addOccurrence
+myResult.mergeInt
 (new
@@ -1023,14 +1023,8 @@
 hash
-NoCase
 ), o
@@ -1040,138 +1040,86 @@
 Mask
-);%0A    %7D%0A  %7D%0A%0A  public void addOccurrence(char%5B%5D chars, int start, int end, int occurrenceMask) %7B%0A    addOccurrence(new CharArrayC
+, (prev, cur) -%3E prev %7C cur);%0A%0A    int hashNoCase = IdIndexEntry.getWordHash(c
 harS
@@ -1117,39 +1117,32 @@
 ash(charSequence
-(chars)
 , start, end, oc
@@ -1143,137 +1143,42 @@
 nd, 
-occurrenceMask);%0A  %7D%0A%0A  private void addOccurrence(@NotNull IdIndexEntry entry, int occurrenceMask) %7B%0A    if (occurrenceMask
+false);%0A    if (hashNoCase
  != 
-0
+hash
 ) %7B%0A
@@ -1187,154 +1187,99 @@
     
-final int old = myResult.getOrDefault(entry, 0);%0A      int v = old %7C occurrenceMask;%0A      if (v != old) %7B%0A        myResult.put(entry, v);%0A      %7D
+myResult.mergeInt(new IdIndexEntry(hashNoCase), occurrenceMask, (prev, cur) -%3E prev %7C cur);
 %0A   

use fastutil in-place merge