unlab-gpu 0.1.0

Micro scripting language for neural networks that uses unmtx-gpu.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
//
// Copyright (c) 2025-2026 Łukasz Szpakowski
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
//! A plotting module.
use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::error;
use std::fmt;
use std::mem::size_of;
use std::mem::transmute;
use std::num::NonZeroU32;
use std::ops::Range;
use std::path::PathBuf;
use std::result;
use std::slice;
use std::sync::mpsc::Sender;
use std::sync::mpsc::channel;
use std::sync::Arc;
use plotters::backend::BGRXPixel;
use plotters::drawing::IntoDrawingArea;
use plotters::element::DashedPathElement;
use plotters::element::DottedPathElement;
use plotters::prelude::*;
use softbuffer::Context;
use softbuffer::Surface;
use crate::winit::application::ApplicationHandler;
use crate::winit::dpi::PhysicalSize;
use crate::winit::event::WindowEvent;
use crate::winit::event_loop::ActiveEventLoop;
use crate::winit::event_loop::EventLoop;
use crate::winit::raw_window_handle::DisplayHandle;
use crate::winit::raw_window_handle::HasDisplayHandle;
use crate::winit::window::Icon;
use crate::winit::window::Window;
use crate::env::*;
use crate::error::*;
use crate::interp::*;
use crate::utils::*;
use crate::value::*;

/// A structure of floating-point number key.
///
/// The floating-point number is used by surfaces which are drawn.
#[derive(Copy, Clone, Debug)]
pub struct F32Key
{
    value: f32,
}

impl F32Key
{
    /// Creates a floating-point number key.
    pub fn new(value: f32) -> Self
    { F32Key { value, } }
    
    /// Retursn the floating-point number.
    pub fn to_f32(&self) -> f32
    { self.value }
    
    /// Converts the floating-point number to a key floating-point number.
    ///
    /// This method retuns negated infinity if the floating-point number is NaN, otherwise the
    /// floating-point number.
    pub fn to_key_f32(&self) -> f32
    {
        if !self.value.is_nan() {
            self.value
        } else {
            -f32::INFINITY
        }
    }
}

impl Eq for F32Key
{}

impl PartialEq for F32Key
{
    fn eq(&self, other: &Self) -> bool
    { self.to_key_f32() == other.to_key_f32() }
}

impl Ord for F32Key
{
    fn cmp(&self, other: &Self) -> Ordering
    { self.to_key_f32().partial_cmp(&other.to_key_f32()).unwrap() }
}

impl PartialOrd for F32Key
{
    fn partial_cmp(&self, other: &Self) -> Option<Ordering>
    { Some(self.cmp(other)) }
}

/// A chart structure.
#[derive(Clone, Debug)]
pub struct Chart
{
    /// A chart title.
    pub title: Option<String>,
    /// A window identfier.
    pub window_id: Option<WindowId>,
    /// If this field is `true`, chart can be shown on the window.
    pub has_window: bool,
    /// A path to a chart file.
    pub file: Option<String>,
    /// A chart size.
    pub size: Option<(u32, u32)>,
}

/// A structure of axes 2D. 
#[derive(Clone, Debug)]
pub struct Axes2d
{
    /// A X axis.
    pub x: Range<f32>,
    /// An Y axis.
    pub y: Range<f32>,
}

/// A structure of axes 3D. 
#[derive(Clone, Debug)]
pub struct Axes3d
{
    /// A X axis.
    pub x: Range<f32>,
    /// An Y axis.
    pub y: Range<f32>,
    /// A Z axis.
    pub z: Range<f32>,
}

/// A structure of histogram value.
#[derive(Clone, Debug)]
pub enum HistogramValue
{
    /// A boolean value.
    Bool(bool),
    /// An integer number.
    Int(i64),
    /// A floating-point number.
    Float(f32),
    /// A string.
    String(Box<String>),
}

impl HistogramValue
{
    /// Converts any value to a boolean value.
    pub fn to_bool(&self) -> bool
    {
        match self {
            HistogramValue::Bool(b) => *b,
            HistogramValue::Int(n) => *n != 0,
            HistogramValue::Float(n) => *n != 0.0,
            HistogramValue::String(_) => true,
        }
    }

    /// Converts any value to an integer number.
    pub fn to_i64(&self) -> i64
    {
        match self {
            HistogramValue::Bool(b) => if *b { 1 } else { 0 },
            HistogramValue::Int(n) => *n,
            HistogramValue::Float(n) => *n as i64,
            HistogramValue::String(_) => 1,
        }
    }

    /// Converts any value to a floating-point number.
    pub fn to_f32(&self) -> f32
    {
        match self {
            HistogramValue::Bool(b) => if *b { 1.0 } else { 0.0 },
            HistogramValue::Int(n) => *n as f32,
            HistogramValue::Float(n) => *n,
            HistogramValue::String(_) => 1.0,
        }
    }

    /// Converts the value to a boolean value if the value is an boolean type, otherwise this
    /// method returns `None`.
    pub fn to_opt_bool(&self) -> Option<bool>
    {
        match self {
            HistogramValue::Bool(b) => Some(*b),
            _ => None,
        }
    }

    /// Converts the value to an integer number if the value is an integer type or floating-point
    /// type, otherwise this method returns `None`.
    pub fn to_opt_i64(&self) -> Option<i64>
    {
        match self {
            HistogramValue::Int(n) => Some(*n),
            HistogramValue::Float(n) => Some(*n as i64),
            _ => None,
        }
    }

    /// Converts the value to a floating-point number if the value is an integer type or
    /// floating-point type, otherwise this method returns `None`.
    pub fn to_opt_f32(&self) -> Option<f32>
    {
        match self {
            HistogramValue::Int(n) => Some(*n as f32),
            HistogramValue::Float(n) => Some(*n),
            _ => None,
        }
    }

    /// Converts the value to a string if the value is a string type, otherwise this method
    /// returns `None`.
    pub fn to_opt_string(&self) -> Option<String>
    {
        match self {
            HistogramValue::String(s) => Some((**s).clone()),
            _ => None,
        }
    }
}

impl PartialEq for HistogramValue
{
    fn eq(&self, other: &Self) -> bool
    {
        match (self, other) {
            (HistogramValue::Bool(b), HistogramValue::Bool(b2)) => b == b2,
            (HistogramValue::Int(n), HistogramValue::Int(n2)) => n == n2,
            (HistogramValue::Int(_) | HistogramValue::Float(_), HistogramValue::Int(_) | HistogramValue::Float(_)) => self.to_f32() == other.to_f32(),
            (HistogramValue::String(s), HistogramValue::String(s2)) => s == s2, 
            (_, _) => false,
        }
    }
}

impl fmt::Display for HistogramValue
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
    {
        match self {
            HistogramValue::Bool(b) => write!(f, "{}", b),
            HistogramValue::Int(n) => write!(f, "{}", n),
            HistogramValue::Float(n) => write!(f, "{}", n),
            HistogramValue::String(s) => write!(f, "{}", *s),
        }
    }
}

/// A structure of histogram axes.
#[derive(Clone, Debug)]
pub struct HistogramAxes
{
    /// A X axis.
    pub x: Vec<HistogramValue>,
    /// An Y axis.
    pub y: Range<usize>,
}

/// An enumeration of series 2D.
#[derive(Clone, Debug)]
pub enum Series2d
{
    /// A line series.
    Line(Vec<f32>, Vec<f32>, RGBColor, Option<String>),
    /// A dashed line series.
    DashedLine(Vec<f32>, Vec<f32>, RGBColor, Option<String>),
    /// A dotted line series.
    DottedLine(Vec<f32>, Vec<f32>, RGBColor, Option<String>),
    /// A circle series.
    Circle(Vec<f32>, Vec<f32>, RGBColor, Option<String>),
    /// A cross series.
    Cross(Vec<f32>, Vec<f32>, RGBColor, Option<String>),
    /// A point series.
    Point(Vec<f32>, Vec<f32>, RGBColor, Option<String>),
    /// A triagle series.
    Triangle(Vec<f32>, Vec<f32>, RGBColor, Option<String>),
}

