diff options
| author | Chris Lattner <sabre@nondot.org> | 2007-01-23 00:59:15 +0000 |
|---|---|---|
| committer | Chris Lattner <sabre@nondot.org> | 2007-01-23 00:59:15 +0000 |
| commit | d51b3ca3ad82c049581d47ce35289166836a63eb (patch) | |
| tree | c799534e6572f9160606aaf02b69832afa95ff94 | |
| parent | 16e58be1bc94ebb0f473aaf08c95e0c51dc4928a (diff) | |
| download | bcm5719-llvm-d51b3ca3ad82c049581d47ce35289166836a63eb.tar.gz bcm5719-llvm-d51b3ca3ad82c049581d47ce35289166836a63eb.zip | |
add a trivial SmallSet class, which operates on a similar principle to
SmallVector.
llvm-svn: 33456
| -rw-r--r-- | llvm/include/llvm/ADT/SmallSet.h | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/llvm/include/llvm/ADT/SmallSet.h b/llvm/include/llvm/ADT/SmallSet.h new file mode 100644 index 00000000000..709c2938c57 --- /dev/null +++ b/llvm/include/llvm/ADT/SmallSet.h @@ -0,0 +1,76 @@ +//===- llvm/ADT/SmallSet.h - 'Normally small' sets --------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by Chris Lattner and is distributed under +// the University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines the SmallSet class. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_ADT_SMALLSET_H +#define LLVM_ADT_SMALLSET_H + +#include "llvm/ADT/SmallVector.h" + +namespace llvm { + +/// SmallSet - This maintains a set of unique values, optimizing for the case +/// when the set is small (less than N). In this case, the set can be +/// maintained with no mallocs. +/// +/// Note that this set does not guarantee that the elements in the set will be +/// ordered. +template <typename T, unsigned N> +class SmallSet { + SmallVector<T, N> Vector; + typedef typename SmallVector<T, N>::iterator mutable_iterator; +public: + SmallSet() {} + + // Support iteration. + typedef typename SmallVector<T, N>::const_iterator iterator; + typedef typename SmallVector<T, N>::const_iterator const_iterator; + + iterator begin() const { return Vector.begin(); } + iterator end() const { return Vector.end(); } + + bool empty() const { return Vector.empty(); } + unsigned size() const { return Vector.size(); } + + /// count - Return true if the element is in the set. + unsigned count(const T &V) const { + // Since the collection is small, just do a linear search. + for (iterator I = begin(), E = end(); I != E; ++I) + if (*I == V) + return 1; + return 0; + } + + /// insert - Insert an element into the set if it isn't already there. + void insert(const T &V) { + if (count(V)) return; // Don't reinsert if it already exists. + Vector.push_back(V); + } + + void erase(const T &V) { + for (mutable_iterator I = Vector.begin(), E = Vector.end(); I != E; ++I) + if (*I == V) { + Vector.erase(I); + return; + } + } + + void clear() { + Vector.clear(); + } + +}; + + +} // end namespace llvm + +#endif |

