content/xul/templates/src/nsRuleNetwork.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7
michael@0 8 Implementations for the rule network classes.
michael@0 9
michael@0 10 To Do.
michael@0 11
michael@0 12 - Constrain() & Propagate() still feel like they are poorly named.
michael@0 13 - As do Instantiation and InstantiationSet.
michael@0 14 - Make InstantiationSet share and do copy-on-write.
michael@0 15 - Make things iterative, instead of recursive.
michael@0 16
michael@0 17 */
michael@0 18
michael@0 19 #include "nscore.h"
michael@0 20 #include "nsCOMPtr.h"
michael@0 21 #include "plhash.h"
michael@0 22
michael@0 23 #include "prlog.h"
michael@0 24 #ifdef PR_LOGGING
michael@0 25 extern PRLogModuleInfo* gXULTemplateLog;
michael@0 26
michael@0 27 #include "nsString.h"
michael@0 28 #include "nsUnicharUtils.h"
michael@0 29 #include "nsXULContentUtils.h"
michael@0 30
michael@0 31 #endif
michael@0 32
michael@0 33 #include "nsRuleNetwork.h"
michael@0 34 #include "nsXULTemplateResultSetRDF.h"
michael@0 35 #include "nsRDFConMemberTestNode.h"
michael@0 36 #include "nsRDFPropertyTestNode.h"
michael@0 37
michael@0 38 using namespace mozilla;
michael@0 39
michael@0 40 //----------------------------------------------------------------------
michael@0 41 //
michael@0 42 // nsRuleNetwork
michael@0 43 //
michael@0 44
michael@0 45 nsresult
michael@0 46 MemoryElementSet::Add(MemoryElement* aElement)
michael@0 47 {
michael@0 48 for (ConstIterator element = First(); element != Last(); ++element) {
michael@0 49 if (*element == *aElement) {
michael@0 50 // We've already got this element covered. Since Add()
michael@0 51 // assumes ownership, and we aren't going to need this,
michael@0 52 // just nuke it.
michael@0 53 delete aElement;
michael@0 54 return NS_OK;
michael@0 55 }
michael@0 56 }
michael@0 57
michael@0 58 List* list = new List;
michael@0 59 if (! list)
michael@0 60 return NS_ERROR_OUT_OF_MEMORY;
michael@0 61
michael@0 62 list->mElement = aElement;
michael@0 63 list->mRefCnt = 1;
michael@0 64 list->mNext = mElements;
michael@0 65
michael@0 66 mElements = list;
michael@0 67
michael@0 68 return NS_OK;
michael@0 69 }
michael@0 70
michael@0 71
michael@0 72 //----------------------------------------------------------------------
michael@0 73
michael@0 74 nsresult
michael@0 75 nsAssignmentSet::Add(const nsAssignment& aAssignment)
michael@0 76 {
michael@0 77 NS_PRECONDITION(! HasAssignmentFor(aAssignment.mVariable), "variable already bound");
michael@0 78
michael@0 79 // XXXndeakin should this just silently fail?
michael@0 80 if (HasAssignmentFor(aAssignment.mVariable))
michael@0 81 return NS_ERROR_UNEXPECTED;
michael@0 82
michael@0 83 List* list = new List(aAssignment);
michael@0 84 if (! list)
michael@0 85 return NS_ERROR_OUT_OF_MEMORY;
michael@0 86
michael@0 87 list->mRefCnt = 1;
michael@0 88 list->mNext = mAssignments;
michael@0 89
michael@0 90 mAssignments = list;
michael@0 91
michael@0 92 return NS_OK;
michael@0 93 }
michael@0 94
michael@0 95 int32_t
michael@0 96 nsAssignmentSet::Count() const
michael@0 97 {
michael@0 98 int32_t count = 0;
michael@0 99 for (ConstIterator assignment = First(); assignment != Last(); ++assignment)
michael@0 100 ++count;
michael@0 101
michael@0 102 return count;
michael@0 103 }
michael@0 104
michael@0 105 bool
michael@0 106 nsAssignmentSet::HasAssignment(nsIAtom* aVariable, nsIRDFNode* aValue) const
michael@0 107 {
michael@0 108 for (ConstIterator assignment = First(); assignment != Last(); ++assignment) {
michael@0 109 if (assignment->mVariable == aVariable && assignment->mValue == aValue)
michael@0 110 return true;
michael@0 111 }
michael@0 112
michael@0 113 return false;
michael@0 114 }
michael@0 115
michael@0 116 bool
michael@0 117 nsAssignmentSet::HasAssignmentFor(nsIAtom* aVariable) const
michael@0 118 {
michael@0 119 for (ConstIterator assignment = First(); assignment != Last(); ++assignment) {
michael@0 120 if (assignment->mVariable == aVariable)
michael@0 121 return true;
michael@0 122 }
michael@0 123
michael@0 124 return false;
michael@0 125 }
michael@0 126
michael@0 127 bool
michael@0 128 nsAssignmentSet::GetAssignmentFor(nsIAtom* aVariable, nsIRDFNode** aValue) const
michael@0 129 {
michael@0 130 for (ConstIterator assignment = First(); assignment != Last(); ++assignment) {
michael@0 131 if (assignment->mVariable == aVariable) {
michael@0 132 *aValue = assignment->mValue;
michael@0 133 NS_IF_ADDREF(*aValue);
michael@0 134 return true;
michael@0 135 }
michael@0 136 }
michael@0 137
michael@0 138 *aValue = nullptr;
michael@0 139 return false;
michael@0 140 }
michael@0 141
michael@0 142 bool
michael@0 143 nsAssignmentSet::Equals(const nsAssignmentSet& aSet) const
michael@0 144 {
michael@0 145 if (aSet.mAssignments == mAssignments)
michael@0 146 return true;
michael@0 147
michael@0 148 // If they have a different number of assignments, then they're different.
michael@0 149 if (Count() != aSet.Count())
michael@0 150 return false;
michael@0 151
michael@0 152 // XXX O(n^2)! Ugh!
michael@0 153 nsCOMPtr<nsIRDFNode> value;
michael@0 154 for (ConstIterator assignment = First(); assignment != Last(); ++assignment) {
michael@0 155 if (! aSet.GetAssignmentFor(assignment->mVariable, getter_AddRefs(value)))
michael@0 156 return false;
michael@0 157
michael@0 158 if (assignment->mValue != value)
michael@0 159 return false;
michael@0 160 }
michael@0 161
michael@0 162 return true;
michael@0 163 }
michael@0 164
michael@0 165 //----------------------------------------------------------------------
michael@0 166
michael@0 167 PLHashNumber
michael@0 168 Instantiation::Hash(const void* aKey)
michael@0 169 {
michael@0 170 const Instantiation* inst = static_cast<const Instantiation*>(aKey);
michael@0 171
michael@0 172 PLHashNumber result = 0;
michael@0 173
michael@0 174 nsAssignmentSet::ConstIterator last = inst->mAssignments.Last();
michael@0 175 for (nsAssignmentSet::ConstIterator assignment = inst->mAssignments.First();
michael@0 176 assignment != last; ++assignment)
michael@0 177 result ^= assignment->Hash();
michael@0 178
michael@0 179 return result;
michael@0 180 }
michael@0 181
michael@0 182
michael@0 183 int
michael@0 184 Instantiation::Compare(const void* aLeft, const void* aRight)
michael@0 185 {
michael@0 186 const Instantiation* left = static_cast<const Instantiation*>(aLeft);
michael@0 187 const Instantiation* right = static_cast<const Instantiation*>(aRight);
michael@0 188
michael@0 189 return *left == *right;
michael@0 190 }
michael@0 191
michael@0 192
michael@0 193 //----------------------------------------------------------------------
michael@0 194 //
michael@0 195 // InstantiationSet
michael@0 196 //
michael@0 197
michael@0 198 InstantiationSet::InstantiationSet()
michael@0 199 {
michael@0 200 mHead.mPrev = mHead.mNext = &mHead;
michael@0 201 MOZ_COUNT_CTOR(InstantiationSet);
michael@0 202 }
michael@0 203
michael@0 204
michael@0 205 InstantiationSet::InstantiationSet(const InstantiationSet& aInstantiationSet)
michael@0 206 {
michael@0 207 mHead.mPrev = mHead.mNext = &mHead;
michael@0 208
michael@0 209 // XXX replace with copy-on-write foo
michael@0 210 ConstIterator last = aInstantiationSet.Last();
michael@0 211 for (ConstIterator inst = aInstantiationSet.First(); inst != last; ++inst)
michael@0 212 Append(*inst);
michael@0 213
michael@0 214 MOZ_COUNT_CTOR(InstantiationSet);
michael@0 215 }
michael@0 216
michael@0 217 InstantiationSet&
michael@0 218 InstantiationSet::operator=(const InstantiationSet& aInstantiationSet)
michael@0 219 {
michael@0 220 // XXX replace with copy-on-write foo
michael@0 221 Clear();
michael@0 222
michael@0 223 ConstIterator last = aInstantiationSet.Last();
michael@0 224 for (ConstIterator inst = aInstantiationSet.First(); inst != last; ++inst)
michael@0 225 Append(*inst);
michael@0 226
michael@0 227 return *this;
michael@0 228 }
michael@0 229
michael@0 230
michael@0 231 void
michael@0 232 InstantiationSet::Clear()
michael@0 233 {
michael@0 234 Iterator inst = First();
michael@0 235 while (inst != Last())
michael@0 236 Erase(inst++);
michael@0 237 }
michael@0 238
michael@0 239
michael@0 240 InstantiationSet::Iterator
michael@0 241 InstantiationSet::Insert(Iterator aIterator, const Instantiation& aInstantiation)
michael@0 242 {
michael@0 243 List* newelement = new List();
michael@0 244 if (newelement) {
michael@0 245 newelement->mInstantiation = aInstantiation;
michael@0 246
michael@0 247 aIterator.mCurrent->mPrev->mNext = newelement;
michael@0 248
michael@0 249 newelement->mNext = aIterator.mCurrent;
michael@0 250 newelement->mPrev = aIterator.mCurrent->mPrev;
michael@0 251
michael@0 252 aIterator.mCurrent->mPrev = newelement;
michael@0 253 }
michael@0 254 return aIterator;
michael@0 255 }
michael@0 256
michael@0 257 InstantiationSet::Iterator
michael@0 258 InstantiationSet::Erase(Iterator aIterator)
michael@0 259 {
michael@0 260 Iterator result = aIterator;
michael@0 261 ++result;
michael@0 262 aIterator.mCurrent->mNext->mPrev = aIterator.mCurrent->mPrev;
michael@0 263 aIterator.mCurrent->mPrev->mNext = aIterator.mCurrent->mNext;
michael@0 264 delete aIterator.mCurrent;
michael@0 265 return result;
michael@0 266 }
michael@0 267
michael@0 268
michael@0 269 bool
michael@0 270 InstantiationSet::HasAssignmentFor(nsIAtom* aVariable) const
michael@0 271 {
michael@0 272 return !Empty() ? First()->mAssignments.HasAssignmentFor(aVariable) : false;
michael@0 273 }
michael@0 274
michael@0 275 //----------------------------------------------------------------------
michael@0 276 //
michael@0 277 // ReteNode
michael@0 278 //
michael@0 279 // The basic node in the network.
michael@0 280 //
michael@0 281
michael@0 282 //----------------------------------------------------------------------
michael@0 283 //
michael@0 284 // TestNode
michael@0 285 //
michael@0 286 // to do:
michael@0 287 // - FilterInstantiations() is poorly named
michael@0 288 //
michael@0 289
michael@0 290
michael@0 291 TestNode::TestNode(TestNode* aParent)
michael@0 292 : mParent(aParent)
michael@0 293 {
michael@0 294 }
michael@0 295
michael@0 296 nsresult
michael@0 297 TestNode::Propagate(InstantiationSet& aInstantiations,
michael@0 298 bool aIsUpdate, bool& aTakenInstantiations)
michael@0 299 {
michael@0 300 PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
michael@0 301 ("TestNode[%p]: Propagate() begin", this));
michael@0 302
michael@0 303 aTakenInstantiations = false;
michael@0 304
michael@0 305 nsresult rv = FilterInstantiations(aInstantiations, nullptr);
michael@0 306 if (NS_FAILED(rv))
michael@0 307 return rv;
michael@0 308
michael@0 309 // if there is more than one child, each will need to be supplied with the
michael@0 310 // original set of instantiations from this node, so create a copy in this
michael@0 311 // case. If there is only one child, optimize and just pass the
michael@0 312 // instantiations along to the child without copying
michael@0 313 bool shouldCopy = (mKids.Count() > 1);
michael@0 314
michael@0 315 // See the header file for details about how instantiation ownership works.
michael@0 316 if (! aInstantiations.Empty()) {
michael@0 317 ReteNodeSet::Iterator last = mKids.Last();
michael@0 318 for (ReteNodeSet::Iterator kid = mKids.First(); kid != last; ++kid) {
michael@0 319 PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
michael@0 320 ("TestNode[%p]: Propagate() passing to child %p", this, kid.operator->()));
michael@0 321
michael@0 322 // create a copy of the instantiations
michael@0 323 if (shouldCopy) {
michael@0 324 bool owned = false;
michael@0 325 InstantiationSet* instantiations =
michael@0 326 new InstantiationSet(aInstantiations);
michael@0 327 if (!instantiations)
michael@0 328 return NS_ERROR_OUT_OF_MEMORY;
michael@0 329 rv = kid->Propagate(*instantiations, aIsUpdate, owned);
michael@0 330 if (!owned)
michael@0 331 delete instantiations;
michael@0 332 if (NS_FAILED(rv))
michael@0 333 return rv;
michael@0 334 }
michael@0 335 else {
michael@0 336 rv = kid->Propagate(aInstantiations, aIsUpdate, aTakenInstantiations);
michael@0 337 if (NS_FAILED(rv))
michael@0 338 return rv;
michael@0 339 }
michael@0 340 }
michael@0 341 }
michael@0 342
michael@0 343 PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
michael@0 344 ("TestNode[%p]: Propagate() end", this));
michael@0 345
michael@0 346 return NS_OK;
michael@0 347 }
michael@0 348
michael@0 349
michael@0 350 nsresult
michael@0 351 TestNode::Constrain(InstantiationSet& aInstantiations)
michael@0 352 {
michael@0 353 nsresult rv;
michael@0 354
michael@0 355 PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
michael@0 356 ("TestNode[%p]: Constrain() begin", this));
michael@0 357
michael@0 358 // if the cantHandleYet flag is set by FilterInstantiations,
michael@0 359 // there isn't enough information yet available to fill in.
michael@0 360 // For this, continue the constrain all the way to the top
michael@0 361 // and then call FilterInstantiations again afterwards. This
michael@0 362 // should fill in any missing information.
michael@0 363 bool cantHandleYet = false;
michael@0 364 rv = FilterInstantiations(aInstantiations, &cantHandleYet);
michael@0 365 if (NS_FAILED(rv)) return rv;
michael@0 366
michael@0 367 if (mParent && (!aInstantiations.Empty() || cantHandleYet)) {
michael@0 368 // if we still have instantiations, or if the instantiations
michael@0 369 // could not be filled in yet, then ride 'em on up to the
michael@0 370 // parent to narrow them.
michael@0 371
michael@0 372 PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
michael@0 373 ("TestNode[%p]: Constrain() passing to parent %p", this, mParent));
michael@0 374
michael@0 375 rv = mParent->Constrain(aInstantiations);
michael@0 376
michael@0 377 if (NS_SUCCEEDED(rv) && cantHandleYet)
michael@0 378 rv = FilterInstantiations(aInstantiations, nullptr);
michael@0 379 }
michael@0 380 else {
michael@0 381 PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
michael@0 382 ("TestNode[%p]: Constrain() failed", this));
michael@0 383
michael@0 384 rv = NS_OK;
michael@0 385 }
michael@0 386
michael@0 387 PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
michael@0 388 ("TestNode[%p]: Constrain() end", this));
michael@0 389
michael@0 390 return rv;
michael@0 391 }
michael@0 392
michael@0 393
michael@0 394 //----------------------------------------------------------------------
michael@0 395
michael@0 396 ReteNodeSet::ReteNodeSet()
michael@0 397 : mNodes(nullptr), mCount(0), mCapacity(0)
michael@0 398 {
michael@0 399 }
michael@0 400
michael@0 401 ReteNodeSet::~ReteNodeSet()
michael@0 402 {
michael@0 403 Clear();
michael@0 404 }
michael@0 405
michael@0 406 nsresult
michael@0 407 ReteNodeSet::Add(ReteNode* aNode)
michael@0 408 {
michael@0 409 NS_PRECONDITION(aNode != nullptr, "null ptr");
michael@0 410 if (! aNode)
michael@0 411 return NS_ERROR_NULL_POINTER;
michael@0 412
michael@0 413 if (mCount >= mCapacity) {
michael@0 414 int32_t capacity = mCapacity + 4;
michael@0 415 ReteNode** nodes = new ReteNode*[capacity];
michael@0 416 if (! nodes)
michael@0 417 return NS_ERROR_OUT_OF_MEMORY;
michael@0 418
michael@0 419 for (int32_t i = mCount - 1; i >= 0; --i)
michael@0 420 nodes[i] = mNodes[i];
michael@0 421
michael@0 422 delete[] mNodes;
michael@0 423
michael@0 424 mNodes = nodes;
michael@0 425 mCapacity = capacity;
michael@0 426 }
michael@0 427
michael@0 428 mNodes[mCount++] = aNode;
michael@0 429 return NS_OK;
michael@0 430 }
michael@0 431
michael@0 432 nsresult
michael@0 433 ReteNodeSet::Clear()
michael@0 434 {
michael@0 435 delete[] mNodes;
michael@0 436 mNodes = nullptr;
michael@0 437 mCount = mCapacity = 0;
michael@0 438 return NS_OK;
michael@0 439 }

mercurial