/// An enumeration of series 3D.
#[derive(Clone, Debug)]
pub enum Series3d
{
    /// A line series.
    Line(Vec<f32>, Vec<f32>, Vec<f32>, RGBColor, Option<String>),
    /// A dashed line series.
    DashedLine(Vec<f32>, Vec<f32>, Vec<f32>, RGBColor, Option<String>),
    /// A dotted line series.
    DottedLine(Vec<f32>, Vec<f32>, Vec<f32>, RGBColor, Option<String>),
    /// A circle series.
    Circle(Vec<f32>, Vec<f32>, Vec<f32>, RGBColor, Option<String>),
    /// A cross series.
    Cross(Vec<f32>, Vec<f32>, Vec<f32>, RGBColor, Option<String>),
    /// A point series.
    Point(Vec<f32>, Vec<f32>, Vec<f32>, RGBColor, Option<String>),
    /// A triagle series.
    Triangle(Vec<f32>, Vec<f32>, Vec<f32>, RGBColor, Option<String>),
    /// A surface on a X axis and an Y axis.
    XYSurface(Vec<f32>, Vec<f32>, Vec<f32>, RGBColor, Option<String>, BTreeMap<F32Key, usize>, BTreeMap<F32Key, usize>),
    /// A surface on a X axis and a Z axis.
    XZSurface(Vec<f32>, Vec<f32>, Vec<f32>, RGBColor, Option<String>, BTreeMap<F32Key, usize>, BTreeMap<F32Key, usize>),
    /// A surface on an Y axis and a Z axis.
    YZSurface(Vec<f32>, Vec<f32>, Vec<f32>, RGBColor, Option<String>, BTreeMap<F32Key, usize>, BTreeMap<F32Key, usize>),
}

/// A structure of histogram series.
#[derive(Clone, Debug)]
pub struct HistogramSeries(pub Vec<HistogramValue>, pub RGBColor, pub Option<String>);

const DEFAULT_SIZE: (u32, u32) = (640, 480);

const TITLE_FONT_SIZE: i32 = 40;
const MARGIN: i32 = 10;
const X_LABEL_AREA_SIZE: i32 = 30;
const Y_LABEL_AREA_SIZE: i32 = 60;

const LEGEND_WIDTH: i32 = 20;
const LEGEND_HEIGHT: i32 = 10;

const DASH_SIZE: i32 = 8;
const DASH_SPACING: i32 = 2;
const DOT_SHIFT: i32 = 0;
const DOT_SPACING: i32 = 4;
const MARKER_SIZE: i32 = 4;
const POINT_SIZE: i32 = 1;

const SURFACE_MIX: f64 = 0.2;
const HISTOGRAM_MIX: f64 = 0.6;

static COLORS: [RGBColor; 6] = [RED, BLUE, GREEN, CYAN, YELLOW, MAGENTA];

fn draw_chart2d<T: IntoDrawingArea>(backend: T, chart_desc: &Chart, axes: &Axes2d, serieses: &[Series2d]) -> result::Result<(), Box<dyn error::Error>>
    where T::ErrorType: 'static
{
    let root = backend.into_drawing_area();
    root.fill(&WHITE)?;
    let mut chart_builder = ChartBuilder::on(&root);
    match &chart_desc.title {
        Some(title) => {
            chart_builder.caption(title, ("sans-serif", TITLE_FONT_SIZE).into_font());
        },
        None => (),
    }
    let mut chart = chart_builder
        .margin(MARGIN)
        .x_label_area_size(X_LABEL_AREA_SIZE)
        .y_label_area_size(Y_LABEL_AREA_SIZE)
        .build_cartesian_2d(axes.x.clone(), axes.y.clone())?;
    chart.configure_mesh().draw()?;
    for series in serieses {
        match series {
            Series2d::Line(xs, ys, color, label) => {
                let series_anno = chart.draw_series(LineSeries::new(xs.iter().zip(ys.iter()).map(|(x, y)| (*x, *y)), *color))?;
                let color2 = *color;
                match label {
                    Some(label) => series_anno.label(label.as_str()),
                    None => series_anno,
                }.legend(move |(x, y)| PathElement::new(vec![(x, y), (x + LEGEND_WIDTH, y)], &color2));
            },
            Series2d::DashedLine(xs, ys, color, label) => {
                let series_anno = chart.draw_series(DashedLineSeries::new(xs.iter().zip(ys.iter()).map(|(x, y)| (*x, *y)), DASH_SIZE, DASH_SPACING, Into::<ShapeStyle>::into(color)))?;
                let color2 = *color;
                match label {
                    Some(label) => series_anno.label(label.as_str()),
                    None => series_anno,
                }.legend(move |(x, y)| DashedPathElement::new(vec![(x, y), (x + LEGEND_WIDTH, y)], DASH_SIZE, DASH_SPACING, Into::<ShapeStyle>::into(&color2)));
            },
            Series2d::DottedLine(xs, ys, color, label) => {
                let color2 = *color;
                let series_anno = chart.draw_series(DottedLineSeries::new(xs.iter().zip(ys.iter()).map(|(x, y)| (*x, *y)), DOT_SHIFT, DOT_SPACING, move |p| Circle::new(p, POINT_SIZE, Into::<ShapeStyle>::into(&color2).filled())))?;
                let color3 = *color;
                match label {
                    Some(label) => series_anno.label(label.as_str()),
                    None => series_anno,
                }.legend(move |(x, y)| DottedPathElement::new(vec![(x, y), (x + LEGEND_WIDTH, y)], DOT_SHIFT, DOT_SPACING, move |p| Circle::new(p, POINT_SIZE, Into::<ShapeStyle>::into(&color3).filled())));
            },
            Series2d::Circle(xs, ys, color, label) => {
                let series_anno = chart.draw_series(PointSeries::<_, _, Circle<_, i32>, i32>::new(xs.iter().zip(ys.iter()).map(|(x, y)| (*x, *y)), MARKER_SIZE, color))?;
                let color2 = *color;
                match label {
                    Some(label) => series_anno.label(label.as_str()),
                    None => series_anno,
                }.legend(move |(x, y)| Circle::new((x + LEGEND_WIDTH / 2, y), MARKER_SIZE, &color2));
            },
            Series2d::Cross(xs, ys, color, label) => {
                let series_anno = chart.draw_series(PointSeries::<_, _, Cross<_, i32>, i32>::new(xs.iter().zip(ys.iter()).map(|(x, y)| (*x, *y)), MARKER_SIZE, color))?;
                let color2 = *color;
                match label {
                    Some(label) => series_anno.label(label.as_str()),
                    None => series_anno,
                }.legend(move |(x, y)| Cross::new((x + LEGEND_WIDTH / 2, y), MARKER_SIZE, &color2));
            },
            Series2d::Point(xs, ys, color, label) => {
                let series_anno = chart.draw_series(PointSeries::<_, _, Circle<_, i32>, i32>::new(xs.iter().zip(ys.iter()).map(|(x, y)| (*x, *y)), POINT_SIZE, color.filled()))?;
                let color2 = *color;
                match label {
                    Some(label) => series_anno.label(label.as_str()),
                    None => series_anno,
                }.legend(move |(x, y)| Circle::new((x + LEGEND_WIDTH / 2, y), POINT_SIZE, color2.filled()));
            },
            Series2d::Triangle(xs, ys, color, label) => {
                let series_anno = chart.draw_series(PointSeries::<_, _, TriangleMarker<_, i32>, i32>::new(xs.iter().zip(ys.iter()).map(|(x, y)| (*x, *y)), MARKER_SIZE, color))?;
                let color2 = *color;
                match label {
                    Some(label) => series_anno.label(label.as_str()),
                    None => series_anno,
                }.legend(move |(x, y)| TriangleMarker::new((x + LEGEND_WIDTH / 2, y), MARKER_SIZE, &color2));
            },
        }
    }
    chart
        .configure_series_labels()
        .background_style(&WHITE)
        .border_style(&BLACK)
        .draw()?;
    root.present()?;
    Ok(())
}

