2001-09-25 05:32:19 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2000-04-01 05:38:55 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
2000-04-14 00:04:13 +04:00
|
|
|
Implementations for the rule network classes.
|
|
|
|
|
2000-04-01 05:38:55 +04:00
|
|
|
To Do.
|
|
|
|
|
2001-11-07 07:25:41 +03:00
|
|
|
- Constrain() & Propagate() still feel like they are poorly named.
|
2000-04-01 05:38:55 +04:00
|
|
|
- As do Instantiation and InstantiationSet.
|
|
|
|
- Make InstantiationSet share and do copy-on-write.
|
|
|
|
- Make things iterative, instead of recursive.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2001-08-14 11:59:59 +04:00
|
|
|
#include "nscore.h"
|
2000-04-01 05:38:55 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "plhash.h"
|
|
|
|
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2001-03-23 13:56:18 +03:00
|
|
|
extern PRLogModuleInfo* gXULTemplateLog;
|
2000-04-01 05:38:55 +04:00
|
|
|
|
2001-08-30 06:29:05 +04:00
|
|
|
#include "nsString.h"
|
2006-02-13 19:02:08 +03:00
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "nsXULContentUtils.h"
|
2001-08-30 06:29:05 +04:00
|
|
|
|
2006-02-13 19:02:08 +03:00
|
|
|
#include "nsRuleNetwork.h"
|
|
|
|
#include "nsXULTemplateResultSetRDF.h"
|
2007-04-26 17:03:55 +04:00
|
|
|
#include "nsRDFConMemberTestNode.h"
|
|
|
|
#include "nsRDFPropertyTestNode.h"
|
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2000-04-01 05:38:55 +04:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
//
|
2006-02-13 19:02:08 +03:00
|
|
|
// nsRuleNetwork
|
2000-04-01 05:38:55 +04:00
|
|
|
//
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MemoryElementSet::Add(MemoryElement* aElement)
|
|
|
|
{
|
|
|
|
for (ConstIterator element = First(); element != Last(); ++element) {
|
2000-04-12 02:35:43 +04:00
|
|
|
if (*element == *aElement) {
|
|
|
|
// We've already got this element covered. Since Add()
|
|
|
|
// assumes ownership, and we aren't going to need this,
|
|
|
|
// just nuke it.
|
2013-03-05 08:49:50 +04:00
|
|
|
delete aElement;
|
2000-04-01 05:38:55 +04:00
|
|
|
return NS_OK;
|
2000-04-12 02:35:43 +04:00
|
|
|
}
|
2000-04-01 05:38:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
List* list = new List;
|
|
|
|
if (! list)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
list->mElement = aElement;
|
|
|
|
list->mRefCnt = 1;
|
|
|
|
list->mNext = mElements;
|
|
|
|
|
|
|
|
mElements = list;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsresult
|
2000-05-04 13:57:59 +04:00
|
|
|
nsAssignmentSet::Add(const nsAssignment& aAssignment)
|
2000-04-01 05:38:55 +04:00
|
|
|
{
|
2000-05-04 13:57:59 +04:00
|
|
|
NS_PRECONDITION(! HasAssignmentFor(aAssignment.mVariable), "variable already bound");
|
2006-02-13 19:02:08 +03:00
|
|
|
|
|
|
|
// XXXndeakin should this just silently fail?
|
2000-05-04 13:57:59 +04:00
|
|
|
if (HasAssignmentFor(aAssignment.mVariable))
|
2000-04-01 05:38:55 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2008-07-15 15:06:54 +04:00
|
|
|
List* list = new List(aAssignment);
|
2000-04-01 05:38:55 +04:00
|
|
|
if (! list)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2000-05-04 13:57:59 +04:00
|
|
|
list->mRefCnt = 1;
|
|
|
|
list->mNext = mAssignments;
|
2000-04-01 05:38:55 +04:00
|
|
|
|
2000-05-04 13:57:59 +04:00
|
|
|
mAssignments = list;
|
2000-04-01 05:38:55 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2000-05-04 13:57:59 +04:00
|
|
|
nsAssignmentSet::Count() const
|
2000-04-01 05:38:55 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t count = 0;
|
2000-05-04 13:57:59 +04:00
|
|
|
for (ConstIterator assignment = First(); assignment != Last(); ++assignment)
|
2000-04-01 05:38:55 +04:00
|
|
|
++count;
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2006-02-13 19:02:08 +03:00
|
|
|
nsAssignmentSet::HasAssignment(nsIAtom* aVariable, nsIRDFNode* aValue) const
|
2000-04-01 05:38:55 +04:00
|
|
|
{
|
2000-05-04 13:57:59 +04:00
|
|
|
for (ConstIterator assignment = First(); assignment != Last(); ++assignment) {
|
|
|
|
if (assignment->mVariable == aVariable && assignment->mValue == aValue)
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2000-04-01 05:38:55 +04:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2000-04-01 05:38:55 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2006-02-13 19:02:08 +03:00
|
|
|
nsAssignmentSet::HasAssignmentFor(nsIAtom* aVariable) const
|
2000-04-01 05:38:55 +04:00
|
|
|
{
|
2000-05-04 13:57:59 +04:00
|
|
|
for (ConstIterator assignment = First(); assignment != Last(); ++assignment) {
|
|
|
|
if (assignment->mVariable == aVariable)
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2000-04-01 05:38:55 +04:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2000-04-01 05:38:55 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2006-02-13 19:02:08 +03:00
|
|
|
nsAssignmentSet::GetAssignmentFor(nsIAtom* aVariable, nsIRDFNode** aValue) const
|
2000-04-01 05:38:55 +04:00
|
|
|
{
|
2000-05-04 13:57:59 +04:00
|
|
|
for (ConstIterator assignment = First(); assignment != Last(); ++assignment) {
|
|
|
|
if (assignment->mVariable == aVariable) {
|
|
|
|
*aValue = assignment->mValue;
|
2006-02-13 19:02:08 +03:00
|
|
|
NS_IF_ADDREF(*aValue);
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2000-04-01 05:38:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
*aValue = nullptr;
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2000-04-01 05:38:55 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2000-05-04 13:57:59 +04:00
|
|
|
nsAssignmentSet::Equals(const nsAssignmentSet& aSet) const
|
2000-04-01 05:38:55 +04:00
|
|
|
{
|
2000-05-04 13:57:59 +04:00
|
|
|
if (aSet.mAssignments == mAssignments)
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2000-04-01 05:38:55 +04:00
|
|
|
|
2000-05-04 13:57:59 +04:00
|
|
|
// If they have a different number of assignments, then they're different.
|
2000-04-01 05:38:55 +04:00
|
|
|
if (Count() != aSet.Count())
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2000-04-01 05:38:55 +04:00
|
|
|
|
|
|
|
// XXX O(n^2)! Ugh!
|
2006-02-13 19:02:08 +03:00
|
|
|
nsCOMPtr<nsIRDFNode> value;
|
2000-05-04 13:57:59 +04:00
|
|
|
for (ConstIterator assignment = First(); assignment != Last(); ++assignment) {
|
2006-02-13 19:02:08 +03:00
|
|
|
if (! aSet.GetAssignmentFor(assignment->mVariable, getter_AddRefs(value)))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2000-04-01 05:38:55 +04:00
|
|
|
|
2000-05-04 13:57:59 +04:00
|
|
|
if (assignment->mValue != value)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2000-04-01 05:38:55 +04:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2000-04-01 05:38:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
PLHashNumber
|
|
|
|
Instantiation::Hash(const void* aKey)
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
const Instantiation* inst = static_cast<const Instantiation*>(aKey);
|
2000-04-01 05:38:55 +04:00
|
|
|
|
|
|
|
PLHashNumber result = 0;
|
|
|
|
|
2000-05-04 13:57:59 +04:00
|
|
|
nsAssignmentSet::ConstIterator last = inst->mAssignments.Last();
|
2006-02-13 19:02:08 +03:00
|
|
|
for (nsAssignmentSet::ConstIterator assignment = inst->mAssignments.First();
|
|
|
|
assignment != last; ++assignment)
|
2000-05-04 13:57:59 +04:00
|
|
|
result ^= assignment->Hash();
|
2000-04-01 05:38:55 +04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-09 11:09:40 +04:00
|
|
|
int
|
2000-04-01 05:38:55 +04:00
|
|
|
Instantiation::Compare(const void* aLeft, const void* aRight)
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
const Instantiation* left = static_cast<const Instantiation*>(aLeft);
|
|
|
|
const Instantiation* right = static_cast<const Instantiation*>(aRight);
|
2000-04-01 05:38:55 +04:00
|
|
|
|
|
|
|
return *left == *right;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// InstantiationSet
|
|
|
|
//
|
|
|
|
|
|
|
|
InstantiationSet::InstantiationSet()
|
|
|
|
{
|
|
|
|
mHead.mPrev = mHead.mNext = &mHead;
|
2000-04-02 13:11:00 +04:00
|
|
|
MOZ_COUNT_CTOR(InstantiationSet);
|
2000-04-01 05:38:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
InstantiationSet::InstantiationSet(const InstantiationSet& aInstantiationSet)
|
|
|
|
{
|
|
|
|
mHead.mPrev = mHead.mNext = &mHead;
|
|
|
|
|
|
|
|
// XXX replace with copy-on-write foo
|
|
|
|
ConstIterator last = aInstantiationSet.Last();
|
|
|
|
for (ConstIterator inst = aInstantiationSet.First(); inst != last; ++inst)
|
|
|
|
Append(*inst);
|
2000-04-02 13:11:00 +04:00
|
|
|
|
|
|
|
MOZ_COUNT_CTOR(InstantiationSet);
|
2000-04-01 05:38:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
InstantiationSet&
|
|
|
|
InstantiationSet::operator=(const InstantiationSet& aInstantiationSet)
|
|
|
|
{
|
|
|
|
// XXX replace with copy-on-write foo
|
|
|
|
Clear();
|
|
|
|
|
|
|
|
ConstIterator last = aInstantiationSet.Last();
|
|
|
|
for (ConstIterator inst = aInstantiationSet.First(); inst != last; ++inst)
|
|
|
|
Append(*inst);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
InstantiationSet::Clear()
|
|
|
|
{
|
|
|
|
Iterator inst = First();
|
|
|
|
while (inst != Last())
|
|
|
|
Erase(inst++);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
InstantiationSet::Iterator
|
|
|
|
InstantiationSet::Insert(Iterator aIterator, const Instantiation& aInstantiation)
|
|
|
|
{
|
|
|
|
List* newelement = new List();
|
|
|
|
if (newelement) {
|
|
|
|
newelement->mInstantiation = aInstantiation;
|
|
|
|
|
|
|
|
aIterator.mCurrent->mPrev->mNext = newelement;
|
|
|
|
|
|
|
|
newelement->mNext = aIterator.mCurrent;
|
|
|
|
newelement->mPrev = aIterator.mCurrent->mPrev;
|
|
|
|
|
|
|
|
aIterator.mCurrent->mPrev = newelement;
|
|
|
|
}
|
|
|
|
return aIterator;
|
|
|
|
}
|
|
|
|
|
|
|
|
InstantiationSet::Iterator
|
|
|
|
InstantiationSet::Erase(Iterator aIterator)
|
|
|
|
{
|
|
|
|
Iterator result = aIterator;
|
|
|
|
++result;
|
|
|
|
aIterator.mCurrent->mNext->mPrev = aIterator.mCurrent->mPrev;
|
|
|
|
aIterator.mCurrent->mPrev->mNext = aIterator.mCurrent->mNext;
|
|
|
|
delete aIterator.mCurrent;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2006-02-13 19:02:08 +03:00
|
|
|
InstantiationSet::HasAssignmentFor(nsIAtom* aVariable) const
|
2000-04-01 05:38:55 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return !Empty() ? First()->mAssignments.HasAssignmentFor(aVariable) : false;
|
2000-04-01 05:38:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// ReteNode
|
|
|
|
//
|
|
|
|
// The basic node in the network.
|
|
|
|
//
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// TestNode
|
|
|
|
//
|
|
|
|
// to do:
|
|
|
|
// - FilterInstantiations() is poorly named
|
|
|
|
//
|
|
|
|
|
|
|
|
|
2006-02-13 19:02:08 +03:00
|
|
|
TestNode::TestNode(TestNode* aParent)
|
2000-04-01 05:38:55 +04:00
|
|
|
: mParent(aParent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2006-02-13 19:02:08 +03:00
|
|
|
TestNode::Propagate(InstantiationSet& aInstantiations,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aIsUpdate, bool& aTakenInstantiations)
|
2000-04-01 05:38:55 +04:00
|
|
|
{
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gXULTemplateLog, LogLevel::Debug,
|
2001-11-07 07:25:41 +03:00
|
|
|
("TestNode[%p]: Propagate() begin", this));
|
2001-03-23 13:56:18 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
aTakenInstantiations = false;
|
2006-02-13 19:02:08 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsresult rv = FilterInstantiations(aInstantiations, nullptr);
|
2006-02-13 19:02:08 +03:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
// if there is more than one child, each will need to be supplied with the
|
|
|
|
// original set of instantiations from this node, so create a copy in this
|
|
|
|
// case. If there is only one child, optimize and just pass the
|
|
|
|
// instantiations along to the child without copying
|
2011-09-29 10:19:26 +04:00
|
|
|
bool shouldCopy = (mKids.Count() > 1);
|
2000-04-01 05:38:55 +04:00
|
|
|
|
2006-02-13 19:02:08 +03:00
|
|
|
// See the header file for details about how instantiation ownership works.
|
|
|
|
if (! aInstantiations.Empty()) {
|
2001-06-08 11:12:41 +04:00
|
|
|
ReteNodeSet::Iterator last = mKids.Last();
|
|
|
|
for (ReteNodeSet::Iterator kid = mKids.First(); kid != last; ++kid) {
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gXULTemplateLog, LogLevel::Debug,
|
2001-11-07 07:25:41 +03:00
|
|
|
("TestNode[%p]: Propagate() passing to child %p", this, kid.operator->()));
|
2001-03-23 13:56:18 +03:00
|
|
|
|
2006-02-13 19:02:08 +03:00
|
|
|
// create a copy of the instantiations
|
|
|
|
if (shouldCopy) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool owned = false;
|
2006-02-13 19:02:08 +03:00
|
|
|
InstantiationSet* instantiations =
|
|
|
|
new InstantiationSet(aInstantiations);
|
|
|
|
if (!instantiations)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
rv = kid->Propagate(*instantiations, aIsUpdate, owned);
|
|
|
|
if (!owned)
|
|
|
|
delete instantiations;
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rv = kid->Propagate(aInstantiations, aIsUpdate, aTakenInstantiations);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
2001-03-23 13:56:18 +03:00
|
|
|
}
|
2000-04-01 05:38:55 +04:00
|
|
|
}
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gXULTemplateLog, LogLevel::Debug,
|
2001-11-07 07:25:41 +03:00
|
|
|
("TestNode[%p]: Propagate() end", this));
|
2001-03-23 13:56:18 +03:00
|
|
|
|
2000-04-01 05:38:55 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2006-02-13 19:02:08 +03:00
|
|
|
TestNode::Constrain(InstantiationSet& aInstantiations)
|
2000-04-01 05:38:55 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gXULTemplateLog, LogLevel::Debug,
|
2001-03-23 13:56:18 +03:00
|
|
|
("TestNode[%p]: Constrain() begin", this));
|
|
|
|
|
2006-03-27 17:53:27 +04:00
|
|
|
// if the cantHandleYet flag is set by FilterInstantiations,
|
|
|
|
// there isn't enough information yet available to fill in.
|
|
|
|
// For this, continue the constrain all the way to the top
|
|
|
|
// and then call FilterInstantiations again afterwards. This
|
|
|
|
// should fill in any missing information.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool cantHandleYet = false;
|
2006-03-27 17:53:27 +04:00
|
|
|
rv = FilterInstantiations(aInstantiations, &cantHandleYet);
|
2000-04-01 05:38:55 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2011-03-26 07:39:29 +03:00
|
|
|
if (mParent && (!aInstantiations.Empty() || cantHandleYet)) {
|
2006-03-27 17:53:27 +04:00
|
|
|
// if we still have instantiations, or if the instantiations
|
|
|
|
// could not be filled in yet, then ride 'em on up to the
|
2000-04-01 05:38:55 +04:00
|
|
|
// parent to narrow them.
|
2001-03-23 13:56:18 +03:00
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gXULTemplateLog, LogLevel::Debug,
|
2001-03-23 13:56:18 +03:00
|
|
|
("TestNode[%p]: Constrain() passing to parent %p", this, mParent));
|
|
|
|
|
2006-02-13 19:02:08 +03:00
|
|
|
rv = mParent->Constrain(aInstantiations);
|
2006-03-27 17:53:27 +04:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) && cantHandleYet)
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = FilterInstantiations(aInstantiations, nullptr);
|
2000-04-01 05:38:55 +04:00
|
|
|
}
|
|
|
|
else {
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gXULTemplateLog, LogLevel::Debug,
|
2001-03-23 13:56:18 +03:00
|
|
|
("TestNode[%p]: Constrain() failed", this));
|
|
|
|
|
2000-04-01 05:38:55 +04:00
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gXULTemplateLog, LogLevel::Debug,
|
2001-03-23 13:56:18 +03:00
|
|
|
("TestNode[%p]: Constrain() end", this));
|
|
|
|
|
2000-04-01 05:38:55 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-13 19:02:08 +03:00
|
|
|
//----------------------------------------------------------------------
|
2000-04-01 05:38:55 +04:00
|
|
|
|
2001-06-08 11:12:41 +04:00
|
|
|
ReteNodeSet::ReteNodeSet()
|
2012-07-30 18:20:58 +04:00
|
|
|
: mNodes(nullptr), mCount(0), mCapacity(0)
|
2000-04-01 05:38:55 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2001-06-08 11:12:41 +04:00
|
|
|
ReteNodeSet::~ReteNodeSet()
|
2000-04-01 05:38:55 +04:00
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2001-06-08 11:12:41 +04:00
|
|
|
ReteNodeSet::Add(ReteNode* aNode)
|
2000-04-01 05:38:55 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_PRECONDITION(aNode != nullptr, "null ptr");
|
2000-04-01 05:38:55 +04:00
|
|
|
if (! aNode)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
if (mCount >= mCapacity) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t capacity = mCapacity + 4;
|
2000-04-01 05:38:55 +04:00
|
|
|
ReteNode** nodes = new ReteNode*[capacity];
|
|
|
|
if (! nodes)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = mCount - 1; i >= 0; --i)
|
2000-04-01 05:38:55 +04:00
|
|
|
nodes[i] = mNodes[i];
|
|
|
|
|
|
|
|
delete[] mNodes;
|
|
|
|
|
|
|
|
mNodes = nodes;
|
|
|
|
mCapacity = capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
mNodes[mCount++] = aNode;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2001-06-08 11:12:41 +04:00
|
|
|
ReteNodeSet::Clear()
|
2000-04-01 05:38:55 +04:00
|
|
|
{
|
|
|
|
delete[] mNodes;
|
2012-07-30 18:20:58 +04:00
|
|
|
mNodes = nullptr;
|
2000-04-01 05:38:55 +04:00
|
|
|
mCount = mCapacity = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|