示例#1
0
string Dtime::Time_Label (Units_Type time_format)
{
	int hour, minute, seconds, min_time, hour_time;
	double time;
	String text;

	if (time_format == NO_UNITS) time_format = Time_Format ();

	time = UnRound (DTOI (dtime));

	min_time = 60;
	hour_time = min_time * 60;

	if (time > 3 * MIDNIGHT) {
		time = 3 * MIDNIGHT;
	}
	if (time_format == HOURS) {
		text (time / hour_time);
	} else if (time_format == SECONDS) {
		text (time);
	} else {
		seconds = DTOI (time);
		hour = seconds / hour_time;
		seconds -= hour * hour_time;
		minute = seconds / min_time;
		seconds -= minute * min_time;

		if (seconds > 0) {
			text ("%d%02d%02d") % hour % minute % seconds;
		} else {
			text ("%d%02d") % hour % minute;
		}
	}
	return (text);
}
示例#2
0
string Dtime::Time_Label (bool pad_flag)
{
	if (!pad_flag) return (Time_Label ());

	int hour, minute, seconds, min_time, hour_time;
	double time;
	String text;
	Units_Type time_format = Time_Format ();

	time = UnRound (DTOI (dtime));

	min_time = 60;
	hour_time = min_time * 60;

	if (time > 3 * MIDNIGHT) {
		time = 3 * MIDNIGHT;
	}
	if (time_format == HOURS) {
		text ("%09.6lf") % (time / hour_time);
	} else if (time_format == SECONDS) {
		text ("%05d") % time;
	} else {
		seconds = DTOI (time);
		hour = seconds / hour_time;
		seconds -= hour * hour_time;
		minute = seconds / min_time;
		seconds -= minute * min_time;

		text ("%02d%02d%02d") % hour % minute % seconds;
	}
	return (text);
}
示例#3
0
void PlanSum::Write_Times (void)
{
	int i, start, mid, end;
	Dtime low, high;

	fstream &file = time_file.File ();

	Show_Message ("Writing Trip Time File");

	file << "Time1\tTime2\tStart\tMid-Trip\tEnd" << endl;

	for (i=0; i < sum_periods.Num_Periods (); i++) {
		sum_periods.Period_Range (i, low, high);

		start = DTOI (start_time [i]);
		mid = DTOI (mid_time [i]);
		end = DTOI (end_time [i]);

		if (start > 0 || mid > 0 || end > 0) {
			file << low.Time_String () << "\t" << high.Time_String () << "\t" << start << "\t" << mid << "\t" << end << endl;
		}
	}
	Show_Message (1);
	time_file.Close ();
}
示例#4
0
void Db_Base::Binary_Text_Size (Field_Type type, double *size) 
{
	size_t length = (size_t) *size;
	int decimal = DTOI ((*size - length) * 10.0);

	switch (type) {
		case DB_INTEGER:
		case DB_UNSIGNED:
			if (length == sizeof (char)) {
				*size = 3;
			} else if (length == sizeof (short)) {
				*size = 5;
			} else {
				*size = 10;
			}
			break;
		case DB_DOUBLE:
			if (decimal == 0) {
				if (length == sizeof (float)) {
					*size = 8.2;
				} else {
					*size = 12.4;
				}
			} else {
				if (length == sizeof (float)) {
					*size = ((int) ((decimal + 7) * 10 + decimal)) / 10.0;
				} else {
					*size = ((int) ((decimal + 10) * 10 + decimal)) / 10.0;
				}
			}
			break;
		case DB_FIXED:
			if (decimal == 0) {
				if (length == sizeof (char)) {
					*size = 4.2;
				} else if (length == sizeof (short)) {
					*size = 6.2;
				} else {
					*size = 8.2;
				}
			} else {
				if (length == sizeof (char)) {
					*size = 4 + decimal / 10.0;
				} else if (length == sizeof (short)) {
					*size = 6 + decimal / 10.0;
				} else {
					*size = 10 + decimal / 10.0;
				}
			}
			break;
		case DB_STRING:
		case DB_CHAR:
			break;
		case DB_TIME:
			*size = 12;
			break;
	}
}
示例#5
0
void TransitAccess::Read_Link (void)
{
	int anode, bnode, dist, zone, type;
	double len;
	String text;
	Zone_Map_Itr zone_itr;
	Node_Map_Itr node_itr;

	//---- walk link data ----

	Show_Message (String ("Reading %s -- Record") % link_data_file.File_Type ());
	Set_Progress ();

	while (link_data_file.Read ()) {
		Show_Progress ();

		anode = link_data_file.Get_Integer (link_anode_fld);
		bnode = link_data_file.Get_Integer (link_bnode_fld);

		if (skip_links.find (Int2_Key (anode, bnode)) != skip_links.end ()) continue;
		if (skip_links.find (Int2_Key (bnode, anode)) != skip_links.end ()) continue;

		len = link_data_file.Get_Double (link_len_fld);
		zone = link_data_file.Get_Integer (link_zone_fld);
		type = link_data_file.Get_Integer (link_type_fld);

		if (!type_range.In_Range (type)) continue;

		zone_itr = zone_map.find (zone);
		if (zone_itr == zone_map.end ()) continue;
		if (zone_itr->second.walk == 0) continue;

		dist = DTOI (len * 100.0 * zone_itr->second.weight);

		node_itr = node_map.find (anode);
		if (node_itr != node_map.end ()) {
			node_itr->second.use = 1;
		}
		node_itr = node_map.find (bnode);
		if (node_itr != node_map.end ()) {
			node_itr->second.use = 1;
		}
	
		text (" N=%d-%d MODE=13 SPEED=3 ONEWAY=T DIST=%d") % anode % bnode % dist;

		sidewalk_file.File () << "SUPPORT" << text << endl;

		walk_link_file.File () << "SUPPLINK" << text << endl;
	}
	End_Progress ();
	link_data_file.Close ();

	Print (2, "Number of Link Data Records = ") << Progress_Count ();
}
示例#6
0
Dtime::Dtime (double time, Units_Type units)
{
	if (units == SECONDS) {
		dtime = Round (time);
	} else if (units == MINUTES) {
		dtime = Round (time * 60.0);
	} else if (units == HOURS) {
		dtime = Round (time * 3600.0);
	} else {
		dtime = DTOI (time);
	}
}
示例#7
0
bool Data_Range::Add_Breaks (string text)
{
	int low, high;
	double d1, d2;
	Strings breaks;
	Str_Itr itr;

	//---- check for special conditions ----

	if (text.empty () || text [0] == '\n') return (true);

	if (String_Ptr (text)->Equals ("None")) {
		return (Add_Range (minimum, maximum));
	}

	//---- unpack the break string ----	

	low = minimum;

	String_Ptr (text)->Parse (breaks);

	for (itr = breaks.begin (); itr != breaks.end (); itr++) {
		if (!String_Ptr (*itr)->Range (d1, d2)) continue;

		if (d1 == 0.0 && d2 == 0.0) continue;
		if (d2 > d1) goto break_error;

		high = DTOI (d2 * factor);

		if (low > high || low < minimum || high > maximum) goto break_error;

		if (low < high) {
			if (!Add_Range (low, high)) return (false);
		}
		low = high;
	}
	high = maximum;

	if (low < high) {
		return (Add_Range (low, high));
	}
	return (Num_Ranges () > 0);

break_error:
	if (exe->Send_Messages ()) {
		exe->Error (String ("Range Breaks %s are Illogical") % text);
	}
	return (false);
}
示例#8
0
bool Data_Range::Add_Ranges (string text)
{
	int low, high;
	double d1, d2;
	Strings ranges;
	Str_Itr itr;

	//---- check for special conditions ----

	if (text.empty () || text [0] == '\n') return (true);
	
	if (String_Ptr (text)->Equals ("All")) {
		return (Add_Range (minimum, maximum, increment));
	}

	//---- unpack the range string ----	

	String_Ptr (text)->Parse (ranges);

	for (itr = ranges.begin (); itr != ranges.end (); itr++) {
		if (!String_Ptr (*itr)->Range (d1, d2)) continue;

		if (d1 == 0.0 && d2 == 0.0 && !itr->Equals ("0")) continue;

		low = DTOI (d1 * factor);
		high = DTOI (d2 * factor);

		if (low > high || low < minimum || high > maximum) {
			if (exe->Send_Messages ()) {
				exe->Error (String ("Range %g-%g is Out of Range") % d1 % d2);
			}
		}
		if (!Add_Range (low, high, increment)) return (false);
	}
	return (Num_Ranges () > 0);
}
示例#9
0
void LinkSum::Top_100_Ratios (int type)
{
	int i, j, k, anode, bnode, an, bn, min_ratio, ratio, tod_list, lanes, lane;
	int load, base, cap, capacity, tim, vol, time0, len, index, flow_index;
	double flow;
	Dtime low, high, tod;

	Link_Itr link_itr;
	Dir_Data *dir_ptr;
	Link_Perf_Period_Itr period_itr;
	Link_Perf_Array *period_ptr;
	Flow_Time_Data flow_data;
	Lane_Use_Period *use_ptr;

	//---- Top 100 Ratio Report Data ----

	typedef struct {
		int link;
		int from;
		int to;
		int period;
		int base;
		int load;
		int ratio;
	} Ratio_Data;

	Ratio_Data ratios [100], *ptr;

	Show_Message ("Creating the Top 100 Ratios Report -- Record");
	Set_Progress ();

	memset (ratios, '\0', 100 * sizeof (Ratio_Data));

	//---- find Top 100 Ratios ----

	min_ratio = base = load = ratio = 0;

	for (link_itr = link_array.begin (); link_itr != link_array.end (); link_itr++) {
		Show_Progress ();

		if (select_flag && link_itr->Use () == 0) continue;

		an = node_array [link_itr->Anode ()].Node ();
		bn = node_array [link_itr->Bnode ()].Node ();

		for (i=0; i < 2; i++) {
			if (i) {
				if (link_itr->Use () == -1) continue;
				index = link_itr->BA_Dir ();
				anode = bn;
				bnode = an;
			} else {
				if (link_itr->Use () == -2) continue;
				index = link_itr->AB_Dir ();
				anode = an;
				bnode = bn;
			}
			if (index < 0) continue;
			dir_ptr = &dir_array [index];

			len = link_itr->Length ();

			lanes = dir_ptr->Lanes ();
			if (lanes < 1) lanes = 1;

			tod_list = dir_ptr->First_Lane_Use ();
			flow_index = dir_ptr->Flow_Index ();

			time0 = dir_ptr->Time0 ();
			if (time0 == 0) continue;

			capacity = cap = DTOI (dir_ptr->Capacity () * cap_factor);

			//---- scan each time period ----

			for (j=0, period_itr = link_perf_array.begin (); period_itr != link_perf_array.end (); period_itr++, j++) {
				flow_data = period_itr->Total_Flow_Time (index, flow_index);

				flow = flow_data.Flow ();
				if (flow < minimum_flow) continue;

				tim = flow_data.Time ();
				vol = DTOI (flow);

				switch (type) {
					case TOP_SPEED:
						if (tim == 0) continue;
						base = (len * 10 + time0 / 2) / time0;
						load = (len * 10 + tim / 2) / tim;
						ratio = ((base - load) * 1000 + base / 2) / base;
						break;
					case TOP_TIME_RATIO:
						base = time0;
						load = tim;
						ratio = (load * 1000 + base / 2) / base;
						break;
					case TOP_VC_RATIO:
						if (tod_list >= 0) {
							sum_periods.Period_Range (j, low, high);

							tod = (low + high + 1) / 2;
							cap = capacity;
							k = tod_list;

							for (use_ptr = &use_period_array [k]; ; use_ptr = &use_period_array [++k]) {
								if (use_ptr->Start () <= tod && tod < use_ptr->End ()) {
									lane = use_ptr->Lanes0 () + use_ptr->Lanes1 ();
									cap = capacity * lane / lanes;
									break;
								}
								if (use_ptr->Periods () == 0) break;
							}
						}
						base = cap;
						if (base == 0) continue;
						load = vol;
						ratio = (load * 1000 + base / 2) / base;
						break;
					case TOP_TIME_CHANGE:
						period_ptr = &compare_link_array [j];

						flow_data = period_ptr->Total_Flow_Time (index, flow_index);
						if (flow_data.Flow () < minimum_flow) continue;
						base = flow_data.Time ();
						if (base == 0) continue;
						load = tim;
						ratio = ((load - base) * 1000 + base / 2) / base;
						break;
					case TOP_VOL_CHANGE:
						period_ptr = &compare_link_array [j];

						flow_data = period_ptr->Total_Flow_Time (index, flow_index);
						base = DTOI (flow_data.Flow ());
						if (base < minimum_flow) continue;
						load = vol;
						ratio = ((load - base) * 1000 + base / 2) / base;
						break;
				} 
				
				if (ratio > min_ratio) {
					ptr = ratios;

					for (k=0; k < 100; k++, ptr++) {
						if (ratio > ptr->ratio) {
							if (ptr->ratio > 0 && k < 99) {
								memmove (ptr+1, ptr, (99-k) * sizeof (Ratio_Data));							
								min_ratio = ratios [99].ratio;
							}
							ptr->link = link_itr->Link ();
							ptr->from = anode;
							ptr->to = bnode;
							ptr->period = j;
							ptr->base = base;
							ptr->load = load;
							ptr->ratio = ratio;
							break;
						}
					}
				}
			}
		}
	}
	End_Progress ();

	//---- print the report ----

	Header_Number (type);
	New_Page ();

	ptr = ratios;

	for (i=0; i < 100; i++, ptr++) {
		if (ptr->ratio == 0) break;

		//---- print the data record ----

		Print (1, String ("%10ld%10ld%10ld  %12.12s   ") % ptr->link % ptr->from % ptr->to % 
			sum_periods.Range_Format (ptr->period));

		if (type == TOP_VC_RATIO) {
			Print (0, String ("%7d  %7d   %5.2lf") % ptr->base % ptr->load % (ptr->ratio / 1000.0));
		} else if (type == TOP_VOL_CHANGE) {
			Print (0, String ("%7d  %7d   %5.1lf") % ptr->base % ptr->load % (ptr->ratio / 10.0));
		} else {
			Print (0, String ("%7.1lf  %7.1lf   %5.1lf") % (ptr->base / 10.0) % (ptr->load / 10.0) % (ptr->ratio / 10.0));
		}
	}
	if (i) {
		Print (2, "Number of Records in the Report = ") << i;
	}
	Header_Number (0);
}
示例#10
0
void RiderSum::Write_Group_Rider (void)
{
	int n, run, runs, num, length, period, num_periods, routes, index, stop, to, link, capacity;
	double factor, sum_time, time, capfac;
	Dtime low, high;
	String label;
	bool flag;
	
	Int_Set *group;
	Int_Set_Itr itr;
	Int_Map_Itr map_itr, to_itr;
	Line_Data *line_ptr;
	Stop_Data *stop_ptr;
	Line_Stop_Itr stop_itr, next_itr;
	Line_Run_Itr run_itr;
	Int_Map stop_index;
	Int2_Map link_index;
	Int2_Map_Itr map2_itr;
	Int2_Key int2_key;
	Int2_Map_Stat map2_stat;
	Int_Map_Stat map_stat;
	Stop_Group_Data data, *data_ptr, *link_ptr;
	Stop_Group_Itr data_itr;
	Veh_Type_Data *veh_type_ptr, *run_type_ptr;

	fstream &file = line_group_file.File ();
	
	num_periods = sum_periods.Num_Periods ();
	if (num_periods == 0) num_periods = 1;

	Show_Message (String ("Writing % -- Record") % line_group_file.File_Type ());
	Set_Progress ();

	file << "Group\tPeriod\tRoutes\tStops\tDescription\n";
	file << "Stop\tLength\tTTime\tAlight\tBoard\tRiders\tRuns\tLoadFac\tCapacity\tCapFac\tStopName\n";

	//---- process each line group ----

	routes = 0;
	memset (&data, '\0', sizeof (data));
	
	for (n = line_equiv.First_Group (); n > 0; n = line_equiv.Next_Group ()) {

		group = line_equiv.Group_List (n);
		if (group == 0) continue;

		label = line_equiv.Group_Label (n);

		//---- process each time period ----

		for (period = 0; period < num_periods; period++) {

			//---- initialize stop data ----

			stop_index.clear ();
			link_index.clear ();
			stop_group_array.clear ();
			routes = 0;

			//---- process each line in the line group ----

			for (itr = group->begin (); itr != group->end (); itr++) {

				map_itr = line_map.find (*itr);
				if (map_itr == line_map.end ()) continue;

				Show_Progress ();

				line_ptr = &line_array [map_itr->second];

				//---- set the run flags ----

				if (!Run_Selection (line_ptr)) continue;

				//---- check the period flag ----

				if (period_flag [period] == 0) continue;

				time = 0.0;
				length = 0;
				flag = false;
				veh_type_ptr = &veh_type_array [line_ptr->Type ()];

				sum_periods.Period_Range (period, low, high);

				for (stop_itr = line_ptr->begin (); stop_itr != line_ptr->end (); stop_itr++) {
					
					stop_ptr = &stop_array [stop_itr->Stop ()];
					stop = stop_ptr->Stop ();

					map_itr = stop_index.find (stop);

					next_itr = stop_itr + 1;

					if (next_itr != line_ptr->end ()) {
						stop_ptr = &stop_array [next_itr->Stop ()];

						to = stop_ptr->Stop ();
					} else {
						to = 0;
					}

					//---- create the link ----

					int2_key = Int2_Key (stop, to);

					map2_itr = link_index.find (int2_key);

					if (map2_itr == link_index.end ()) {
						if (to == 0 && map_itr != stop_index.end ()) {
							link = map_itr->second;
						} else {
							link = (int) stop_group_array.size ();

							link_index.insert (Int2_Map_Data (int2_key, link));

							data.stop = stop;
							data.to = to;
							stop_group_array.push_back (data);

							//---- check for a merge ----

							if (to != 0) {
								to_itr = stop_index.find (to);

								if (to_itr != stop_index.end ()) {
									index = (int) stop_group_array.size ();

									int2_key = Int2_Key (to, 0);

									map2_stat = link_index.insert (Int2_Map_Data (int2_key, index));

									if (map2_stat.second) {
										data.stop = to;
										data.to = 0;
										stop_group_array.push_back (data);
									}
								}
							}
						}
					} else {
						link = map2_itr->second;
					}
					link_ptr = &stop_group_array [link];

					//---- crete the stop index ----

					if (map_itr == stop_index.end ()) {
						map_stat = stop_index.insert (Int_Map_Data (stop, link));
						index = link;
					} else {
						index = map_itr->second;
					}
					data_ptr = &stop_group_array [index];

					if (next_itr != line_ptr->end ()) {
						length = next_itr->Length () - stop_itr->Length ();
					} else {
						length = 0;
					}
					sum_time = 0.0;
					num = runs = 0;

					for (run=0, run_itr = stop_itr->begin (); run_itr != stop_itr->end (); run_itr++, run++) {
						if (run_flag [run] == 0) continue;
						if (run_period [run] != period) continue;

						data_ptr->board += run_itr->Board ();
						data_ptr->alight += run_itr->Alight ();
						link_ptr->riders += run_itr->Load ();
						runs++;
						flag = true;

						if (next_itr != line_ptr->end ()) {
							if (line_ptr->run_types.size () > 0) {
								run_type_ptr = &veh_type_array [line_ptr->Run_Type (run)];
								capacity = run_type_ptr->Capacity ();
							} else {
								capacity = veh_type_ptr->Capacity ();
							}
							link_ptr->runs++;
							link_ptr->capacity += capacity;
							time = next_itr->at (run).Schedule ().Seconds () - run_itr->Schedule ().Seconds ();
							sum_time += time;
							num++;
						}
					}
					if (runs == 0) continue;
						
					if (link_ptr->length == 0.0) {
						link_ptr->length = length;
					}
					if (num > 0) {
						time = sum_time / num;
					} else {
						time = 0;
					}
					if (link_ptr->time > 0) {
						link_ptr->time = (link_ptr->time + time) / 2.0;
					} else {
						link_ptr->time = time;
					}
				}
				if (flag) routes++;
			}
			file << n << "\t" << sum_periods.Range_Format (period) << "\t" << routes << "\t" << stop_index.size () << "\t" << label << "\n";

			for (data_itr = stop_group_array.begin (); data_itr != stop_group_array.end (); data_itr++) {

				map_itr = stop_map.find (data_itr->stop);
					
				stop_ptr = &stop_array [map_itr->second];
				
				if (data_itr->runs > 0) {
					factor = DTOI (data_itr->riders * 10.0 / data_itr->runs) / 10.0;
				} else {
					factor = 0;
				}
				if (data_itr->capacity > 0) {
					capfac = DTOI (data_itr->riders * 10.0 / data_itr->capacity) / 10.0;
				} else {
					capfac = 0;
				}
				file << data_itr->stop << "\t" << UnRound (data_itr->length) << "\t" << data_itr->time << "\t" <<
					data_itr->alight << "\t" << data_itr->board << "\t" << data_itr->riders << "\t" << 
					data_itr->runs << "\t" << factor << "\t" << data_itr->capacity << "\t" << capfac << "\t";

				if (Notes_Name_Flag ()) {
					file << stop_ptr->Name ();

					if (!stop_ptr->Notes ().empty ()) {
						file << " -- " << stop_ptr->Notes ();
					}
				}
				file << "\n";
			}
		}
	}
	End_Progress ();
	line_group_file.Close ();
}
示例#11
0
void Data_Service::Read_Veh_Types (Veh_Type_File &file)
{
	int num, cell_size;
	double length, min_len;
	Veh_Type_Data veh_type_rec;
	Int_Map_Stat map_stat;
	Veh_Type_Itr veh_type_itr;

	//---- store the vehicle type data ----

	Show_Message (String ("Reading %s -- Record") % file.File_Type ());
	Set_Progress ();
	
	Initialize_Veh_Types (file);

	while (file.Read ()) {
		Show_Progress ();

		veh_type_rec.Clear ();

		if (Get_Veh_Type_Data (file, veh_type_rec)) {
			map_stat = veh_type_map.insert (Int_Map_Data (veh_type_rec.Type (), (int) veh_type_array.size ()));

			if (!map_stat.second) {
				Warning ("Duplicate Vehicle Type Number = ") << veh_type_rec.Type ();
				continue;
			} else {
				veh_type_array.push_back (veh_type_rec);
			}
		}
	}
	End_Progress ();
	file.Close ();
	
	Print (2, String ("Number of %s Records = %d") % file.File_Type () % Progress_Count ());

	num = (int) veh_type_array.size ();

	if (num && num != Progress_Count ()) {
		Print (1, String ("Number of %d Data Records = %d") % file.File_ID () % num);
	}
	if (num > 0) System_Data_True (VEHICLE_TYPE);

	//---- set the PCE factor ----

	num = Use_Code ("CAR");
	length = 0.0;
	min_len = MAX_INTEGER;

	for (veh_type_itr = veh_type_array.begin (); veh_type_itr != veh_type_array.end (); veh_type_itr++) {
		if ((veh_type_itr->Use () & num) != 0) {
			length = veh_type_itr->Length ();
			break;
		}
		if (veh_type_itr->Length () < min_len) min_len = veh_type_itr->Length ();
	}
	if (length <= 0.0) length = min_len;
	if (length <= 0.0) return;
	cell_size = DTOI (min_len);

	for (veh_type_itr = veh_type_array.begin (); veh_type_itr != veh_type_array.end (); veh_type_itr++) {
		veh_type_itr->PCE (Round (veh_type_itr->Length () / length));
		veh_type_itr->Cells (MAX (((veh_type_itr->Length () + cell_size / 2) / cell_size), 1));
	}
}
示例#12
0
void PlanSum::Link_Group (double min_vc)
{
	int i, num, dir, link, cap, tod_cap, tod_cap2, lns, lanes, period, periods, index, flow_index;
	Dtime low, high;
	double flow, vc;
    string label;
	bool flag;

	Flow_Time_Period_Itr array_itr;
	Dir_Data *dir_ptr;
	Link_Data *link_ptr;
	Lane_Use_Period *period_ptr;
	Int_Set *list;
	Int_Set_Itr list_itr;
	Int_Map_Itr int_itr;

	Group_Array group_array;
	Group_Data zero, *data_ptr;

	memset (&zero, '\0', sizeof (zero));
	periods = sum_periods.Num_Periods ();

	Show_Message ("Writing the Link Group Report -- Record");
	Set_Progress ();

	minimum_vc = min_vc;

	//---- find V/C ratios for each link group ----

	num = link_equiv.Num_Groups ();

	Header_Number (LINK_GROUP);

	if (!Break_Check (num + 7)) {
		Print (1);
		Link_Group_Header ();
	}
	num = link_equiv.Max_Group ();

	for (i=1; i <= num; i++) {

		list = link_equiv.Group_List (i);
		if (list == 0) continue;

		label = link_equiv.Group_Label (i);

		flag = false;
		group_array.assign (periods, zero);

		//---- process each link in the link group ----

		for (list_itr = list->begin (); list_itr != list->end (); list_itr++) {

			link = abs (*list_itr);
			
			int_itr = link_map.find (link);
			if (int_itr == link_map.end ()) continue;

			link_ptr = &link_array [int_itr->second];

			if (*list_itr < 0) {
				dir = link_ptr->BA_Dir ();
			} else {
				dir = link_ptr->AB_Dir ();
			}
			if (dir < 0) continue;
			
			//---- get the directional data ----

			dir_ptr = &dir_array [dir];

			cap = (int) (dir_ptr->Capacity () * cap_factor + 0.5);
			if (cap <= 0) continue;

			lanes = dir_ptr->Lanes ();
			if (lanes < 1) lanes = 1;

			flow_index = dir_ptr->Flow_Index ();

			//---- process each time period ----

			for (period=0, array_itr = link_delay_array.begin (); array_itr != link_delay_array.end (); array_itr++, period++) {
				flow = array_itr->Flow (dir);

				//---- time-of-day capacity ----

				tod_cap = tod_cap2 = cap;
				index = dir_ptr->First_Lane_Use ();

				if (index >= 0) {
					sum_periods.Period_Range (period, low, high);
					low = (low + high) / 2;

					for (period_ptr = &use_period_array [index]; ; period_ptr = &use_period_array [++index]) {
						if (period_ptr->Start () <= low && low < period_ptr->End ()) {
							lns = period_ptr->Lanes (0);
							tod_cap = (tod_cap * lns + lanes / 2) / lanes;
							if (tod_cap == 0) tod_cap = cap / 2;

							lns = period_ptr->Lanes (1);
							tod_cap2 = (tod_cap2 * lns + lanes / 2) / lanes;
							if (tod_cap2 == 0) tod_cap2 = cap / 2;
							break;
						}
						if (period_ptr->Periods () == 0) break;
					}
				}
				if (tod_cap > 0) {
					data_ptr = &group_array [period];

					flag = true;
					data_ptr->links++;
					data_ptr->volume += DTOI (flow);
					data_ptr->capacity += tod_cap;

					if (flow_index >= 0 && tod_cap2 > 0) {
						flow = array_itr->Flow (flow_index);

						data_ptr->volume += DTOI (flow);
						data_ptr->capacity += tod_cap2;
					}
				}
			}
		}
		if (!flag) continue;

		//---- print the link group data ----

		flag = true;

		for (period=0; period < periods; period++) {
			data_ptr = &group_array [period];

			if (data_ptr->capacity <= 0) continue;

			vc = (double) data_ptr->volume / data_ptr->capacity;

			if (vc > min_vc) {
				Show_Progress ();

				//---- print the data record ----

				if (flag) {
					flag = false;
					Print (1, label);
				}
				Print (1, String ("%20c%5d   %7d  %12.12s  %7d   %6.2lf") %
					BLANK % data_ptr->links % data_ptr->capacity % 
					sum_periods.Range_Format (period) % data_ptr->volume % vc);
			}
		}
	}
	End_Progress ();

	if (Progress_Count () > 0) {
		Print (2, "Number of Records in the Report = ") << Progress_Count ();
	}
	Header_Number (0);
}
示例#13
0
void LinkSum::Link_Report (double min_volume)
{
	bool flag;
	int i, ab_index, ba_index, ab_flow, ba_flow, an, bn;
	double flow;
	String ab, ba;
	
	Link_Itr link_itr;
	Link_Perf_Period_Itr period_itr;
	Flow_Time_Data flow_data;

	Show_Message ("Creating a Link Event Report -- Record");
	Set_Progress ();

	//---- find volumes greater than min_volume ----

	header_value = DTOI (min_volume);

	Header_Number (LINK_REPORT);
	New_Page ();

	for (link_itr = link_array.begin (); link_itr != link_array.end (); link_itr++) {
		Show_Progress ();

		if (select_flag && link_itr->Use () == 0) continue;

		ab_index = (link_itr->Use () != -2) ? link_itr->AB_Dir () : -1;
		ba_index = (link_itr->Use () != -1) ? link_itr->BA_Dir () : -1;

		ab_flow = (ab_index >= 0) ? dir_array [ab_index].Flow_Index () : -1;
		ba_flow = (ba_index >= 0) ? dir_array [ba_index].Flow_Index () : -1;

		//---- scan each period volume ----

		for (i=0, period_itr = link_perf_array.begin (); period_itr != link_perf_array.end (); period_itr++, i++) {
			ab.clear ();
			ba.clear ();
			flag = false;

			if (ab_index >= 0) {
				flow_data = period_itr->Total_Flow_Time (ab_index, ab_flow);

				flow = flow_data.Flow ();
				if (flow >= min_volume) {
					ab = String ("%d") % DTOI (flow);
					flag = true;
				}
			}
			if (ba_index >= 0) {
				flow_data = period_itr->Total_Flow_Time (ba_index, ba_flow);

				flow = flow_data.Flow ();
				if (flow >= min_volume) {
					ba = String ("%d") % DTOI (flow);
					flag = true;
				}
			}
			if (!flag) continue;

			Show_Progress ();

			//---- print the data record ----
		
			an = node_array [link_itr->Anode ()].Node ();
			bn = node_array [link_itr->Bnode ()].Node ();

			Print (1, String ("%10d%10d%10d  %12.12s%10.10s %10.10s") % link_itr->Link () % an % 
				bn % sum_periods.Range_Format (i) % ab % ba);
		}
	}
	End_Progress ();

	if (Progress_Count ()) {
		Print (2, "Number of Records in the Report = ") << Progress_Count ();
	}
	Header_Number (0);
}
示例#14
0
	void   Speed (double value)       { if (compress) rec->speed = DTOI (value); else Put_Field (speed, value); }