fn draw_chart3d<T: IntoDrawingArea>(backend: T, chart_desc: &Chart, axes: &Axes3d, serieses: &[Series3d]) -> result::Result<(), Box<dyn error::Error>>
    where T::ErrorType: 'static
{
    let root = backend.into_drawing_area();
    root.fill(&WHITE)?;
    let mut chart_builder = ChartBuilder::on(&root);
    match &chart_desc.title {
        Some(title) => {
            chart_builder.caption(title, ("sans-serif", TITLE_FONT_SIZE).into_font());
        },
        None => (),
    }
    let mut chart = chart_builder
        .margin(MARGIN)
        .build_cartesian_3d(axes.x.clone(), axes.y.clone(), axes.z.clone())?;
    chart.configure_axes().draw()?;
    for series in serieses {
        match series {
            Series3d::Line(xs, ys, zs, color, label) => {
                let series_anno = chart.draw_series(LineSeries::new(xs.iter().zip(ys.iter()).zip(zs.iter()).map(|((x, y), z)| (*x, *y, *z)), *color))?;
                let color2 = *color;
                match label {
                    Some(label) => series_anno.label(label.as_str()),
                    None => series_anno,
                }.legend(move |(x, y)| PathElement::new(vec![(x, y), (x + LEGEND_WIDTH, y)], &color2));
            },
            Series3d::DashedLine(xs, ys, zs, color, label) => {
                let series_anno = chart.draw_series(DashedLineSeries::new(xs.iter().zip(ys.iter()).zip(zs.iter()).map(|((x, y), z)| (*x, *y, *z)), DASH_SIZE, DASH_SPACING, Into::<ShapeStyle>::into(color)))?;
                let color2 = *color;
                match label {
                    Some(label) => series_anno.label(label.as_str()),
                    None => series_anno,
                }.legend(move |(x, y)| DashedPathElement::new(vec![(x, y), (x + LEGEND_WIDTH, y)], DASH_SIZE, DASH_SPACING, Into::<ShapeStyle>::into(&color2)));
            },
            Series3d::DottedLine(xs, ys, zs, color, label) => {
                let color2 = *color;
                let series_anno = chart.draw_series(DottedLineSeries::new(xs.iter().zip(ys.iter()).zip(zs.iter()).map(|((x, y), z)| (*x, *y, *z)), DOT_SHIFT, DOT_SPACING, move |p| Circle::new(p, POINT_SIZE, Into::<ShapeStyle>::into(&color2).filled())))?;
                let color3 = *color;
                match label {
                    Some(label) => series_anno.label(label.as_str()),
                    None => series_anno,
                }.legend(move |(x, y)| DottedPathElement::new(vec![(x, y), (x + LEGEND_WIDTH, y)], DOT_SHIFT, DOT_SPACING, move |p| Circle::new(p, POINT_SIZE, Into::<ShapeStyle>::into(&color3).filled())));
            },
            Series3d::Circle(xs, ys, zs, color, label) => {
                let series_anno = chart.draw_series(PointSeries::<_, _, Circle<_, i32>, i32>::new(xs.iter().zip(ys.iter()).zip(zs.iter()).map(|((x, y), z)| (*x, *y, *z)), MARKER_SIZE, color))?;
                let color2 = *color;
                match label {
                    Some(label) => series_anno.label(label.as_str()),
                    None => series_anno,
                }.legend(move |(x, y)| Circle::new((x + LEGEND_WIDTH / 2, y), MARKER_SIZE, &color2));
            },
            Series3d::Cross(xs, ys, zs, color, label) => {
                let series_anno = chart.draw_series(PointSeries::<_, _, Cross<_, i32>, i32>::new(xs.iter().zip(ys.iter()).zip(zs.iter()).map(|((x, y), z)| (*x, *y, *z)), MARKER_SIZE, color))?;
                let color2 = *color;
                match label {
                    Some(label) => series_anno.label(label.as_str()),
                    None => series_anno,
                }.legend(move |(x, y)| Cross::new((x + LEGEND_WIDTH / 2, y), MARKER_SIZE, &color2));
            },
            Series3d::Point(xs, ys, zs, color, label) => {
                let series_anno = chart.draw_series(PointSeries::<_, _, Circle<_, i32>, i32>::new(xs.iter().zip(ys.iter()).zip(zs.iter()).map(|((x, y), z)| (*x, *y, *z)), POINT_SIZE, color.filled()))?;
                let color2 = *color;
                match label {
                    Some(label) => series_anno.label(label.as_str()),
                    None => series_anno,
                }.legend(move |(x, y)| Circle::new((x + LEGEND_WIDTH / 2, y), POINT_SIZE, color2.filled()));
            },
            Series3d::Triangle(xs, ys, zs, color, label) => {
                let series_anno = chart.draw_series(PointSeries::<_, _, TriangleMarker<_, i32>, i32>::new(xs.iter().zip(ys.iter()).zip(zs.iter()).map(|((x, y), z)| (*x, *y, *z)), MARKER_SIZE, color))?;
                let color2 = *color;
                match label {
                    Some(label) => series_anno.label(label.as_str()),
                    None => series_anno,
                }.legend(move |(x, y)| TriangleMarker::new((x + LEGEND_WIDTH / 2, y), MARKER_SIZE, &color2));
            },
            Series3d::XYSurface(xs, ys, zs, color, label, xis, yis) => {
                let series_anno = chart.draw_series(SurfaceSeries::xoy(xs.iter().map(|x| *x), ys.iter().map(|y| *y), |x, y| {
                        let xi = xis.get(&F32Key::new(x));
                        let yi = yis.get(&F32Key::new(y));
                        match (xi, yi) {
                            (Some(xi), Some(yi)) => zs.get(yi * xs.len() + xi).map(|z| *z).unwrap_or(0.0),
                            (_, _) => 0.0,
                        }
                }).style(color.mix(SURFACE_MIX).filled()))?;
                let color2 = *color;
                match label {
                    Some(label) => series_anno.label(label.as_str()),
                    None => series_anno,
                }.legend(move |(x, y)| Rectangle::new([(x, y - LEGEND_HEIGHT / 2), (x + LEGEND_WIDTH, y + LEGEND_HEIGHT / 2)], color2.mix(SURFACE_MIX).filled()));
            },
            Series3d::XZSurface(xs, ys, zs, color, label, xis, zis) => {
                let series_anno = chart.draw_series(SurfaceSeries::xoz(xs.iter().map(|x| *x), zs.iter().map(|z| *z), |x, z| {
                        let xi = xis.get(&F32Key::new(x));
                        let zi = zis.get(&F32Key::new(z));
                        match (xi, zi) {
                            (Some(xi), Some(zi)) => ys.get(zi * xs.len() + xi).map(|y| *y).unwrap_or(0.0),
                            (_, _) => 0.0,
                        }
                }).style(color.mix(SURFACE_MIX).filled()))?;
                let color2 = *color;
                match label {
                    Some(label) => series_anno.label(label.as_str()),
                    None => series_anno,
                }.legend(move |(x, y)| Rectangle::new([(x, y - LEGEND_HEIGHT / 2), (x + LEGEND_WIDTH, y + LEGEND_HEIGHT / 2)], color2.mix(SURFACE_MIX).filled()));
            },
            Series3d::YZSurface(xs, ys, zs, color, label, yis, zis) => {
                let series_anno = chart.draw_series(SurfaceSeries::yoz(ys.iter().map(|y| *y), zs.iter().map(|z| *z), |y, z| {
                        let yi = yis.get(&F32Key::new(y));
                        let zi = zis.get(&F32Key::new(z));
                        match (yi, zi) {
                            (Some(yi), Some(zi)) => xs.get(zi * ys.len() + yi).map(|x| *x).unwrap_or(0.0),
                            (_, _) => 0.0,
                        }
                }).style(color.mix(SURFACE_MIX).filled()))?;
                let color2 = *color;
                match label {
                    Some(label) => series_anno.label(label.as_str()),
                    None => series_anno,
                }.legend(move |(x, y)| Rectangle::new([(x, y - LEGEND_HEIGHT / 2), (x + LEGEND_WIDTH, y + LEGEND_HEIGHT / 2)], color2.mix(SURFACE_MIX).filled()));
            },
        }
    }
    chart
        .configure_series_labels()
        .background_style(&WHITE)
        .border_style(&BLACK)
        .draw()?;
    root.present()?;
    Ok(())
}

