コード例 #1
0
        public BatteryBalancer(string values, int cellsTotal)
        {
            Cells       = new List <BatteryCell>();
            _totalValue = 0;
            var valArray = values.Split('\n');

            if (valArray.Length % cellsTotal != 0)
            {
                throw new ArgumentOutOfRangeException("The total number of values must be divisible by the number of cells total");
            }

            _totalCells = cellsTotal;
            var totalBatteriesInEachCell = valArray.Length / cellsTotal;

            var bufferBatteryCell = new BatteryCell();

            for (int i = 0; i < valArray.Length; i++)
            {
                bufferBatteryCell.Add(i + 1, valArray[i]);

                if ((i + 1) % totalBatteriesInEachCell == 0)
                {
                    _totalValue += bufferBatteryCell.TotalValue;
                    Cells.Add(bufferBatteryCell);
                    bufferBatteryCell = new BatteryCell();
                }
            }

            _targetCellValue = _totalValue / _totalCells;
        }
コード例 #2
0
        private void swap(BatteryCell sourceCell, int sourceIndex, BatteryCell targetCell, int targetIndex, bool useActIndex)
        {
            var sourceBatteryValue = sourceCell[sourceIndex];
            var targetBatteryValue = targetCell[targetIndex];

            sourceCell.RemoveAt(sourceIndex);
            targetCell.RemoveAt(targetIndex);

            sourceCell.Add(targetBatteryValue);
            targetCell.Add(sourceBatteryValue);
        }
コード例 #3
0
        private void swap(BatteryCell sourceCell, int sourceIndex, BatteryCell targetCell, int targetIndex)
        {
            var sourceBatteryValue = sourceCell.GetWhereIndex(sourceIndex);
            var targetBatteryValue = targetCell.GetWhereIndex(targetIndex);

            sourceCell.RemoveWhereIndex(sourceIndex);
            targetCell.RemoveWhereIndex(targetIndex);

            sourceCell.Add(targetBatteryValue);
            targetCell.Add(sourceBatteryValue);
        }