summaryrefslogtreecommitdiffstats
path: root/llvm/include/llvm/ADT/SmallSet.h
blob: 709c2938c5772267e492cb718e3e94e067a758f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
OpenPOWER on IntegriCloud