[chronojump] new class ConvertBooleansInt to convert from/to n booleans



commit 77ca07504f3f03d6d54e3d7e979b5611332557e3
Author: Xavier de Blas <xaviblas gmail com>
Date:   Mon Jul 19 16:30:22 2021 +0200

    new class ConvertBooleansInt to convert from/to n booleans

 src/gui/app1/chronojump.cs |  4 ++-
 src/util.cs                | 67 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 70 insertions(+), 1 deletion(-)
---
diff --git a/src/gui/app1/chronojump.cs b/src/gui/app1/chronojump.cs
index 9e66ffd8d..ee2ca5291 100644
--- a/src/gui/app1/chronojump.cs
+++ b/src/gui/app1/chronojump.cs
@@ -947,10 +947,12 @@ public partial class ChronoJumpWindow
                //Util.TestSortDoublesListstring();
 
                //Test ForceSensor GetVariabilityAndAccuracy: getVariabilityCVRMSSD
-               ForceSensorCapturePoints.TestVariabilityCVRMSSD();
+               //ForceSensorCapturePoints.TestVariabilityCVRMSSD();
 
                //InterpolateSignal.TestInterpolateBetween();
                //InterpolateSignal.TestCosineAndCubicInterpolate(true);
+
+               //ConvertBooleansInt.Test();
        }
 
 
diff --git a/src/util.cs b/src/util.cs
index 5eb1970da..85350777c 100644
--- a/src/util.cs
+++ b/src/util.cs
@@ -2535,6 +2535,7 @@ public class UtilCopy
    for(int i = 0; i <= 7; i++)
      LogB.Information(new SessionLoadDisplay(i).ToString());
 */
+//see SessionLoadDisplay (right now limited to 3 bits)
 public class BooleansInt
 {
        protected int i;
@@ -2601,3 +2602,69 @@ public class BooleansInt
                                i, Bit1, Bit2, Bit3);
        }
 }
+
+/*
+   Used for SQL store (non limited to 3 bits)
+   works two sided
+   See Test below to know its usage
+   */
+public class ConvertBooleansInt
+{
+       public ConvertBooleansInt ()
+       {
+       }
+
+       public int GetInt (List<bool> bool_l)
+       {
+               int intValue = 0;
+
+               for (int i = 0 ; i < bool_l.Count; i ++)
+                       if(bool_l[i])
+                               intValue += Convert.ToInt32(Math.Pow(2, bool_l.Count -1 - i));
+
+               return intValue;
+       }
+
+       //adapted from https://stackoverflow.com/a/49418086
+       public List<bool> GetBooleans (int intValue, int sizeBool_l)
+       {
+               List<bool> bool_l = new List<bool>();
+               int pow = 2 * sizeBool_l;
+
+               for (var i = 0; i < sizeBool_l; ++i)
+               {
+                       bool_l.Add(intValue > 0 ? (intValue & pow) != 0 : (intValue & pow) == 0);
+                       pow /= 2;
+               }
+
+               return bool_l;
+       }
+
+       public string PrintBooleans(List<bool> bool_l)
+       {
+               string s = "";
+               string sep = "";
+
+               foreach(bool b in bool_l)
+               {
+                       s += sep + b.ToString();
+                       sep = ", ";
+               }
+
+               return s;
+       }
+
+       public static void Test()
+       {
+               ConvertBooleansInt cbi = new ConvertBooleansInt();
+               List<bool> bool_l = new List<bool> { true, true, false, true}; //13
+
+               LogB.Information(string.Format("ConvertBooleansInt for values: {0} is: {1}",
+                                       cbi.PrintBooleans(bool_l), cbi.GetInt(bool_l) ));
+
+               int i = 13;
+               int iSize = 4; //4 booleans
+               LogB.Information(string.Format("ConvertBooleansInt for int: {0} is: {1}",
+                                       i, cbi.PrintBooleans(cbi.GetBooleans(i, iSize)) ));
+       }
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]