SetOperations.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/ADT/SetOperations.h - Generic Set Operations -------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This file defines generic set operations that may be used on set's of
  16. /// different types, and different element types.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_ADT_SETOPERATIONS_H
  20. #define LLVM_ADT_SETOPERATIONS_H
  21. namespace llvm {
  22. /// set_union(A, B) - Compute A := A u B, return whether A changed.
  23. ///
  24. template <class S1Ty, class S2Ty>
  25. bool set_union(S1Ty &S1, const S2Ty &S2) {
  26. bool Changed = false;
  27. for (typename S2Ty::const_iterator SI = S2.begin(), SE = S2.end();
  28. SI != SE; ++SI)
  29. if (S1.insert(*SI).second)
  30. Changed = true;
  31. return Changed;
  32. }
  33. /// set_intersect(A, B) - Compute A := A ^ B
  34. /// Identical to set_intersection, except that it works on set<>'s and
  35. /// is nicer to use. Functionally, this iterates through S1, removing
  36. /// elements that are not contained in S2.
  37. ///
  38. template <class S1Ty, class S2Ty>
  39. void set_intersect(S1Ty &S1, const S2Ty &S2) {
  40. for (typename S1Ty::iterator I = S1.begin(); I != S1.end();) {
  41. const auto &E = *I;
  42. ++I;
  43. if (!S2.count(E)) S1.erase(E); // Erase element if not in S2
  44. }
  45. }
  46. /// set_difference(A, B) - Return A - B
  47. ///
  48. template <class S1Ty, class S2Ty>
  49. S1Ty set_difference(const S1Ty &S1, const S2Ty &S2) {
  50. S1Ty Result;
  51. for (typename S1Ty::const_iterator SI = S1.begin(), SE = S1.end();
  52. SI != SE; ++SI)
  53. if (!S2.count(*SI)) // if the element is not in set2
  54. Result.insert(*SI);
  55. return Result;
  56. }
  57. /// set_subtract(A, B) - Compute A := A - B
  58. ///
  59. template <class S1Ty, class S2Ty>
  60. void set_subtract(S1Ty &S1, const S2Ty &S2) {
  61. for (typename S2Ty::const_iterator SI = S2.begin(), SE = S2.end();
  62. SI != SE; ++SI)
  63. S1.erase(*SI);
  64. }
  65. /// set_is_subset(A, B) - Return true iff A in B
  66. ///
  67. template <class S1Ty, class S2Ty>
  68. bool set_is_subset(const S1Ty &S1, const S2Ty &S2) {
  69. if (S1.size() > S2.size())
  70. return false;
  71. for (const auto It : S1)
  72. if (!S2.count(It))
  73. return false;
  74. return true;
  75. }
  76. } // End llvm namespace
  77. #endif
  78. #ifdef __GNUC__
  79. #pragma GCC diagnostic pop
  80. #endif