### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: convert from c to pascal vector of booleans  (Read 718 times)

#### mika

• Jr. Member
• Posts: 77
##### convert from c to pascal vector of booleans
« on: November 26, 2022, 10:14:13 pm »
I have this code in c and want to convert it to Pascal
Code: Pascal  [Select][+][-]
1. const std::vector<std::vector<bool>> Duplication1 =
2. {
3.                 { true, false }, // 2
4.                 { true, true }, // 3
5.                 { false, false, false }, // 4
6.                 { false, false, true, false } // 5
7. }
« Last Edit: November 27, 2022, 04:00:53 pm by mika »

#### marcov

• Administrator
• Hero Member
• Posts: 10498
• FPC developer.
##### Re: covert from c to pascal vector of booleans
« Reply #1 on: November 26, 2022, 10:25:56 pm »
That code doesn't look like C at all, but like C++.

#### mika

• Jr. Member
• Posts: 77
##### Re: covert from c to pascal vector of booleans
« Reply #2 on: November 26, 2022, 11:06:29 pm »
That code doesn't look like C at all, but like C++.
Exactly, i can't tell C and C++ apart. I need some help.

#### 440bx

• Hero Member
• Posts: 3271
##### Re: covert from c to pascal vector of booleans
« Reply #3 on: November 26, 2022, 11:24:19 pm »
Exactly, i can't tell C and C++ apart. I need some help.
if you intend to port C and C++ code to Pascal, you'll need at least a basic understanding of C and C++.

For a Pascal programmer, a very _basic_ understanding of C can be obtained in a few hours.  For C++ it's a different story.  Even a very basic understanding will probably require 3 to 4 days.

Pluralsight has a very good introduction to C++ given by Kate Gregory called "C++ fundamentals including C++ 17" and a follow up by the same author named "C++ beyond the basics".  An experienced programmer can go through both courses in about a day.  Of course, in one day, you won't be an expert but, you'll know enough to make sense of a fair amount of code.

HTH.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 SP1 64bit.

#### ASerge

• Hero Member
• Posts: 2049
##### Re: covert from c to pascal vector of booleans
« Reply #4 on: November 26, 2022, 11:38:43 pm »
I have this code in c and want to convert it to Pascal
Code: Pascal  [Select][+][-]
1. {\$APPTYPE CONSOLE}
2. {\$MODE OBJFPC}
3.
4. type
5.   TBoolVector = bitpacked record
6.     b0, b1, b2, b3, b4, b5, b6, b7: Boolean;
7.   end;
8.
9. {\$WARN 3177 off : Some fields coming after "\$1" were not initialized}
10. const
11.   DuplicationV: array of TBoolVector = (
12.     (b0:False; b1:True), // 2
13.     (b0:True;  b1:True), // 3
14.     (b0:False; b1:False; b2:True), // 4
15.     (b0:True;  b1:False; b2:True; b3:False)); // 5
16.
17. var
18.   V: TBoolVector;
19. begin
20.   Writeln(SizeOf(V));
21.   for V in DuplicationV do
22.     Write(Byte(V), ' ');
23.   Writeln;
24.   Readln;
25. end.

#### mika

• Jr. Member
• Posts: 77
##### Re: covert from c to pascal vector of booleans
« Reply #5 on: November 26, 2022, 11:58:18 pm »
@ASerge, I like your suggestion, I will use it.

#### 440bx

• Hero Member
• Posts: 3271
##### Re: covert from c to pascal vector of booleans
« Reply #6 on: November 27, 2022, 12:05:11 am »
@Serge,

Any particular reason why you didn't use a bitpacked array of boolean, which is closer to a C++ vector of bool than a bitpacked record of booleans ?
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 SP1 64bit.

#### jamie

• Hero Member
• Posts: 5076
##### Re: covert from c to pascal vector of booleans
« Reply #7 on: November 27, 2022, 12:26:06 am »
of course, the C++ is most likely relying on Template code so I would assume that maybe in the end if a working copy is going to happen then maybe some Generics is in order?
The only true wisdom is knowing you know nothing

#### mika

• Jr. Member
• Posts: 77
##### Re: covert from c to pascal vector of booleans
« Reply #8 on: November 27, 2022, 12:57:57 am »
Any particular reason why you didn't use a bitpacked array of boolean, which is closer to a C++ vector of bool than a bitpacked record of booleans ?
It's all fine. I don't have to mimic internals structure of vector. Vector is used to build a tree. "true" for right branch, "false" for left branch. All program is only 400+ lines.

#### ASerge

• Hero Member
• Posts: 2049
##### Re: covert from c to pascal vector of booleans
« Reply #9 on: November 27, 2022, 10:14:15 am »
Any particular reason why you didn't use a bitpacked array of boolean, which is closer to a C++ vector of bool than a bitpacked record of booleans ?
The record supports partial initialization, the array requires full.

#### dje

• Full Member
• Posts: 134
##### Re: covert from c to pascal vector of booleans
« Reply #10 on: November 27, 2022, 11:01:43 am »
Code: Pascal  [Select][+][-]
1. const
2.
3.   Duplication1: array of array of boolean = (
4.     (True, False), // 2
5.     (True, True),  // 3
6.     (False, False, False), // 4
7.     (False, False, True, False) // 5
8.     );

#### ASerge

• Hero Member
• Posts: 2049
##### Re: covert from c to pascal vector of booleans
« Reply #11 on: November 27, 2022, 11:28:24 am »
Code: Pascal  [Select][+][-]
1. const
2.
3.   Duplication1: array of array of boolean = (
4.     (True, False), // 2
5.     (True, True),  // 3
6.     (False, False, False), // 4
7.     (False, False, True, False) // 5
8.     );
Please provide the full program. It should print 2, 3, 4, 5 from this array.

#### 440bx

• Hero Member
• Posts: 3271
##### Re: covert from c to pascal vector of booleans
« Reply #12 on: November 27, 2022, 01:01:51 pm »
The record supports partial initialization, the array requires full.
Hadn't thought about that.  Thank you.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 SP1 64bit.

#### mika

• Jr. Member
• Posts: 77
##### Re: covert from c to pascal vector of booleans
« Reply #13 on: November 27, 2022, 04:05:41 pm »
Code: Pascal  [Select][+][-]
1. const
2.
3.   Duplication1: array of array of boolean = (
4.     (True, False), // 2
5.     (True, True),  // 3
6.     (False, False, False), // 4
7.     (False, False, True, False) // 5
8.     );
Please provide the full program. It should print 2, 3, 4, 5 from this array.
This is even better solution.
@ASerge Sorry, numbers in comments are misleading and have nothing to do with boolean values.
« Last Edit: November 27, 2022, 09:02:40 pm by mika »

#### Thaddy

• Hero Member
• Posts: 12642
##### Re: convert from c to pascal vector of booleans
« Reply #14 on: November 27, 2022, 08:03:18 pm »
Alternatively you can simply use logic. e.g.
Code: Pascal  [Select][+][-]
1. uses sysutils;
2. var
3.   m:cardinal = %00000000000000000;
4. begin
5.   m.setbit(1); // should print 2
6.   writeln(m);
7.   writeln (m.testbit(0)); // should print false;
8. end.

It seems to me that a vector type is not warranted here. C++ programmers tend to make even more mistakes than Pascal programmers.
« Last Edit: November 27, 2022, 08:05:14 pm by Thaddy »
"Looks" OK, doesn't mean it is IS OK.