SetOperations.h 2.6 KB

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