Sync AMTL.
authorDavid Anderson <dvander@alliedmods.net>
Sat Aug 31 19:52:41 2013 -0700 (2013-08-31)
changeset 17563aea5eba06
parent 16 c513ef40773e
child 18 9b30f80f6e49
Sync AMTL.
include/am-inlinelist.h
include/am-refcounting.h
include/am-string.h
include/am-vector.h
tests/test-inlinelist.cpp
tests/test-vector.cpp
     1.1 --- a/include/am-inlinelist.h	Thu Aug 29 23:51:30 2013 -0700
     1.2 +++ b/include/am-inlinelist.h	Sat Aug 31 19:52:41 2013 -0700
     1.3 @@ -140,6 +140,22 @@
     1.4      return iterator(&head_);
     1.5    }
     1.6  
     1.7 +  iterator erase(iterator &at) {
     1.8 +    iterator next = at;
     1.9 +    next++;
    1.10 +
    1.11 +    remove(at.iter_);
    1.12 +
    1.13 +    // Iterator is no longer valid.
    1.14 +    at.iter_ = NULL;
    1.15 +
    1.16 +    return next;
    1.17 +  }
    1.18 +
    1.19 +  bool empty() const {
    1.20 +    return head_.next_ == &head_;
    1.21 +  }
    1.22 +
    1.23    void remove(Node *t) {
    1.24      t->prev_->next_ = t->next_;
    1.25      t->next_->prev_ = t->prev_;
     2.1 --- a/include/am-refcounting.h	Thu Aug 29 23:51:30 2013 -0700
     2.2 +++ b/include/am-refcounting.h	Sat Aug 31 19:52:41 2013 -0700
     2.3 @@ -58,6 +58,13 @@
     2.4      mutable T *thing_;
     2.5  };
     2.6  
     2.7 +template <typename T>
     2.8 +static inline Newborn<T>
     2.9 +NoAddRef(T *t)
    2.10 +{
    2.11 +    return Newborn<T>(t);
    2.12 +}
    2.13 +
    2.14  // When returning a value, we'd rather not be needlessly changing the refcount,
    2.15  // so we have a special type to use for returns.
    2.16  template <typename T>
    2.17 @@ -191,9 +198,9 @@
    2.18          AddRef();
    2.19      }
    2.20      Ref(Moveable<Ref> other)
    2.21 -      : thing_(other.thing_)
    2.22 +      : thing_(other->thing_)
    2.23      {
    2.24 -        other.thing_ = NULL;
    2.25 +        other->thing_ = NULL;
    2.26      }
    2.27      template <typename S>
    2.28      Ref(const Ref<S> &other)
    2.29 @@ -261,6 +268,13 @@
    2.30          return *this;
    2.31      }
    2.32  
    2.33 +    Ref &operator =(Moveable<Ref> other) {
    2.34 +        Release();
    2.35 +        thing_ = other->thing_;
    2.36 +        other->thing_ = NULL;
    2.37 +        return *this;
    2.38 +    }
    2.39 +
    2.40    private:
    2.41      void AddRef() {
    2.42          if (thing_)
     3.1 --- a/include/am-string.h	Thu Aug 29 23:51:30 2013 -0700
     3.2 +++ b/include/am-string.h	Sat Aug 31 19:52:41 2013 -0700
     3.3 @@ -45,6 +45,7 @@
     3.4     : length_(0)
     3.5    {
     3.6    }
     3.7 +
     3.8    explicit AString(const char *str) {
     3.9      set(str, strlen(str));
    3.10    }
    3.11 @@ -80,6 +81,12 @@
    3.12      }
    3.13      return *this;
    3.14    }
    3.15 +  AString &operator =(Moveable<AString> other) {
    3.16 +    chars_ = other->chars_.take();
    3.17 +    length_ = other->length_;
    3.18 +    other->length_ = 0;
    3.19 +    return *this;
    3.20 +  }
    3.21  
    3.22    int compare(const char *str) const {
    3.23      return strcmp(chars(), str);
    3.24 @@ -97,17 +104,29 @@
    3.25      return chars()[index];
    3.26    }
    3.27  
    3.28 +  void setVoid() {
    3.29 +    chars_ = NULL;
    3.30 +    length_ = kInvalidLength;
    3.31 +  }
    3.32 +
    3.33 +  bool isVoid() const {
    3.34 +    return length_ == kInvalidLength;
    3.35 +  }
    3.36 +
    3.37    size_t length() const {
    3.38 +    assert(!isVoid());
    3.39      return length_;
    3.40    }
    3.41  
    3.42    const char *chars() const {
    3.43      if (!chars_)
    3.44 -      return "";
    3.45 +      return isVoid() ? NULL : "";
    3.46      return chars_;
    3.47    }
    3.48  
    3.49   private:
    3.50 +  static const size_t kInvalidLength = (size_t)-1;
    3.51 +
    3.52    void set(const char *str, size_t length) {
    3.53      chars_ = new char[length + 1];
    3.54      length_ = length;
     4.1 --- a/include/am-vector.h	Thu Aug 29 23:51:30 2013 -0700
     4.2 +++ b/include/am-vector.h	Sat Aug 31 19:52:41 2013 -0700
     4.3 @@ -85,6 +85,29 @@
     4.4      nitems_++;
     4.5    }
     4.6  
     4.7 +  // Shift all elements including |at| up by one, and insert |item| at the
     4.8 +  // given position. This is a linear-time operation.
     4.9 +  bool insert(size_t at, const T &item) {
    4.10 +    if (!moveUp(at))
    4.11 +      return false;
    4.12 +    new (&data_[at]) T(item);
    4.13 +    return true;
    4.14 +  }
    4.15 +  bool insert(size_t at, Moveable<T> item) {
    4.16 +    if (!moveUp(at))
    4.17 +      return false;
    4.18 +    new (&data_[at]) T(item);
    4.19 +    return true;
    4.20 +  }
    4.21 +
    4.22 +  // Shift all elements at the given position down, removing the given
    4.23 +  // element. This is a linear-time operation.
    4.24 +  void remove(size_t at) {
    4.25 +    for (size_t i = at; i < length() - 1; i++)
    4.26 +      data_[i] = T(Moveable<T>(data_[i + 1]));
    4.27 +    pop();
    4.28 +  }
    4.29 +
    4.30    T popCopy() {
    4.31      T t = at(length() - 1);
    4.32      pop();
    4.33 @@ -154,6 +177,15 @@
    4.34      maxsize_ = 0;
    4.35    }
    4.36  
    4.37 +  bool moveUp(size_t at) {
    4.38 +    if (!growIfNeeded(1))
    4.39 +      return false;
    4.40 +    nitems_++;
    4.41 +    for (size_t i = nitems_ - 1; i > at; i--)
    4.42 +      data_[i] = T(Moveable<T>(data_[i - 1]));
    4.43 +    return true;
    4.44 +  }
    4.45 +
    4.46    bool growIfNeeded(size_t needed)
    4.47    {
    4.48      if (!IsUintPtrAddSafe(nitems_, needed)) {
     5.1 --- a/tests/test-inlinelist.cpp	Thu Aug 29 23:51:30 2013 -0700
     5.2 +++ b/tests/test-inlinelist.cpp	Sat Aug 31 19:52:41 2013 -0700
     5.3 @@ -117,6 +117,13 @@
     5.4        iter++;
     5.5        if (!check(iter == list.end(), "iterator should have ended"))
     5.6          return false;
     5.7 +
     5.8 +      iter = list.begin();
     5.9 +      while (iter != list.end())
    5.10 +        iter = list.erase(iter);
    5.11 +
    5.12 +      if (!check(list.begin() == list.end(), "list should be empty"))
    5.13 +        return false;
    5.14      }
    5.15  
    5.16      return true;
     6.1 --- a/tests/test-vector.cpp	Thu Aug 29 23:51:30 2013 -0700
     6.2 +++ b/tests/test-vector.cpp	Sat Aug 31 19:52:41 2013 -0700
     6.3 @@ -125,6 +125,35 @@
     6.4      if (!check(vector.length() == 3, "vector length is 3"))
     6.5        return false;
     6.6  
     6.7 +    vector.insert(0, 88);
     6.8 +    vector.insert(0, 99);
     6.9 +    vector.insert(4, 111);
    6.10 +    if (!check(vector[0] == 99, "[0] should be 99"))
    6.11 +      return false;
    6.12 +    if (!check(vector[1] == 88, "[1] should be 88"))
    6.13 +      return false;
    6.14 +    if (!check(vector[2] == 1, "[2] should be 1"))
    6.15 +      return false;
    6.16 +    if (!check(vector[3] == 2, "[3] should be 2"))
    6.17 +      return false;
    6.18 +    if (!check(vector[4] == 111, "[4] should be 111"))
    6.19 +      return false;
    6.20 +    if (!check(vector[5] == 3, "[5] should be 3"))
    6.21 +      return false;
    6.22 +    if (!check(vector.length() == 6, "length should be 6"))
    6.23 +      return false;
    6.24 +
    6.25 +    vector.remove(5);
    6.26 +    if (!check(vector[4] == 111, "[4] should still be 111"))
    6.27 +      return false;
    6.28 +    vector.remove(0);
    6.29 +    if (!check(vector[0] == 88, "[0] should be 88"))
    6.30 +      return false;
    6.31 +    if (!check(vector[3] == 111, "[3] should be 111"))
    6.32 +      return false;
    6.33 +    if (!check(vector.length() == 4, "length should be 4"))
    6.34 +      return false;
    6.35 +
    6.36      while (!vector.empty())
    6.37        vector.pop();
    6.38