1 // Copyright (C) 2011 The Libphonenumber Authors
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 // Author: Fredrik Roubert
17 #include "phonenumbers/regexp_cache.h"
23 #include "phonenumbers/base/synchronization/lock.h"
24 #include "phonenumbers/regexp_adapter.h"
29 namespace phonenumbers {
31 RegExpCache::RegExpCache(const AbstractRegExpFactory& regexp_factory,
33 : regexp_factory_(regexp_factory),
34 #ifdef I18N_PHONENUMBERS_USE_TR1_UNORDERED_MAP
35 cache_impl_(new CacheImpl(min_items))
37 cache_impl_(new CacheImpl())
41 RegExpCache::~RegExpCache() {
43 for (CacheImpl::const_iterator
44 it = cache_impl_->begin(); it != cache_impl_->end(); ++it) {
49 const RegExp& RegExpCache::GetRegExp(const string& pattern) {
51 CacheImpl::const_iterator it = cache_impl_->find(pattern);
52 if (it != cache_impl_->end()) return *it->second;
54 const RegExp* regexp = regexp_factory_.CreateRegExp(pattern);
55 cache_impl_->insert(std::make_pair(pattern, regexp));
59 } // namespace phonenumbers