fn draw_histogram<T: IntoDrawingArea>(backend: T, chart_desc: &Chart, axes: &HistogramAxes, serieses: &[HistogramSeries]) -> result::Result<(), Box<dyn error::Error>>
    where T::ErrorType: 'static
{
    let root = backend.into_drawing_area();
    root.fill(&WHITE)?;
    let mut chart_builder = ChartBuilder::on(&root);
    match &chart_desc.title {
        Some(title) => {
            chart_builder.caption(title, ("sans-serif", TITLE_FONT_SIZE).into_font());
        },
        None => (),
    }
    let mut chart = chart_builder
        .margin(MARGIN)
        .x_label_area_size(X_LABEL_AREA_SIZE)
        .y_label_area_size(Y_LABEL_AREA_SIZE)
        .build_cartesian_2d(axes.x.as_slice().into_segmented(), axes.y.clone())?;
    chart
        .configure_mesh()
        .x_label_formatter(&|x| {
                match x {
                    SegmentValue::Exact(x) => format!("{}", x),
                    SegmentValue::CenterOf(x) => format!("{}", x),
                    SegmentValue::Last => format!("last"),
                }
        }).draw()?;
    for series in serieses {
        match series {
            HistogramSeries(data, color, label) => {
                let series_anno = chart.draw_series(Histogram::vertical(&chart).style(color.mix(HISTOGRAM_MIX).filled()).data(data.iter().map(|v| (v, 1))))?;
                let color2 = *color;
                match label {
                    Some(label) => series_anno.label(label.as_str()),
                    None => series_anno,
                }.legend(move |(x, y)| Rectangle::new([(x, y - LEGEND_HEIGHT / 2), (x + LEGEND_WIDTH, y + LEGEND_HEIGHT / 2)], color2.mix(HISTOGRAM_MIX).filled()));
            },
        }
    }
    chart
        .configure_series_labels()
        .background_style(&WHITE)
        .border_style(&BLACK)
        .draw()?;
    root.present()?;
    Ok(())
}

/// A structure of plotting.
///
/// The plotting contains chart, axes, and series.
#[derive(Clone, Debug)]
pub enum Plot
{
    /// A plotting for a `plot` built-in function.
    Plot(Chart, Axes2d, Vec<Series2d>),
    /// A plotting for a `plot3` built-in function.
    Plot3(Chart, Axes3d, Vec<Series3d>),
    /// A plotting for a `histogram` built-in function.
    Histogram(Chart, HistogramAxes, Vec<HistogramSeries>),
}

impl Plot
{
    /// Returns the chart.
    pub fn chart(&self) -> &Chart
    {
        match self {
            Plot::Plot(chart, _, _) => chart,
            Plot::Plot3(chart, _, _) => chart,
            Plot::Histogram(chart, _, _) => chart,
        }
    }
    
    fn draw_with_backend<T: IntoDrawingArea>(&self, backend: T) -> result::Result<(), Box<dyn error::Error>>
        where T::ErrorType: 'static
    {
        match self {
            Plot::Plot(chart, axes, serieses) => draw_chart2d(backend, chart, axes, serieses.as_slice()),
            Plot::Plot3(chart, axes, serieses) => draw_chart3d(backend, chart, axes, serieses.as_slice()),
            Plot::Histogram(chart, axes, serieses) => draw_histogram(backend, chart, axes, serieses.as_slice()),
        }
    }

    /// Draws and saves chart to the file.
    pub fn draw_and_save_to_file(&self) -> result::Result<(), Box<dyn error::Error>>
    {
        match &self.chart().file {
            Some(file) => {
                let path_buf = PathBuf::from(file);
                let size = self.chart().size.unwrap_or(DEFAULT_SIZE);
                match path_buf.extension() {
                    Some(ext) if ext == "svg" => self.draw_with_backend(SVGBackend::new(path_buf.as_path(), size)),
                    _ => self.draw_with_backend(BitMapBackend::new(path_buf.as_path(), size)),
                }
            },
            None => Ok(()),
        }
    }
    
    /// Draws the chart on the buffer.
    pub fn draw_on_buffer(&self, buf: &mut [u8], size: (u32, u32)) -> result::Result<(), Box<dyn error::Error>>
    { self.draw_with_backend(BitMapBackend::<BGRXPixel>::with_buffer_and_format(buf, size)?) }

    /// Draws the chart on the window.
    pub fn draw_on_window(plot: &Arc<Self>, env: &Env) -> Result<Option<Option<WindowId>>>
    {
        if plot.chart().has_window {
            let shared_env_g = rw_lock_read(env.shared_env())?;
            match shared_env_g.event_loop_proxy() {
                Some(event_loop_proxy) => {
                    let (tx, rx) = channel();
                    match event_loop_proxy.send_event(PlotterAppEvent::Plot(plot.clone(), tx)) {
                        Ok(()) => (),
                        Err(err) => return Err(Error::Winit(Box::new(err))),
                    }
                    Ok(Some(receiver_recv(&rx)?))
                },
                None => Ok(Some(None)),
            }
        } else {
            Ok(None)
        }
    }
}

/// An enumeration of plotter application event.
///
/// The plotter application events are used by a thread of main loop to communication with a
/// thread of plotter application.
#[derive(Clone, Debug)]
pub enum PlotterAppEvent
{
    /// A plotting event.
    Plot(Arc<Plot>, Sender<Option<WindowId>>),
    /// A quit event
    Quit,
}

struct WindowState
{
    window: Arc<Window>,
    surface: Surface<DisplayHandle<'static>, Arc<Window>>,
    plot: Arc<Plot>,
    size: (u32, u32),
}

impl WindowState
{
    fn new(app: &PlotterApp, window: Arc<Window>, plot: Arc<Plot>, size: (u32, u32)) -> result::Result<WindowState, Box<dyn error::Error>>
    {
        let surface = Surface::new(app.context.as_ref().unwrap(), window.clone())?;
        Ok(WindowState { window, surface, plot, size })
    }
    
    fn resize(&mut self, size: PhysicalSize<u32>)
    {
        self.size = (size.width, size.height);
        match (NonZeroU32::new(size.width), NonZeroU32::new(size.height)) {
            (Some(width), Some(height)) => self.surface.resize(width, height).unwrap(),
            (_, _) => (),
        }
        self.window.request_redraw();
    }
    
    fn draw(&mut self) -> result::Result<(), Box<dyn error::Error>>
    {
        if self.size.0 > 0 && self.size.1 > 0 {
            let mut buf = self.surface.buffer_mut()?;
            let plot_buf: &mut [u8] = unsafe { slice::from_raw_parts_mut(buf.as_mut_ptr() as *mut u8, buf.len() * size_of::<u32>()) };
            self.plot.draw_on_buffer(plot_buf, self.size)?;
            self.window.pre_present_notify();
            buf.present()?;
        }
        Ok(())
    }
}

