LLVM API Documentation

IntEqClasses.h
Go to the documentation of this file.
00001 //===-- llvm/ADT/IntEqClasses.h - Equiv. Classes of Integers ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Equivalence classes for small integers. This is a mapping of the integers
00011 // 0 .. N-1 into M equivalence classes numbered 0 .. M-1.
00012 //
00013 // Initially each integer has its own equivalence class. Classes are joined by
00014 // passing a representative member of each class to join().
00015 //
00016 // Once the classes are built, compress() will number them 0 .. M-1 and prevent
00017 // further changes.
00018 //
00019 //===----------------------------------------------------------------------===//
00020 
00021 #ifndef LLVM_ADT_INTEQCLASSES_H
00022 #define LLVM_ADT_INTEQCLASSES_H
00023 
00024 #include "llvm/ADT/SmallVector.h"
00025 
00026 namespace llvm {
00027 
00028 class IntEqClasses {
00029   /// EC - When uncompressed, map each integer to a smaller member of its
00030   /// equivalence class. The class leader is the smallest member and maps to
00031   /// itself.
00032   ///
00033   /// When compressed, EC[i] is the equivalence class of i.
00034   SmallVector<unsigned, 8> EC;
00035 
00036   /// NumClasses - The number of equivalence classes when compressed, or 0 when
00037   /// uncompressed.
00038   unsigned NumClasses;
00039 
00040 public:
00041   /// IntEqClasses - Create an equivalence class mapping for 0 .. N-1.
00042   IntEqClasses(unsigned N = 0) : NumClasses(0) { grow(N); }
00043 
00044   /// grow - Increase capacity to hold 0 .. N-1, putting new integers in unique
00045   /// equivalence classes.
00046   /// This requires an uncompressed map.
00047   void grow(unsigned N);
00048 
00049   /// clear - Clear all classes so that grow() will assign a unique class to
00050   /// every integer.
00051   void clear() {
00052     EC.clear();
00053     NumClasses = 0;
00054   }
00055 
00056   /// join - Join the equivalence classes of a and b. After joining classes,
00057   /// findLeader(a) == findLeader(b).
00058   /// This requires an uncompressed map.
00059   void join(unsigned a, unsigned b);
00060 
00061   /// findLeader - Compute the leader of a's equivalence class. This is the
00062   /// smallest member of the class.
00063   /// This requires an uncompressed map.
00064   unsigned findLeader(unsigned a) const;
00065 
00066   /// compress - Compress equivalence classes by numbering them 0 .. M.
00067   /// This makes the equivalence class map immutable.
00068   void compress();
00069 
00070   /// getNumClasses - Return the number of equivalence classes after compress()
00071   /// was called.
00072   unsigned getNumClasses() const { return NumClasses; }
00073 
00074   /// operator[] - Return a's equivalence class number, 0 .. getNumClasses()-1.
00075   /// This requires a compressed map.
00076   unsigned operator[](unsigned a) const {
00077     assert(NumClasses && "operator[] called before compress()");
00078     return EC[a];
00079   }
00080 
00081   /// uncompress - Change back to the uncompressed representation that allows
00082   /// editing.
00083   void uncompress();
00084 };
00085 
00086 } // End llvm namespace
00087 
00088 #endif