1e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov//===--- StringSet.h - The LLVM Compiler Driver -----------------*- C++ -*-===// 2e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov// 3e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov// The LLVM Compiler Infrastructure 4e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov// 5e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov// This file is distributed under the University of Illinois Open 6e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov// Source License. See LICENSE.TXT for details. 7e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov// 8e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov//===----------------------------------------------------------------------===// 9e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov// 10e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov// StringSet - A set-like wrapper for the StringMap. 11e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov// 12e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov//===----------------------------------------------------------------------===// 13e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov 14e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov#ifndef LLVM_ADT_STRINGSET_H 15e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov#define LLVM_ADT_STRINGSET_H 16e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov 17e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov#include "llvm/ADT/StringMap.h" 18e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov 19e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikovnamespace llvm { 20e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov 21dfe91cefd25614bc9ac1626d67df4d5ad5d3553fDaniel Dunbar /// StringSet - A wrapper for StringMap that provides set-like functionality. 22e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov template <class AllocatorTy = llvm::MallocAllocator> 23e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov class StringSet : public llvm::StringMap<char, AllocatorTy> { 24e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov typedef llvm::StringMap<char, AllocatorTy> base; 25e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov public: 26dfe91cefd25614bc9ac1626d67df4d5ad5d3553fDaniel Dunbar 27dfe91cefd25614bc9ac1626d67df4d5ad5d3553fDaniel Dunbar /// insert - Insert the specified key into the set. If the key already 28dfe91cefd25614bc9ac1626d67df4d5ad5d3553fDaniel Dunbar /// exists in the set, return false and ignore the request, otherwise insert 29dfe91cefd25614bc9ac1626d67df4d5ad5d3553fDaniel Dunbar /// it and return true. 30dfe91cefd25614bc9ac1626d67df4d5ad5d3553fDaniel Dunbar bool insert(StringRef Key) { 31dfe91cefd25614bc9ac1626d67df4d5ad5d3553fDaniel Dunbar // Get or create the map entry for the key; if it doesn't exist the value 32dfe91cefd25614bc9ac1626d67df4d5ad5d3553fDaniel Dunbar // type will be default constructed which we use to detect insert. 33dfe91cefd25614bc9ac1626d67df4d5ad5d3553fDaniel Dunbar // 34dfe91cefd25614bc9ac1626d67df4d5ad5d3553fDaniel Dunbar // We use '+' as the sentinel value in the map. 35dfe91cefd25614bc9ac1626d67df4d5ad5d3553fDaniel Dunbar assert(!Key.empty()); 36dfe91cefd25614bc9ac1626d67df4d5ad5d3553fDaniel Dunbar StringMapEntry<char> &Entry = this->GetOrCreateValue(Key); 37dfe91cefd25614bc9ac1626d67df4d5ad5d3553fDaniel Dunbar if (Entry.getValue() == '+') 38ce892ca9bc7f9924a69ce6c844e1dff5aa4049e6Michael J. Spencer return false; 39dfe91cefd25614bc9ac1626d67df4d5ad5d3553fDaniel Dunbar Entry.setValue('+'); 40ce892ca9bc7f9924a69ce6c844e1dff5aa4049e6Michael J. Spencer return true; 41e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov } 42e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov }; 43e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov} 44e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov 45e269f434b1f4e81974b2c68abf84d4b64419edd4Anton Korobeynikov#endif // LLVM_ADT_STRINGSET_H 46