/// A structure of plotter application.
///
/// A plotter application shows windows with charts.
pub struct PlotterApp
{
    icon: Icon,
    windows: HashMap<WindowId, WindowState>,
    context: Option<Context<DisplayHandle<'static>>>,
}

impl PlotterApp
{
    /// Creates a plotter application.
    pub fn new(event_loop: &EventLoop<PlotterAppEvent>) -> Self
    {
        let context = Some(Context::new(unsafe { transmute::<DisplayHandle<'_>, DisplayHandle<'static>>(event_loop.display_handle().unwrap()) }).unwrap());
        let image = image::load_from_memory(include_bytes!("icon.png")).unwrap();
        let rgba_image = image.into_rgba8();
        let (width, height) = rgba_image.dimensions();
        let rgba = rgba_image.into_raw();
        let icon = Icon::from_rgba(rgba, width, height).unwrap();
        PlotterApp { icon, windows: HashMap::new(), context, }
    }

    fn create_window(&mut self, event_loop: &ActiveEventLoop, size: (u32, u32), plot: Arc<Plot>) -> result::Result<WindowId, Box<dyn error::Error>>
    {
        let window_attrs = Window::default_attributes().with_title("Unlab-gpu window").with_inner_size(PhysicalSize::new(size.0, size.1)).with_window_icon(Some(self.icon.clone()));
        let window = event_loop.create_window(window_attrs)?;
        let window_id = window.id();
        self.windows.insert(window_id, WindowState::new(self, Arc::new(window), plot, size)?);
        Ok(window_id)
    }
}

impl ApplicationHandler<PlotterAppEvent> for PlotterApp
{
    fn resumed(&mut self, _event_loop: &ActiveEventLoop)
    {}
    
    fn window_event(&mut self, _event_loop: &ActiveEventLoop, window_id: WindowId, event: WindowEvent)
    {
        let window = match self.windows.get_mut(&window_id) {
            Some(tmp_window) => tmp_window,
            None => return,
        };
        match event {
           WindowEvent::Resized(size) => window.resize(size),
           WindowEvent::CloseRequested => {
               self.windows.remove(&window_id);
           },
           WindowEvent::RedrawRequested => {
               match window.draw() {
                   Ok(()) => (),
                   Err(err) => eprintln!("plotter app error: {}", err),
               }
           },
           _ => (),
        }
    }

    fn user_event(&mut self, event_loop: &ActiveEventLoop, event: PlotterAppEvent)
    {
        match event {
            PlotterAppEvent::Plot(plot, tx) => {
                let window_id = match plot.chart().window_id {
                    Some(tmp_window_id) => {
                        match self.windows.get_mut(&tmp_window_id) {
                            Some(window) => {
                                window.plot = plot;
                                window.window.request_redraw();
                                Some(tmp_window_id)
                            },
                            None => None,
                        }
                    }
                    None => {
                        match self.create_window(event_loop, plot.chart().size.unwrap_or(DEFAULT_SIZE), plot) {
                            Ok(tmp_window_id) => Some(tmp_window_id),
                            Err(err) => {
                                eprintln!("{}", err);
                                None
                            },
                        }
                    },
                };
                match tx.send(window_id) {
                    Ok(()) => (),
                    Err(_) => eprintln!("plotter app error: can't send object"),
                }
            },
            PlotterAppEvent::Quit => event_loop.exit(),
        }
    }
    
    fn exiting(&mut self, _event_loop: &ActiveEventLoop)
    { self.context = None; }
}

fn create_size(value: &Value) -> Result<(u32, u32)>
{
    match value {
        Value::Ref(object) => {
            let object_g = rw_lock_read(&*object)?;
            match &*object_g {
                MutObject::Array(elems) => {
                    if elems.len() != 2 {
                        return Err(Error::Interp(String::from("invalid numner of elements for size")));
                    }
                    let width = match elems.get(0) {
                        Some(elem) => elem.to_i64(),
                        None => return Err(Error::Interp(String::from("no element for size"))),
                    };
                    let height = match elems.get(1) {
                        Some(elem) => elem.to_i64(),
                        None => return Err(Error::Interp(String::from("no element for size"))),
                    };
                    if width < 0 {
                        return Err(Error::Interp(String::from("too small width")));
                    }
                    if width > (u32::MAX as i64) {
                        return Err(Error::Interp(String::from("too large width")));
                    }
                    if height < 0 {
                        return Err(Error::Interp(String::from("too small height")));
                    }
                    if height > (u32::MAX as i64) {
                        return Err(Error::Interp(String::from("too large height")));
                    }
                    Ok((width as u32, height as u32))
                },
                _ => Err(Error::Interp(String::from("unsupported type for size"))),
            }
        },
        _ => Err(Error::Interp(String::from("unsupported type for size"))),
    }
}

fn create_chart(value: &Value) -> Result<Chart>
{
    match value {
        Value::Ref(object) => {
            let object_g = rw_lock_read(&*object)?;
            match &*object_g {
                MutObject::Struct(fields) => {
                    let title = match fields.get(&String::from("title")) {
                        Some(field) => {
                            match field {
                                Value::None => None,
                                _ => Some(format!("{}", field)),
                            }
                        },
                        None => None,
                    };
                    let window_id = match fields.get(&String::from("windowid")) {
                        Some(field) => {
                            match field {
                                Value::None => None,
                                Value::Object(object) => {
                                    match &**object {
                                        Object::WindowId(tmp_window_id) => Some(*tmp_window_id),
                                        _ => return Err(Error::Interp(String::from("invalid type for window identifier"))),
                                    }
                                },
                                _ => return Err(Error::Interp(String::from("unsupported type for window identifier"))),
                            }
                        },
                        None => None,
                    };
                    let has_window = match fields.get(&String::from("haswindow")) {
                        Some(field) => {
                            match field {
                                Value::None => true,
                                _ => field.to_bool(),
                            }
                        },
                        None => true,
                    };
                    let file = match fields.get(&String::from("file")) {
                        Some(field) => {
                            match field {
                                Value::None => None,
                                _ => Some(format!("{}", field)),
                            }
                        },
                        None => None,
                    };
                    let size = match fields.get(&String::from("size")) {
                        Some(field) => {
                            match field {
                                Value::None => None,
                                _ => Some(create_size(field)?),
                            }
                        },
                        None => None,
                    };
                    Ok(Chart { title, window_id, has_window, file, size, })
                },
                _ => Err(Error::Interp(String::from("unsupported type for plotter function"))),
            }
        },
        _ => Err(Error::Interp(String::from("unsupported type for plotter function"))),
    }
}

fn create_f32_range(value: &Value) -> Result<Range<f32>>
{
    match value {
        Value::Ref(object) => {
            let object_g = rw_lock_read(&*object)?;
            match &*object_g {
                MutObject::Array(elems) => {
                    if elems.len() != 2 {
                        return Err(Error::Interp(String::from("invalid number of elements for range")));
                    }
                    let start = match elems.get(0) {
                        Some(elem) => elem.to_f32(),
                        None => return Err(Error::Interp(String::from("no element for range"))),
                    };
                    let end = match elems.get(1) {
                        Some(elem) => elem.to_f32(),
                        None => return Err(Error::Interp(String::from("no element for range"))),
                    };
                    if start.is_nan() {
                        return Err(Error::Interp(String::from("range start is nan")));
                    }
                    if start.is_infinite() {
                        return Err(Error::Interp(String::from("range start is infinite")));
                    }
                    if end.is_nan() {
                        return Err(Error::Interp(String::from("range end is nan")));
                    }
                    if end.is_infinite() {
                        return Err(Error::Interp(String::from("range end is infinite")));
                    }
                    Ok(start..end)
                },
                _ => Err(Error::Interp(String::from("unsupported type for range"))),
            }
        },
        _ => Err(Error::Interp(String::from("unsupported type for range"))),
    }
}

