michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsResourceSet.h" michael@0: michael@0: nsResourceSet::nsResourceSet(const nsResourceSet& aResourceSet) michael@0: : mResources(nullptr), michael@0: mCount(0), michael@0: mCapacity(0) michael@0: { michael@0: ConstIterator last = aResourceSet.Last(); michael@0: for (ConstIterator resource = aResourceSet.First(); resource != last; ++resource) michael@0: Add(*resource); michael@0: } michael@0: michael@0: michael@0: nsResourceSet& michael@0: nsResourceSet::operator=(const nsResourceSet& aResourceSet) michael@0: { michael@0: Clear(); michael@0: ConstIterator last = aResourceSet.Last(); michael@0: for (ConstIterator resource = aResourceSet.First(); resource != last; ++resource) michael@0: Add(*resource); michael@0: return *this; michael@0: } michael@0: michael@0: nsResourceSet::~nsResourceSet() michael@0: { michael@0: MOZ_COUNT_DTOR(nsResourceSet); michael@0: Clear(); michael@0: delete[] mResources; michael@0: } michael@0: michael@0: nsresult michael@0: nsResourceSet::Clear() michael@0: { michael@0: while (--mCount >= 0) { michael@0: NS_RELEASE(mResources[mCount]); michael@0: } michael@0: mCount = 0; michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsResourceSet::Add(nsIRDFResource* aResource) michael@0: { michael@0: NS_PRECONDITION(aResource != nullptr, "null ptr"); michael@0: if (! aResource) michael@0: return NS_ERROR_NULL_POINTER; michael@0: michael@0: if (Contains(aResource)) michael@0: return NS_OK; michael@0: michael@0: if (mCount >= mCapacity) { michael@0: int32_t capacity = mCapacity + 4; michael@0: nsIRDFResource** resources = new nsIRDFResource*[capacity]; michael@0: if (! resources) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: for (int32_t i = mCount - 1; i >= 0; --i) michael@0: resources[i] = mResources[i]; michael@0: michael@0: delete[] mResources; michael@0: michael@0: mResources = resources; michael@0: mCapacity = capacity; michael@0: } michael@0: michael@0: mResources[mCount++] = aResource; michael@0: NS_ADDREF(aResource); michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: nsResourceSet::Remove(nsIRDFResource* aProperty) michael@0: { michael@0: bool found = false; michael@0: michael@0: nsIRDFResource** res = mResources; michael@0: nsIRDFResource** limit = mResources + mCount; michael@0: while (res < limit) { michael@0: if (found) { michael@0: *(res - 1) = *res; michael@0: } michael@0: else if (*res == aProperty) { michael@0: NS_RELEASE(*res); michael@0: found = true; michael@0: } michael@0: ++res; michael@0: } michael@0: michael@0: if (found) michael@0: --mCount; michael@0: } michael@0: michael@0: bool michael@0: nsResourceSet::Contains(nsIRDFResource* aResource) const michael@0: { michael@0: for (int32_t i = mCount - 1; i >= 0; --i) { michael@0: if (mResources[i] == aResource) michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: