~paul-lucas/zorba/bug-932374

« back to all changes in this revision

Viewing changes to src/util/unordered_set.h

  • Committer: Paul J. Lucas
  • Date: 2012-09-21 20:26:47 UTC
  • mfrom: (10819.2.235 zorba)
  • Revision ID: paul@lucasmail.org-20120921202647-fy9n4jduhrnljrnb
MergeĀ fromĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2006-2008 The FLWOR Foundation.
 
3
 * 
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 * 
 
8
 * http://www.apache.org/licenses/LICENSE-2.0
 
9
 * 
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
#pragma once
 
18
#ifndef ZORBA_UNORDERED_SET_H
 
19
#define ZORBA_UNORDERED_SET_H
 
20
 
 
21
#include <zorba/config.h>
 
22
 
 
23
#ifdef ZORBA_HAVE_UNORDERED_SET
 
24
# include <unordered_set>               /* use the implementation version */
 
25
#else
 
26
 
 
27
// local
 
28
#include "util/hash/hash.h"
 
29
#include "util/hash/hashtable.h"
 
30
#include "util/hash/rehash_policy.h"
 
31
#ifndef ZORBA_UNORDERED_SET_REHASH_POLICY
 
32
# define ZORBA_UNORDERED_SET_REHASH_POLICY zorba::ztd::prime_rehash_policy
 
33
#endif /* ZORBA_UNORDERED_SET_REHASH_POLICY */
 
34
#include "cxx_util.h"
 
35
#include "stl_util.h"
 
36
 
 
37
namespace std {
 
38
 
 
39
///////////////////////////////////////////////////////////////////////////////
 
40
 
 
41
/**
 
42
 * This is an implementation of the C++ %unordered_set class, but for C++98.
 
43
 * As such, it lacks member functions that use r-value references.
 
44
 *
 
45
 * @tparam KeyType They set's key type.
 
46
 * @tparam KeyHash The unary_function to use for generating hash codes.
 
47
 * @tparam KeyEqual The binary_function to use to test for value equality.
 
48
 * @tparam Allocator The allocator to use.
 
49
 */
 
50
template<
 
51
  typename KeyType,
 
52
  class KeyHash = std::hash<KeyType>,
 
53
  class KeyEqual = std::equal_to<KeyType>,
 
54
  class Allocator = std::allocator<KeyType>
 
55
>
 
56
class unordered_set :
 
57
  public zorba::ztd::hashtable<
 
58
    KeyType, KeyType, zorba::ztd::identity<KeyType>,
 
59
    KeyHash, KeyEqual, Allocator, ZORBA_UNORDERED_SET_REHASH_POLICY
 
60
  >
 
61
{
 
62
  typedef zorba::ztd::hashtable<
 
63
    KeyType, KeyType, zorba::ztd::identity<KeyType>,
 
64
    KeyHash, KeyEqual, Allocator, ZORBA_UNORDERED_SET_REHASH_POLICY
 
65
  > base_type;
 
66
 
 
67
  typedef typename base_type::rehash_policy_type rehash_policy_type;
 
68
public:
 
69
  typedef typename base_type::key_type key_type;
 
70
  typedef typename base_type::value_type value_type;
 
71
  typedef typename base_type::hasher hasher;
 
72
  typedef typename base_type::key_equal key_equal;
 
73
  typedef typename base_type::allocator_type allocator_type;
 
74
 
 
75
  typedef typename base_type::pointer pointer;
 
76
  typedef typename base_type::const_pointer const_pointer;
 
77
  typedef typename base_type::reference reference;
 
78
  typedef typename base_type::const_reference const_reference;
 
79
 
 
80
  typedef typename base_type::size_type size_type;
 
81
  typedef typename base_type::difference_type difference_type;
 
82
 
 
83
  typedef typename base_type::local_iterator local_iterator;
 
84
  typedef typename base_type::const_local_iterator const_local_iterator;
 
85
  typedef typename base_type::iterator iterator;
 
86
  typedef typename base_type::const_iterator const_iterator;
 
87
 
 
88
  /**
 
89
   * Constructs an %unordered_set.
 
90
   *
 
91
   * @param bucket_count The initial number of buckets.
 
92
   * @param hash The unary_function to use for generating hash codes.
 
93
   * @param equal The binary_function to use to test for key equality.
 
94
   * @param alloc The allocator to use.
 
95
   */
 
96
  explicit unordered_set(
 
97
    size_type bucket_count = rehash_policy_type::default_bucket_count,
 
98
    hasher const &hash = hasher(),
 
99
    key_equal const &equal = key_equal(),
 
100
    allocator_type const &alloc = allocator_type()
 
101
  ) :
 
102
    base_type( bucket_count, hash, equal, alloc )
 
103
  {
 
104
  }
 
105
 
 
106
  /**
 
107
   * Constructs an %unordered_set.
 
108
   *
 
109
   * @param alloc The allocator to use.
 
110
   */
 
111
  explicit unordered_set( allocator_type const &alloc ) : base_type( alloc ) {
 
112
  }
 
113
 
 
114
  /**
 
115
   * Copy-constructs an %unordered_set.
 
116
   *
 
117
   * @param that The %unordered_set to copy from.
 
118
   */
 
119
  unordered_set( unordered_set const &that ) : base_type( that ) {
 
120
  }
 
121
 
 
122
  /**
 
123
   * Assignment.
 
124
   *
 
125
   * @param that The %unordered_set to assign from.
 
126
   */
 
127
  unordered_set& operator=( unordered_set const &that ) {
 
128
    base_type::operator=( that );
 
129
    return *this;
 
130
  }
 
131
};
 
132
 
 
133
///////////////////////////////////////////////////////////////////////////////
 
134
 
 
135
/**
 
136
 * Global %swap operator for unordered_set.
 
137
 *
 
138
 * @param a The first unordered_set.
 
139
 * @param b The second unordered_set.
 
140
 */
 
141
template<typename V,class H,class E,class A> inline
 
142
void swap( unordered_set<V,H,E,A> &a, unordered_set<V,H,E,A> &b ) {
 
143
  a.swap( b );
 
144
}
 
145
 
 
146
///////////////////////////////////////////////////////////////////////////////
 
147
 
 
148
} // namespace std
 
149
 
 
150
#endif /* ZORBA_HAVE_UNORDERED_SET */
 
151
#endif /* ZORBA_UNORDERED_SET_H */
 
152
/* vim:set et ts=2 sw=2: */