fn create_axes2d(value: &Value) -> Result<Axes2d>
{
    match value {
        Value::Ref(object) => {
            let object_g = rw_lock_read(&*object)?;
            match &*object_g {
                MutObject::Struct(fields) => {
                    let x = match fields.get(&String::from("x")) {
                        Some(field) => create_f32_range(field)?,
                        None => return Err(Error::Interp(String::from("no field x"))),
                    };
                    let y = match fields.get(&String::from("y")) {
                        Some(field) => create_f32_range(field)?,
                        None => return Err(Error::Interp(String::from("no field y"))),
                    };
                    Ok(Axes2d { x, y, })
                },
                _ => Err(Error::Interp(String::from("unsupported type for plotter function"))),
            }
        },
        _ => Err(Error::Interp(String::from("unsupported type for plotter function"))),
    }
}

fn create_axes3d(value: &Value) -> Result<Axes3d>
{
    match value {
        Value::Ref(object) => {
            let object_g = rw_lock_read(&*object)?;
            match &*object_g {
                MutObject::Struct(fields) => {
                    let x = match fields.get(&String::from("x")) {
                        Some(field) => create_f32_range(field)?,
                        None => return Err(Error::Interp(String::from("no field x"))),
                    };
                    let y = match fields.get(&String::from("y")) {
                        Some(field) => create_f32_range(field)?,
                        None => return Err(Error::Interp(String::from("no field y"))),
                    };
                    let z = match fields.get(&String::from("z")) {
                        Some(field) => create_f32_range(field)?,
                        None => return Err(Error::Interp(String::from("no field z"))),
                    };
                    Ok(Axes3d { x, y, z })
                },
                _ => Err(Error::Interp(String::from("unsupported type for plotter function"))),
            }
        },
        _ => Err(Error::Interp(String::from("unsupported type for plotter function"))),
    }
}

fn create_histogram_values(value: &Value) -> Result<Vec<HistogramValue>>
{
    match value.iter()? {
        Some(iter) => {
            let mut values: Vec<HistogramValue> = Vec::new();
            for elem in iter {
                match elem {
                    Ok(elem) => {
                        match elem {
                            Value::Bool(b) => values.push(HistogramValue::Bool(b)),
                            Value::Int(n) => values.push(HistogramValue::Int(n)),
                            Value::Float(n) => values.push(HistogramValue::Float(n)),
                            _ => values.push(HistogramValue::String(Box::new(format!("{}", elem)))),
                        }
                    },
                    Err(err) => return Err(err),
                }
            }
            Ok(values)
        },
        None => Err(Error::Interp(String::from("value isn't iterable"))),
    }
}

fn create_usize_range(value: &Value) -> Result<Range<usize>>
{
    match value {
        Value::Ref(object) => {
            let object_g = rw_lock_read(&*object)?;
            match &*object_g {
                MutObject::Array(elems) => {
                    if elems.len() != 2 {
                        return Err(Error::Interp(String::from("invalid numner of elements for range")));
                    }
                    let start = match elems.get(0) {
                        Some(elem) => elem.to_i64(),
                        None => return Err(Error::Interp(String::from("no element for range"))),
                    };
                    let end = match elems.get(1) {
                        Some(elem) => elem.to_i64(),
                        None => return Err(Error::Interp(String::from("no element for range"))),
                    };
                    if start < 0 {
                        return Err(Error::Interp(String::from("too small range start")));
                    }
                    if start > (isize::MAX as i64) {
                        return Err(Error::Interp(String::from("too large range start")));
                    }
                    if end < 0 {
                        return Err(Error::Interp(String::from("too small range end")));
                    }
                    if end > (isize::MAX as i64) {
                        return Err(Error::Interp(String::from("too large range end")));
                    }
                    Ok((start as usize)..(end as usize))
                },
                _ => Err(Error::Interp(String::from("unsupported type for range"))),
            }
        },
        _ => Err(Error::Interp(String::from("unsupported type for range"))),
    }
}

fn create_histogram_axes(value: &Value) -> Result<HistogramAxes>
{
    match value {
        Value::Ref(object) => {
            let object_g = rw_lock_read(&*object)?;
            match &*object_g {
                MutObject::Struct(fields) => {
                    let x = match fields.get(&String::from("x")) {
                        Some(field) => create_histogram_values(field)?,
                        None => return Err(Error::Interp(String::from("no field x"))),
                    };
                    let y = match fields.get(&String::from("y")) {
                        Some(field) => create_usize_range(field)?,
                        None => return Err(Error::Interp(String::from("no field y"))),
                    };
                    Ok(HistogramAxes { x, y, })
                },
                _ => Err(Error::Interp(String::from("unsupported type for plotter function"))),
            }
        },
        _ => Err(Error::Interp(String::from("unsupported type for plotter function"))),
    }
}

fn create_f32s(value: &Value) -> Result<Vec<f32>>
{
    match value.iter()? {
        Some(iter) => {
            let mut xs: Vec<f32> = Vec::new();
            for elem in iter {
                match elem {
                    Ok(elem) => xs.push(elem.to_f32()),
                    Err(err) => return Err(err),
                }
            }
            Ok(xs)
        },
        None => Err(Error::Interp(String::from("value isn't iterable"))),
    }
}

fn create_f32s_for_fun_value(interp: &mut Interp, env: &mut Env, fun_value: &Value, xs: &[f32]) -> Result<Vec<f32>>
{
    let mut ys = vec![0.0f32; xs.len()];
    for (i, x) in xs.iter().enumerate() {
        ys[i] = fun_value.apply(interp, env, &[Value::Float(*x)])?.to_f32();
    }
    Ok(ys)
}

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
enum SeriesKind
{
    Line,
    DashedLine,
    DottedLine,
    Circle,
    Cross,
    Point,
    Triangle,
    XYSurface,
    XZSurface,
    YZSurface,
}

fn str_to_color(s: &str, color_idx: usize) -> Result<RGBColor>
{
    if s.is_empty() {
        match COLORS.get(color_idx) {
            Some(color) => Ok(*color),
            None => Err(Error::Interp(String::from("invalid color index"))),
        }
    } else if s == "r" || s == "red" {
        Ok(RED)
    } else if s == "g" || s == "green" {
        Ok(GREEN)
    } else if s == "b" || s == "blue" {
        Ok(BLUE)
    } else if s == "c" || s == "cyan" {
        Ok(CYAN)
    } else if s == "m" || s == "magenta" {
        Ok(MAGENTA)
    } else if s == "y" || s == "yellow" {
        Ok(YELLOW)
    } else if s == "k" || s == "black" {
        Ok(BLACK)
    } else if s == "w" || s == "white" {
        Ok(WHITE)
    } else {
        Err(Error::Interp(String::from("invalid color")))
    }
}

fn str_to_opt_string(s: &str) -> Option<String>
{
    if !s.is_empty() {
        Some(String::from(s))
    } else {
        None
    }
}

