blob: 1c060e5a79573a0e1974521de9544220a36a1870 [file] [log] [blame]
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001// Licensed to the Apache Software Foundation(ASF) under one
2// or more contributor license agreements.See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with 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,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18using System.Collections;
19using System.Collections.Generic;
20
21namespace Thrift.Collections
22{
23 // ReSharper disable once InconsistentNaming
24 public class THashSet<T> : ICollection<T>
25 {
Jens Geyer5a17b132019-05-26 15:53:37 +020026 private readonly HashSet<T> Items;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010027
Jens Geyer5a17b132019-05-26 15:53:37 +020028 public THashSet()
29 {
30 Items = new HashSet<T>();
31 }
32
33 public THashSet(int capacity)
34 {
Jens Geyer4c7b9fd2021-12-04 22:48:37 +010035 #if NET5_0_OR_GREATER
Jens Geyer526a1ac2021-02-13 13:58:09 +010036 Items = new HashSet<T>(capacity);
37 #elif NETFRAMEWORK || NETSTANDARD
38 Items = new HashSet<T>(/*capacity not supported*/);
39 #else
40 #error Unknown platform
41 #endif
Jens Geyer5a17b132019-05-26 15:53:37 +020042 }
43
44 public int Count => Items.Count;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010045
46 public bool IsReadOnly => false;
47
48 public void Add(T item)
49 {
Jens Geyer5a17b132019-05-26 15:53:37 +020050 Items.Add(item);
Jens Geyeraa0c8b32019-01-28 23:27:45 +010051 }
52
53 public void Clear()
54 {
Jens Geyer5a17b132019-05-26 15:53:37 +020055 Items.Clear();
Jens Geyeraa0c8b32019-01-28 23:27:45 +010056 }
57
58 public bool Contains(T item)
59 {
Jens Geyer5a17b132019-05-26 15:53:37 +020060 return Items.Contains(item);
Jens Geyeraa0c8b32019-01-28 23:27:45 +010061 }
62
63 public void CopyTo(T[] array, int arrayIndex)
64 {
Jens Geyer5a17b132019-05-26 15:53:37 +020065 Items.CopyTo(array, arrayIndex);
Jens Geyeraa0c8b32019-01-28 23:27:45 +010066 }
67
Jens Geyeraffea7b2020-05-22 17:28:30 +020068 IEnumerator IEnumerable.GetEnumerator()
Jens Geyeraa0c8b32019-01-28 23:27:45 +010069 {
Jens Geyer5a17b132019-05-26 15:53:37 +020070 return Items.GetEnumerator();
Jens Geyeraa0c8b32019-01-28 23:27:45 +010071 }
72
Jens Geyeraffea7b2020-05-22 17:28:30 +020073 public IEnumerator<T> GetEnumerator()
Jens Geyeraa0c8b32019-01-28 23:27:45 +010074 {
Jens Geyer5a17b132019-05-26 15:53:37 +020075 return ((IEnumerable<T>) Items).GetEnumerator();
Jens Geyeraa0c8b32019-01-28 23:27:45 +010076 }
77
78 public bool Remove(T item)
79 {
Jens Geyer5a17b132019-05-26 15:53:37 +020080 return Items.Remove(item);
Jens Geyeraa0c8b32019-01-28 23:27:45 +010081 }
82 }
Jens Geyer5a17b132019-05-26 15:53:37 +020083}