summaryrefslogtreecommitdiff
path: root/src/circular-array.h
blob: 8501bc4fad2a8d2f0bc203720b46f4579fb73380 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * Copyright 2017 WebAssembly Community Group participants
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef WABT_CIRCULAR_ARRAY_H_
#define WABT_CIRCULAR_ARRAY_H_

namespace wabt {

// TODO(karlschimpf) Complete the API
// TODO(karlschimpf) Apply constructors/destructors on base type T
//                   as collection size changes (if not POD).
// Note: Capacity must be a power of 2.
template<class T, size_t kCapacity>
class CircularArray {
 public:
  typedef T value_type;
  typedef value_type& reference;
  typedef const value_type& const_reference;
  typedef size_t size_type;
  typedef ptrdiff_t difference_type;
  CircularArray() : size_(0), front_(0), mask_(kCapacity - 1) {
    assert(kCapacity && ((kCapacity & (kCapacity - 1)) == 0));
  }
  ~CircularArray() {}

  reference at(size_type index) {
    assert(index < size_);
    return (*this)[index];
  }

  const_reference at(size_type index) const {
    assert(index < size_);
    return (*this)[index];
  }

  reference operator[](size_type index) {
    return contents_[position(index)];
  }

  const_reference operator[](size_type index) const {
    return contents_[position(index)];
  }

  reference back() {
    return at(size_ - 1);
  }

  const_reference back() const {
    return at(size_  - 1);
  }

  bool empty() const { return size_ == 0; }

  reference front() {
    return at(0);
  }

  const_reference front() const {
    return at(0);
  }

  size_type max_size() const { return kCapacity; }

  void pop_back() {
    assert(size_ > 0);
    --size_;
  }

  void pop_front() {
    assert(size_ > 0);
    front_ = (front_ + 1) & mask_;
    --size_;
  }

  void push_back(value_type& value) {
    assert(size_ < kCapacity);
    contents_[position(size_++)] = value;
  }

  size_type size() const { return size_; }

  void clear() {
    size_ = 0;
  }

 private:
  T contents_[kCapacity];
  size_type size_;
  size_type front_;
  size_type mask_;

  size_t position(size_t index) const { return (front_ + index) & mask_; }
};

}

#endif // WABT_CIRCULAR_ARRAY_H_