fn create_series_tuple(value: &Value, color_idx: usize) -> Result<(SeriesKind, RGBColor, Option<String>)>
{
    let s = format!("{}", value);
    let (t, u) = match s.split_once(",") {
        Some((tmp_t, tmp_u)) => (tmp_t, tmp_u),
        None => (s.as_str(), ""),
    };
    let (series_kind, t2) =  if t.starts_with("--") {
        (SeriesKind::DashedLine, &t[2..])
    } else if t.starts_with("-") {
        (SeriesKind::Line, &t[1..])
    } else if t.starts_with(":") {
        (SeriesKind::DottedLine, &t[1..])
    } else if t.starts_with("o") {
        (SeriesKind::Circle, &t[1..])
    } else if t.starts_with("x") {
        (SeriesKind::Cross, &t[1..])
    } else if t.starts_with(".") {
        (SeriesKind::Point, &t[1..])
    } else if t.starts_with("^") {
        (SeriesKind::Triangle, &t[1..])
    } else if t.starts_with("sxy") {
        (SeriesKind::XYSurface, &t[3..])
    } else if t.starts_with("sxz") {
        (SeriesKind::XZSurface, &t[3..])
    } else if t.starts_with("syz") {
        (SeriesKind::YZSurface, &t[3..])
    } else {
        (SeriesKind::Line, t)
    };
    let color = str_to_color(t2, color_idx)?;
    let label = str_to_opt_string(u);
    Ok((series_kind, color, label))
}

fn create_series2d(interp: &mut Interp, env: &mut Env, x_value: &Value, y_value: &Value, s_value: &Value, color_idx: usize) -> Result<Series2d>
{
    let (series_kind, color, label) = create_series_tuple(s_value, color_idx)?;
    let (xs, ys) = match (x_value.is_fun(), y_value.is_fun()) {
        (false, false) => (create_f32s(x_value)?, create_f32s(y_value)?),
        (false, true) => {
            let tmp_xs = create_f32s(x_value)?;
            let tmp_ys = create_f32s_for_fun_value(interp, env, y_value, tmp_xs.as_slice())?;
            (tmp_xs, tmp_ys)
        },
        (true, false) => {
            let tmp_ys = create_f32s(y_value)?;
            let tmp_xs = create_f32s_for_fun_value(interp, env, x_value, tmp_ys.as_slice())?;
            (tmp_xs, tmp_ys)
        },
        (_, _) => return Err(Error::Interp(String::from("unsupported types for plotter function"))),
    };
    match series_kind {
        SeriesKind::Line => Ok(Series2d::Line(xs, ys, color, label)),
        SeriesKind::DashedLine => Ok(Series2d::DashedLine(xs, ys, color, label)),
        SeriesKind::DottedLine => Ok(Series2d::DottedLine(xs, ys, color, label)),
        SeriesKind::Circle => Ok(Series2d::Circle(xs, ys, color, label)),
        SeriesKind::Cross => Ok(Series2d::Cross(xs, ys, color, label)),
        SeriesKind::Point => Ok(Series2d::Point(xs, ys, color, label)),
        SeriesKind::Triangle => Ok(Series2d::Triangle(xs, ys, color, label)),
        _ => Err(Error::Interp(String::from("invalid series kind")))
    }
}

fn create_surface_f32s(value: &Value, x_count: usize, y_count: usize, x_name: &str, y_name: &str, z_name: &str) -> Result<Vec<f32>>
{
    match value.iter()? {
        Some(iter) => {
            let mut xs: Vec<f32> = Vec::new();
            let mut row_count = 0usize;
            for row in iter {
                match row {
                    Ok(row) => {
                        let ys = create_f32s(&row)?;
                        if ys.len() != x_count {
                            return Err(Error::Interp(format!("number of {} columns isn't equal to number of {} elements", z_name, x_name)))
                        }
                        xs.extend_from_slice(ys.as_slice());
                    },
                    Err(err) => return Err(err),
                }
                match row_count.checked_add(1) {
                    Some(new_row_count) => row_count = new_row_count,
                    None => return Err(Error::Interp(format!("too many {} rows", z_name))),
                }
            }
            if row_count != y_count {
                return Err(Error::Interp(format!("number of {} rows isn't equal to number of {} elements", z_name, y_name)))
            }
            Ok(xs)
        },
        None => Err(Error::Interp(String::from("value isn't iterable"))),
    }
}

fn checked_mul_row_count_and_col_count(row_count: usize, col_count: usize, name: &str) -> Result<usize>
{
    if row_count > (isize::MAX as usize) {
        return Err(Error::Interp(String::from("too large number of rows")));
    }
    if col_count > (isize::MAX as usize) {
        return Err(Error::Interp(String::from("too large number of columns")));
    }
    match row_count.checked_mul(col_count) {
        Some(len) => {
            if len > (isize::MAX as usize) {
                return Err(Error::Interp(format!("too large number of {} elements", name)));
            }
            match (len as isize).checked_mul(size_of::<f32>() as isize) {
                Some(_) => Ok(len as usize),
                None => Err(Error::Interp(format!("too large number of {} elements", name))),
            }
        },
        None => Err(Error::Interp(format!("too large number of {} elements", name))),
    }
}

fn create_surface_f32s_for_fun_value(interp: &mut Interp, env: &mut Env, fun_value: &Value, xs: &[f32], ys: &[f32], z_name: &str) -> Result<Vec<f32>>
{
    let len = checked_mul_row_count_and_col_count(ys.len(), xs.len(), z_name)?;
    let mut zs = vec![0.0f32; len];
    for (yi, y) in ys.iter().enumerate() {
        for (xi, x) in xs.iter().enumerate() {
            zs[yi * xs.len() + xi] = fun_value.apply(interp, env, &[Value::Float(*x), Value::Float(*y)])?.to_f32();
        }
    }
    Ok(zs)
}

fn create_indices(xs: &[f32]) -> BTreeMap<F32Key, usize>
{
    let mut idxs: BTreeMap<F32Key, usize> = BTreeMap::new();
    for (i, x) in xs.iter().enumerate() {
        idxs.insert(F32Key::new(*x), i);
    }
    idxs
}

fn create_series3d(interp: &mut Interp, env: &mut Env, x_value: &Value, y_value: &Value, z_value: &Value, s_value: &Value, color_idx: usize) -> Result<Series3d>
{
    let (series_kind, color, label) = create_series_tuple(s_value, color_idx)?;
    let (xs, ys, zs) = match series_kind {
        SeriesKind::XYSurface => {
            let xs = create_f32s(x_value)?;
            let ys = create_f32s(y_value)?;
            let zs = if z_value.is_fun() {
                create_surface_f32s_for_fun_value(interp, env, z_value, xs.as_slice(), ys.as_slice(), "z")?
            } else {
                create_surface_f32s(z_value, xs.len(), ys.len(), "x", "y", "z")?
            };
            let xis = create_indices(xs.as_slice());
            let yis = create_indices(ys.as_slice());
            return Ok(Series3d::XYSurface(xs, ys, zs, color, label, xis, yis));
        },
        SeriesKind::XZSurface => {
            let xs = create_f32s(x_value)?;
            let zs = create_f32s(z_value)?;
            let ys = if y_value.is_fun() {
                create_surface_f32s_for_fun_value(interp, env, y_value, xs.as_slice(), zs.as_slice(), "y")?
            } else {
                create_surface_f32s(y_value, xs.len(), zs.len(), "x", "z", "y")?
            };
            let xis = create_indices(xs.as_slice());
            let zis = create_indices(zs.as_slice());
            return Ok(Series3d::XZSurface(xs, ys, zs, color, label, xis, zis));
        },
        SeriesKind::YZSurface => {
            let ys = create_f32s(y_value)?;
            let zs = create_f32s(z_value)?;
            let xs = if x_value.is_fun() {
                create_surface_f32s_for_fun_value(interp, env, x_value, ys.as_slice(), zs.as_slice(), "x")?
            } else {
                create_surface_f32s(x_value, ys.len(), zs.len(), "y", "z", "x")?
            };
            let yis = create_indices(ys.as_slice());
            let zis = create_indices(zs.as_slice());
            return Ok(Series3d::YZSurface(xs, ys, zs, color, label, yis, zis));
        },
        _ => {
            match (x_value.is_fun(), y_value.is_fun(), z_value.is_fun()) {
                (false, false, false) => (create_f32s(x_value)?, create_f32s(y_value)?, create_f32s(z_value)?),
                (false, true, true) => {
                    let tmp_xs = create_f32s(x_value)?;
                    let tmp_ys = create_f32s_for_fun_value(interp, env, y_value, tmp_xs.as_slice())?;
                    let tmp_zs = create_f32s_for_fun_value(interp, env, z_value, tmp_xs.as_slice())?;
                    (tmp_xs, tmp_ys, tmp_zs)
                },
                (true, false, true) => {
                    let tmp_ys = create_f32s(y_value)?;
                    let tmp_xs = create_f32s_for_fun_value(interp, env, x_value, tmp_ys.as_slice())?;
                    let tmp_zs = create_f32s_for_fun_value(interp, env, z_value, tmp_ys.as_slice())?;
                    (tmp_xs, tmp_ys, tmp_zs)
                },
                (true, true, false) => {
                    let tmp_zs = create_f32s(z_value)?;
                    let tmp_xs = create_f32s_for_fun_value(interp, env, x_value, tmp_zs.as_slice())?;
                    let tmp_ys = create_f32s_for_fun_value(interp, env, y_value, tmp_zs.as_slice())?;
                    (tmp_xs, tmp_ys, tmp_zs)
                },
                (_, _, _) => return Err(Error::Interp(String::from("unsupported types for plotter function"))),
            }
        },
    };
    match series_kind {
        SeriesKind::Line => Ok(Series3d::Line(xs, ys, zs, color, label)),
        SeriesKind::DashedLine => Ok(Series3d::DashedLine(xs, ys, zs, color, label)),
        SeriesKind::DottedLine => Ok(Series3d::DottedLine(xs, ys, zs, color, label)),
        SeriesKind::Circle => Ok(Series3d::Circle(xs, ys, zs, color, label)),
        SeriesKind::Cross => Ok(Series3d::Cross(xs, ys, zs, color, label)),
        SeriesKind::Point => Ok(Series3d::Point(xs, ys, zs, color, label)),
        SeriesKind::Triangle => Ok(Series3d::Triangle(xs, ys, zs, color, label)),
        _ => Err(Error::Interp(String::from("invalid series kind")))
    }
}