示例#15
0
	void   Offset (double value)      { if (compress) rec->offset = DTOI (value); else Put_Field (offset, value); }
示例#16
0
void LinkSum::Link_Group (double min_volume)
{
	int i, j, num, lnk, dir, link, use_index, group_num;
	String label;

	Link_Data *link_ptr;
	Perf_Period_Itr period_itr;
	Perf_Data perf_data;
	Int_Set *group;
	Int_Set_Itr itr;
	Int_Map_Itr map_itr;
	Doubles group_data;

	Show_Message ("Creating the Link Group Report -- Record");
	Set_Progress ();

	header_value = DTOI (min_volume);

	//---- find events for each link group ----

	num = link_equiv.Num_Groups ();

	Header_Number (LINK_GROUP);

	if (!Break_Check (num + 7)) {
		Print (1);
		Link_Group_Header ();
	}

	for (i = link_equiv.First_Group (); i > 0; i = link_equiv.Next_Group ()) {

		group = link_equiv.Group_List (i);
		if (group == 0) continue;

		label = link_equiv.Group_Label (i);

		group_num = 0;
		group_data.assign (num_inc, 0);

		//---- process each link in the link group ----

		for (itr = group->begin (); itr != group->end (); itr++) {

			link = *itr;
			lnk = abs (link);

			map_itr = link_map.find (lnk);
			if (map_itr == link_map.end ()) continue;

			link_ptr = &link_array [map_itr->second];

			if (link < 0) {
				dir = link_ptr->BA_Dir ();
			} else {
				dir = link_ptr->AB_Dir ();
			}
			if (dir < 0) continue;
			use_index = dir_array [dir].Use_Index ();

			group_num++;

			//---- get the directional data ----
		
			for (j=0, period_itr = perf_period_array.begin (); period_itr != perf_period_array.end (); period_itr++, j++) {
				perf_data = period_itr->Total_Performance (dir, use_index);

				group_data [j] += perf_data.Volume ();
			}
		}
		if (group_num == 0) continue;

		//---- print the link group data ----

		for (j=0; j < num_inc; j++) {
			if (group_data [j] < min_volume) continue;

			Show_Progress ();

			//---- print the data record ----

			Print (1, String ("%4d  %-40.40s  %5d  %12.12s%9d") % 
				i % label % group_num % sum_periods.Range_Format (j) % group_data [j]);
		}
	}
	End_Progress ();

	if (Progress_Count ()) {
		Print (2, "Number of Records in the Report = ") << Progress_Count ();
	}
	Header_Number (0);
}
示例#17
0
bool Path_Builder::Destination_Parking (Trip_End_Array *des_ptr, Path_End_Array *to_ptr, int parking)
{
    int index, length, offset, diff, lot, des, imped, ttime, cost, acc, low_imp, hi_imp, tot_imp, tod;
    bool link_flag, ab_flag;

    Location_Data *loc_ptr;
    Link_Data *link_ptr;
    Parking_Data *lot_ptr;
    Park_Nest_Itr park_itr;
    Trip_End_Itr des_itr;
    Path_End path_end;
    Path_Data path_data;
    List_Data *acc_ptr;
    Access_Data *access_ptr;

    to_ptr->clear ();
    low_imp = MAX_INTEGER;
    hi_imp = 0;

    park_flag = walk_acc_flag = access_flag = false;

    for (des=0, des_itr = des_ptr->begin (); des_itr != des_ptr->end (); des_itr++, des++) {
        if (des_itr->Index () < 0) continue;

        link_flag = false;
        loc_ptr = &exe->location_array [des_itr->Index ()];

        //---- add the access links to parking ----

        if (exe->access_link_flag) {
            acc = exe->loc_access [des_itr->Index ()].Next (!forward_flag);

            //---- process each access link to the destination ----

            for (; acc >= 0; acc = acc_ptr->Next (ab_flag)) {
                access_ptr = &exe->access_array [acc];
                acc_ptr = &exe->next_access (!forward_flag) [acc];

                ab_flag = (access_ptr->From_Type () == LOCATION_ID && access_ptr->From_ID () == des_itr->Index ());

                if (access_ptr->Type (!ab_flag) != PARKING_ID) continue;
                lot = access_ptr->ID (!ab_flag);

                if (!forward_flag && parking >= 0 && parking != lot) continue;

                //---- save a parking connection ----

                lot_ptr = &exe->parking_array [lot];

                if (lot_ptr->Link () == loc_ptr->Link ()) link_flag = true;

                path_end.Clear ();
                path_end.Trip_End (des);
                path_end.End_Type (LOCATION_ID);
                path_end.Type (LINK_ID);
                path_end.Index (lot_ptr->Link ());

                if (lot_ptr->Dir () == 1) {
                    link_ptr = &exe->link_array [lot_ptr->Link ()];
                    path_end.Offset (link_ptr->Length () - lot_ptr->Offset ());
                } else {
                    path_end.Offset (lot_ptr->Offset ());
                }
                path_data.Clear ();
                tot_imp = 0;
                tod = des_itr->Time ();

                //--- save the access link attributes ----

                ttime = access_ptr->Time ();
                length = (int) (ttime * param.walk_speed + 0.5);
                cost = Round (access_ptr->Cost ());
                imped = DTOI (ttime * param.value_walk + cost * param.value_cost);

                tot_imp += imped;
                tod += ((!forward_flag) ? ttime : -ttime);

                path_data.Imped (imped);
                path_data.Time (ttime);
                path_data.Length (length);
                path_data.Cost (cost);
                path_data.Walk (length);
                path_data.Mode (WALK_MODE);
                path_data.From (acc);
                path_data.Type (ACCESS_ID);
                if (ab_flag) path_data.Dir (1);

                path_end.push_back (path_data);

                //---- calculate the parking attributes ----

                cost = ttime = 0;

                for (park_itr = lot_ptr->begin (); park_itr != lot_ptr->end (); park_itr++) {
                    if (park_itr->Start () <= tod && tod <= park_itr->End ()) {
                        if (park_itr->Use () == 0 || Use_Permission (park_itr->Use (), param.use)) {
                            if (!forward_flag) {
                                ttime = park_itr->Time_Out ();
                            } else {
                                cost = DTOI (park_itr->Hourly () * parking_duration.Hours ());

                                if (cost > park_itr->Daily ()) {
                                    cost = park_itr->Daily ();
                                }
                                ttime = park_itr->Time_In ();
                                cost = Round (cost);
                            }
                            break;
                        } else {
                            park_flag = true;
                        }
                    }
                }
                imped = DTOI (ttime * param.value_park + cost * param.value_cost);

                //---- add the parking penalty ----

                if (forward_flag && param.park_pen_flag) {
                    imped += exe->park_penalty [lot];
                }
                tot_imp += imped;
                tod += (!forward_flag) ? ttime : -ttime;

                //---- save the parking lot attributes ----

                path_data.Clear ();
                path_data.Imped (imped);
                path_data.Time (ttime);
                path_data.Cost (cost);
                path_data.Mode (OTHER_MODE);
                path_data.From (lot);
                path_data.Type (PARKING_ID);

                path_end.push_back (path_data);

                //---- check the impedance range ----

                if (tot_imp < low_imp) low_imp = tot_imp;
                if (tot_imp > hi_imp) hi_imp = tot_imp;

                path_data.Clear ();

                path_end.push_back (path_data);
                to_ptr->push_back (path_end);
            }
        }
        if (link_flag) continue;

        //---- find the closest parking lot on the link ----

        diff = MAX_INTEGER;
        lot = -1;
        index = loc_ptr->Link ();

        link_ptr = &exe->link_array [index];

        if (loc_ptr->Dir () == 1) {
            offset = link_ptr->Length () - loc_ptr->Offset ();
        } else {
            offset = loc_ptr->Offset ();
        }

        for (index = exe->link_parking [index]; index >= 0; index = exe->next_parking [index]) {
            if (!forward_flag && parking >= 0 && parking != index) continue;

            lot_ptr = &exe->parking_array [index];

            if (lot_ptr->Dir () == 1) {
                length = abs (link_ptr->Length () - lot_ptr->Offset () - offset);
            } else {
                length = abs (lot_ptr->Offset () - offset);
            }
            if (length <= diff) {
                diff = length;
                lot = index;
                if ((!forward_flag && parking >= 0) ||
                        (length <= near_offset && loc_ptr->Location () == lot_ptr->Parking ())) break;
            }
        }
        if (lot < 0) continue;
        if (diff > near_offset && !Use_Permission (link_ptr->Use (), WALK)) {
            walk_acc_flag = true;
            continue;
        }

        //---- save a parking lot ----

        lot_ptr = &exe->parking_array [lot];

        path_end.Clear ();
        path_end.Trip_End (des);
        path_end.End_Type (LOCATION_ID);
        path_end.Type (LINK_ID);
        path_end.Index (lot_ptr->Link ());

        if (lot_ptr->Dir () == 1) {
            path_end.Offset (link_ptr->Length () - lot_ptr->Offset ());
        } else {
            path_end.Offset (lot_ptr->Offset ());
        }
        path_data.Clear ();
        tot_imp = 0;
        tod = des_itr->Time ();

        //---- walk from the location to the link ----

        length = loc_ptr->Setback ();
        if (length < 1) length = 1;

        if (!param.walk_detail && diff > near_offset) length += diff;

        ttime = (int) (length / param.walk_speed + 0.5);
        imped = DTOI (ttime * param.value_walk);

        tot_imp += imped;
        tod += (!forward_flag) ? ttime : -ttime;

        path_data.Imped (imped);
        path_data.Time (ttime);
        path_data.Length (length);
        path_data.Walk (length);
        path_data.Mode (WALK_MODE);
        path_data.From (des_itr->Index ());
        path_data.Type (LOCATION_ID);

        path_end.push_back (path_data);

        //---- walk along the link ----

        if (param.walk_detail && diff > near_offset) {
            length = diff;

            ttime = (int) (length / param.walk_speed + 0.5);
            imped = DTOI (ttime * param.value_walk);

            tot_imp += imped;
            tod += (!forward_flag) ? ttime : -ttime;

            path_data.Imped (imped);
            path_data.Time (ttime);
            path_data.Length (length);
            path_data.Walk (length);
            path_data.Mode (WALK_MODE);
            path_data.From (lot_ptr->Link ());
            path_data.Type (LINK_ID);
            if (!forward_flag) {
                if (path_end.Offset () < offset) path_data.Dir (1);
            } else {
                if (path_end.Offset () > offset) path_data.Dir (1);
            }
            path_end.push_back (path_data);
        }

        //---- calculate the parking attributes ----

        cost = ttime = 0;

        for (park_itr = lot_ptr->begin (); park_itr != lot_ptr->end (); park_itr++) {
            if (park_itr->Start () <= tod && tod <= park_itr->End ()) {
                if (park_itr->Use () == 0 || Use_Permission (park_itr->Use (), param.use)) {
                    if (!forward_flag) {
                        ttime = park_itr->Time_Out ();
                    } else {
                        cost = DTOI (park_itr->Hourly () * parking_duration.Hours ());

                        if (cost > park_itr->Daily ()) {
                            cost = park_itr->Daily ();
                        }
                        ttime = park_itr->Time_In ();
                        cost = Round (cost);
                    }
                    break;
                } else {
                    park_flag = true;
                }
            }
        }
        imped = DTOI (ttime * param.value_park + cost * param.value_cost);

        //---- add the parking penalty ----

        if (forward_flag && param.park_pen_flag) {
            imped += exe->park_penalty [lot];
        }
        tot_imp += imped;

        //---- save the parking lot attributes ----

        path_data.Clear ();
        path_data.Imped (imped);
        path_data.Time (ttime);
        path_data.Cost (cost);
        path_data.Mode (OTHER_MODE);
        path_data.From (lot);
        path_data.Type (PARKING_ID);

        path_end.push_back (path_data);

        //---- check the impedance range ----

        if (tot_imp < low_imp) low_imp = tot_imp;
        if (tot_imp > hi_imp) hi_imp = tot_imp;

        path_data.Clear ();

        path_end.push_back (path_data);

        to_ptr->push_back (path_end);
    }
    if (low_imp < MAX_INTEGER) {
        imp_diff = hi_imp - low_imp;
    } else {
        imp_diff = 0;
    }
    access_flag = (to_ptr->size () == 0);
    return (!access_flag);
}
示例#18
0
void  Dtime::Time_String (string text, bool duration, Units_Type time_format)
{
	//---- NOON ----
	//---- MIDNIGHT ----
	//---- d@hh:mm:ss.x -----
	//---- d@hh:mm -----
	//---- [email protected] ----
	//---- d@ssssss ----
	//---- hh:mm:ss -----
	//---- hh:mm -----
	//---- hh:mmpm -----
	//---- hh.xxx ----
	//---- ssssss ----
	//---- dddhh:mm ----
	
	static const char *day_text [] = {
		"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT", "WKE", "WKD", "ALL", 0
	};
	int hour, min_time, hour_time, day_time;
	size_t index;
	char ch;
	bool sign;
	String result, time;
	Strings clock;
	Str_Itr itr;

	if (time_format == NO_UNITS) time_format = Time_Format ();

	dtime = 0;
	time = text;
	time.Trim (" \t");

	if (time.empty ()) return;

	//---- negative ----

	if (time [0] == '-') {
		if (!duration && time_format != SECONDS && time_format != MINUTES) {
			if (!warning_flag) goto time_error;
			return;
		}
		time.erase (0, 1);
		sign = true;
	} else {
		sign = false;
	}
	min_time = Round (60);
	hour_time = min_time * 60;
	day_time = hour_time * 24;

	//---- day ----

	if (time.find ("@") != time.npos) {
		if (time.Split (result, "@")) {
			if (result.empty () || result [0] < '0' || result [0] > '9') goto time_error;

			dtime = result.Integer () * day_time;
			if (time.empty ()) goto finish;
		}
	}

	//---- check for special codes ----

	if (time [0] < '0' || time [0] > '9') {
		if (time.Equals ("NOON")) {
			dtime += day_time / 2;
			goto finish;
		} 
		if (time.Equals ("MIDNIGHT")) {
			goto finish;
		}

		//---- VERSION3 Time Codes ----

		for (int i=0; day_text [i] != 0; i++) {
			if (time.Starts_With (day_text [i])) {
				time.erase (0, strlen (day_text [i]));

				if (time.empty ()) goto finish;
				if (time.find (":") != time.npos) {
					if (time.Split (result, ":")) {
						dtime = Round (result.Integer () * 3600 + time.Integer () * 60);
					}
				} else if (time [0] < '0' || time [0] > '9') {
					goto time_error;
				} else {
					dtime = Round (time.Integer () * 3600);
				}
				goto finish;
			}
		}
	}

	//---- check time units ----

	if (time.Parse (clock, ":") < 1) goto finish;

	itr = clock.begin ();
	ch = (*itr) [0];

	if (clock.size () == 1) {
		if ((ch < '0' || ch > '9') && ch != '.') goto time_error;

		if (time_format == SECONDS) {
			dtime += Round (itr->Double ()); 
		} else if (time_format == MINUTES) {
			dtime += DTOI (itr->Double () * min_time);
		} else if (time_format == HOURS) {
			dtime += DTOI (itr->Double () * hour_time); 
		} else if (itr->find ('.') != itr->npos) {
			dtime += DTOI (itr->Double () * hour_time); 
		} else {
			dtime += Round (itr->Double ());
		}
		goto finish;
	}

	//---- hour of the day ----

	if (ch < '0' || ch > '9') goto time_error;

	hour = itr->Integer ();

	dtime += hour * hour_time;

	if (++itr == clock.end ()) goto finish;

	//---- minute of the hour ----
	
	ch = (*itr) [0];
	if (ch < '0' || ch > '5' || itr->length () != 2) goto time_error;

	dtime += itr->Integer () * min_time;

	if (++itr == clock.end ()) goto finish;

	//---- seconds of the minute ----

	ch = (*itr) [0];
	if (ch < '0' || ch > '5') goto time_error;

	index = itr->find ('.');

	if (index != itr->npos) {
		if (index != 2) goto time_error;
		dtime += Round (itr->Double ()); 
	} else {
		if (itr->length () != 2) goto time_error;
		dtime += Round (itr->Double ());
	}
finish:
	if (sign) dtime = -dtime;
	return;

time_error:
	exe->Warning ("Time Conversion ") << text;
	warning_flag = true;
}
示例#19
0
void RoutePrep::Transfer_Link (void)
{
	int link_index, dir_index, length, best_len, best;
	double dx, dy, speed;

	Ints_Itr st1_itr, st2_itr;
	Int_Itr a_itr, b_itr;
	Node_Data *a_ptr, *b_ptr;
	Link_Data link_rec;
	Dir_Data dir_rec;

	if (station_nodes.size () < 2) return;

	link_rec.Use (Use_Code ("WALK"));
	link_rec.Type (WALKWAY);
	link_rec.Divided (1);

	link_rec.Name ("Transfer Link");

	dir_rec.Lanes (1);
	dir_rec.Speed (Internal_Units (3.0, MPH));
	dir_rec.Capacity (1000);
	speed = UnRound (dir_rec.Speed ());

	for (st1_itr = station_nodes.begin (); st1_itr != station_nodes.end (); st1_itr++) {
		st2_itr = st1_itr + 1;
		if (st2_itr == station_nodes.end ()) break;

		for (a_itr = st1_itr->begin (); a_itr != st1_itr->end (); a_itr++) {
			a_ptr = &node_array [*a_itr];

			best = -1; 
			best_len = MAX_INTEGER;

			for (b_itr = st2_itr->begin (); b_itr != st2_itr->end (); b_itr++) {
				b_ptr = &node_array [*b_itr];

				dx = a_ptr->X () - b_ptr->X ();
				dy = a_ptr->Y () - b_ptr->Y ();

				length = DTOI (sqrt (dx * dx + dy * dy));

				if (length < best_len) {
					best = *b_itr;
					best_len = length;
				}
			}
			if (best < 0) continue;

			link_index = (int) link_array.size ();
			dir_index = (int) dir_array.size ();
	
			link_rec.Anode (*a_itr);
			link_rec.Bnode (best);
			link_rec.Length (best_len);
			dir_rec.Time0 (UnRound (best_len) / speed);

			link_rec.AB_Dir (dir_index);

			dir_rec.Link (link_index);
			dir_map.insert (Int_Map_Data (dir_rec.Link_Dir (), dir_index));
			dir_array.push_back (dir_rec);

			link_rec.BA_Dir (++dir_index);

			dir_rec.Dir (1);

			dir_map.insert (Int_Map_Data (dir_rec.Link_Dir (), dir_index));
			dir_array.push_back (dir_rec);

			link_rec.Link (new_link++);
			link_map.insert (Int_Map_Data (link_rec.Link (), link_index));
			link_array.push_back (link_rec);
		}
	}
}
示例#20
0
void LinkSum::Top_100_Report (int type)
{
	int i, j, k, anode, bnode, an, bn, max_tim, max_an, max_bn;
	int tod_list, lanes, max_lanes, index, flow_index;
	double flow, min_flow, max_flow;
	Dtime low, high, tod;

	Link_Itr link_itr;
	Dir_Data *dir_ptr;
	Link_Perf_Period_Itr period_itr;
	Flow_Time_Data flow_time;
	Lane_Use_Period *use_ptr;

	//---- Top 100 Data ----

	typedef struct {
		int    link;
		int    from;
		int    to;
		int    period;
		double flow;
	} Flow_Data;

	Flow_Data flow_data [100], *flow_ptr;

	Show_Message ("Creating the Top 100 Flow Report -- Record");
	Set_Progress ();

	memset (flow_data, '\0', 100 * sizeof (Flow_Data));

	//---- find Top 100 Flows ----

	min_flow = 0;

	for (link_itr = link_array.begin (); link_itr != link_array.end (); link_itr++) {
		Show_Progress ();

		if (select_flag && link_itr->Use () == 0) continue;

		an = node_array [link_itr->Anode ()].Node ();
		bn = node_array [link_itr->Bnode ()].Node ();

		for (i=0; i < 2; i++) {
			if (i) {
				if (link_itr->Use () == -1) continue;
				index = link_itr->BA_Dir ();
				anode = bn;
				bnode = an;
			} else {
				if (link_itr->Use () == -2) continue;
				index = link_itr->AB_Dir ();
				anode = an;
				bnode = bn;
			}
			if (index < 0) continue;
			dir_ptr = &dir_array [index];

			max_flow = max_tim = max_an = max_bn = 0;

			max_lanes = lanes = dir_ptr->Lanes ();
			tod_list = dir_ptr->First_Lane_Use ();
			flow_index = dir_ptr->Flow_Index ();

			//---- scan each time period ----

			for (j=0, period_itr = link_perf_array.begin (); period_itr != link_perf_array.end (); period_itr++, j++) {
				flow_time = period_itr->Total_Flow_Time (index, flow_index);

				flow = flow_time.Flow ();
				if (flow < minimum_flow) continue;

				if (type == TOP_PERIOD) {
					if (flow <= min_flow) continue;

					flow_ptr = flow_data;

					for (k=0; k < 100; k++, flow_ptr++) {
						if (flow > flow_ptr->flow) {
							if (flow_ptr->flow > 0 && k < 99) {
								memmove (flow_ptr+1, flow_ptr, (99-k) * sizeof (Flow_Data));
								min_flow = flow_data [99].flow;
							}
							flow_ptr->link = link_itr->Link ();
							flow_ptr->from = anode;
							flow_ptr->to = bnode;
							flow_ptr->period = j;
							flow_ptr->flow = flow;
							break;
						}
					}
				} else {
					if (type == TOP_LANE_FLOW) {
						if (tod_list >= 0) {

							//---- get the time period ----

							sum_periods.Period_Range (j, low, high);
							tod = (low + high + 1) / 2;

							lanes = max_lanes;
							k = tod_list;

							for (use_ptr = &use_period_array [k]; ; use_ptr = &use_period_array [++k]) {
								if (use_ptr->Start () <= tod && tod < use_ptr->End ()) {
									lanes = use_ptr->Lanes0 () + use_ptr->Lanes1 ();
									break;
								}
								if (use_ptr->Periods () == 0) break;
							}
						}
						if (lanes > 1) flow /= lanes;
					}
					flow = DTOI (flow);

					if (flow > max_flow) {
						max_flow = flow;
						max_tim = j;
						max_an = anode;
						max_bn = bnode;
					}
				}
			}

			//---- add to the list ----

			if (max_flow > min_flow) {
				flow_ptr = flow_data;

				for (k=0; k < 100; k++, flow_ptr++) {
					if (max_flow > flow_ptr->flow) {
						if (flow_ptr->flow > 0 && k < 99) {
							memmove (flow_ptr+1, flow_ptr, (99-k) * sizeof (Flow_Data));							
							min_flow = flow_data [99].flow;
						}
						flow_ptr->link = link_itr->Link ();
						flow_ptr->from = max_an;
						flow_ptr->to = max_bn;
						flow_ptr->period = max_tim;
						flow_ptr->flow = max_flow;
						break;
					}
				}
			}
		}
	}
	End_Progress ();

	//---- print the report ----

	flow_ptr = flow_data;

	Header_Number (type);
	New_Page ();

	for (i=0; i < 100; i++, flow_ptr++) {
		if (flow_ptr->flow <= 0.0) break;

		//---- print the data record ----

		Print (1, String ("%10d%10d%10d  %12.12s%12.1lf") % flow_ptr->link % flow_ptr->from % flow_ptr->to % 
			sum_periods.Range_Format (flow_ptr->period) % flow_ptr->flow);
	}
	if (i) {
		Print (2, "Number of Records in the Report = ") << i;
	}
	Header_Number (0);
}
示例#21
0
int Data_Service::Put_Lane_Use_Data (Lane_Use_File &file, Lane_Use_Data &data)
{
	double rate, toll;

	Dir_Data *dir_ptr;
	Link_Data *link_ptr;
	Veh_Type_Data *veh_type_ptr;

	dir_ptr = &dir_array [data.Dir_Index ()];
	link_ptr = &link_array [dir_ptr->Link ()];

	file.Link (link_ptr->Link ());
	file.Dir (dir_ptr->Dir ());

	file.Lanes (Make_Lane_Range (dir_ptr, data.Low_Lane (), data.High_Lane ()));

	file.Use (data.Use ());
	file.Type (data.Type ());

	if (data.Min_Veh_Type () < 0) {
		file.Min_Veh_Type (0);
	} else if (veh_type_array.size () > 0) {
		veh_type_ptr = &veh_type_array [data.Min_Veh_Type ()];
		file.Min_Veh_Type (veh_type_ptr->Type ());
	} else {
		file.Min_Veh_Type (data.Min_Veh_Type ());
	}
	if (data.Max_Veh_Type () < 0) {
		file.Max_Veh_Type (0);
	} else if (veh_type_array.size () > 0) {
		veh_type_ptr = &veh_type_array [data.Max_Veh_Type ()];
		file.Max_Veh_Type (veh_type_ptr->Type ());
	} else {
		file.Max_Veh_Type (data.Max_Veh_Type ());
	}
	file.Min_Traveler (MAX (data.Min_Traveler (), 0)); 
	file.Max_Traveler (MAX (data.Max_Traveler (), 0));

	file.Start (data.Start ());
	file.End (data.End ());
	file.Length (UnRound (data.Length ()));
	file.Offset (UnRound (data.Offset ()));

	toll = UnRound (data.Toll ());
	rate = UnRound (data.Toll_Rate ());

	if (rate > 0) {
		if (Metric_Flag ()) {
			rate /= 1000.0;
		} else {
			rate /= MILETOFEET;
		}
		toll -= DTOI (rate * link_ptr->Length ());
	}
	file.Toll (DTOI (toll));
	file.Toll_Rate (rate);

	file.Min_Delay (UnRound (data.Min_Delay ()));
	file.Max_Delay (UnRound (data.Max_Delay ()));
	file.Speed (UnRound (data.Speed ()));
	file.Speed_Factor (UnRound (data.Spd_Fac ()) / 10.0);
	file.Capacity (data.Capacity ());
	file.Cap_Factor (UnRound (data.Cap_Fac ()) / 10.0);
	file.Notes (data.Notes ());

	if (!file.Write ()) {
		Error (String ("Writing %s") % file.File_Type ());
	}
	return (1);
}
示例#22
0
void RiderSum::Group_Rider_Report (void)
{
	int n, run, runs, num, length, tot_len, period, num_periods, routes, index, stop, to, link;
	int max_riders, max_board, max_alight, max_runs, total;
	double factor, max_fac, sum_time, tot_time, time;
	double vmt, vht, pmt, pht;
	Dtime low, high;
	String label;
	bool flag;

	Int_Set *group;
	Int_Set_Itr itr;
	Int_Map_Itr map_itr, to_itr;
	Line_Data *line_ptr;
	Stop_Data *stop_ptr;
	Line_Stop_Itr stop_itr, next_itr;
	Line_Run_Itr run_itr;
	Int_Map stop_index;
	Int2_Map link_index;
	Int2_Map_Itr map2_itr;
	Int2_Key int2_key;
	Int2_Map_Stat map2_stat;
	Int_Map_Stat map_stat;
	Stop_Group_Data data, *data_ptr, *link_ptr;
	Stop_Group_Itr data_itr;

	Show_Message ("Line Group Profile -- Record");
	Set_Progress ();

	//---- print the report ----

	Header_Number (GROUP_RIDERS);

	num_periods = sum_periods.Num_Periods ();
	if (num_periods == 0) num_periods = 1;

	num = (int) (line_map.size () * stop_map.size ());

	if (!Break_Check (num + 5)) {
		Print (1);
		Group_Rider_Header ();
	}

	routes = 0;
	memset (&data, '\0', sizeof (data));
	
	for (n = line_equiv.First_Group (); n > 0; n = line_equiv.Next_Group ()) {

		group = line_equiv.Group_List (n);
		if (group == 0) continue;

		label = line_equiv.Group_Label (n);

		//---- process each time period ----

		for (period = 0; period < num_periods; period++) {

			//---- initialize stop data ----

			stop_index.clear ();
			link_index.clear ();
			stop_group_array.clear ();
			routes = 0;
			vmt = vht = pmt = pht = 0.0;

			//---- process each line in the line group ----

			for (itr = group->begin (); itr != group->end (); itr++) {

				map_itr = line_map.find (*itr);
				if (map_itr == line_map.end ()) continue;

				Show_Progress ();

				line_ptr = &line_array [map_itr->second];

				//---- set the run flags ----

				if (!Run_Selection (line_ptr)) continue;

				if (period_flag [period] == 0) continue;

				time = 0.0;
				total = length = 0;
				flag = false;

				sum_periods.Period_Range (period, low, high);

				for (stop_itr = line_ptr->begin (); stop_itr != line_ptr->end (); stop_itr++) {

					stop_ptr = &stop_array [stop_itr->Stop ()];
					stop = stop_ptr->Stop ();

					map_itr = stop_index.find (stop);

					next_itr = stop_itr + 1;

					if (next_itr != line_ptr->end ()) {
						stop_ptr = &stop_array [next_itr->Stop ()];

						to = stop_ptr->Stop ();
					} else {
						to = 0;
					}

					//---- create the link ----

					int2_key = Int2_Key (stop, to);

					map2_itr = link_index.find (int2_key);

					if (map2_itr == link_index.end ()) {
						if (to == 0 && map_itr != stop_index.end ()) {
							link = map_itr->second;
						} else {
							link = (int) stop_group_array.size ();

							link_index.insert (Int2_Map_Data (int2_key, link));

							data.stop = stop;
							data.to = to;
							stop_group_array.push_back (data);

							//---- check for a merge ----

							if (to != 0) {
								to_itr = stop_index.find (to);

								if (to_itr != stop_index.end ()) {
									index = (int) stop_group_array.size ();

									int2_key = Int2_Key (to, 0);

									map2_stat = link_index.insert (Int2_Map_Data (int2_key, index));

									if (map2_stat.second) {
										data.stop = to;
										data.to = 0;
										stop_group_array.push_back (data);
									}
								}
							}
						}
					} else {
						link = map2_itr->second;
					}
					link_ptr = &stop_group_array [link];

					//---- crete the stop index ----

					if (map_itr == stop_index.end ()) {
						map_stat = stop_index.insert (Int_Map_Data (stop, link));
						index = link;
					} else {
						index = map_itr->second;
					}
					data_ptr = &stop_group_array [index];

					if (next_itr != line_ptr->end ()) {
						length = next_itr->Length () - stop_itr->Length ();
					} else {
						length = 0;
					}
					sum_time = 0.0;
					num = runs = 0;

					for (run=0, run_itr = stop_itr->begin (); run_itr != stop_itr->end (); run_itr++, run++) {
						if (run_flag [run] == 0) continue;
						if (run_period [run] != period) continue;

						data_ptr->board += run_itr->Board ();
						data_ptr->alight += run_itr->Alight ();
						link_ptr->riders += run_itr->Load ();
						runs++;
						flag = true;

						if (next_itr != line_ptr->end ()) {
							link_ptr->runs++;
							time = next_itr->at (run).Schedule ().Seconds () - run_itr->Schedule ().Seconds ();
							sum_time += time;
							num++;

							vmt += length;
							vht += time;
							pmt += length * run_itr->Load ();
							pht += time * run_itr->Load ();
						}
					}
					if (runs == 0) continue;

					if (link_ptr->length == 0.0) {
						link_ptr->length = length;
					}
					if (num > 0) {
						time = sum_time / num;
					} else {
						time = 0;
					}
					if (link_ptr->time > 0) {
						link_ptr->time = (link_ptr->time + time) / 2.0;
					} else {
						link_ptr->time = time;
					}
				}
				if (flag) routes++;
			}

			//---- print the report ----

			if (!Break_Check ((int) stop_index.size () + 15)) {
				Print (1);
				Group_Rider_Header ();
			}
			Print (1, "Group   Time Period  Routes  Description");

			Print (2, String ("%5d  %12.12s  %6d  %s") % n %	sum_periods.Range_Format (period) %
				routes % label);

			Print (2, "    Stop  Length   TTime   Alight    Board   Riders  Runs  LoadFac");
			Print (1);

			tot_time = 0.0;
			max_alight = max_board = max_riders = max_runs = total = tot_len = 0;
			max_fac = 0.0;

			for (data_itr = stop_group_array.begin (); data_itr != stop_group_array.end (); data_itr++) {

				map_itr = stop_map.find (data_itr->stop);
					
				stop_ptr = &stop_array [map_itr->second];
				
				if (data_itr->runs > 0) {
					factor = DTOI (data_itr->riders * 10.0 / data_itr->runs) / 10.0;
				} else {
					factor = 0.0;
				}
				Print (1, String ("%8d %7.0lf %7.0lf %8d %8d %8d %5d %8.1lf") % data_itr->stop  % 
					UnRound (data_itr->length) % data_itr->time % data_itr->alight % data_itr->board % 
					data_itr->riders % data_itr->runs % factor);

				if (Notes_Name_Flag ()) {
					if (!stop_ptr->Name ().empty ()) {
						Print (0, String ("  %s") % stop_ptr->Name ());
					}
					if (!stop_ptr->Notes ().empty ()) {
						Print (0, " -- ") << stop_ptr->Notes ();
					}
				}
				if (data_itr->alight > max_alight) max_alight = data_itr->alight;
				if (data_itr->board > max_board) max_board = data_itr->board;
				if (data_itr->riders > max_riders) max_riders = data_itr->riders;
				if (data_itr->runs > max_runs) max_runs = data_itr->runs;
				if (factor > max_fac) max_fac = factor;

				tot_len += data_itr->length;
				tot_time += data_itr->time;
				total += data_itr->board;
			}
			if (max_runs == 0) continue;

			Print (2, String (" Maximum                 %8ld %8ld %8ld %5ld %8.1lf") %
				max_alight % max_board % max_riders % max_runs % max_fac);

			Print (2, "Total Boardings = ") << total;

			if (total == 0 || tot_time == 0) continue;

			factor = UnRound (tot_len);
			vmt = UnRound (vmt) / 5280.0;
			vht = vht / 3600.0;
			pmt = UnRound (pmt) / 5280.0;
			pht = pht / 3600.0;


			Print (1, String ("Route Length = %.1lf miles, %.1lf minutes  Average Speed = %.1lf mph") %
				External_Units (factor, MILES) % (tot_time / 60.0) % External_Units ((factor / tot_time), MPH));
			Print (1, String ("Vehicle Miles = %.1lf  Vehicle Hours = %.1lf") % vmt % vht);
			Print (1, String ("Passenger Miles = %.1lf  Passenger Hours = %.1lf") %	pmt % pht);
			Print (1, String ("Passengers per Vehicle Mile = %.1lf  Passengers per Vehicle Hour = %.1lf") %	
				(pmt / vht) % (pht / vht));

			vmt = pmt / total;
			vht = pht * 60.0 / total;

			Print (1, String ("Average Trip Length = %.1lf miles, %.1lf minutes") % vmt % vht);
		}
	}
	End_Progress ();

	Header_Number (0);
}
示例#23
0
int Sim_Node_Process::Sum_Path (Sim_Dir_Ptr sim_dir_ptr, int lane, int cell, Travel_Step &step)
{
	int cells, num_cells, sum_speed, num_lanes, num_veh, best_weight, weight;
	int speed, max_cell, dir_index, traveler, from_lane, from_index, l, high, low;

	Dir_Data *dir_ptr;
	Sim_Plan_Ptr sim_plan_ptr;
	Sim_Leg_Ptr leg_ptr, next_ptr;
	Sim_Park_Ptr sim_park_ptr;
	Sim_Travel_Ptr sim_travel_ptr;

	//---- initial conditions ----

	num_cells = sim->Offset_Cell (sim->param.look_ahead);
	sum_speed = num_lanes = num_veh = 0;

	speed = step.sim_travel_ptr->Speed ();
	sim_plan_ptr = step.sim_travel_ptr->Get_Plan ();

	//---- get the plan data ----

	leg_ptr = step.sim_leg_ptr;
	next_ptr = sim_plan_ptr->Get_Next (leg_ptr);

	dir_index = leg_ptr->Index ();
	max_cell = sim_dir_ptr->Max_Cell ();

	if (next_ptr != 0 && next_ptr->Type () == PARKING_ID) {
		sim_park_ptr = &sim->sim_park_array [next_ptr->Index ()];

		if (sim_park_ptr->Type () != BOUNDARY) {
			if (sim_dir_ptr->Dir () == 0) {
				max_cell = sim->Offset_Cell (sim_park_ptr->Offset_AB ());
			} else {
				max_cell = sim->Offset_Cell (sim_park_ptr->Offset_BA ());
			}
		}
	}

	for (cells=1; cells <= num_cells; cells++) {
		cell++;

		//---- move forward ----

		if (cell > max_cell) {

			//---- set the exit speed ----

			if (leg_ptr->Max_Speed () < speed) {
				speed = leg_ptr->Max_Speed ();
			}
		
			//---- check the exit lane ----

			if (lane < leg_ptr->Out_Lane_Low ()) {
				num_lanes += leg_ptr->Out_Lane_Low () - lane;
				lane = leg_ptr->Out_Lane_Low ();
				speed = 0;
			} else if (lane > leg_ptr->Out_Lane_High ()) {
				num_lanes += lane - leg_ptr->Out_Lane_High ();
				lane = leg_ptr->Out_Lane_High ();
				speed = 0;
			}
			from_lane = lane;
			from_index = dir_index;

			if (next_ptr == 0 || next_ptr->Type () != DIR_ID) break;

			dir_index = next_ptr->Index ();

			sim_dir_ptr = &sim->sim_dir_array [dir_index];

			if (sim_dir_ptr->Method () < MESOSCOPIC) break;

			leg_ptr = next_ptr;

			//---- find the best connection ----

			lane = -1;
			best_weight = 0;

			low = leg_ptr->In_Lane_Low ();
			high = leg_ptr->In_Lane_High ();

			for (l=low; l < high; l++) {
				weight = 0;
				if (sim_dir_ptr->Thru_Link (l) == from_index && sim_dir_ptr->Thru_Lane (l) == from_lane) {
					weight += sim->param.connect_lane_weight;
				}
				if (Cell_Use (sim_dir_ptr, l, 0, step)) {
					weight += sim->param.lane_use_weight;
				}
				if (l >= leg_ptr->In_Best_Low () && l <= leg_ptr->In_Best_High ()) {
					weight += sim->param.connect_lane_weight;
				}

				if (weight > best_weight) {
					lane = l;
					best_weight = weight;
				}
			}
			if (lane < 0) break;
			cell = 0;

			next_ptr = sim_plan_ptr->Get_Next (leg_ptr);

			max_cell = sim_dir_ptr->Max_Cell ();

			if (next_ptr != 0 && next_ptr->Type () == PARKING_ID) {
				sim_park_ptr = &sim->sim_park_array [next_ptr->Index ()];

				if (sim_park_ptr->Type () != BOUNDARY) {
					if (sim_dir_ptr->Dir () == 0) {
						max_cell = sim->Offset_Cell (sim_park_ptr->Offset_AB ());
					} else {
						max_cell = sim->Offset_Cell (sim_park_ptr->Offset_BA ());
					}
				}
			}
		}

		//---- check the use restrictions ----

		if (!Cell_Use (sim_dir_ptr, lane, cell, step)) {
			if (lane > 0) {
				while (lane > 0) {
					num_lanes++;
					if (Cell_Use (sim_dir_ptr, --lane, cell, step)) break;
				}
			} else {
				while (lane < (sim_dir_ptr->Lanes () - 1)) {
					num_lanes++;
					if (Cell_Use (sim_dir_ptr, ++lane, cell, step)) break;
				}
			}
			speed = 0;

		} else {

			//---- check the cell availability -----

			traveler = sim_dir_ptr->Get (lane, cell);

			if (traveler == -1) {

				//---- pocket lane ----

				dir_ptr = &sim->dir_array [dir_index];

				if (lane <= dir_ptr->Left ()) {
					while (lane <= dir_ptr->Left ()) {
						num_lanes++;
						if (sim_dir_ptr->Get (++lane, cell) >= 0) break;
					}
				} else {
					while (lane >= (dir_ptr->Left () + dir_ptr->Lanes ())) {
						num_lanes++;
						if (sim_dir_ptr->Get (--lane, cell) >= 0) break;
					}
				}
				speed = 0;

			} else {

				//---- accelerate ----

				speed += step.veh_type_ptr->Max_Accel ();

				if (speed > step.veh_type_ptr->Max_Speed ()) {
					speed = step.veh_type_ptr->Max_Speed ();
				}
				if (speed > sim_dir_ptr->Speed ()) {
					speed = sim_dir_ptr->Speed ();
				}

				//---- check the vehicle speed ----

				traveler = abs (traveler);

				if (traveler > 1) {
					sim_travel_ptr = &sim->sim_travel_array [traveler];

					if (sim_travel_ptr->Speed () < speed) {
						speed = sim_travel_ptr->Speed ();
					}
					num_veh++;
				}
			}
		}
		sum_speed += speed - step.Delay ();
	}
	sim->Offset_Cell (sum_speed);
	sum_speed = DTOI (sum_speed * sim->param.time_factor - num_lanes * sim->param.lane_factor - num_veh * sim->param.veh_factor);

	return (sum_speed);	
}
示例#24
0
bool Data_Service::Get_Lane_Use_Data (Lane_Use_File &file, Lane_Use_Data &lane_use_rec)
{
	int link, dir, lanes, offset, dir_index, low, high;
	double rate;

	Link_Data *link_ptr;
	Int_Map_Itr map_itr;

	//---- check/convert the link number and direction ----

	link = file.Link ();
	if (link <= 0) return (false);

	dir = file.Dir ();
	offset = Round (file.Offset ());

	link_ptr = Set_Link_Direction (file, link, dir, offset);

	if (link_ptr == 0) return (false);

	if (dir) {
		dir_index = link_ptr->BA_Dir ();
	} else {
		dir_index = link_ptr->AB_Dir ();
	}
	if (dir_index < 0) {
		Warning (String ("Lane Use %d Link %d Direction %s was Not Found") % Progress_Count () % link_ptr->Link () % ((dir) ? "BA" : "AB"));
		return (false);
	}
	lane_use_rec.Dir_Index (dir_index);

	//---- set the restriction type ----

	lane_use_rec.Type (file.Type ());
	lane_use_rec.Use (file.Use ());

	//---- convert the vehicle type range ----

	low = file.Min_Veh_Type ();
	high = file.Max_Veh_Type ();

	if (low > 0) {
		map_itr = veh_type_map.find (low);
		if (map_itr == veh_type_map.end ()) {
			map_itr = veh_type_map.lower_bound (low);
			if (map_itr == veh_type_map.end ()) {
				Warning (String ("Lane Use %d Vehicle Type %d was Not Found") % Progress_Count () % low);
				low = -1;
			} else {
				low = map_itr->second;
			}
		} else {
			low = map_itr->second;
		}
	} else {
		low = -1;
	}
	if (high > 0) {
		map_itr = veh_type_map.find (high);
		if (map_itr == veh_type_map.end ()) {
			int h = high;
			while (h > 0) {
				map_itr = veh_type_map.find (--h);
				if (map_itr != veh_type_map.end ()) break;
			}
			if (h >= 0) {
				high = map_itr->second;
			} else {
				Warning (String ("Lane Use %d Vehicle Type %d was Not Found") % Progress_Count () % high);
				high = -1;
			}
		} else {
			high = map_itr->second;
		}
		if (high >= 0 && low < 0) low = 0;
	} else {
		high = -1;
	}
	if (low > high) {
		if (high == -1) {
			high = low;
		} else {
			Warning (String ("Lane Use %d Vehicle Type Range %d-%d is Illegal") % Progress_Count () % file.Min_Veh_Type () % file.Max_Veh_Type ());
			high = low;
		}
	}
	lane_use_rec.Min_Veh_Type (low);
	lane_use_rec.Max_Veh_Type (high);

	//---- convert the traveler type range ----

	low = file.Min_Traveler ();
	high = file.Max_Traveler ();

	if (low <= 0) low = -1;
	if (high > 0) {
		if (low < 0) low = 0;
	} else {
		high = -1;
	}
	if (low > high) {
		if (high == -1) {
			high = low;
		} else {
			Warning (String ("Lane Use %d Traveler Type Range %d-%d is Illegal") % Progress_Count () % file.Min_Traveler () % file.Max_Traveler ());
			high = low;
		}
	}
	lane_use_rec.Min_Traveler (low);
	lane_use_rec.Max_Traveler (high);

	//----- length and offset ----

	lane_use_rec.Length (file.Length ());

	if (lane_use_rec.Length () > 0) {
		if (offset > 0 || lane_use_rec.Length () < link_ptr->Length ()) {
			lane_use_rec.Offset (offset);
		} else {
			lane_use_rec.Offset (0);
			lane_use_rec.Length (0);
		}
	} else {
		lane_use_rec.Offset (0);
	}

	//---- lane number ----

	lanes = file.Lanes ();

	if (file.Version () <= 40 && lanes > 0) {
		low = high = lanes - 1;
	} else {
		Convert_Lane_Range (dir_index, lanes, low, high);
	}
	lane_use_rec.Low_Lane (low);
	lane_use_rec.High_Lane (high);

	//----- optional fields ----

	lane_use_rec.Start (file.Start ());
	lane_use_rec.End (file.End ());
	if (lane_use_rec.End () == 0) lane_use_rec.End (Model_End_Time ());

	lane_use_rec.Toll (Round (file.Toll ()));
	lane_use_rec.Toll_Rate (file.Toll_Rate ());

	if (lane_use_rec.Toll_Rate () > 0) {
		rate = UnRound (lane_use_rec.Toll_Rate ());

		if (Metric_Flag ()) {
			rate /= 1000.0;
		} else {
			rate /= MILETOFEET;
		}
		lane_use_rec.Toll (lane_use_rec.Toll () + DTOI (rate * link_ptr->Length ()));
	}
	lane_use_rec.Min_Delay (file.Min_Delay ());
	lane_use_rec.Max_Delay (file.Max_Delay ());
	lane_use_rec.Speed (file.Speed ());	
	lane_use_rec.Spd_Fac (file.Speed_Factor ());
	lane_use_rec.Capacity (file.Capacity ());
	lane_use_rec.Cap_Fac (file.Cap_Factor ());
	lane_use_rec.Notes (file.Notes ());

	return (true);
}
示例#25
0
bool Path_Builder::Best_Lane_Use (int index, Dtime time, double len_factor, Dtime &ttime, Dtime &delay, int &cost, int &group)
{
	int i, num, tt, cst, imp, lanes, type, grp, use_type [2], costs [2], best [2];
	Dtime ttimes [2], delays [2];
	double factor;

	Dir_Data *dir_ptr;
	Lane_Use_Data *use_ptr;
	Int_Map_Stat map_stat;
	Lane_Use_Period *period_ptr;
	Link_Dir_Data *use_index;

	dir_ptr = &exe->dir_array [index];

	if (path_param.delay_flag) {
		ttimes [0] = exe->perf_period_array.Travel_Time (index, time, len_factor, forward_flag);
		if (dir_ptr->Use_Index () >= 0) {
			ttimes [1] = exe->perf_period_array.Travel_Time (dir_ptr->Use_Index (), time, len_factor, forward_flag);
		}
	}
	if (ttimes [0] < 0 && ttimes [1] <= 0) return (false);

	if (ttimes [0] == 0) ttimes [0] = dir_ptr->Time0 ();
	if (ttimes [1] == 0) ttimes [1] = dir_ptr->Time0 ();

	ttime = ttimes [0];
	delay = 0;
	cost = group = 0;

	//---- find the time period ----

	index = dir_ptr->First_Lane_Use ();
	if (index < 0) return (true);

	for (period_ptr = &exe->use_period_array [index]; ; period_ptr = &exe->use_period_array [++index]) {
		if (period_ptr->Start () <= time && time < period_ptr->End ()) break;
		if (period_ptr->Periods () == 0) return (true);
	}

	//---- set the lane group data ----

	best [0] = best [1] = 0;
	delays [0] = delays [1] = 0;
	costs [0] = costs [1] = 0;

	use_type [0] = (period_ptr->Lanes (0) == 0) ? PROHIBIT : APPLY;
	use_type [1] = (period_ptr->Lanes (1) == 0) ? PROHIBIT : APPLY;

	type = path_param.traveler_type;

	num = period_ptr->Records ();
	index = period_ptr->Index ();

	for (i=0; i < num; i++, index++) {
		use_index = &exe->use_period_index [index];
		use_ptr = &exe->lane_use_array [use_index->Link ()];

		grp = use_index->Dir ();
		if (use_type [grp] == PROHIBIT) continue;

		lanes = use_ptr->High_Lane () - use_ptr->Low_Lane () + 1;

		if (lanes >= period_ptr->Lanes (grp)) {
			if (path_param.veh_type < 0 || use_ptr->Min_Veh_Type () < 0 || 
				(use_ptr->Min_Veh_Type () <= path_param.veh_type && path_param.veh_type <= use_ptr->Max_Veh_Type ())) {

				if (type == 0 || use_ptr->Min_Traveler () < 0 || 
					(use_ptr->Min_Traveler () <= type && type <= use_ptr->Max_Traveler ())) {

					if (Use_Permission (use_ptr->Use (), path_param.use)) {
						if (use_ptr->Type () == APPLY) {
							cst = use_ptr->Toll ();
							tt = use_ptr->Min_Delay ();
							if (use_ptr->Max_Delay () > use_ptr->Min_Delay ()) {
								map_stat = lane_use_delay.insert (Int_Map_Data (use_index->Link_Dir (), 0));
								if (map_stat.second) {
									map_stat.first->second = DTOI ((use_ptr->Max_Delay () - use_ptr->Min_Delay ()) * path_param.random.Probability ());
								}
								tt += map_stat.first->second;
							}
							imp = Resolve (tt * path_param.value_time + cst * path_param.value_cost);
							if (imp < best [grp] || best [grp] == 0) {
								delays [grp] = tt;
								costs [grp] = cst;
								best [grp] = imp;
							}
							use_type [grp] = APPLY;
						} else if (use_ptr->Type () == PROHIBIT) {
							use_type [grp] = PROHIBIT;
						} else if (use_ptr->Type () == REQUIRE) {
							use_type [1-grp] = PROHIBIT;
						}
						continue;
					}
				}
			}
			if (use_ptr->Type () == LIMIT) {
				use_type [grp] = PROHIBIT;
			}
		}
	}

	//---- select the best lane group ----

	if (use_type [0] == PROHIBIT && use_type [1] == PROHIBIT) return (false);

	if (use_type [0] == APPLY && use_type [1] == APPLY) {
		best [0] += Resolve (ttimes [0] * path_param.value_time);
		best [1] += Resolve (ttimes [1] * path_param.value_time);

		if (random_flag) {
			factor = 1.0 + path_param.random_imped * (path_param.random.Probability () - 0.5) / 100.0;
			best [1] = DTOI (best [1] * factor);
		}
		if (best [1] > 0 && best [0] > 0) {
			group = (best [1] < best [0]) ? 1 : 0;
		} else {
			group = 0;
		}
	} else if (use_type [0] == APPLY) {
		group = 0;
	} else {
		group = 1;
	}
	delay = delays [group];
	cost = costs [group];
	ttime = ttimes [group];
	return ((ttime > 0));
}
示例#26
0
bool Sim_Method::Cell_Use (Sim_Lane_Data *lane_ptr, int lane, int cell, Travel_Step &step, bool use_flag)
{
	int i, index, num, seed;
	Lane_Use_Data *use_ptr;
	Lane_Use_Period *period_ptr;
	Link_Dir_Data *use_index;

	int type = step.sim_traveler_ptr->Type ();
	int veh_type = step.sim_veh_ptr->Type ();
	Use_Type use = step.veh_type_ptr->Use ();

	step.Delay (0);

	if (use_flag) {
		if (use != BUS && use != HOV2 && use != HOV3 && use != HOV4) return (false);
		if (Use_Permission (lane_ptr->Use (), SOV)) return (false);
	}
	if (!Use_Permission (lane_ptr->Use (), use)) return (false);

	if (veh_type >= 0 && lane_ptr->Min_Veh_Type () >= 0) {
		if (lane_ptr->Type () == PROHIBIT) {
			if (veh_type >= lane_ptr->Min_Veh_Type () && veh_type <= lane_ptr->Max_Veh_Type ()) return (false);
		} else {
			if (veh_type < lane_ptr->Min_Veh_Type () || veh_type > lane_ptr->Max_Veh_Type ()) return (false);
		}
	}
	if (type > 0 && lane_ptr->Min_Traveler () > 0) {
		if (lane_ptr->Type () == PROHIBIT) {
			if (type >= lane_ptr->Min_Traveler () && type <= lane_ptr->Max_Traveler ()) return (false);
		} else {
			if (type < lane_ptr->Min_Traveler () || type > lane_ptr->Max_Traveler ()) return (false);
		}
	}
	index = lane_ptr->First_Use ();
	if (index < 0) return (true);

	period_ptr = &exe->use_period_array [index];

	num = period_ptr->Records ();
	index = period_ptr->Index ();

	for (i=0; i < num; i++, index++) {
		use_index = &exe->use_period_index [index];
		use_ptr = &exe->lane_use_array [use_index->Link ()];

		if (use_ptr->Offset () > 0 || use_ptr->Length () > 0) {
			int offset = cell * param.cell_size;

			if (use_ptr->Offset () > offset || offset > (use_ptr->Offset () + use_ptr->Length ())) continue;
		}
		if (use_ptr->Type () == REQUIRE) {
			if (!Use_Permission (use_ptr->Use (), use)) continue;
			if (veh_type >= 0 && use_ptr->Min_Veh_Type () >= 0) {
				if (veh_type < use_ptr->Min_Veh_Type () || veh_type > use_ptr->Max_Veh_Type ()) continue;
			}
			if (type > 0 && use_ptr->Min_Traveler () > 0) {
				if (type < use_ptr->Min_Traveler () || type > use_ptr->Max_Traveler ()) continue;
			}
			return (lane >= use_ptr->Low_Lane () && lane <= use_ptr->High_Lane ());
		} else {
			if (lane < use_ptr->Low_Lane () || lane > use_ptr->High_Lane ()) continue;

			if (use_ptr->Type () == PROHIBIT) {
				if (use_flag && !Use_Permission (use_ptr->Use (), SOV)) return (false);
				if (!Use_Permission (use_ptr->Use (), use)) continue;
				if (veh_type >= 0 && use_ptr->Min_Veh_Type () >= 0) {
					if (veh_type < use_ptr->Min_Veh_Type () || veh_type > use_ptr->Max_Veh_Type ()) continue;
				}
				if (type > 0 && use_ptr->Min_Traveler () > 0) {
					if (type < use_ptr->Min_Traveler () || type > use_ptr->Max_Traveler ()) continue;
				}
				return (false);
			} else if (use_ptr->Type () == LIMIT) {
				if (use_flag && Use_Permission (use_ptr->Use (), SOV)) return (false);
				if (!Use_Permission (use_ptr->Use (), use)) return (false);
				if (veh_type >= 0 && use_ptr->Min_Veh_Type () >= 0) {
					if (veh_type < use_ptr->Min_Veh_Type () || veh_type > use_ptr->Max_Veh_Type ()) return (false);
				}
				if (type > 0 && use_ptr->Min_Traveler () > 0) {
					if (type < use_ptr->Min_Traveler () || type > use_ptr->Max_Traveler ()) return (false);
				}
				return (true);
			} else if (use_ptr->Type () == APPLY) {
				if (!Use_Permission (use_ptr->Use (), use)) continue;
				if (veh_type >= 0 && use_ptr->Min_Veh_Type () >= 0) {
					if (veh_type < use_ptr->Min_Veh_Type () || veh_type > use_ptr->Max_Veh_Type ()) continue;
				}
				if (type >= 0 && use_ptr->Min_Traveler () > 0) {
					if (type < use_ptr->Min_Traveler () || type > use_ptr->Max_Traveler ()) continue;
				}
				if (use_ptr->Min_Delay () > 0 || use_ptr->Max_Delay () > 0) {
					if (use_ptr->Offset () > 0 || use_ptr->Length () > 0) {
						if (cell != (use_ptr->Offset () + (use_ptr->Length () / 2)) / param.cell_size) break;
					} else {
						if (cell != (int) (lane_ptr->size () / 2)) break;
					}
					int diff = 0;
					if (use_ptr->Max_Delay () > use_ptr->Min_Delay ()) {
						seed = abs (index + exe->Random_Seed () + step.Vehicle () + lane);
						diff = DTOI ((use_ptr->Max_Delay () - use_ptr->Min_Delay ()) * exe->random.Probability (seed));
					}
					step.Delay (use_ptr->Min_Delay () + diff);
				}
				break;
			}
		}
	}
	return (true);
}
示例#27
0
void Sim_Method::Network_Prep (void)
{
	int i, dir_index, index, dir, record, next, lanes, use_code, min_lane, max_lane, num;
	int j, k, n, cells, cell_out, bnode, *list, bear1, bear2, change, capacity, first [20];
	double factor;
	bool flag;

	Link_Data *link_ptr;
	Dir_Data *dir_ptr, *app_ptr;
	Node_Itr node_itr;
	Connect_Data *connect_ptr, *cross_ptr;
	Sim_Dir_Itr sim_dir_itr;
	Sim_Lane_Ptr sim_lane_ptr;
	Sim_Cap_Ptr sim_cap_ptr;
	Sim_Connection *sim_con_ptr;
	Lane_Use_Data *use_ptr;
	Lane_Use_Period *period_ptr;
	Link_Dir_Data *use_index;
	Pocket_Data *pocket_ptr;
	Lane_Map_Array lane_map;
	Lane_Map_Itr lane_itr;
	Turn_Pen_Data *turn_ptr;

	Integers node_link;
	Integers link_list;

	//---- initialize link dir data ----

	if (method == MESOSCOPIC) {
		node_link.assign (exe->node_array.size (), -1);
		link_list.assign (exe->dir_array.size (), -1);
	}

	use_code = Use_Code ("CAR/TRUCK/BUS/RAIL");

	for (dir_index=0, sim_dir_itr = exe->sim_dir_array.begin (); sim_dir_itr != exe->sim_dir_array.end (); sim_dir_itr++, dir_index++) {
		if (sim_dir_itr->To_Part () != partition && (sim_dir_itr->From_Part () != partition || !sim_dir_itr->Transfer ())) continue;

		dir_ptr = &exe->dir_array [dir_index];

		min_lane = dir_ptr->Left ();
		max_lane = dir_ptr->Lanes () + min_lane - 1;
	
		//---- link data by subarea method ----

		sim_dir_itr->Make_Data ();

		if (sim_dir_itr->Method () == MACROSCOPIC) {
			sim_cap_ptr = sim_dir_itr->Sim_Cap ();

			//---- calculate link pce capacity ----

			lanes = sim_dir_itr->Lanes ();
			cells = sim_dir_itr->Cells ();

			capacity = lanes * cells;

			cells--;
			cell_out = cells - sim_dir_itr->Out_Cell ();

			//----- add pocket lane cells ----

			for (index = dir_ptr->First_Pocket (); index >= 0; index = pocket_ptr->Next_Index ()) {
				pocket_ptr = &exe->pocket_array [index];

				if (pocket_ptr->Type () == LEFT_TURN || pocket_ptr->Type () == RIGHT_TURN) {
					k = cells;
					j = MIN ((pocket_ptr->Offset () / param.cell_size), cell_out);
					factor = param.turn_factor;
				} else if (pocket_ptr->Type () == LEFT_MERGE || pocket_ptr->Type () == RIGHT_MERGE) {
					j = 0;
					k = MIN (((pocket_ptr->Length () + param.cell_size - 1) / param.cell_size), cells);
					factor = param.merge_factor;
				} else {
					j = pocket_ptr->Offset () / param.cell_size;
					k = MIN (((pocket_ptr->Offset () + pocket_ptr->Length () + param.cell_size - 1) / param.cell_size), cells);
					factor = param.other_factor;
				}
				if (j < 0) j = 0;

				capacity += DTOI (pocket_ptr->Lanes () * (k - j + 1) * factor);
			}
			sim_cap_ptr->Max_Capacity (capacity);

			//---- set the link use at the start of the simulation ----

			sim_dir_itr->Reset_Use (min_lane, max_lane);

			index = dir_ptr->First_Lane_Use ();

			if (index >= 0) {
				for (period_ptr = &exe->use_period_array [index]; ; period_ptr = &exe->use_period_array [++index]) {
					if (period_ptr->Start () > exe->step) {
						if (period_ptr->Start () < use_update_time) {
							use_update_time = period_ptr->Start ();
						}
						continue;
					} else if (exe->step < period_ptr->End ()) {
						if (period_ptr->End () < use_update_time) {
							use_update_time = period_ptr->End ();
						}
						break;
					}
					if (period_ptr->Periods () == 0) goto exit_use;
				}
				num = period_ptr->Records ();
				index = period_ptr->Index ();

				for (i=0; i < num; i++, index++) {
					use_index = &exe->use_period_index [index];
					use_ptr = &exe->lane_use_array [use_index->Link ()];

					//---- must be a full length restriction ----
	
					if (use_ptr->Offset () > 0 || use_ptr->Length () > 0 || use_ptr->Type () == APPLY) continue;

					//---- apply to all main lanes ----

					if (use_ptr->Low_Lane () <= min_lane && use_ptr->High_Lane () >= max_lane) {

						if (use_ptr->Type () == LIMIT) {
							sim_cap_ptr->High_Use (use_ptr->Use ());
						} else if (use_ptr->Type () == PROHIBIT) {
							sim_cap_ptr->High_Use (ANY_USE_CODE ^ use_ptr->Use ());
						}
						sim_cap_ptr->High_Min_Lane (min_lane);
						sim_cap_ptr->High_Max_Lane (max_lane);

					} else {

						//---- split the capacity into two restriction groups ----

						sim_cap_ptr->High_Min_Lane ((use_ptr->Low_Lane () < min_lane) ? min_lane : use_ptr->Low_Lane ());
						sim_cap_ptr->High_Max_Lane ((use_ptr->High_Lane () > max_lane) ? max_lane : use_ptr->High_Lane ());

						lanes = sim_cap_ptr->High_Max_Lane () - sim_cap_ptr->High_Min_Lane () + 1;
						capacity = lanes * sim_dir_itr->Cells ();

						sim_cap_ptr->Low_Capacity (sim_cap_ptr->Max_Capacity () - capacity);
						sim_cap_ptr->High_Capacity (capacity);

						if (use_ptr->Type () == LIMIT) {
							sim_cap_ptr->High_Use (use_ptr->Use ());
						} else if (use_ptr->Type () == PROHIBIT) {
							sim_cap_ptr->High_Use (ANY_USE_CODE ^ use_ptr->Use ());
						}
						if (sim_cap_ptr->High_Min_Lane () == min_lane) {
							sim_cap_ptr->Low_Min_Lane (sim_cap_ptr->High_Max_Lane () + 1);
							sim_cap_ptr->Low_Max_Lane (max_lane);
						} else {
							sim_cap_ptr->Low_Min_Lane (min_lane);
							sim_cap_ptr->Low_Max_Lane (sim_cap_ptr->High_Min_Lane () - 1);
						}
					}
				}
			}

		} else if (sim_dir_itr->Method () == MESOSCOPIC) {
			
			link_ptr = &exe->link_array [dir_ptr->Link ()];

			bnode = (sim_dir_itr->Dir () == 0) ? link_ptr->Anode () : link_ptr->Bnode ();

			list = &node_link [bnode];
			link_list [dir_index] = *list;
			*list = dir_index;

			//---- initialize the pocket lanes and access restrictions ----

			cells = sim_dir_itr->Cells ();
			lanes = sim_dir_itr->Lanes ();

			for (i=0; i < lanes; i++) {
				sim_lane_ptr = sim_dir_itr->Lane (i);

				if (i < min_lane || i > max_lane) {
					sim_lane_ptr->assign (cells, -2);
				} else {
					sim_lane_ptr->assign (cells, -1);
				}
				sim_lane_ptr->Type (LIMIT);
				sim_lane_ptr->Use (link_ptr->Use ());
				sim_lane_ptr->Min_Veh_Type (-1);
				sim_lane_ptr->Max_Veh_Type (0);
				sim_lane_ptr->Min_Traveler (0);
				sim_lane_ptr->Max_Traveler (0);
				sim_lane_ptr->First_Use (-1);

				sim_lane_ptr->Thru_Lane (0);
				sim_lane_ptr->Thru_Link (-1);
			}

			//----- add pocket lane cells ----

			cells--;
			cell_out = cells - sim_dir_itr->Out_Cell ();

			for (index = dir_ptr->First_Pocket (); index >= 0; index = pocket_ptr->Next_Index ()) {
				pocket_ptr = &exe->pocket_array [index];

				if (pocket_ptr->Type () == LEFT_TURN || pocket_ptr->Type () == RIGHT_TURN) {
					k = cells;
					j = MIN ((pocket_ptr->Offset () / param.cell_size), cell_out);
				} else if (pocket_ptr->Type () == LEFT_MERGE || pocket_ptr->Type () == RIGHT_MERGE) {
					j = 0;
					k = MIN (((pocket_ptr->Length () + param.cell_size - 1) / param.cell_size), cells);
				} else {
					j = pocket_ptr->Offset () / param.cell_size;
					k = MIN (((pocket_ptr->Offset () + pocket_ptr->Length () + param.cell_size - 1) / param.cell_size), cells);
				}
				if (j < 0) j = 0;

				if (pocket_ptr->Type () == LEFT_TURN || pocket_ptr->Type () == LEFT_MERGE) {
					lanes = dir_ptr->Left () - pocket_ptr->Lanes ();
				} else {
					lanes = dir_ptr->Left () + dir_ptr->Lanes ();
				}

				for (n=0; n < pocket_ptr->Lanes (); n++, lanes++) {
					sim_lane_ptr = sim_dir_itr->Lane (lanes);

					for (i=j; i <= k; i++) {
						(*sim_lane_ptr) [i] = -1;
					}
				}
			}

			//---- initialize the connection array ----

			for (index = dir_ptr->First_Connect (); index >= 0; index = connect_ptr->Next_Index ()) {
				connect_ptr = &exe->connect_array [index];
				sim_con_ptr = &exe->sim_connection [index];

				exe->Lane_Map (connect_ptr, *sim_con_ptr);
			}

			//---- set the thru link and lane for each entry lane ----

			for (index = dir_ptr->First_Connect_From (); index >= 0; index = connect_ptr->Next_From ()) {
				connect_ptr = &exe->connect_array [index];
				
				app_ptr = &exe->dir_array [connect_ptr->Dir_Index ()];
				if ((exe->link_array [app_ptr->Link ()].Use () & use_code) == 0) continue;

				exe->Lane_Map (connect_ptr, lane_map);

				for (lane_itr = lane_map.begin (); lane_itr != lane_map.end (); lane_itr++) {
					if (lane_itr->In_Thru () && lane_itr->Out_Thru ()) {
						sim_lane_ptr = sim_dir_itr->Lane (lane_itr->Out_Lane ());
						if (sim_lane_ptr->Thru_Link () < 0) {
							sim_lane_ptr->Thru_Lane (lane_itr->In_Lane ());
							sim_lane_ptr->Thru_Link (connect_ptr->Dir_Index ());
						}
					}
				}
			}

			//---- set the lane use at the start of the simulation ----

			memset (first, -1, sizeof (first));

			index = dir_ptr->First_Lane_Use ();

			if (index >= 0) {
				for (period_ptr = &exe->use_period_array [index]; ; period_ptr = &exe->use_period_array [++index]) {
					if (period_ptr->Start () > exe->step) {
						if (period_ptr->Start () < use_update_time) {
							use_update_time = period_ptr->Start ();
						}
						continue;
					} else if (exe->step < period_ptr->End ()) {
						if (period_ptr->End () < use_update_time) {
							use_update_time = period_ptr->End ();
						}
						break;
					}
					if (period_ptr->Periods () == 0) goto exit_use;
				}
				num = period_ptr->Records ();
				index = period_ptr->Index ();

				for (i=0; i < num; i++, index++) {
					use_index = &exe->use_period_index [index];
					use_ptr = &exe->lane_use_array [use_index->Link ()];

					if (use_ptr->Type () == REQUIRE) {
						lanes = sim_dir_itr->Lanes ();

						for (i=0; i < lanes; i++) {
							if (i < use_ptr->Low_Lane () || i > use_ptr->High_Lane ()) {
								sim_lane_ptr = sim_dir_itr->Lane (i);
								if (sim_lane_ptr->First_Use () >= 0) continue;

								if (first [i] >= 0) {
									sim_lane_ptr->First_Use (first [i]);
									sim_lane_ptr->Type (LIMIT);
									sim_lane_ptr->Use (link_ptr->Use ());
									sim_lane_ptr->Min_Veh_Type (-1);
									sim_lane_ptr->Max_Veh_Type (0);
									sim_lane_ptr->Min_Traveler (0);
									sim_lane_ptr->Max_Traveler (0);
								} else {
									first [i] = index;
									sim_lane_ptr->Use (sim_lane_ptr->Use () ^ use_ptr->Use ());
									sim_lane_ptr->Type (LIMIT);
									if (use_ptr->Min_Veh_Type () >= 0) {
										sim_lane_ptr->Min_Veh_Type (use_ptr->Min_Veh_Type ());
										sim_lane_ptr->Max_Veh_Type (use_ptr->Max_Veh_Type ());
									}
									if (use_ptr->Min_Traveler () > 0) {
										sim_lane_ptr->Min_Traveler (use_ptr->Min_Traveler ());
										sim_lane_ptr->Max_Traveler (use_ptr->Max_Traveler ());
									}
								}
							}
						}
					} else {
						for (i=use_ptr->Low_Lane (); i <= use_ptr->High_Lane (); i++) {
							sim_lane_ptr = sim_dir_itr->Lane (i);
							if (sim_lane_ptr->First_Use () >= 0) continue;

							if (first [i] >= 0) {
								sim_lane_ptr->First_Use (first [i]);
								sim_lane_ptr->Type (LIMIT);
								sim_lane_ptr->Use (link_ptr->Use ());
								sim_lane_ptr->Min_Veh_Type (-1);
								sim_lane_ptr->Max_Veh_Type (0);
								sim_lane_ptr->Min_Traveler (0);
								sim_lane_ptr->Max_Traveler (0);
							} else {
								first [i] = index;

								if (use_ptr->Type () == PROHIBIT || use_ptr->Type () == LIMIT) {
									if (use_ptr->Type () == PROHIBIT) {
										sim_lane_ptr->Use (sim_lane_ptr->Use () ^ use_ptr->Use ());
									} else {
										sim_lane_ptr->Use (use_ptr->Use ());
									}
									sim_lane_ptr->Type (use_ptr->Type ());
									if (use_ptr->Min_Veh_Type () >= 0) {
										sim_lane_ptr->Min_Veh_Type (use_ptr->Min_Veh_Type ());
										sim_lane_ptr->Max_Veh_Type (use_ptr->Max_Veh_Type ());
									}
									if (use_ptr->Min_Traveler () > 0) {
										sim_lane_ptr->Min_Traveler (use_ptr->Min_Traveler ());
										sim_lane_ptr->Max_Traveler (use_ptr->Max_Traveler ());
									}
								}
							}
						}
					}
				}
			}
		}
exit_use:
		//---- set the turn prohibition flag ----

		sim_dir_itr->Turn (false);

		for (index = dir_ptr->First_Turn (); index >= 0; index = turn_ptr->Next_Index ()) {
			turn_ptr = &exe->turn_pen_array [index];

			if (turn_ptr->Penalty () != 0) continue;

			if (turn_ptr->Start () > exe->step) {
				if (turn_ptr->Start () < turn_update_time) {
					turn_update_time = turn_ptr->Start ();
				}
			} else if (exe->step < turn_ptr->End ()) {
				if (turn_ptr->End () < turn_update_time) {
					turn_update_time = turn_ptr->End ();
				}
				sim_dir_itr->Turn (true);
				break;
			}
		}
	}

	//---- initialize the traffic controls ----

	Traffic_Controls (true);

	if (method == MACROSCOPIC) return;

	//---- identify conflict links ----

	for (bnode = 0, node_itr = exe->node_array.begin (); node_itr != exe->node_array.end (); node_itr++, bnode++) {
		if (node_itr->Control () == -1 || node_itr->Partition () != partition) continue;

		flag = (node_itr->Control () >= 0);		//---- signal flag ----

		//---- process each link entering the node ----

		list = &node_link [bnode];

		for (index = *list; index >= 0; index = link_list [index]) {
			dir_ptr = &exe->dir_array [index];

			bear1 = dir_ptr->Out_Bearing ();
			if (flag) bear1 = exe->compass.Flip (bear1);

			//---- find connections with potential conflicts ----

			for (next = dir_ptr->First_Connect (); next >= 0; next = connect_ptr->Next_Index ()) {
				connect_ptr = &exe->connect_array [next];

				if (flag) {
					if (connect_ptr->Type () != LEFT) continue;
				} else {
					if (connect_ptr->Control () != STOP_GREEN) continue;
					if (connect_ptr->Type () == RIGHT || connect_ptr->Type () == UTURN) continue;
				}
				sim_con_ptr = &exe->sim_connection [next];
				n = sim_con_ptr->Max_Conflicts ();

				//---- search for conflicting approach links ----

				for (dir = *list; dir >= 0; dir = link_list [dir]) {
					if (dir == index) continue;

					app_ptr = &exe->dir_array [dir];

					bear2 = app_ptr->Out_Bearing ();
					change = exe->compass.Change (bear1, bear2);

					//---- check the angle ----

					if (change >= -45 && change <= 45) {
						if (!flag && connect_ptr->Type () != LEFT) continue;
						i = 0;
					} else if (change > -135 && change < -45) {
						if (flag || connect_ptr->Type () == LEFT) continue;
						i = 0;
					} else if (change > 45 && change < 135) {
						if (flag) continue;
						i = 1;
					} else {
						continue;
					}
					
					//---- find the thru movement ----
					
					for (record = app_ptr->First_Connect (); record >= 0; record = cross_ptr->Next_Index ()) {
						cross_ptr = &exe->connect_array [record];

						if (cross_ptr->Type () != THRU) continue;	// r_split l_split, r_merge, l_merge??

						if (sim_con_ptr->Conflict (i) < 0) {
							sim_con_ptr->Conflict (i, record);
						} else if (!flag && 
							((change >= -100 && change <= -80) || (change >= 80 && change <= 100))) {
							sim_con_ptr->Conflict (i, record);
						}
					}
				}
			}
		}
	}
}
示例#28
0
bool Db_Base::Read_Field (Db_Field *fld, void *data, Field_Type type)
{
	if (data == 0) return (Status (NULL_POINTER));
	
	//---- initialize the data field ----

	switch (type) {
		case DB_INTEGER:
			*((int *) data) = 0;
			break;
		case DB_DOUBLE:
			*((double *) data) = 0.0;
			break;
		case DB_STRING:
			((string *) data)->clear ();
			break;
		case DB_TIME:
			*((Dtime *) data) = 0;
			break;
		default:
			return (Status (DB_ERROR));
	}
	if (fld == 0) return (Status (NO_FIELD, false));

	Nested (fld->Nested ());

	char *field = Record_String ();

	if (field == 0) return (Status (RECORD_SIZE));

	Field_Type fld_type;
	bool asc_flag = true;
	int len, lvalue = 0;
	double dvalue = 0.0;
	String svalue;
	Dtime tvalue = 0;

	int size = fld->Width ();
	int offset = fld->Offset ();

	switch (Record_Format ()) {
		case BINARY:
			if (fld->Type () != DB_STRING) {
				field += offset;
				asc_flag = false;
				break;
			} else {
				len = (int) strlen (field + offset);
				if (len < size) size = len;
			}
		case FIXED_COLUMN:
			svalue.assign (field + offset, size);
			svalue.Clean ();
			break;
		case COMMA_DELIMITED:
		case SPACE_DELIMITED:
		case TAB_DELIMITED:
		case CSV_DELIMITED:
			svalue (fld->Buffer ());
			size = (int) svalue.size ();
			break;
		default:
			svalue  = Get_Field_Number (offset);
			size = (int) svalue.size ();
			break;
	}

	//---- read the field from the current record ----

	switch (fld->Type ()) {
		default:
			return (Status (DB_ERROR));

		case DB_INTEGER:
			if (asc_flag) {
				lvalue = svalue.Integer ();
			} else if (size == sizeof (int)) {
				lvalue = *((int *) field);
			} else if (size == sizeof (short)) {
				lvalue = *((short *) field);
			} else if (size == sizeof (char)) {
				lvalue = *((char *) field);
			} else {
				return (Status (FIELD_BYTES));
			}
			fld_type = DB_INTEGER;
			break;
			
		case DB_UNSIGNED:
			if (asc_flag) {
				lvalue = svalue.Integer ();
			} else if (size == sizeof (int)) {
				lvalue = *((unsigned int *) field);
			} else if (size == sizeof (short)) {
				lvalue = *((unsigned short *) field);
			} else if (size == sizeof (char)) {
				lvalue = *((unsigned char *) field);
			} else {
				return (Status (FIELD_BYTES));
			}
			fld_type = DB_INTEGER;
			break;
			
		case DB_DOUBLE:
			if (asc_flag) {
				dvalue = svalue.Double ();
			} else if (size == sizeof (double)) {
				dvalue = *((double *) field);
			} else if (size == sizeof (float)) {
				dvalue = *((float *) field);
			} else {
				return (Status (FIELD_BYTES));
			}
			fld_type = DB_DOUBLE;
			break;
			
		case DB_FIXED:
			if (asc_flag) {
				dvalue = svalue.Double ();
				if (svalue.find ('.') == 0) {
					dvalue = dvalue / pow (10.0, fld->Decimal ());
				}
			} else {
				if (size == sizeof (int)) {
					lvalue = *((int *) field);
				} else if (size == sizeof (short)) {
					lvalue = *((short *) field);
				} else if (size == sizeof (char)) {
					lvalue = *((char *) field);
				} else {
					return (Status (FIELD_BYTES));
				}
				dvalue = (double) lvalue / pow (10.0, fld->Decimal ());
			}
			fld_type = DB_DOUBLE;
			break;
			
		case DB_STRING:
			fld_type = DB_STRING;
			break;

		case DB_CHAR:
			if (asc_flag) {
				if (svalue.empty ()) {
					svalue = " ";
				}
			} else {
				if (*field == '\0') {
					svalue = " ";
				} else {
					svalue.insert (0, field, 1);
				}
			}
			fld_type = DB_STRING;
			break;
			
		case DB_TIME:
			if (asc_flag) {
				tvalue.Time_String (svalue, fld->Units ());
			} else if (size == sizeof (Dtime)) {
				tvalue = *((int *) field);
			} else if (size == sizeof (short)) {
				tvalue = *((short *) field);
			} else {
				return (Status (FIELD_BYTES));
			}
			fld_type = DB_TIME;
			break;
	}

	//---- convert to internal units ----

	if (fld->Units () != NO_UNITS) {
		if (fld_type == DB_INTEGER) {
			lvalue = Internal_Units (lvalue, fld->Units ());
		} else if (fld_type == DB_DOUBLE) {
			dvalue = Internal_Units (dvalue, fld->Units ());
		} else if (fld->Units () < SECONDS || fld->Units () > TIME_CODE) {
			if (fld_type == DB_STRING && fld->Units () >= FACILITY_CODE) {
				if (type == DB_INTEGER) {
					lvalue = Internal_Units (svalue, fld->Units ());
					if (lvalue < -1) return (Status (FIELD_UNITS));
					if (lvalue >= 0) fld_type = DB_INTEGER;
				}
			} else {
				return (Status (FIELD_UNITS));
			}
		}
	}

	//---- convert to the output data type ----

	switch (type) {
		case DB_INTEGER:
			switch (fld_type) {
				case DB_INTEGER:
					*((int *) data) = lvalue;
					break;
				case DB_DOUBLE:
					*((int *) data) = DTOI (dvalue);
					break;
				case DB_STRING:
					if (svalue [0] < '0' || svalue [0] > '9' || svalue.find_first_of ("@:") != svalue.npos) {
						*((int *) data) = (int) Dtime (svalue);
					} else {
						*((int *) data) = svalue.Integer ();
					}
					break;
				case DB_TIME:
					*((int *) data) = (int) tvalue;
					break;
				default:
					return (Status (DB_ERROR));
			}
			break;

		case DB_DOUBLE:
			switch (fld_type) {
				case DB_INTEGER:
					*((double *) data) = lvalue;
					break;
				case DB_DOUBLE:
					*((double *) data) = dvalue;
					break;
				case DB_STRING:
					if (svalue [0] < '0' || svalue [0] > '9' || svalue.find_first_of ("@:") != svalue.npos) {
						*((double *) data) = (int) Dtime (svalue);
					} else {
						*((double *) data) = svalue.Double ();
					}
					break;
				case DB_TIME:
					*((double *) data) = (double) tvalue;
					break;
				default:
					return (Status (DB_ERROR));
			}
			break;

		case DB_STRING:
			switch (fld->Type ()) {
				case DB_TIME:
					svalue = tvalue.Time_String ();
					break;
				default:
					if (fld_type == DB_INTEGER) {
						svalue (lvalue);
					} else if (fld_type == DB_DOUBLE) {
						svalue (dvalue, fld->Decimal ());
					}
					break;
			}
			len = (int) svalue.size ();
			if (len > 0) {
				if (len > fld->Width ()) len = fld->Width ();

				*((string *) data) = svalue.substr (0, len);
			} else {
				(*((string *) data)).clear ();
			}
			break;

		case DB_TIME:
			switch (fld_type) {
				case DB_INTEGER:
					*((Dtime *) data) = Dtime (lvalue, fld->Units ());
					break;
				case DB_DOUBLE:
					*((Dtime *) data) = Dtime (dvalue, fld->Units ());
					break;
				case DB_STRING:
					*((Dtime *) data) = svalue;
					break;
				case DB_TIME:
					*((Dtime *) data) = tvalue;
					break;
				default:
					return (Status (DB_ERROR));
			}
			break;
		default:
			return (Status (DB_ERROR));
	}
	return (true);
}
示例#29
0
void RiderSum::Line_Rider_Report (void)
{
	int riders, board, alight, run, runs, num, length, tot_len, period, num_periods;
	int max_riders, max_board, max_alight, max_runs, total, capacity, max_cap;
	double factor, max_fac, sum_time, tot_time, time, capfac, max_capfac;
	double vmt, vht, pmt, pht;
	Dtime low, high;

	Int_Map_Itr map_itr;
	Line_Data *line_ptr;
	Stop_Data *stop_ptr;
	Line_Stop_Itr stop_itr, next_itr;
	Line_Run_Itr run_itr;
	Veh_Type_Data *veh_type_ptr, *run_type_ptr;

	Show_Message ("Line Rider Profile -- Record");
	Set_Progress ();

	//---- print the report ----

	Header_Number (LINE_RIDERS);

	num_periods = sum_periods.Num_Periods ();
	if (num_periods == 0) num_periods = 1;

	num = (int) (line_map.size () * stop_map.size ());

	if (!Break_Check (num + 5)) {
		Print (1);
		Line_Rider_Header ();
	}

	//---- process each route ----

	for (map_itr = line_map.begin (); map_itr != line_map.end (); map_itr++) {
		Show_Progress ();

		if (select_routes && !route_range.In_Range (map_itr->first)) continue;

		line_ptr = &line_array [map_itr->second];

		if (select_modes && !select_mode [line_ptr->Mode ()]) continue;

		//---- check the link criteria ----

		if (!Link_Selection (line_ptr)) continue;

		//---- set the run flags ----

		if (!Run_Selection (line_ptr)) continue;

		//---- save the route ridership data ----
		
		veh_type_ptr = &veh_type_array [line_ptr->Type ()];

		for (period = 0; period < num_periods; period++) {
			if (period_flag [period] == 0) continue;

			if (!Break_Check ((int) line_ptr->size () + 15)) {
				Print (1);
				Line_Rider_Header ();
			}
			veh_type_ptr = &veh_type_array [line_ptr->Type ()];

			Print (1, "   Route    Mode     Type   Time Period  Name");

			Print (2, String ("%8d  %10.10s %4d  %12.12s") % line_ptr->Route () %
				Transit_Code ((Transit_Type) line_ptr->Mode ()) % veh_type_ptr->Type () % 
				sum_periods.Range_Format (period));

			if (!line_ptr->Name ().empty ()) {
				Print (0, String ("  %s") % line_ptr->Name ());
			}
			if (Notes_Name_Flag ()) {
				if (!line_ptr->Notes ().empty ()) {
					Print (0, String (" -- %s") % line_ptr->Notes ());
				}
			}
			Print (2, "    Stop  Length   TTime   Alight    Board   Riders  Runs  LoadFac Capacity CapFac");
			Print (1);

			time = tot_time = 0.0;
			max_alight = max_board = max_riders = max_runs = total = length = tot_len = max_cap = 0;
			max_fac = vmt = vht = pmt = pht = max_capfac = 0.0;

			sum_periods.Period_Range (period, low, high);

			for (stop_itr = line_ptr->begin (); stop_itr != line_ptr->end (); stop_itr++) {
				riders = board = alight = runs = capacity = 0;

				stop_ptr = &stop_array [stop_itr->Stop ()];

				next_itr = stop_itr + 1;

				if (next_itr != line_ptr->end ()) {
					length = next_itr->Length () - stop_itr->Length ();
				} else {
					length = 0;
				}
				sum_time = 0.0;
				num = 0;

				for (run=0, run_itr = stop_itr->begin (); run_itr != stop_itr->end (); run_itr++, run++) {
					if (run_flag [run] == 0) continue;
					if (run_period [run] != period) continue;

					board += run_itr->Board ();
					alight += run_itr->Alight ();
					riders += run_itr->Load ();
					runs++;

					if (line_ptr->run_types.size () > 0) {
						run_type_ptr = &veh_type_array [line_ptr->Run_Type (run)];
						capacity += run_type_ptr->Capacity ();
					} else {
						capacity += veh_type_ptr->Capacity ();
					}
					if (next_itr != line_ptr->end ()) {
						time = next_itr->at (run).Schedule ().Seconds () - run_itr->Schedule ().Seconds ();

						vmt += length;
						vht += time;
						pmt += length * run_itr->Load ();
						pht += time * run_itr->Load ();

						sum_time += time;
						num++;
					}
				}
				if (runs == 0) continue;
				if (capacity == 0) capacity = runs;

				factor = (double) riders / runs;
				capfac = DTOI (riders * 10.0 / capacity) / 10.0;

				if (next_itr == line_ptr->end ()) runs = 0;

				if (num > 0) {
					time = sum_time / num;
				} else {
					time = 0;
				}
				Print (1, String ("%8d %7.0lf %7.0lf %8d %8d %8d %5d %8.1lf %6d %8.1lf") % stop_ptr->Stop () % 
					UnRound (length) % time % alight % board % riders % runs % factor % capacity % capfac);

				if (Notes_Name_Flag ()) {
					if (!stop_ptr->Name ().empty ()) {
						Print (0, String ("  %s") % stop_ptr->Name ());
					}
					if (!stop_ptr->Notes ().empty ()) {
						Print (0, " -- ") << stop_ptr->Notes ();
					}
				}
				if (alight > max_alight) max_alight = alight;
				if (board > max_board) max_board = board;
				if (riders > max_riders) max_riders = riders;
				if (runs > max_runs) max_runs = runs;
				if (factor > max_fac) max_fac = factor;
				if (capacity > max_cap) max_cap = capacity;
				if (capfac > max_capfac) max_capfac = capfac;

				tot_len += length;
				tot_time += time;
				total += board;
			}
			if (max_runs == 0) continue;

			Print (2, String (" Maximum                 %8ld %8ld %8ld %5ld %8.1lf %6lf %8.1lf") %
				max_alight % max_board % max_riders % max_runs % max_fac % max_cap % max_capfac);

			Print (2, "Total Boardings = ") << total;

			if (total == 0 || tot_time == 0) continue;

			factor = UnRound (tot_len);
			vmt = UnRound (vmt) / 5280.0;
			vht = vht / 3600.0;
			pmt = UnRound (pmt) / 5280.0;
			pht = pht / 3600.0;


			Print (1, String ("Route Length = %.1lf miles, %.1lf minutes  Average Speed = %.1lf mph") %
				External_Units (factor, MILES) % (tot_time / 60.0) % External_Units ((factor / tot_time), MPH));
			Print (1, String ("Vehicle Miles = %.1lf  Vehicle Hours = %.1lf") % vmt % vht);
			Print (1, String ("Passenger Miles = %.1lf  Passenger Hours = %.1lf") %	pmt % pht);
			Print (1, String ("Passengers per Vehicle Mile = %.1lf  Passengers per Vehicle Hour = %.1lf") %	
				(pmt / vht) % (pht / vht));

			vmt = pmt / total;
			vht = pht * 60.0 / total;

			Print (1, String ("Average Trip Length = %.1lf miles, %.1lf minutes") % vmt % vht);
		}
	}
	End_Progress ();

	Header_Number (0);
}
示例#30
0
bool Db_Base::Write_Field (Db_Field *fld, void *data, Field_Type type)
{
	if (data == 0) return (Status (NULL_POINTER));
	if (fld == 0) return (Status (NO_FIELD, false));
	if (!Record ().OK ()) return (Status (RECORD_SIZE));

	int lvalue = 0;
	unsigned uvalue = 0;
	double dvalue = 0.0;
	Dtime tvalue;
	String svalue;
	Field_Type fld_type;

	//---- convert the input data type to generic variables ----

	fld_type = fld->Type ();

	switch (fld_type) {
		default:
			return (Status (DB_ERROR));
			
		case DB_INTEGER:
			switch (type) {
				case DB_INTEGER:
					lvalue = *((int *) data);
					break;
				case DB_DOUBLE:
					dvalue = *((double *) data);
					if (dvalue > MAX_INTEGER) {
						lvalue = MAX_INTEGER;
					} else if (dvalue < -MAX_INTEGER) {
						lvalue = -MAX_INTEGER;
					} else {
						lvalue = DTOI (dvalue);
					}
					break;
				case DB_STRING:
					lvalue = ((String *) data)->Integer ();
					break;
				case DB_TIME:
					lvalue = (int) (*((Dtime *) data));
					break;
				default:
					return (Status (DB_ERROR));
			}
			break;

		case DB_UNSIGNED:
			switch (type) {
				case DB_INTEGER:
					uvalue = *((unsigned *) data);
					break;
				case DB_DOUBLE:
					dvalue = *((double *) data);
					if (dvalue > 2.0 * MAX_INTEGER) {
						uvalue = MAX_INTEGER;
						uvalue *= 2;
					} else if (dvalue < 0.0) {
						uvalue = 0;
					} else {
						uvalue = (unsigned) (dvalue + 0.5);
					}
					break;
				case DB_STRING:
					uvalue = ((String *) data)->Integer ();
					break;
				case DB_TIME:
					uvalue = (unsigned) (*((Dtime *) data));
					break;
				default:
					return (Status (DB_ERROR));
			}
			break;

		case DB_FIXED:
		case DB_DOUBLE:
			switch (type) {
				case DB_INTEGER:
					dvalue = (double) *((int *) data);
					break;
				case DB_DOUBLE:
					dvalue = *((double *) data);
					break;
				case DB_STRING:
					dvalue = ((String *) data)->Double ();
					break;
				case DB_TIME:
					dvalue = (double) *((Dtime *) data);
					break;
				default:
					return (Status (DB_ERROR));
			}
			break;
		
		case DB_STRING:
		case DB_CHAR:
			switch (type) {
				case DB_INTEGER:
					if (fld->Units () >= FACILITY_CODE) {
						External_Units (*((int *) data), fld->Units (), svalue);
					} else {
						svalue (*((int *) data));
					}
					break;
				case DB_DOUBLE:
					svalue (*((double *) data), fld->Decimal ());
					break;
				case DB_STRING:
					svalue = *((string *) data);
					break;
				case DB_TIME:
					svalue = ((Dtime *) data)->Time_String (fld->Units ());
					break;
				default:
					return (Status (DB_ERROR));
			}
			break;

		case DB_TIME:
			switch (type) {
				case DB_INTEGER:
					tvalue = *((int *) data);
					break;
				case DB_DOUBLE:
					tvalue = *((double *) data);
					break;
				case DB_STRING:
					tvalue = *((string *) data);
					break;
				case DB_TIME:
					tvalue = *((Dtime *) data);
					break;
				default:
					return (Status (DB_ERROR));
			}
			break;
	}

	//---- convert to external units ----

	if (fld->Units () != NO_UNITS) {
		if (fld_type == DB_INTEGER) {
			lvalue = (int) External_Units (lvalue, fld->Units ());
		} else if (fld_type == DB_UNSIGNED) {
			uvalue = (unsigned) External_Units ((int) uvalue, fld->Units ());
		} else if (fld_type == DB_DOUBLE || fld_type == DB_FIXED) {
			dvalue = External_Units (dvalue, fld->Units ());
		} else if ((fld_type == DB_STRING || fld_type == DB_CHAR) && fld->Units () < FACILITY_CODE) {
			return (Status (FIELD_UNITS));
		}
	}

	//---- place the data onto the data record -----

	Nested (fld->Nested ());

	char *field = Record_String ();

	bool asc_flag = false;
	bool justify = false;
	int len, position;
	int size = fld->Width ();
	int offset = fld->Offset ();

	switch (Record_Format ()) {
		case BINARY:
			field += offset;
			asc_flag = false;
			break;
		case FIXED_COLUMN:
			field += offset;
			memset (field, ' ', size);
			asc_flag = justify = true;
			break;
		case COMMA_DELIMITED:
		case SPACE_DELIMITED:
		case TAB_DELIMITED:
		case CSV_DELIMITED:
		default:		
			asc_flag = true;
			justify = false;
			break;
	}

	switch (fld_type) {
		default:
			return (Status (DB_ERROR));
			
		case DB_INTEGER:
			if (asc_flag) {
				svalue (lvalue);

				if (justify) {
					len = (int) svalue.size ();
					position = size - len;
					if (position < 0) {
						position = 0;
						len = size;
					}
					memcpy (field + position, svalue.c_str (), len);
				}
			} else if (size == sizeof (int)) {
				*((int *) field) = lvalue;
			} else if (size == sizeof (short)) {
				if (lvalue > 32767) lvalue = 32767;
				*((short *) field) = (short) lvalue;
			} else if (size == sizeof (char)) {
				if (lvalue > 127) lvalue = 127;
				*((char *) field) = (char) lvalue;
			} else {
				return (Status (FIELD_BYTES));
			}
			break;
			
		case DB_UNSIGNED:
			if (asc_flag) {
				svalue ((size_t) uvalue);

				if (justify) {
					len = (int) svalue.size ();
					position = size - len;
					if (position < 0) {
						position = 0;
						len = size;
					}
					memcpy (field + position, svalue.c_str (), len);
				}
			} else if (size == sizeof (int)) {
				*((unsigned int *) field) = (unsigned int) uvalue;
			} else if (size == sizeof (short)) {
				if (uvalue > 65535) uvalue = 65535;
				*((unsigned short *) field) = (unsigned short) uvalue;
			} else if (size == sizeof (char)) {
				if (uvalue > 255) uvalue = 255;
				*((unsigned char *) field) = (unsigned char) uvalue;
			} else {
				return (Status (FIELD_BYTES));
			}
			break;

		case DB_DOUBLE:
			if (asc_flag) {
				svalue (dvalue, fld->Decimal ());
				if (justify) {
					len = (int) svalue.size ();
					position = size - len;
					if (position < 0) {
						position = 0;
						len = size;
					}
					memcpy (field + position, svalue.c_str (), len);
				}
			} else if (size == sizeof (double)) {
				*((double *) field) = dvalue;
			} else if (size == sizeof (float)) {
				*((float *) field) = (float) dvalue;
			} else {
				return (Status (FIELD_BYTES));
			}
			break;

		case DB_FIXED:
			if (asc_flag) {
				svalue (dvalue, fld->Decimal ());

				if (justify) {
					len = (int) svalue.size ();
					position = size - len;
					if (position < 0) {
						position = 0;
						len = size;
					}
					memcpy (field + position, svalue.c_str (), len);
				}
			} else {
				if (dvalue < 0.0) {
					lvalue = (int) (dvalue * pow (10.0, fld->Decimal ()) - 0.5);
				} else {
					lvalue = (int) (dvalue * pow (10.0, fld->Decimal ()) + 0.5);
				}

				if (size == sizeof (int)) {
					*((int *) field) = lvalue;
				} else if (size == sizeof (short)) {
					*((short *) field) = (short) lvalue;
				} else if (size == sizeof (char)) {
					*((char *) field) = (char) lvalue;
				} else {
					return (Status (FIELD_BYTES));
				}
			}
			break;

		case DB_STRING:
			if (asc_flag) {
				if (justify) {
					len = (int) svalue.size ();
					if (len > size) len = size;
					memcpy (field, svalue.c_str (), len);
				}
			} else {
				memset (field, '\0', size);
				memcpy (field, svalue.c_str (), MIN ((int) svalue.size (), size));
			}
			break;
			
		case DB_CHAR:
			if (!asc_flag || justify) {
				memset (field, '\0', size);
				field [0] = svalue [0];
			}
			break;
			
		case DB_TIME:
			if (asc_flag) {
				svalue = tvalue.Time_String (fld->Units ());
				if (justify) {
					len = (int) svalue.size ();
					if (len > size) len = size;
					memcpy (field, svalue.c_str (), len);
				}
			} else if (size == sizeof (Dtime)) {
				*((Dtime *) field) = tvalue;
			} else if (size == sizeof (short)) {
				*((short *) field) = (short) tvalue;
			} else {
				return (Status (FIELD_BYTES));
			}
			break;
	}
	if (asc_flag && !justify) {
		if (Record_Format () == UNFORMATED) {
			return (Set_Field_Number (offset, svalue));
		} else {
			fld->Buffer (svalue);
		}
	}
	return (true);
}