From 82dec4824e3d560092777a83d43a3cae50f47c65 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@Germany>
Date: Wed, 21 Jul 2010 11:11:42 +0200
Subject: [PATCH] ENH: support find via keyType for coordinateSystems

- findAll() method returns a labelList of all matching names
- find() method returns the index to the first matching name

  For example, use a regex to specify alternative coordinate systems
  in porousZones
  (
      "cat1?(Back|Front)*"
      {
          coordinateSystem    "(cat1|system_10)";
          porosity    0.781;
          ...
      }
  )
---
 .../coordinateSystems/coordinateSystem.C      | 21 ++++----
 .../coordinateSystems/coordinateSystem.H      |  2 +-
 .../coordinateSystems/coordinateSystems.C     | 52 ++++++++++++++++---
 .../coordinateSystems/coordinateSystems.H     | 13 +++--
 4 files changed, 64 insertions(+), 24 deletions(-)

diff --git a/src/meshTools/coordinateSystems/coordinateSystem.C b/src/meshTools/coordinateSystems/coordinateSystem.C
index 8e5fc7bceb1..0a5fe400f7e 100644
--- a/src/meshTools/coordinateSystems/coordinateSystem.C
+++ b/src/meshTools/coordinateSystems/coordinateSystem.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -136,37 +136,36 @@ Foam::coordinateSystem::coordinateSystem
 {
     const entry* entryPtr = dict.lookupEntryPtr(typeName_(), false, false);
 
-    // a simple entry is a lookup into global coordinateSystems
+    // non-dictionary entry is a lookup into global coordinateSystems
     if (entryPtr && !entryPtr->isDict())
     {
-        word csName;
-        entryPtr->stream() >> csName;
+        keyType key(entryPtr->stream());
 
-        const coordinateSystems& csLst = coordinateSystems::New(obr);
+        const coordinateSystems& lst = coordinateSystems::New(obr);
+        const label id = lst.find(key);
 
-        label csId = csLst.find(csName);
         if (debug)
         {
             Info<< "coordinateSystem::coordinateSystem"
                 "(const dictionary&, const objectRegistry&):"
                 << nl << "using global coordinate system: "
-                << csName << "=" << csId << endl;
+                << key << "=" << id << endl;
         }
 
-        if (csId < 0)
+        if (id < 0)
         {
             FatalErrorIn
             (
                 "coordinateSystem::coordinateSystem"
                 "(const dictionary&, const objectRegistry&)"
-            )   << "could not find coordinate system: " << csName << nl
-                << "available coordinate systems: " << csLst.toc() << nl << nl
+            )   << "could not find coordinate system: " << key << nl
+                << "available coordinate systems: " << lst.toc() << nl << nl
                 << exit(FatalError);
         }
 
         // copy coordinateSystem, but assign the name as the typeName
         // to avoid strange things in writeDict()
-        operator=(csLst[csId]);
+        operator=(lst[id]);
         name_ = typeName_();
     }
     else
diff --git a/src/meshTools/coordinateSystems/coordinateSystem.H b/src/meshTools/coordinateSystems/coordinateSystem.H
index e2a8f47affb..11a48d6eb77 100644
--- a/src/meshTools/coordinateSystems/coordinateSystem.H
+++ b/src/meshTools/coordinateSystems/coordinateSystem.H
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
diff --git a/src/meshTools/coordinateSystems/coordinateSystems.C b/src/meshTools/coordinateSystems/coordinateSystems.C
index 28632d0ae0a..4d829004081 100644
--- a/src/meshTools/coordinateSystems/coordinateSystems.C
+++ b/src/meshTools/coordinateSystems/coordinateSystems.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -26,6 +26,7 @@ License
 #include "coordinateSystems.H"
 #include "IOPtrList.H"
 #include "Time.H"
+#include "stringListOps.H"
 
 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
 
@@ -97,13 +98,25 @@ const Foam::coordinateSystems& Foam::coordinateSystems::New
 
 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
 
-Foam::label Foam::coordinateSystems::find(const word& keyword) const
+Foam::label Foam::coordinateSystems::find(const keyType& key) const
 {
-    forAll(*this, i)
+    if (key.isPattern())
     {
-        if (keyword == operator[](i).name())
+        labelList allFound = findAll(key);
+        // return first element
+        if (!allFound.empty())
         {
-            return i;
+            return allFound[0];
+        }
+    }
+    else
+    {
+        forAll(*this, i)
+        {
+            if (key == operator[](i).name())
+            {
+                return i;
+            }
         }
     }
 
@@ -111,9 +124,34 @@ Foam::label Foam::coordinateSystems::find(const word& keyword) const
 }
 
 
-bool Foam::coordinateSystems::found(const word& keyword) const
+Foam::labelList Foam::coordinateSystems::findAll(const keyType& key) const
+{
+    labelList allFound;
+    if (key.isPattern())
+    {
+        allFound = findStrings(key, toc());
+    }
+    else
+    {
+        allFound.setSize(size());
+        label nFound = 0;
+        forAll(*this, i)
+        {
+            if (key == operator[](i).name())
+            {
+                allFound[nFound++] = i;
+            }
+        }
+        allFound.setSize(nFound);
+    }
+
+    return allFound;
+}
+
+
+bool Foam::coordinateSystems::found(const keyType& key) const
 {
-    return find(keyword) >= 0;
+    return find(key) >= 0;
 }
 
 
diff --git a/src/meshTools/coordinateSystems/coordinateSystems.H b/src/meshTools/coordinateSystems/coordinateSystems.H
index 60bd169ba0d..2976a6f94ef 100644
--- a/src/meshTools/coordinateSystems/coordinateSystems.H
+++ b/src/meshTools/coordinateSystems/coordinateSystems.H
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -97,11 +97,14 @@ public:
 
     // Member Functions
 
-        //- Find and return index for a given keyword, returns -1 if not found
-        label find(const word& key) const;
+        //- Find and return index for the first match, returns -1 if not found
+        label find(const keyType& key) const;
 
-        //- Search for given keyword
-        bool found(const word& keyword) const;
+        //- Find and return indices for all matches
+        labelList findAll(const keyType& key) const;
+
+        //- Search for given key
+        bool found(const keyType& key) const;
 
         //- Return the table of contents (list of all keywords)
         wordList toc() const;
-- 
GitLab