fn create_color_and_label(value: &Value, color_idx: usize) -> Result<(RGBColor, Option<String>)>
{
    let s = format!("{}", value);
    let (t, u) = match s.split_once(",") {
        Some((tmp_t, tmp_u)) => (tmp_t, tmp_u),
        None => (s.as_str(), ""),
    };
    let color = str_to_color(t, color_idx)?;
    let label = str_to_opt_string(u);
    Ok((color, label))
}

fn create_histogram_series(data_value: &Value, s_value: &Value, color_idx: usize) -> Result<HistogramSeries>
{
    let (color, label) = create_color_and_label(s_value, color_idx)?;
    let data = create_histogram_values(data_value)?;
    Ok(HistogramSeries(data, color, label))
}

fn plot_for_plot(plot: &Arc<Plot>, env: &Env) -> Result<Value>
{
    let window_id = match Plot::draw_on_window(plot, env)? {
        Some(Some(tmp_window_id)) => Some(tmp_window_id),
        Some(None) => return Ok(Value::Object(Arc::new(Object::Error(String::from("plot"), String::from("can't create or find window"))))),
        None => None,
    };
    match plot.draw_and_save_to_file() {
        Ok(()) => (),
        Err(err) => return Ok(Value::Object(Arc::new(Object::Error(String::from("plot"), format!("{}", err))))),
    }
    match window_id {
        Some(window_id) => Ok(Value::Object(Arc::new(Object::WindowId(window_id)))),
        None => Ok(Value::Bool(true)),
    }
}

/// A `plot` built-in function.
pub fn plot(interp: &mut Interp, env: &mut Env, arg_values: &[Value]) -> Result<Value>
{
    if arg_values.len() < 1 {
        return Err(Error::Interp(String::from("invalid number of arguments")));
    }
    let mut arg_value_iter = arg_values.iter();
    let (chart, axes) = match arg_value_iter.next() {
        Some(chart_value) => (create_chart(chart_value)?, create_axes2d(chart_value)?),
        None => return Err(Error::Interp(String::from("no argument"))),
    };
    let mut serieses: Vec<Series2d> = Vec::new();
    let mut color_idx = 0usize;
    loop {
        let x_value = match arg_value_iter.next() {
            Some(tmp_x_value) => tmp_x_value,
            None => break,
        };
        let y_value = match arg_value_iter.next() {
            Some(tmp_y_value) => tmp_y_value,
            None => return Err(Error::Interp(String::from("no argument y"))),
        };
        let s_value = match arg_value_iter.next() {
            Some(tmp_s_value) => tmp_s_value,
            None => return Err(Error::Interp(String::from("no argument s"))),
        };
        serieses.push(create_series2d(interp, env, x_value, y_value, s_value, color_idx)?);
        color_idx = (color_idx + 1) % COLORS.len();
    }
    let plot = Arc::new(Plot::Plot(chart, axes, serieses));
    plot_for_plot(&plot, env)
}

/// A `plot3` built-in function.
pub fn plot3(interp: &mut Interp, env: &mut Env, arg_values: &[Value]) -> Result<Value>
{
    if arg_values.len() < 1 {
        return Err(Error::Interp(String::from("invalid number of arguments")));
    }
    let mut arg_value_iter = arg_values.iter();
    let (chart, axes) = match arg_value_iter.next() {
        Some(chart_value) => (create_chart(chart_value)?, create_axes3d(chart_value)?),
        None => return Err(Error::Interp(String::from("no argument"))),
    };
    let mut serieses: Vec<Series3d> = Vec::new();
    let mut color_idx = 0usize;
    loop {
        let x_value = match arg_value_iter.next() {
            Some(tmp_x_value) => tmp_x_value,
            None => break,
        };
        let y_value = match arg_value_iter.next() {
            Some(tmp_y_value) => tmp_y_value,
            None => return Err(Error::Interp(String::from("no argument y"))),
        };
        let z_value = match arg_value_iter.next() {
            Some(tmp_y_value) => tmp_y_value,
            None => return Err(Error::Interp(String::from("no argument z"))),
        };
        let s_value = match arg_value_iter.next() {
            Some(tmp_s_value) => tmp_s_value,
            None => return Err(Error::Interp(String::from("no argument s"))),
        };
        serieses.push(create_series3d(interp, env, x_value, y_value, z_value, s_value, color_idx)?);
        color_idx = (color_idx + 1) % COLORS.len();
    }
    let plot = Arc::new(Plot::Plot3(chart, axes, serieses));
    plot_for_plot(&plot, env)
}

/// A `histogram` built-in function.
pub fn histogram(_interp: &mut Interp, env: &mut Env, arg_values: &[Value]) -> Result<Value>
{
    if arg_values.len() < 1 {
        return Err(Error::Interp(String::from("invalid number of arguments")));
    }
    let mut arg_value_iter = arg_values.iter();
    let (chart, axes) = match arg_value_iter.next() {
        Some(chart_value) => (create_chart(chart_value)?, create_histogram_axes(chart_value)?),
        None => return Err(Error::Interp(String::from("no argument"))),
    };
    let mut serieses: Vec<HistogramSeries> = Vec::new();
    let mut color_idx = 0usize;
    loop {
        let data_value = match arg_value_iter.next() {
            Some(tmp_data_value) => tmp_data_value,
            None => break,
        };
        let s_value = match arg_value_iter.next() {
            Some(tmp_s_value) => tmp_s_value,
            None => return Err(Error::Interp(String::from("no argument s"))),
        };
        serieses.push(create_histogram_series(data_value, s_value, color_idx)?);
        color_idx = (color_idx + 1) % COLORS.len();
    }
    let plot = Arc::new(Plot::Histogram(chart, axes, serieses));
    plot_for_plot(&plot, env)
}